-
-
Save xieyuheng/b90898000668e5cac10f to your computer and use it in GitHub Desktop.
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stddef.h> | |
#include <time.h> | |
// the following are reported by : man 2 stat | |
struct the_stat { | |
dev_t st_dev; /* ID of device containing file */ | |
ino_t st_ino; /* inode number */ | |
mode_t st_mode; /* protection */ | |
nlink_t st_nlink; /* number of hard links */ | |
uid_t st_uid; /* user ID of owner */ | |
gid_t st_gid; /* group ID of owner */ | |
dev_t st_rdev; /* device ID (if special file) */ | |
off_t st_size; /* total size, in bytes */ | |
blksize_t st_blksize; /* blocksize for filesystem I/O */ | |
blkcnt_t st_blocks; /* number of 512B blocks allocated */ | |
/* Since Linux 2.6, the kernel supports nanosecond | |
precision for the following timestamp fields. | |
For the details before Linux 2.6, see NOTES. */ | |
struct timespec st_atim; /* time of last access */ | |
struct timespec st_mtim; /* time of last modification */ | |
struct timespec st_ctim; /* time of last status change */ | |
#define st_atime st_atim.tv_sec /* Backward compatibility */ | |
#define st_mtime st_mtim.tv_sec | |
#define st_ctime st_ctim.tv_sec | |
}; | |
int | |
main(int argc, char *argv[]) | |
{ | |
printf("\n"); | |
printf("\n(%d) (%u) dev_t st_dev; ", sizeof(dev_t), offsetof(struct stat, st_dev)); | |
printf("\n(%d) (%u) ino_t st_ino; ", sizeof(ino_t), offsetof(struct stat, st_ino)); | |
printf("\n(%d) (%u) mode_t st_mode; ", sizeof(mode_t), offsetof(struct stat, st_mode)); | |
printf("\n(%d) (%u) nlink_t st_nlink; ", sizeof(nlink_t), offsetof(struct stat, st_nlink)); | |
printf("\n(%d) (%u) uid_t st_uid; ", sizeof(uid_t), offsetof(struct stat, st_uid)); | |
printf("\n(%d) (%u) gid_t st_gid; ", sizeof(gid_t), offsetof(struct stat, st_gid)); | |
printf("\n(%d) (%u) dev_t st_rdev; ", sizeof(dev_t), offsetof(struct stat, st_rdev)); | |
printf("\n(%d) (%u) off_t st_size; ", sizeof(off_t), offsetof(struct stat, st_size)); | |
printf("\n(%d) (%u) blksize_t st_blksize; ", sizeof(blksize_t), offsetof(struct stat, st_blksize)); | |
printf("\n(%d) (%u) blkcnt_t st_blocks; ", sizeof(blkcnt_t), offsetof(struct stat, st_blocks)); | |
printf("\n"); | |
printf("\n(%d) (%u) __time_t tv_sec; ", sizeof(__time_t), offsetof(struct timespec, tv_sec)); | |
printf("\n(%d) (%u) __syscall_slong_t tv_nsec; ", sizeof(__syscall_slong_t), offsetof(struct timespec, tv_nsec)); | |
printf("\n"); | |
} | |
// the following is example result : | |
/* | |
(8) (0) dev_t st_dev; | |
(8) (8) ino_t st_ino; | |
(4) (24) mode_t st_mode; | |
(8) (16) nlink_t st_nlink; | |
(4) (28) uid_t st_uid; | |
(4) (32) gid_t st_gid; | |
(8) (40) dev_t st_rdev; | |
(8) (48) off_t st_size; | |
(8) (56) blksize_t st_blksize; | |
(8) (64) blkcnt_t st_blocks; | |
(8) (0) __time_t tv_sec; | |
(8) (8) __syscall_slong_t tv_nsec; | |
*/ |
another useful comment from #forth
proteusguy:
one comment though - you need to make sure that you build
the code with the same parameters as your kernel was built with
regarding alignment and padding or else this perfectly good result
could be perfectly incompatible with your kernel. So best to check
what options the kernel is using. However I would anticipate that
kernel devs would (hopefully) ensure their structure aligned on
natural boundaries already. But if you find these offsets don't work
then that's the next thing to check.
@xieyuheng maybe you need __attribute__ ((packed))
for you structure fields for avoiding padding.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-g_t_0040code_007bpacked_007d-variable-attribute-3331
scherrey@redsky:~$ ./xyh
(8) (0) dev_t st_dev;
(8) (8) ino_t st_ino;
(4) (24) mode_t st_mode;
(8) (16) nlink_t st_nlink;
(4) (28) uid_t st_uid;
(4) (32) gid_t st_gid;
(8) (40) dev_t st_rdev;
(8) (48) off_t st_size;
(8) (56) blksize_t st_blksize;
(8) (64) blkcnt_t st_blocks;
(8) (0) __time_t tv_sec;
(8) (8) __syscall_slong_t tv_nsec;
scherrey@redsky:~$ uname -a
Linux redsky 3.16.0-31-generic #43-Ubuntu SMP Tue Mar 10 17:37:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux