Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- /* for linux type systems */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <pwd.h>
- #include <grp.h>
- #include <time.h>
- #include <locale.h>
- #include <langinfo.h>
- #include <stdint.h>
- //#include <fcntl.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <getopt.h>
- /*************************/
- /**********************
- * open directory and
- * print filenames to
- * screen
- *
- +
- *Sep.4, 2022 (Sunday)
- *
- * a work in progress
- */
- void GetFiles(char *name,
- int indent, int ck);
- int check_file_ext(char *name1);
- void seek_directory(char *path);
- char const * sperm(__mode_t mode);
- char * filename_path( char * FName ,
- char * d_name);
- void usage(char *argv[]);
- void flower_line(int s);
- int main(int argc, char **argv) {
- if(argc<=1){usage(argv);};
- // int aflag = 0,bflag = 0;
- int index,c,tp;
- char *path;
- opterr = 0;
- /* required arguments use : after them
- optional arguments :: after them */
- static char string_options[]=
- "::p:t:";
- static struct option long_options[]=
- { /* required_argument 1 { : }
- optional_argument 2 { ::} */
- {"path",1,0,'p'},
- {"type",1,0,'t'},
- {0,0,0,0} // required at end
- };
- while ((c = getopt_long(argc, argv,
- string_options,long_options,
- &index)) != -1)
- {
- switch (c)
- {
- case 'p':
- if( strncmp(optarg,"-",1)==0){
- printf("option -p requires an argument,"
- " optarg[0]= %c\n",optarg[0]);
- exit(0);
- }else{
- // aflag = 1;
- path=strdup(optarg);
- }
- break;
- case 't':
- if( strncmp(optarg,"-",1)==0){
- printf("option -t requires an argument,"
- " optarg[0]= %c\n",optarg[0]);
- exit(0);
- }else{
- printf("case t optarg= %s\n",optarg);
- // bflag = 1;
- tp=atol(optarg);
- }
- break;
- case ':':
- printf(": %c\n",optopt);
- break;
- case '?':
- if (optopt == 'p' ||
- optopt == 't')
- fprintf (stderr, "Option -%c "
- "requires an argument.\n", optopt);
- else if (isprint (optopt))
- fprintf (stderr, "Unknown option"
- " `-%c'.\n", optopt);
- else
- fprintf (stderr,
- "Unknown option "
- "character `\\x%x'.\n",
- optopt);
- break;
- default:
- // abort();
- break;
- } //end switch
- } // end while
- for (index = optind; index < argc; index++)
- printf ("Non-option argument %s\n",
- argv[index]);
- /* debugging */
- /*
- if( isdigit(bflag)) printf("int tp= %d\n",tp);
- if(aflag)printf(" path= %s\n",path);
- if( tp%2==0 || tp%2==1){
- printf("int tp= %d\n",tp);
- }else{
- printf("tp= not int\n");
- }
- */
- switch(tp){
- case 1:
- GetFiles(path,2,tp);
- break;
- case 2:
- GetFiles(path,2,tp);
- break;
- case 3:
- //seek_directory(path);
- GetFiles(path,2,tp);
- break;
- default:
- printf(" wrong argument %d\n",tp);
- exit(0);
- break;
- } // end switch
- return 0;
- }
- char const * sperm(__mode_t mode) {
- static char local_buff[16] = {0};
- int i = 0;
- // user permissions
- if ((mode & S_IRUSR) == S_IRUSR) local_buff[i] = 'r';
- else local_buff[i] = '-';
- i++;
- if ((mode & S_IWUSR) == S_IWUSR) local_buff[i] = 'w';
- else local_buff[i] = '-';
- i++;
- if ((mode & S_IXUSR) == S_IXUSR) local_buff[i] = 'x';
- else local_buff[i] = '-';
- i++;
- // group permissions
- if ((mode & S_IRGRP) == S_IRGRP) local_buff[i] = 'r';
- else local_buff[i] = '-';
- i++;
- if ((mode & S_IWGRP) == S_IWGRP) local_buff[i] = 'w';
- else local_buff[i] = '-';
- i++;
- if ((mode & S_IXGRP) == S_IXGRP) local_buff[i] = 'x';
- else local_buff[i] = '-';
- i++;
- // other permissions
- if ((mode & S_IROTH) == S_IROTH) local_buff[i] = 'r';
- else local_buff[i] = '-';
- i++;
- if ((mode & S_IWOTH) == S_IWOTH) local_buff[i] = 'w';
- else local_buff[i] = '-';
- i++;
- if ((mode & S_IXOTH) == S_IXOTH) local_buff[i] = 'x';
- else local_buff[i] = '-';
- return local_buff;
- }
- void GetFiles(char *name, int indent, int ck)
- {
- DIR *dir;
- struct dirent *entry;
- char path[1024];
- char *newName;
- int file_count=0,dir_count=0;
- newName = strdup(name);
- // to remove leading /
- //so it does not have /dir//sub-dir
- int len = strlen(newName);
- if (newName[len - 1] == '/')
- newName[len - 1] = '\0';
- if (!(dir = opendir(newName)))
- {
- free(newName);
- return;
- }
- while ((entry = readdir(dir)) != NULL)
- {
- if (entry->d_type == DT_DIR)
- {
- printf("if (entry->d_type == DT_DIR) %s\n",entry->d_name);
- if (strcmp(entry->d_name, ".") == 0 ||
- strcmp(entry->d_name, "..") == 0)
- continue;
- printf("\n\nnewName"
- " %s\n\nemyry->d_name %s\n\n",
- newName, entry->d_name);
- snprintf(path, sizeof(path), "%s/%s", newName,
- entry->d_name);
- /* get into sub-directories */
- GetFiles(path, indent + 2,ck);
- }
- switch(ck){
- case 1:
- if ( check_file_ext(entry->d_name) )
- {
- printf(" %s\n", filename_path( newName,
- entry->d_name ));
- file_count++;
- }
- break;
- case 2:
- printf(" %s\n", filename_path(newName,
- entry->d_name));
- file_count++;
- break;
- case 3:
- printf("%d seek_directory\n"
- "%s\n",ck,path);
- seek_directory(path);
- break;
- } // end switch
- dir_count++;
- }
- printf("t \n directorys %d, files %d\n",dir_count,file_count);
- closedir(dir);
- }
- int check_file_ext(char *name1)
- {
- /*
- returns:
- 1 if proper extension found
- 0 if improper extension found
- */
- char *ext = NULL;
- char *extensions[] = {
- ".jpg", ".JPG",".png", ".PNG",
- ".jpeg",".JPEG", ".xpm", ".gif",
- ".cpp", ".c",".h" };
- ext = strrchr(name1, '.');
- if (!ext)
- return 0;
- for (int i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
- if ( !strcmp( extensions[i], ext) )
- return 1;
- return 0;
- }
- void seek_directory(char *path)
- {
- struct dirent *de;
- struct stat statbuf;
- struct passwd *pwd;
- struct group *grp;
- struct tm *tm;
- char datestring[256];
- DIR *dir = opendir(path);
- if(dir == NULL){
- printf("could'not open %s\n",path);
- return;
- }
- while((de=readdir(dir))!=NULL){
- //printf(" %s\n",de->d_name);
- if(stat(de->d_name, &statbuf)== -1)
- continue;
- if( (strcmp(de->d_name,"."))==0 ||
- (strcmp(de->d_name,".."))==0)
- printf("found %s\n",de->d_name);
- /* print type,permossions & num of
- links. */
- printf(" %10.10s\n",sperm(statbuf.st_mode));
- printf(" %4d\n",statbuf.st_nlink);
- /* print owner name if found using
- getpwiid(). */
- if((pwd=getpwuid(statbuf.st_uid))!=NULL)
- {
- printf(" %-8.8s\n",pwd->pw_name);
- }else{
- printf(" %-8d\n",statbuf.st_uid);
- }
- /* print groupname if it is found */
- if( (grp=getgrgid(statbuf.st_gid) )!=NULL)
- {
- printf(" %-8.9s\n",grp->gr_name);
- }
- else
- {
- printf(" %-8d\n", statbuf.st_gid);
- }
- /* print size of file */
- printf(" %9jd\n",(intmax_t)statbuf.st_size);
- tm=localtime(&statbuf.st_mtime);
- /* get localized date string. */
- strftime(datestring,sizeof(datestring),
- nl_langinfo(D_T_FMT),tm);
- printf(" %s %s\n",datestring,de->d_name);
- }
- closedir(dir);
- }
- char * filename_path( char * FName,char * d_name ){
- char *fullname =
- malloc(strlen(FName)+
- strlen(d_name)+2);
- //+2 null-terminator and /
- if ( fullname == NULL)
- {
- printf("Not enough Memory\n");
- exit(1);
- }
- /* create full directory path and filename */
- strcpy(fullname, FName);
- strcat(fullname, "/");
- strcat(fullname, d_name);
- //printf("%s\n", fullname);
- char * fullpath=strdup(fullname);
- free(fullname);
- return fullpath;
- }
- void flower_line(int s){
- for(int i=0;i<s;i++) {
- printf("*");
- }
- printf("\n");
- }
- void usage(char *argv[]){
- flower_line(51);
- printf("Usage:\n.%s <option> [argument] <option> [argument]"
- "\noptions\n <-path , -p> [path to search]\n<-type -t> \n"
- "[1 full infomation search : 2 file search]\n",argv[0]);
- flower_line(42);
- }
Advertisement
Add Comment
Please, Sign In to add comment