Skip to content

Instantly share code, notes, and snippets.

@TACIXAT
Created August 10, 2021 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TACIXAT/54694c19dc6f65e27dfa5b6425302f92 to your computer and use it in GitHub Desktop.
Save TACIXAT/54694c19dc6f65e27dfa5b6425302f92 to your computer and use it in GitHub Desktop.
print values, offsets, sizes for members of struct stat
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
void
fstat_filesize(const char *filename)
{
int fd;
struct stat statbuf;
fd = open(filename, O_RDONLY, S_IRUSR | S_IRGRP);
if (fd == -1)
{
printf("failed to open %s\n", filename);
exit(1);
}
if (fstat(fd, &statbuf) == -1)
{
printf("failed to fstat %s\n", filename);
exit(1);
}
printf("st_dev: %lx\n", statbuf.st_dev);
printf("st_ino: %lx\n", statbuf.st_ino);
printf("st_mode: %x\n", statbuf.st_mode);
printf("st_nlink: %lx\n", statbuf.st_nlink);
printf("st_uid: %x\n", statbuf.st_uid);
printf("st_gid: %x\n", statbuf.st_gid);
printf("st_rdev: %lx\n", statbuf.st_rdev);
printf("st_size: %lx\n", statbuf.st_size);
printf("st_blksize: %lx\n", statbuf.st_blksize);
printf("st_blocks: %lx\n", statbuf.st_blocks);
printf("st_atime: %lx\n", statbuf.st_atime);
printf("st_mtime: %lx\n", statbuf.st_mtime);
printf("st_ctime: %lx\n", statbuf.st_ctime);
printf("\n\n");
printf("st_dev: %ld\t%ld\t%p\n", offsetof(struct stat, st_dev), sizeof(statbuf.st_dev), (void*)(&((struct stat *)NULL)->st_dev));
printf("st_ino: %ld\t%ld\t%p\n", offsetof(struct stat, st_ino), sizeof(statbuf.st_ino), (void*)(&((struct stat *)NULL)->st_ino));
printf("st_mode: %ld\t%ld\t%p\n", offsetof(struct stat, st_mode), sizeof(statbuf.st_mode), (void*)(&((struct stat *)NULL)->st_mode));
printf("st_nlink: %ld\t%ld\t%p\n", offsetof(struct stat, st_nlink), sizeof(statbuf.st_nlink), (void*)(&((struct stat *)NULL)->st_nlink));
printf("st_uid: %ld\t%ld\t%p\n", offsetof(struct stat, st_uid), sizeof(statbuf.st_uid), (void*)(&((struct stat *)NULL)->st_uid));
printf("st_gid: %ld\t%ld\t%p\n", offsetof(struct stat, st_gid), sizeof(statbuf.st_gid), (void*)(&((struct stat *)NULL)->st_gid));
printf("st_rdev: %ld\t%ld\t%p\n", offsetof(struct stat, st_rdev), sizeof(statbuf.st_rdev), (void*)(&((struct stat *)NULL)->st_rdev));
printf("st_size: %ld\t%ld\t%p\n", offsetof(struct stat, st_size), sizeof(statbuf.st_size), (void*)(&((struct stat *)NULL)->st_size));
printf("st_blksize: %ld\t%ld\t%p\n", offsetof(struct stat, st_blksize), sizeof(statbuf.st_blksize), (void*)(&((struct stat *)NULL)->st_blksize));
printf("st_blocks: %ld\t%ld\t%p\n", offsetof(struct stat, st_blocks), sizeof(statbuf.st_blocks), (void*)(&((struct stat *)NULL)->st_blocks));
printf("st_atime: %ld\t%ld\t%p\n", offsetof(struct stat, st_atime), sizeof(statbuf.st_atime), (void*)(&((struct stat *)NULL)->st_atime));
printf("st_mtime: %ld\t%ld\t%p\n", offsetof(struct stat, st_mtime), sizeof(statbuf.st_mtime), (void*)(&((struct stat *)NULL)->st_mtime));
printf("st_ctime: %ld\t%ld\t%p\n", offsetof(struct stat, st_ctime), sizeof(statbuf.st_ctime), (void*)(&((struct stat *)NULL)->st_ctime));
// printf("[*] fstat_filesize - file: %s, size: %lld\n", filename, statbuf.st_size);
if (close(fd) == -1)
{
printf("failed to fclose %s\n", filename);
exit(1);
}
}
int
main(int argc, const char *argv[])
{
if (argc < 2)
{
printf("%s <file1>\n", argv[0]);
exit(0);
}
fstat_filesize(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment