stat.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

int main(int argc, char *argv[])
{
	struct stat stbuf;
	int i;

	if (argc == 1){
		fprintf(stderr, "Usage: %s file [ file . . .]\n", argv[0]);
		return(1);
	}

	for(i = 1; i < argc; i++){
		if (stat(argv[i], &stbuf) < 0){
			perror(argv[i]);
			continue;
		}
		printf("Information about %s:\n", argv[i]);
		printf("\tID of device containing file: %ld\n", stbuf.st_dev);
		printf("\tinode number: %ld\n", stbuf.st_ino); 
		printf("\tprotection mode: %04o\n", stbuf.st_mode);
		printf("\tnumber of hard links: %ld\n", stbuf.st_nlink);
		printf("\tuser ID of owner: %d\n", stbuf.st_uid);
		printf("\tgroup ID of owner: %d\n", stbuf.st_gid);
		printf("\tdevice ID (if special file): %ld\n", stbuf.st_rdev);
		printf("\tsize of file in bytes %ld\n", stbuf.st_size);
		printf("\tfile system I/O block size: %ld\n", stbuf.st_blksize);
		printf("\tnumber of 512-byte blocks: %ld\n", stbuf.st_blocks);
		printf("\ttime of last access: %s", ctime(&stbuf.st_atime));
		printf("\ttime of last modification: %s", ctime(&stbuf.st_mtime));
		printf("\ttime of last status change: %s", ctime(&stbuf.st_ctime));
	}
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh