Question

This is the code i have so far, it finds where the root is fine, but when i add the line:

printf("        name: %s\n", readdir(opendir(cur_spot))->d_name);

it changes cur_spot and adds weird characters to it (filename: .~?) is what it prints.. any idea why this is happening?

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

int main(int argc, char *argv[]) {
    struct stat file_stats;
    struct stat parent_stats;
    struct dirent temp_dir;

    char cwd_name[256]; //directory name
    char cur_spot[256]; //current directory spot

    cur_spot[0] = '.';
    stat(cur_spot, &file_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", file_stats.st_ino);


    strcat(cur_spot, ".");
    stat(cur_spot, &parent_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", parent_stats.st_ino);

    while(file_stats.st_ino != parent_stats.st_ino) {
        printf("not at root yet\n\n");

        stat(cur_spot, &file_stats);
        printf("    current child\n");
        printf("        inode: %ld\n", file_stats.st_ino);
        printf("        name: %s\n", readdir(opendir(cur_spot))->d_name);
        strcat(cur_spot, "/..");
        stat(cur_spot, &parent_stats);
        printf("    current parent\n");
        printf("        inode: %ld\n", parent_stats.st_ino);
    }
        printf("at root\n");


    return 0;
}
Was it helpful?

Solution

This works fine for me on GCC:

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

int main(int argc, char *argv[]) {
    struct stat file_stats;
    struct stat parent_stats;
    struct dirent temp_dir;
    DIR *dirp;

    char cwd_name[256]; //directory name
    char cur_spot[256]; //current directory spot

    strcpy(cur_spot,".");  // <------------ changed
    stat(cur_spot, &file_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", file_stats.st_ino);


    strcat(cur_spot, ".");
    stat(cur_spot, &parent_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", parent_stats.st_ino);

    while(file_stats.st_ino != parent_stats.st_ino) {
        printf("not at root yet\n\n");

        stat(cur_spot, &file_stats);
        printf("    current child\n");
        printf("        inode: %ld\n", file_stats.st_ino);
        dirp=opendir(cur_spot); // <----------------added
        printf("        name: %s\n", readdir(dirp)->d_name);  // <----changed
        closedir(dirp); // <------------------------added
        strcat(cur_spot, "/..");
        stat(cur_spot, &parent_stats);
        printf("    current parent\n");
        printf("        inode: %ld\n", parent_stats.st_ino);
    }
        printf("at root\n");


    return 0;
}

OTHER TIPS

You should add a NULL character at the end of a character array.

Wrong:

cur_spot[0] = '.';

Right:

cur_spot[0] = '.';
cur_spot[1] = '\0';

The NULL character terminates a string. A string may or may not be initialized to NULL by default.

You do a bunch of "opendir" in a loop but i see no traces of "closedir". You should be punished...

This goes beyond the scope of your initial question, so I'm adding it as a separate answer from my first. As I said in my comment under my first answer, if you're looking to print the filenames of all files within the directory, you just keep calling readdir() until some termination condition, which from reading the man page is simply a NULL return value. BTW, if your man page says READDIR(2) or This is not the function you are interested in, what you really want to do is see the section 3 man page on readdir, which you can get with man -s 3 readdir.

Anyway, I modded the code from my first answer to do what I think you want:

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

int main(int argc, char *argv[]) {
    struct stat file_stats;
    struct stat parent_stats;
    struct dirent temp_dir;
    DIR *dirp;
    struct dirent* dent;

    char cwd_name[256]; //directory name
    char cur_spot[256]; //current directory spot

    strcpy(cur_spot,".");          // <------------- changed
    stat(cur_spot, &file_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", file_stats.st_ino);


    strcat(cur_spot, ".");
    stat(cur_spot, &parent_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", parent_stats.st_ino);

    while(file_stats.st_ino != parent_stats.st_ino) {
        printf("not at root yet\n\n");

        stat(cur_spot, &file_stats);
        printf("    current child\n");
        printf("        inode: %ld\n", file_stats.st_ino);
        dirp=opendir(cur_spot);    // <------------- added
        do {                       // <------------- NEW
            dent = readdir(dirp);  // <------------- NEW
            if (dent)              // <------------- NEW
                printf("        name: %s\n", dent->d_name);
        } while (dent);            // <------------- NEW
        closedir(dirp);            // <------------- added
        strcat(cur_spot, "/..");
        stat(cur_spot, &parent_stats);
        printf("    current parent\n");
        printf("        inode: %ld\n", parent_stats.st_ino);
    }
        printf("at root\n");


    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top