|
CS456 - Systems Programming
Spring 2026
|
Displaying ./code/mar18/fileinfo.c
/*
this program takes a file, and prints some info from the stat structure
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
int main (int argc, char **argv){
// prints usage statement if no file found
if(argc < 2){
fprintf(stderr,"Usage %s file\n", argv[0]);
exit(1);
}
// calling lstat as we want to get symbolic links
struct stat st;
int rt = lstat(argv[1], &st);
//checking return value for errors
if (rt < 0){
perror("Stat Error:");
exit(-1);
}
// calling stat on its own executable
struct stat self;
lstat(argv[0], &self);
//get what filetype this file is
int type = st.st_mode & 0170000;
//get the overall permissions
int perms = st.st_mode & 0777;
//printing info
printf("Filename : %s\n", argv[1]);
printf("Device: %ld\n", st.st_dev);
printf("Inode: %ld\n", st.st_ino);
printf("File Type: %o\n", type);
printf("Permissions: %o\n", perms);
printf("File Size: %ld\n", st.st_size);
//checking if we're calling stat on itself
if(st.st_dev == self.st_dev && st.st_ino == self.st_ino)
printf("Hey, you called stat on your own stat program!\n");
return 0;
}
|