Advertisement
shiftdot515

finddot.c

Aug 16th, 2019
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. void do_dir(const char * dirpath);
  8.  
  9. int main ( int argc, char * argv[] )
  10. {
  11.   do_dir(".");
  12.   return 0;
  13. }
  14.  
  15. char pathbuf[(4096*2)];
  16. /* I doubt that was it */
  17. /* no, it was, that needs be malloced.
  18.    God, when did I start this weak stab at this?
  19.    *  right, a program that uses normal C string ops, stays runnable
  20.    * longer, so actually, can perform, better, even though
  21.    * counting strlen all the time, *is* wasteful, it's always
  22.    * interruptable, the process is, when doing byte operations.
  23.    * Programs spend resources in the Economy of Operations.
  24.    * just use a 4GL, then anyone half try is basically just as GOOD.
  25.    * hahah!
  26.    *
  27.  */
  28.  
  29. void do_dir(const char * dirpath)
  30. {
  31.   struct dirent entry,*entry_ptr;
  32.   long loc;
  33.   DIR * dir;
  34.   char * pathbuf;
  35.   {
  36.     int len;
  37.     len=4096+strlen(dirpath);
  38.     pathbuf=malloc(len);
  39.     if ( pathbuf == NULL ) {
  40.       perror("malloc pathbuf");
  41.       goto END;
  42.     }
  43.   }
  44.      
  45.  
  46.  
  47.   dir = opendir(dirpath);
  48.   if ( NULL == dir )
  49.     goto END;
  50.   /* skip over . and .., but check for errors */
  51.   if ( readdir_r(dir, &entry, &entry_ptr) ) {
  52.     perror("reading dot");
  53.     goto END;
  54.   }
  55.   if ( readdir_r(dir, &entry, &entry_ptr) ) {
  56.     perror("reading dotdot");
  57.     goto END;
  58.   }
  59.  
  60.   loc=telldir(dir);
  61.   do {
  62.     readdir_r(dir, &entry, &entry_ptr);
  63.       if ( entry_ptr == &entry )
  64.         printf("%s/%s\n",dirpath,&entry.d_name);
  65.   } while ( NULL != entry_ptr ) ;
  66.   seekdir(dir,loc); /* jump back to after . and .. */
  67.   /* now recurse into directories */
  68.   /* ok, dan? but ur not recursing ? */
  69.   do {
  70.     readdir_r(dir, &entry, &entry_ptr);
  71.     if ( entry_ptr == &entry && entry.d_type == DT_DIR ) {
  72.       strcpy(pathbuf,dirpath);
  73.       strcat(pathbuf,"/");
  74.       strcat(pathbuf,entry.d_name);
  75.       do_dir(pathbuf);
  76.     }
  77.   } while ( NULL != entry_ptr ) ;
  78.  END:
  79.   closedir(dir);
  80.   free(pathbuf);
  81. }
  82. /* I call this dot and compile# gcc -o dot findot.c -O1 -g */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement