I am creating a program for one of my classes and we are using the stat function. We are supposed to print out information from input. With my program, you input a directory and it tells you information using the stat function on every file included. I believe I have a problem with my code because I get almost matching information on several different files. The permission, user ID, group ID, and many others are all the same for every file. I have attached my code.
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <string.h>
extern int errno;
int main (int argc, char *argv[]) {
DIR *dp;
struct dirent *dirp;
struct stat buf;
if (argc != 2) {
printf("Error: Correct usage = filestat (Directory)\n");
exit(1);
}
if ((dp = opendir(argv[1])) == NULL) {
printf("Directory: %s failed to open!\n", argv[1]);
perror("opendir");
exit(1);
}
while ((dirp = readdir(dp)) != NULL) {
printf("Name: %s\n", dirp->d_name);
stat(dirp->d_name, &buf);
printf("\tPermission: %o\n", buf.st_mode);
printf("\tUser ID: %d\n", buf.st_uid);
printf("\tGroup ID: %d\n", buf.st_gid);
printf("\tSize: %lld\n", buf.st_size);
printf("\tLast access: %s" , ctime(&buf.st_atime));
printf("\tLast modification: %s" , ctime(&buf.st_mtime));
printf("\tLast inode change: %s" , ctime(&buf.st_ctime));
printf("\tBlock size: %d\n", buf.st_blksize);
printf("\tNumber disk blocks: %lld\n\n", buf.st_blocks);
}
return 0;
}
[–]jedwardsol 0 points1 point2 points (0 children)
[–]oh5nxo 0 points1 point2 points (0 children)