Hmmmmm Hmmmmm - 2 months ago 9
C Question

checking directory paths ends with ".", ".."

I have a programming problem that I hope someone out there can help me with. I am trying to learn C programming for a task at work and I have set myself a little project, which consists of reading down a file tree including all the sub directories obtaining information about each file.

The problem I get is that my program dosen't ignore the directory path ends with either /. or /.. and when it prints all the directories, I want to give space in front of the subdirectories for the readablity.

so the error is occured at this part:

int isDir(const char *parent, char *name) {

struct stat st_buf; // file info
char buf[BUF_SIZE];

if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
return 0;
}
char *path = malloc(strlen(name) + strlen(parent) + 2);
//sprintf(char *buf, const char *format, [arg1],[arg2],...)
sprintf(path, "%s/%s", parent, name);
stat(path, &st_buf); //

return S_ISDIR(st_buf.st_mode); //directory
}


And this is main and list function:

int list(const char *name) {
DIR *dirp = opendir(name);
struct dirent *dentry;
char buf[BUF_SIZE];

while ((dentry = readdir(dirp)) != NULL) {
char *dir_name = dentry->d_name;

printf(" %s\n", dir_name);

//if it's dir, then go into dir
if (isDir(name, dir_name)) { //name : parent, dir_name : child
chdir(dir_name);
getcwd(buf, BUF_SIZE);
list(buf);
}
}
closedir(dirp);
}

int main()
{
list(".");
return 0;
}


The result is like this:


hm1.c
Data
lab1.txt
result1
lab3.txt
.
..
.
..
result2
lab3.txt
.
..
result3
lab3.txt
.
..
a.c
.
..
a.out



result I want to print


hm1.c
Data
lab1.txt
result1
lab3.txt
result2
lab3.txt
result3
lab3.txt
a.c
a.out


Answer

is this what you mean? I add 4 space when I see a directory and remove 4 after coming out of directory

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

#define BUF_SIZE 1024

int space_count = 0;

int isDir(const char *parent, char *name) {

    struct stat st_buf; // file info
    char buf[BUF_SIZE];

    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
        return 0;
    }   
    char *path = malloc(strlen(name) + strlen(parent) + 2);
    //sprintf(char *buf, const char *format, [arg1],[arg2],...)
    sprintf(path, "%s/%s", parent, name);
    stat(path, &st_buf); //

    return S_ISDIR(st_buf.st_mode); //directory
}


int list(const char *name) {
    DIR *dirp = opendir(name);
    struct dirent *dentry;
    char buf[BUF_SIZE];

    while ((dentry = readdir(dirp)) != NULL) {
    char *dir_name = dentry->d_name;        
        if((strcmp(dir_name, ".") != 0) && (strcmp(dir_name, "..") != 0)){
            for(int i = 0; i<space_count;i++)printf(" ");
            printf("%s\n", dir_name);
    }

        //if it's dir, then go into dir
        if (isDir(name, dir_name)) { //name : parent, dir_name : child
            chdir(dir_name);
            getcwd(buf, BUF_SIZE);
            space_count+=4;
            list(buf);
            space_count-=4;
        }
    }
    closedir(dirp);
}

int main()
{
    list(".");
    return 0;
}
Comments