0
#include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> int main(int argc, char** argv) { struct stat *buf; buf = malloc(sizeof(struct stat)); DIR * current_directory_ptr; /* DIR is a type from dirent.h */ struct dirent * next_entry_ptr; /* struct dirent is a type from dirent.h */ char* dirToView [200]; printf("Enter path of desired directory\n"); scanf("%s)", &dirToView); current_directory_ptr = opendir(dirToView); next_entry_ptr = readdir (current_directory_ptr); while(next_entry_ptr != NULL){ printf("File has inode number %d and is called %s \n", (int) next_entry_ptr ->d_ino, next_entry_ptr->d_name); next_entry_ptr=readdir(current_directory_ptr); } char* fileToView [200]; printf("Enter name of desired file\n"); scanf("%s)", &fileToView); stat(fileToView, buf); off_t size = buf -> st_size; printf("Size = %ld \n", size); uid_t owner = buf ->st_uid; printf("owner = %d \n", owner); closedir(current_directory_ptr); return (EXIT_SUCCESS); } 

The intent of this code is to use scan to obtain and output details of the chosen file. Opening a directory works, but when it comes to opening a file the result for both size and owner are 0 regardless of the actual values for that file. I believe the reason for this is that I am printf'ing them as the wrong type, but I am not certain of this. What is the correct system to output the result of stat()?

Edit for clarity

The issue lies with the lines highlighted below

 char* fileToView [200]; printf("Enter name of desired file\n"); scanf("%s)", &fileToView); stat(fileToView, buf); off_t size = buf -> st_size; printf("Size = %ld \n", size); uid_t owner = buf ->st_uid; printf("owner = %d \n", owner); 

Code earlier in the program functions as intended, at least one response has been referring to earlier lines. My apologies for any lack of clarity.

3
  • 1
    Do not do scanf("%s)", &dirToView); and scanf("%s)", &fileToView);, which do cause type mismatch and undefined behavior. Commented Dec 9, 2015 at 15:45
  • What is the correct type in this case? Is there a directory type? Commented Dec 9, 2015 at 15:48
  • Use char dirToView [200]; scanf("%199s)", dirToView); and char fileToView [200]; scanf("%s)", fileToView); Commented Dec 9, 2015 at 15:50

2 Answers 2

2

The correct way to print integers of an unknown size is to use %jd/%ju with an intermediate cast to intmax_t/uintmax_t:

printf("File has inode number %jd and is called %s\n", (intmax_t) next_entry_ptr ->d_ino, next_entry_ptr->d_name); 

intmax_t/uintmax_t are defined as the largest integral signed/unsigned types that can represent the values of any other integral type.

The cast essentially enlarges the integer to the maximum size and then allows you to match the format specifier and the type in a portable way.

Sign up to request clarification or add additional context in comments.

Comments

0

You really should compile with all warnings & debug info (gcc -Wall -Wextra -g if using GCC) and read the documentation of functions like scanf(3) & stat(2). Notice that both could fail and you should test that.

You might try, instead of

/// BAD CODE char* fileToView [200]; printf("Enter name of desired file\n"); scanf("%s)", &fileToView); stat(fileToView, buf); off_t size = buf -> st_size; printf("Size = %ld \n", size); 

Something better like:

char fileToView[200]; memset (fileToView, 0, sizeof(fileToView)); printf("Enter name of desired file:\n"); if (scanf("%199s", fileToView)<=0) { perror("scanf"); exit(EXIT_FAILURE); } struct stat mystat; memset (&mystat, 0, sizeof(mystat)); if (stat(fileToView, &mystat)) { fprintf(stderr, "stat '%s' failed: %s\n", fileToView, strerror(errno)); exit(EXIT_FAILURE); } printf("for file '%s' size = %ld\n", fileToView, (long) mystat.st_size); 

Notice that I am testing success of scanf; I am limiting to 199 the name input (and %s won't accept any space, because it stops scanning at them); I am allocating the mystat as a local variable (no need to heap-allocate it), and I am testing against failure of stat; your fileToView was incorrectly declared as an array of 200 pointers to char. I also have the habit to zero all local variables.

BTW, you could try to strace(1) your program. You'll understand what system calls it is doing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.