Skip to content

Instantly share code, notes, and snippets.

@mather
Last active December 12, 2015 10:48
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 mather/4761265 to your computer and use it in GitHub Desktop.
Save mather/4761265 to your computer and use it in GitHub Desktop.
stat関数でできることをチェック。コマンドライン引数でファイルとかシンボリックリンクとか指定するとstatまたはlstatを呼び出してチェックできる。
CC = gcc
OPT = -O2 -Wall
BIN = stat_test
default: bin
bin: $(BIN)
stat_test: stat_test.o
$(CC) $(OPT) -o $@ $<
.c.o:
$(CC) $(OPT) -o $@ -c $<
clean:
rm -f $(BIN) *.o *~
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
void profile_stat(const struct stat st);
int main(int argc, char* argv[])
{
int i;
struct stat st;
for(i = 1; i < argc; i++){
printf("argv[%d]: %s\n",i,argv[i]);
}
if(argc < 2){
printf("No arguments for stat_test.\n");
} else {
for(i = 1; i < argc; i++){
printf("==== target -> \"%s\" ====\n",argv[i]);
// stat check
printf("== [stat mode] ==\n");
if(stat(argv[i],&st) == 0){
profile_stat(st);
} else { // stat error
perror("stat error");
printf("== [lstat mode] ==\n");
if(lstat(argv[i],&st) == 0){
profile_stat(st);
} else { // lstat error
perror("lstat error");
}
}
} /* end for-loop */
}
return 0;
}
void profile_stat(const struct stat st)
{
int flag;
struct passwd *pw;
/* File Type */
printf(" Type: ");
switch(st.st_mode & S_IFMT){
case S_IFSOCK: printf("Socket\n"); break;
case S_IFLNK: printf("SymLink\n"); break;
case S_IFREG: printf("RegularFile\n"); break;
case S_IFBLK: printf("BlockDevice\n"); break;
case S_IFDIR: printf("Directory\n"); break;
case S_IFCHR: printf("CharDevice\n"); break;
case S_IFIFO: printf("FIFO\n"); break;
default: printf("UNKNOWN\n"); break;
}
/* Permission */
flag = st.st_mode & (~S_IFMT);
printf(" User: ");
if((flag & S_IRUSR) != 0) printf("Readable "); else printf(" ");
if((flag & S_IWUSR) != 0) printf("Writable "); else printf(" ");
if((flag & S_IXUSR) != 0) printf("Executable");
printf("\n");
printf(" Group: ");
if((flag & S_IRGRP) != 0) printf("Readable "); else printf(" ");
if((flag & S_IWGRP) != 0) printf("Writable "); else printf(" ");
if((flag & S_IXGRP) != 0) printf("Executable");
printf("\n");
printf("Others: ");
if((flag & S_IROTH) != 0) printf("Readable "); else printf(" ");
if((flag & S_IWOTH) != 0) printf("Writable "); else printf(" ");
if((flag & S_IXOTH) != 0) printf("Executable");
printf("\n");
/* get user name, group name */
pw = getpwuid(st.st_uid);
/* Miscellaneous Attributes */
printf(" Device ID : %10d\n",(int)st.st_dev);
printf(" inode ID : %10d\n",(int)st.st_ino);
printf("Hard Link Num : %10d\n",(int)st.st_nlink);
printf(" User ID : %10d(%s)\n",(int)st.st_uid, pw->pw_name);
printf(" Group ID : %10d\n",(int)st.st_gid);
printf("Device ID(sp) : %10d\n",(int)st.st_rdev);
printf(" Size : %10d(byte)\n",(int)st.st_size);
printf(" Block Size : %10d\n",(int)st.st_blksize);
printf(" Blocks : %10d\n",(int)st.st_blocks);
printf("Last Access : %s",ctime(&(st.st_atime)));
printf("Last Modified : %s",ctime(&(st.st_mtime)));
printf("Last Create : %s",ctime(&(st.st_ctime)));
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment