logoISU  

CS456 - Systems Programming

Spring 2025

Displaying ./code/stat/stat.c

/*

gives information about a specified file

*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv){

	if(argc < 2){
		fprintf(stderr, "Usage %s file\n", argv[0]);
		exit(1);
	}

	int fd;

	fd = open(argv[1], O_RDONLY);

	if(fd == -1){
		fprintf(stderr, "ERROR: Cannot open %s, file probably does not exist.\n", argv[1]);
		exit(-1);
	}

	struct stat st;
	fstat(fd, &st);

	printf("Device: %ld\n", st.st_dev);
	printf("Inode: %lu\n", st.st_ino);
	printf("Mode: %d\n", st.st_mode);
	printf("File Size: %ld\n", st.st_size);
	printf("Last Access: %ld\n", st.st_atime);
	printf("S_IXUSR: %d\n", S_IXUSR);
	printf("%d\n", st.st_mode & S_IXUSR);

	close(fd);

	return 0;

}