Question
#include #include #include #include #include #include #include void readable(char *path); int isDirectory(char *pathname); void printFile(int index, char *pathname); int main(int argc, char *argv[]){ char* path
#include #include #include #include #include #include #include
void readable(char *path); int isDirectory(char *pathname); void printFile(int index, char *pathname);
int main(int argc, char *argv[]){
char* path = (char* ) malloc(sizeof(char) * 4096); //main path memory
if(argc == 1){ //if there is no argument list the redabile files from current directory getcwd(path, 4096); strcat(path, "/"); } else if(argc == 2){ //path in the argument path = argv[1]; } else if(argc > 2){ fprintf(stderr, "To many arguments"); } readable(path); fprintf(stderr," Finished listing "); return 0; }
void readable(char *path){
int i = 1; DIR *directory = opendir(path); //main directory open struct dirent *d; //dirent struct stat area, *s = &area;
//mode_t newMode = S_IRUSR | S_IWUSR;
if(directory == NULL){ //if the directory is empty or invalid fprintf(stderr, " Invalid or empty directory! "); exit(1); }
fprintf(stderr," List of readable files in the directory: ");
while(d = readdir(directory)){ //read until there are no more files to be red char *temp;
if(strcmp(d -> d_name, ".") == 0 || strcmp(d -> d_name, "..") == 0){ //if there is a directory skip continue; }
temp = (char* ) calloc(sizeof(char), 4096); //new path from the main path strcpy(temp, path); strcat(temp, d -> d_name);
if(isDirectory(temp)){ //if its a directory recurvisely go throw the new path if(access(temp, R_OK | X_OK) == 0){ //if there is access to the file //printf("%s ", temp); strcat(temp, "/"); readable(temp); //new path from the main path called recursively } continue; } else if((stat(temp, s) == 0) && S_ISREG(s -> st_mode)){ // if its not a directory then it is a file and print it if(access(temp, R_OK) == 0){ //if there is access to the file printFile(i++,temp); //print the file } } //printf("%s ", temp); free(temp); //free the memory for the temperary path } closedir(directory); //close directory }
int isDirectory(char *pathname){ //given code struct stat area, *s = &area; return (stat(pathname, s) == 0) && S_ISDIR(s->st_mode); }
void printFile(int index, char *pathname){ //print file printf("%i: %s ", index, pathname); }
The C code work fine, but it need to change a liitle bit.
In the Bold part, can you help me change from void readable(char *path)
to int readable(char *inputPath). It means we cannot use void, just use int for the function.
Thank you for helping.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started