-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestatus.c
44 lines (42 loc) · 1.17 KB
/
filestatus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
int main() {
DIR *dp;
struct dirent *dirp;
struct stat file_stat;
char *filename;
char *pathname;
if ((dp = opendir(".")) == NULL)
{
perror("opendir error");
exit(1);
}
while ((dirp = readdir(dp)) != NULL)
{
filename = dirp->d_name;
pathname = malloc(strlen(filename) + 3);
strcpy(pathname, "./");
strcat(pathname, filename);
if (stat(pathname, &file_stat) == -1)
{
perror("stat error");
continue;
}
printf("File: %s\n", filename);
printf("Size: %ld bytes\n", file_stat.st_size);
printf("Owner: %d\n", file_stat.st_uid);
printf("Group: %d\n", file_stat.st_gid);
printf("Permissions: %o\n", file_stat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
printf("Last access time: %s", ctime(&file_stat.st_atime));
printf("Last modification time: %s", ctime(&file_stat.st_mtime));
printf("\n");
free(pathname);
}
closedir(dp);
return 0;
}