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.

183 lines
5.4 KiB

  1. /* echotime - prints current time and input args to stdout
  2. *
  3. * HISTORY:
  4. * 23-Jan-87 danl added /n switch and ';' processing
  5. * 23-Nov-87 brianwi Exit code 0
  6. */
  7. #include <string.h>
  8. #include <time.h>
  9. #include <stdio.h>
  10. #include <process.h>
  11. #include <windows.h>
  12. #include <tools.h>
  13. // Function Forward Parameters...
  14. void Usage( void );
  15. int __cdecl main( int, char ** );
  16. const char rgstrUsage[] = {
  17. "Usage: ECHOTIME [/t] [/WODHMSCYb] [/v] [/n] [/N] [;] text\n"
  18. " /t current day, time and year\n"
  19. " /WODHMSCYb\n"
  20. " Weekday, mOnth, Day, Hour, Min, Sec, Century, Yr, blank\n"
  21. " other char are themselves\n"
  22. " e.g. echotime /O-D-Y this becomes Jan-02-86\n"
  23. " e.g. echotime /ObDbY this becomes Jan 02 86\n"
  24. " /v volume id of C:\n"
  25. " /n no newline after outputting text\n"
  26. " /N no trailing blank at end of lines\n"
  27. " a semicolon surrounded by white space is replaced by a newline\n"};
  28. int fNewline = TRUE;
  29. int fTrailingBlank = TRUE;
  30. void Usage( void )
  31. {
  32. puts(rgstrUsage);
  33. exit (1);
  34. }
  35. int
  36. __cdecl
  37. main (
  38. int c,
  39. char *v[]
  40. )
  41. {
  42. // struct findType findBuf;
  43. time_t now;
  44. char *p, *strTime, *p2, *p3, *printstring;
  45. char ch;
  46. int i, len;
  47. int fFirstWord = TRUE;
  48. char timestring[1000]; //plenty of room for formatted time string
  49. ConvertAppToOem( c, v );
  50. SHIFT( c, v );
  51. while ( c ) {
  52. printstring=""; //default no text for this arg
  53. if ( !strcmp( *v, "/?" ))
  54. Usage ();
  55. if ( !strcmp( *v, "/n" ))
  56. fNewline = FALSE;
  57. else if ( !strcmp( *v, "/N" ))
  58. fTrailingBlank = FALSE;
  59. else if ( !strcmp( *v, "/v" )) {
  60. //
  61. // It would make more sense to replace by the volume id of the
  62. // current drive, but the original code used drive C: and
  63. // it is described as so in the help file, so I'll do the same.
  64. //
  65. char VolumeName[MAX_PATH];
  66. BOOL StatusOk;
  67. StatusOk = GetVolumeInformation( "C:\\",
  68. VolumeName,
  69. MAX_PATH,
  70. NULL,
  71. NULL,
  72. NULL,
  73. NULL,
  74. 0 );
  75. if (!StatusOk) {
  76. printstring = "NO_VOL_ID";
  77. } else {
  78. printstring = VolumeName;
  79. }
  80. }
  81. else if (**v == '/') {
  82. *timestring='\0';
  83. p2 = *v;
  84. time( &now );
  85. strTime = _strdup( ctime( &now ) );
  86. if (!strTime) {
  87. puts("Out of memory");
  88. exit(1);
  89. }
  90. p = strend( strTime );
  91. *--p = '\0';
  92. while ((ch = *++p2)) {
  93. len = 2;
  94. switch (ch) {
  95. case 'W': /* Week */
  96. len = 3;
  97. i = 0;
  98. break;
  99. case 'O': /* mOnth */
  100. len = 3;
  101. i = 4;
  102. break;
  103. case 'D': /* Day */
  104. i = 8;
  105. break;
  106. case 'H': /* Hour */
  107. i = 11;
  108. break;
  109. case 'M': /* Min */
  110. i = 14;
  111. break;
  112. case 'S': /* Sec */
  113. i = 17;
  114. break;
  115. case 'C': /* Century */
  116. i = 20;
  117. break;
  118. case 'Y': /* Year */
  119. i = 22;
  120. break;
  121. case 'b': /* Blank */
  122. len = 1;
  123. i = 3;
  124. break;
  125. case 't':
  126. len = 25;
  127. i = 0;
  128. break;
  129. default:
  130. strTime[3] = ch;
  131. len = 1;
  132. i = 3;
  133. break;
  134. }
  135. p = strTime + i;
  136. p3 = p + len;
  137. ch = *p3;
  138. *p3 = '\0';
  139. strcat(timestring, p); /* N.B. no trailing blanks */
  140. *p3 = ch;
  141. strTime[3] = ' ';
  142. }
  143. printstring = timestring;
  144. }
  145. else if (!strcmp( *v, ";" )) {
  146. if (fTrailingBlank)
  147. printf(" ");
  148. printf ("\n" );
  149. fFirstWord = TRUE;
  150. // printstring remains pointing to empty string
  151. }
  152. else
  153. printstring= *v;
  154. if (*printstring) {
  155. if (!fFirstWord)
  156. printf( " ");
  157. else
  158. fFirstWord=FALSE;
  159. printf("%s", printstring);
  160. }
  161. SHIFT( c, v );
  162. }
  163. if (fTrailingBlank)
  164. printf( " ");
  165. if ( fNewline )
  166. printf("\n" );
  167. return 0;
  168. }