2007年9月19日星期三

Directory Traversal using Recursion by Glen E. Gardner, Jr

Directory Traversal using Recursion by Glen E. Gardner, Jr

A recursive directory tree traversal that finds the path and names for all files in the specified path. Written by Glen E. Gardner, Jr

Submitted By: born2c0de
Actions:
Add Comment
Views: 7,216

Language: C++

Last Modified: 2005-10-26 06:31:28

Snippet


  1. /* DIRTREE.C V 1.0 by Glen E. Gardner, Jr. */
  2. /* A recursive directory tree traversal that finds the path and names */
  3. /* for all files in the specified path. */
  4. /* this program was written and compiled using Borland C++ 5.0 on */
  5. /* Microsoft Windows NT 4.0 */
  6. /* The program was written with no MS API function calls, for as much */
  7. /* OS independence as was practical. */
  8. /* It should compile and run on DOS, Win 3.X , Win 95/98 and Win NT. */
  9. /* This program was written in ansi C as much as was possible, UNIX users might */
  10. /* find this program easy to port. */
  11. #include
  12. #include
  13. #include
  14. #include
  15. #include
  16. int scandir(char *,char *);
  17. int getsize(char *);
  18. void doit(char *);
  19. int scanstring(char *,char *);
  20. void main(int argc,char *argv[])
  21. {
  22. chdir(argv[1]);
  23. if (argc != 2)
  24. {
  25. printf("usage: DIRTREE \n");
  26. exit(1);
  27. }
  28. doit(".");
  29. exit(0);
  30. }
  31. int getsize(char *dirname)
  32. {/* finds and returns the size of the directory entry (in bytes) */
  33. DIR *dir;
  34. struct dirent *ent;
  35. int size=0;
  36. if ((dir = opendir(dirname)) == NULL)
  37. {
  38. perror("Unable to open directory");
  39. return(NULL);
  40. }
  41. while ((ent = readdir(dir)) != NULL);
  42. rewinddir(dir);
  43. while ((ent = readdir(dir)) != NULL)size=size+sizeof(ent->d_name)+1;
  44. if (closedir(dir) != 0)
  45. perror("Unable to close directory");
  46. return(size);
  47. }
  48. int scandir(char *dirname,char *entries)
  49. {/* scan the directory and store the entries in a buffer */
  50. DIR *dir;
  51. struct dirent *ent;
  52. int count=1;
  53. char name[256];
  54. if ((dir = opendir(dirname)) == NULL)
  55. {
  56. perror("Unable to open directory");
  57. return(0);
  58. }
  59. while ((ent = readdir(dir)) != NULL)count++;
  60. rewinddir(dir);
  61. while ((ent = readdir(dir)) != NULL)
  62. {
  63. strcpy(name,ent->d_name);
  64. sprintf(entries,"%s",name);
  65. entries=entries+strlen(name)+1;
  66. count++;
  67. }
  68. if (closedir(dir) != 0)
  69. perror("Unable to close directory");
  70. return(count);
  71. }
  72. void doit(char *dirname)
  73. {/* scan the buffer and recursively enter any directories found */
  74. int size;
  75. char *entries;
  76. char name[256];
  77. char Path[256];
  78. char *path;
  79. char oldpath[256];
  80. char old[256]="none";
  81. FILE *filein;
  82. path=Path;
  83. size=getsize(dirname);
  84. entries=(char *)malloc(size);
  85. scandir(dirname,entries);
  86. getcwd(Path,256);
  87. strcpy(old,Path);
  88. while(scanstring(name,entries)!=EOF)
  89. {
  90. /* store the path for use later */
  91. getcwd(Path,256);
  92. if((strcmp(name,"."))&&(strcmp(name,"..")))
  93. {
  94. /* this is where the valid path variable can be found */
  95. if(!strcmp(Path,old))printf("PATH: %s\n",Path);
  96. strcpy(oldpath,Path);
  97. /* add the next entry to the path for testing */
  98. strcat(Path,"\\");
  99. strcat(Path,name);
  100. /* see if the entry is a file */
  101. /* here is where the valid filename associated with the path can be found */
  102. if((filein=fopen(Path,"r"))!=0)
  103. {
  104. printf("NAME: %s\n",name);
  105. fclose(filein);
  106. }
  107. else
  108. {
  109. if(chdir(Path)==0)
  110. {/* if the entry is a valid directory, go there */
  111. getcwd(path,256);
  112. /* start the recursive traversal */
  113. doit(".");
  114. /* restore the path */
  115. strcpy(Path,oldpath);
  116. chdir(Path);
  117. getcwd(path,256);
  118. }
  119. }
  120. /* set the sentinel variable for state changes */
  121. strcpy(old,Path);
  122. strcpy(Path,oldpath);
  123. chdir(Path);
  124. getcwd(path,256);
  125. }
  126. entries=entries+strlen(name)+1;
  127. }
  128. return;
  129. }
  130. int scanstring(char *string,char *buffer)
  131. {
  132. int i;
  133. int size;
  134. char *temp;
  135. size=0;
  136. temp=buffer;
  137. while(*buffer!=NULL)
  138. {
  139. size++;
  140. buffer++;
  141. }
  142. buffer=temp;
  143. for(i=0;i<=size;i++)
  144. {
  145. *string=*buffer;
  146. buffer++;
  147. string++;
  148. }
  149. *string=*buffer;
  150. buffer=buffer-2;
  151. if(*buffer==NULL)return(EOF);
  152. return(size);
  153. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to to leave comments.

没有评论: