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.

203 lines
6.5 KiB

  1. /* WALK - Walk a directory hierarchy
  2. *
  3. * Mark Z. ??/??/83
  4. *
  5. * WALK walks a directory heirarchy and for each
  6. * file or directory or both,
  7. * prints the pathname, runs a program, or both.
  8. *
  9. * walk [-f] [-d] [-h] [-print] topdir [command]
  10. *
  11. * -f deal with files
  12. * -d deal with directorys
  13. * if neither is specified, deal with both
  14. *
  15. * -h Also find hidden directories and files
  16. * -p[rint] print the pathnames on stdout
  17. *
  18. * command optional command and arguments. Pathname is
  19. * substituted for every "%s" in the arguments
  20. * Modification History
  21. *
  22. * 11/07/83 JGL
  23. * - added -print switch
  24. * - no longer an error to omit [command]
  25. * 15-May-87 bw Add /h switch
  26. * 18-May-87 bw Add code to recognize root directories
  27. * 23-Dec-1987 mz Fix poor ./.. processing; use system
  28. * 18-Oct-1990 w-barry Removed 'dead' code.
  29. *
  30. */
  31. #define INCL_DOSMISC
  32. #include <direct.h>
  33. #include <errno.h>
  34. #include <malloc.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <process.h>
  39. #include <windows.h>
  40. #include <tools.h>
  41. // Forward Function Declarations...
  42. void walk( char *, struct findType *, void* );
  43. void usage( void );
  44. char **vec;
  45. flagType fD = FALSE; /* apply function to directories only */
  46. flagType fF = FALSE; /* apply function to files only */
  47. flagType fPrint = FALSE; /* print pathnames */
  48. unsigned srch_attr = FILE_ATTRIBUTE_DIRECTORY; /* Find non-hidden files and dirs */
  49. char cmdline[MAXLINELEN]; /* command line to be executed */
  50. char dir[MAX_PATH];
  51. char cdir[MAX_PATH];
  52. void walk (p, b, dummy)
  53. char *p;
  54. struct findType *b;
  55. void * dummy;
  56. {
  57. static flagType fFirst = TRUE;
  58. int i;
  59. char *ppat, *pdst;
  60. if (fFirst || strcmp (b->fbuf.cFileName, ".") && strcmp (b->fbuf.cFileName, "..")) {
  61. fFirst = FALSE;
  62. if ((!fD && !fF) || /* no special processing */
  63. (fD && HASATTR(b->fbuf.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY)) || /* only dir and dir */
  64. (fF && !HASATTR(b->fbuf.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY))) { /* only file and file */
  65. if (fPrint)
  66. printf ("%s\n", p);
  67. if (vec[0]) {
  68. cmdline[0] = 0;
  69. for (i = 0; vec[i] != NULL; i++) {
  70. strcat (cmdline, " ");
  71. pdst = strend (cmdline);
  72. ppat = vec[i];
  73. while (*ppat != '\0')
  74. if (ppat[0] == '%') {
  75. if (ppat[1] == 'l') {
  76. strcpy (pdst, p);
  77. _strlwr (pdst);
  78. pdst = strend (pdst);
  79. ppat += 2;
  80. }
  81. else
  82. if (ppat[1] == 'u') {
  83. strcpy (pdst, p);
  84. _strupr (pdst);
  85. pdst = strend (pdst);
  86. ppat += 2;
  87. }
  88. else
  89. if (ppat[1] == 's') {
  90. strcpy (pdst, p);
  91. pdst = strend (pdst);
  92. ppat += 2;
  93. }
  94. else
  95. *pdst++ = *ppat++;
  96. } else
  97. *pdst++ = *ppat++;
  98. *pdst = 0;
  99. }
  100. i = system (cmdline);
  101. if (HIGH(i) != 0)
  102. exit (1);
  103. }
  104. }
  105. if (HASATTR(b->fbuf.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY)) {
  106. switch (p[strlen(p)-1]) {
  107. case '/':
  108. case '\\':
  109. strcat (p, "*.*");
  110. break;
  111. default:
  112. strcat (p, "\\*.*");
  113. }
  114. forfile (p, srch_attr, walk, NULL);
  115. }
  116. }
  117. dummy;
  118. }
  119. int
  120. __cdecl main (c, v)
  121. int c;
  122. char *v[];
  123. {
  124. struct findType buf;
  125. ConvertAppToOem( c, v );
  126. SHIFT (c, v);
  127. while (c && fSwitChr (**v)) {
  128. switch (*(*v+1)) {
  129. case 'd':
  130. fD = TRUE;
  131. break;
  132. case 'f':
  133. fF = TRUE;
  134. break;
  135. case 'p':
  136. fPrint = TRUE;
  137. break;
  138. case 'h':
  139. srch_attr |= (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM); /* Find hidden/system files */
  140. break;
  141. default:
  142. usage ();
  143. }
  144. SHIFT(c,v);
  145. }
  146. if (c == 0)
  147. usage ();
  148. strcpy (dir, *v);
  149. buf.fbuf.dwFileAttributes = GetFileAttributes( *v );
  150. SHIFT (c, v);
  151. if (c == 0 && !fPrint)
  152. usage ();
  153. if ( !HASATTR(buf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY) )/* DOS doesn't think it's a directory, but */
  154. switch (dir[strlen(dir)-1]) {
  155. case '/': /* ... the user does. */
  156. case '\\':
  157. SETFLAG (buf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY);
  158. break;
  159. default: /* ... it could be a root directory */
  160. _getcwd (cdir, MAX_PATH);
  161. if ( _chdir(dir) == 0 ) {
  162. SETFLAG (buf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY);
  163. _chdir (cdir);
  164. }
  165. }
  166. vec = v;
  167. walk (dir, &buf, NULL);
  168. return( 0 );
  169. }
  170. void usage ()
  171. {
  172. printf ("walk [/d] [/f] [/p] [/h] dir [cmd]\n\n");
  173. printf ("WALK walks a directory heirarchy and for each file, directory or both,\n");
  174. printf ("prints the pathname, runs a program, or both.\n\n");
  175. printf (" -f deal with files\n");
  176. printf (" -d deal with directorys\n");
  177. printf (" if neither is specified, deal with both\n");
  178. printf (" -h Also find hidden directories and files\n");
  179. printf (" -p[rint] print the pathnames on stdout\n");
  180. printf (" [cmd] optional command and arguments. \n");
  181. exit (1);
  182. }