Working on code that will open up a directory, read its contents, and print out info about each file. It examines the command line and will print out the contents of the specified directory or the current directory if no arguments provided. The code below is almost the exact same as the code used to print out the current directory. The only differece is that in the first if statement instead of argv[1] there is ".". The code below hits the first if statement in the while loop(The one after I set result equal to the current file) and then says for every file in the directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
. thaddueus thaddueus 4096 Wed May 1 18:34:42 2013
Error while loading statBuffer: : No such file or directory
Here is the code I use.
else if(argc > 1){
if((dir = opendir(argv[1])) == NULL){
perror("opendir() error");
}else{
while((entry = readdir(dir)) != NULL){
result = stat(entry->d_name, &statBuf);
if(result == -1)perror("Error while loading statBuffer: ");
else{
/*Line of code I added directly below this based upon David's suggestion*/
snprint(pathname, sizeof(pathname), "%s/%s", argv[1], entry->d_name);
/*Addition cause Segmentation fault core dumped*/
if(strncmp(entry->d_name, "..",100) == 0 ||strncmp(entry->d_name, ".",100))
continue;
errno = 0;
pws = getpwuid(statBuf.st_uid);
if(errno != 0)
perror("Error while retriving username: ");
errno = 0;
grp = getgrgid(statBuf.st_gid);
if(errno != 0) perror("Error while retriving groupname: ");
/*Prints the filename*/
printf("%s", entry->d_name);
/*Prints the username*/
printf("\t%s", pws->pw_name);
/*Prints the groupname*/
printf("\t%s", grp->gr_name);
/*Prints the file size*/
printf("\t%ld", (long)statBuf.st_size);
/*Prints the last modification time*/
printf("\t%s\n", ctime(&(statBuf.st_mtime)));
}
}
closedir(dir);
}
}
EDIT: That
. thaddueus thaddueus 4096 Wed May 1 18:34:42 2013
is a hidden file. Not sure while that will print and the others won't.
EDIT1: Sorry. Forgot to add that it is getting the filenames.
EDIT2: When I remove the error statement it stops printing out the error statements and just prints out the hidden file.