Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.8 KiB

  1. /* touch.c - make last time on file be current time
  2. *
  3. * touch [files] - requires arg expansion
  4. */
  5. #include <io.h>
  6. #include <sys\types.h>
  7. #include <sys\utime.h>
  8. #include <sys\stat.h>
  9. #include <time.h>
  10. #include <stdio.h>
  11. #include <process.h>
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include <windows.h>
  15. #include <tools.h>
  16. #define year rgInt[0]
  17. #define month rgInt[1]
  18. #define day rgInt[2]
  19. #define hour rgInt[3]
  20. #define mins rgInt[4]
  21. #define sec rgInt[5]
  22. int rgInt[6];
  23. void usage( char *Msg, int MsgArg )
  24. {
  25. printf( "usage: TOUCH [/f] [/t year month day hour min sec] files"
  26. "\n"
  27. "where: /f - force file touch if read only\n"
  28. " /t - specifies a specific time other than the current time.\n"
  29. " /c - specifies to create the file if it does not exist.\n"
  30. );
  31. printf( "\nTOUCH: " );
  32. printf( Msg, MsgArg );
  33. printf( "\n" );
  34. exit( 1 );
  35. }
  36. int
  37. __cdecl main (c, v)
  38. int c;
  39. char *v[];
  40. {
  41. time_t ltime;
  42. struct utimbuf timenow;
  43. int i;
  44. int fh;
  45. char *p;
  46. int ReturnCode = 0;
  47. int force = 0;
  48. int create= 0;
  49. ConvertAppToOem( c, v );
  50. SHIFT (c,v);
  51. if ( c == 0 )
  52. usage( "invalid number of parameters", 0 );
  53. time (&ltime);
  54. while (fSwitChr (*(p = *v))) {
  55. while (*++p) {
  56. if (tolower(*p) == 'f')
  57. force = 1;
  58. else
  59. if (tolower(*p) == 'c')
  60. create = 1;
  61. else
  62. if (tolower(*p) == 't') {
  63. for (i = 0; i < 6; i++) {
  64. SHIFT (c, v);
  65. if (!c)
  66. usage( "incorrect time", 0 );
  67. rgInt[i] = atoi (*v);
  68. }
  69. //
  70. // do some basic date checking
  71. //
  72. if ( (year < 1980) || (month > 12) || (day>31) ||
  73. (hour>23) || (mins>59) || (sec>59) ) {
  74. usage( "incorrect time", 0 );
  75. }
  76. ltime = date2l(year, month, day, hour, mins, sec);
  77. } else
  78. usage( "bad switch '%c'", *p );
  79. }
  80. SHIFT (c, v);
  81. }
  82. timenow.actime = ltime;
  83. timenow.modtime = ltime;
  84. while (c) {
  85. //
  86. // Set the time
  87. //
  88. if (_utime (*v, (void *) &timenow) == -1) {
  89. //
  90. // Failed. Does it exist?
  91. //
  92. if (_access(*v, 0) == -1) {
  93. //
  94. // Does not exist. Create if requested and touch created file.
  95. //
  96. if (create) {
  97. fh = _creat(*v, _S_IREAD|_S_IWRITE);
  98. _close(fh);
  99. if (_utime (*v, (void *) &timenow) == 0) // touch it
  100. goto shift_it;
  101. }
  102. }
  103. else
  104. //
  105. // File exists, is it read-only and /f specified?
  106. //
  107. if (force && (_access(*v,2) == -1)) {
  108. //
  109. // Yes, make it read/write and change the time
  110. // then make it read-only again.
  111. //
  112. if (_chmod (*v, _S_IWRITE) == 0) {
  113. if (_utime (*v, (void *) &timenow) == 0) {
  114. if (_chmod (*v, _S_IREAD) != 0) {
  115. printf ("ERROR - changed 'r' attrib on %s, could not change it back\n", *v);
  116. ReturnCode = 1;
  117. }
  118. goto shift_it;
  119. }
  120. }
  121. }
  122. printf ("Can't touch %s - %s\n", *v, error ());
  123. ReturnCode = 1;
  124. }
  125. shift_it:
  126. SHIFT(c,v);
  127. }
  128. return ReturnCode;
  129. }