stat構造体

stat構造体(struct stat)は、ファイル情報を格納するために使う。
(stat関数と同じstatでややこしいが・・・。)

Man page of STAT

今回は、指定されたファイルの所有者のユーザーIDを取得する。

sst.st_uidに、そのファイルの所有者のユーザーIDが整数で入っている。

$ vi struct_stat.c
$ vi test.txt
$ cat test.txt
ABCDE
$ ls -al
合計 16
drwxrwxr-x. 2 bg1 bg1 4096  8月  8 16:04 .
drwxrwxr-x. 3 bg1 bg1 4096  8月  8 15:57 ..
-rw-rw-r--. 1 bg1 bg1  676  8月  8 16:03 struct_stat.c
-rw-rw-r--. 1 bg1 bg1    6  8月  8 16:04 test.txt
$ id -u bg1
1000
$ gcc struct_stat.c -o struct_stat
$ ./struct_stat
sst.st_uid = 1000
$

test.txtを生成し、そのユーザー名はbg1とわかっている。
idコマンドでbg1のユーザーIDは1000。
プログラムを実行すると、test.txtのsst.st_uidは1000ということで、確かに一致する。

Sample/unixsyscall/struct_stat/struct_stat/src/struct_stat at master · bg1bgst333/Sample · GitHub