0

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;
}
Troy Cosentino
  • 4,658
  • 9
  • 37
  • 59

4 Answers4

1

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;
}
phonetagger
  • 7,701
  • 3
  • 31
  • 55
  • awesome this works for me too. However, i was under the impression that d_name was going to return the name of the directory but it is either printing ., .bash_logout, or srv(srv is where the root is). Is there any way i can get it to print what you see when you use like ls or somthing like that? – Troy Cosentino Dec 07 '12 at 02:26
  • @TroyCosentino - It's been a while, and I don't have time right now to look into it more, but I think you keep calling readdir(), and it'll keep giving you the next file in the directory on each call. I can't remember what the end-of-directory condition is. Do you have the `man` command available? – phonetagger Dec 07 '12 at 02:44
  • @TroyCosentino - See my new answer for additional changes that do what I think you want. – phonetagger Dec 07 '12 at 16:39
0

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.

aakash
  • 751
  • 5
  • 18
0

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

LtWorf
  • 7,286
  • 6
  • 31
  • 45
0

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;
}
phonetagger
  • 7,701
  • 3
  • 31
  • 55