userxbw

Look in directories/sub directories print files C

Sep 5th, 2022
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. /* for linux type systems */
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <pwd.h>
  9. #include <grp.h>
  10. #include <time.h>
  11. #include <locale.h>
  12. #include <langinfo.h>
  13. #include <stdint.h>
  14.  
  15. //#include <fcntl.h>
  16. #include <ctype.h>
  17. #include <unistd.h>
  18. #include <getopt.h>
  19.  
  20. /*************************/
  21.  
  22. /**********************
  23.  * open directory and
  24.  * print filenames to
  25.  * screen
  26.  *
  27.  +
  28.  *Sep.4, 2022 (Sunday)
  29.  *
  30.  * a work in progress
  31.  */
  32.  
  33. void GetFiles(char *name,
  34.         int indent, int ck);
  35. int check_file_ext(char *name1);
  36. void seek_directory(char *path);
  37. char const * sperm(__mode_t mode);
  38. char * filename_path( char * FName ,
  39.         char * d_name);
  40. void usage(char *argv[]);
  41. void flower_line(int s);
  42.  
  43. int main(int argc, char **argv) {
  44. if(argc<=1){usage(argv);};
  45.  
  46.    // int aflag = 0,bflag = 0;
  47.     int  index,c,tp;
  48.     char *path;
  49.     opterr = 0;
  50.  
  51.  /* required arguments use : after them
  52.     optional arguments :: after them */
  53.   static char string_options[]=
  54.   "::p:t:";
  55.   static struct option long_options[]=
  56.   { /* required_argument 1 { : }
  57.        optional_argument 2 { ::} */
  58.     {"path",1,0,'p'},
  59.     {"type",1,0,'t'},
  60.     {0,0,0,0} // required at end
  61.   };
  62.   while ((c = getopt_long(argc, argv,
  63.     string_options,long_options,
  64.     &index)) != -1)
  65.   {
  66.     switch (c)
  67.       {
  68.       case 'p':
  69.         if( strncmp(optarg,"-",1)==0){
  70.         printf("option -p requires an argument,"
  71.         " optarg[0]= %c\n",optarg[0]);
  72.         exit(0);
  73.         }else{
  74.      //   aflag = 1;
  75.         path=strdup(optarg);
  76.         }
  77.         break;
  78.       case 't':
  79.         if( strncmp(optarg,"-",1)==0){
  80.         printf("option -t requires an argument,"
  81.         " optarg[0]= %c\n",optarg[0]);
  82.         exit(0);
  83.         }else{
  84.         printf("case t optarg= %s\n",optarg);
  85.        // bflag = 1;
  86.         tp=atol(optarg);
  87.         }
  88.         break;
  89.         case ':':
  90.             printf(": %c\n",optopt);
  91.             break;
  92.         case '?':
  93.         if (optopt == 'p' ||
  94.         optopt == 't')
  95.           fprintf (stderr, "Option -%c "
  96.           "requires an argument.\n", optopt);
  97.         else if (isprint (optopt))
  98.           fprintf (stderr, "Unknown option"
  99.             "  `-%c'.\n", optopt);
  100.         else
  101.           fprintf (stderr,
  102.                    "Unknown option "
  103.                     "character `\\x%x'.\n",
  104.                    optopt);
  105.            break;
  106.             default:
  107.   //          abort();
  108.             break;
  109.         } //end switch
  110.       } // end while
  111.   for (index = optind; index < argc; index++)
  112.     printf ("Non-option argument %s\n",
  113.              argv[index]);
  114. /* debugging */
  115. /*
  116. if( isdigit(bflag)) printf("int tp= %d\n",tp);
  117. if(aflag)printf(" path= %s\n",path);
  118. if( tp%2==0 || tp%2==1){
  119. printf("int tp= %d\n",tp);
  120. }else{
  121.  printf("tp= not int\n");
  122. }
  123. */
  124. switch(tp){
  125. case 1:
  126.     GetFiles(path,2,tp);
  127. break;
  128. case 2:
  129.     GetFiles(path,2,tp);
  130. break;
  131. case 3:
  132.     //seek_directory(path);
  133.     GetFiles(path,2,tp);
  134. break;
  135. default:
  136. printf(" wrong argument %d\n",tp);
  137. exit(0);
  138. break;
  139. } // end switch
  140.  
  141. return 0;
  142. }
  143.  
  144.  
  145. char const * sperm(__mode_t mode) {
  146.     static char local_buff[16] = {0};
  147.     int i = 0;
  148.     // user permissions
  149.     if ((mode & S_IRUSR) == S_IRUSR) local_buff[i] = 'r';
  150.     else local_buff[i] = '-';
  151.     i++;
  152.     if ((mode & S_IWUSR) == S_IWUSR) local_buff[i] = 'w';
  153.     else local_buff[i] = '-';
  154.     i++;
  155.     if ((mode & S_IXUSR) == S_IXUSR) local_buff[i] = 'x';
  156.     else local_buff[i] = '-';
  157.     i++;
  158.     // group permissions
  159.     if ((mode & S_IRGRP) == S_IRGRP) local_buff[i] = 'r';
  160.     else local_buff[i] = '-';
  161.     i++;
  162.     if ((mode & S_IWGRP) == S_IWGRP) local_buff[i] = 'w';
  163.     else local_buff[i] = '-';
  164.     i++;
  165.     if ((mode & S_IXGRP) == S_IXGRP) local_buff[i] = 'x';
  166.     else local_buff[i] = '-';
  167.     i++;
  168.     // other permissions
  169.     if ((mode & S_IROTH) == S_IROTH) local_buff[i] = 'r';
  170.     else local_buff[i] = '-';
  171.     i++;
  172.     if ((mode & S_IWOTH) == S_IWOTH) local_buff[i] = 'w';
  173.     else local_buff[i] = '-';
  174.     i++;
  175.     if ((mode & S_IXOTH) == S_IXOTH) local_buff[i] = 'x';
  176.     else local_buff[i] = '-';
  177.     return local_buff;
  178. }
  179.  
  180.  
  181.  
  182. void GetFiles(char *name, int indent, int ck)
  183. {
  184.     DIR *dir;
  185.     struct dirent *entry;
  186.     char path[1024];
  187.     char *newName;
  188.     int file_count=0,dir_count=0;
  189.     newName = strdup(name);
  190.     // to remove leading /
  191.     //so it does not have /dir//sub-dir
  192.     int len = strlen(newName);
  193.     if (newName[len - 1] == '/')
  194.         newName[len - 1] = '\0';
  195.  
  196.      if (!(dir = opendir(newName)))
  197.      {
  198.         free(newName);
  199.         return;
  200.     }
  201.  
  202.  
  203.     while ((entry = readdir(dir)) != NULL)
  204.     {
  205.  
  206.  
  207.         if (entry->d_type == DT_DIR)
  208.         {
  209. printf("if (entry->d_type == DT_DIR) %s\n",entry->d_name);
  210.  
  211.             if (strcmp(entry->d_name, ".") == 0 ||
  212.                 strcmp(entry->d_name, "..") == 0)
  213.                 continue;
  214.      printf("\n\nnewName"
  215.      " %s\n\nemyry->d_name %s\n\n",
  216.       newName,  entry->d_name);
  217.  
  218.   snprintf(path, sizeof(path), "%s/%s", newName,
  219.              entry->d_name);
  220.        /* get into sub-directories */
  221.         GetFiles(path, indent + 2,ck);
  222.         }
  223.         switch(ck){
  224.         case 1:
  225.         if ( check_file_ext(entry->d_name) )
  226.         {
  227.          printf(" %s\n", filename_path( newName,
  228.                          entry->d_name ));
  229.          file_count++;
  230.         }
  231.         break;
  232.         case 2:
  233.             printf(" %s\n", filename_path(newName,
  234.             entry->d_name));
  235.             file_count++;
  236.         break;
  237.         case 3:
  238.         printf("%d seek_directory\n"
  239.         "%s\n",ck,path);
  240.         seek_directory(path);
  241.         break;
  242.         } // end switch
  243.             dir_count++;
  244.  
  245.     }
  246.     printf("t \n directorys %d, files %d\n",dir_count,file_count);
  247.  
  248.  
  249.    closedir(dir);
  250.  
  251. }
  252.  
  253. int check_file_ext(char *name1)
  254. {
  255.         /*
  256.  
  257.     returns:
  258.  
  259.          1 if proper extension found
  260.  
  261.          0 if improper extension found
  262.  
  263.     */
  264.  
  265.     char *ext = NULL;
  266.     char *extensions[] = {
  267.     ".jpg", ".JPG",".png", ".PNG",
  268.     ".jpeg",".JPEG", ".xpm", ".gif",
  269.     ".cpp", ".c",".h" };
  270.  
  271.     ext = strrchr(name1, '.');
  272.     if (!ext)
  273.          return 0;
  274.  
  275.     for (int i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
  276.        if ( !strcmp( extensions[i], ext) )
  277.             return 1;
  278. return 0;
  279. }
  280.  
  281.  
  282. void seek_directory(char *path)
  283. {
  284.  
  285. struct dirent *de;
  286. struct stat statbuf;
  287. struct passwd *pwd;
  288. struct group *grp;
  289. struct tm *tm;
  290. char   datestring[256];
  291.  
  292. DIR *dir = opendir(path);
  293. if(dir == NULL){
  294.     printf("could'not open %s\n",path);
  295.     return;
  296.     }
  297.     while((de=readdir(dir))!=NULL){
  298.         //printf(" %s\n",de->d_name);
  299.         if(stat(de->d_name, &statbuf)== -1)
  300.             continue;
  301.         if( (strcmp(de->d_name,"."))==0 ||
  302.             (strcmp(de->d_name,".."))==0)
  303.             printf("found %s\n",de->d_name);
  304.         /* print type,permossions & num of
  305.            links. */
  306.         printf(" %10.10s\n",sperm(statbuf.st_mode));
  307.         printf(" %4d\n",statbuf.st_nlink);
  308.         /* print owner name if found using
  309.            getpwiid(). */
  310.     if((pwd=getpwuid(statbuf.st_uid))!=NULL)
  311.     {
  312.         printf(" %-8.8s\n",pwd->pw_name);
  313.     }else{
  314.         printf(" %-8d\n",statbuf.st_uid);
  315.     }
  316.         /* print groupname if it is found */
  317.     if( (grp=getgrgid(statbuf.st_gid) )!=NULL)
  318.     {
  319.             printf(" %-8.9s\n",grp->gr_name);
  320.     }
  321.     else
  322.    {
  323.            printf(" %-8d\n", statbuf.st_gid);
  324.    }
  325.        /* print size of file */
  326.        printf(" %9jd\n",(intmax_t)statbuf.st_size);
  327.  
  328.        tm=localtime(&statbuf.st_mtime);
  329.        /* get localized date string. */
  330.        strftime(datestring,sizeof(datestring),
  331.        nl_langinfo(D_T_FMT),tm);
  332.  
  333.        printf(" %s %s\n",datestring,de->d_name);
  334.  
  335.     }
  336.    closedir(dir);
  337. }
  338.  
  339. char * filename_path( char * FName,char * d_name ){
  340.  
  341.             char *fullname =
  342.              malloc(strlen(FName)+
  343.              strlen(d_name)+2);
  344.             //+2 null-terminator and /
  345.             if ( fullname == NULL)
  346.             {
  347.                 printf("Not enough Memory\n");
  348.                 exit(1);
  349.             }
  350.        /*  create full directory path and filename */
  351.             strcpy(fullname, FName);
  352.             strcat(fullname, "/");
  353.             strcat(fullname, d_name);
  354.             //printf("%s\n", fullname);
  355.  
  356.             char * fullpath=strdup(fullname);
  357.             free(fullname);
  358.       return fullpath;
  359. }
  360. void flower_line(int s){
  361. for(int i=0;i<s;i++) {
  362.     printf("*");
  363.     }
  364.     printf("\n");
  365. }
  366. void usage(char *argv[]){
  367. flower_line(51);
  368. printf("Usage:\n.%s <option> [argument] <option> [argument]"
  369. "\noptions\n <-path , -p> [path to search]\n<-type -t> \n"
  370. "[1 full infomation search : 2 file search]\n",argv[0]);
  371. flower_line(42);
  372. }
  373.  
Advertisement
Add Comment
Please, Sign In to add comment