linux (with kali)
리눅스 - 파일의 정보 출력(stat, lstat)
1. print_fileinfo.h
#include <sys/stat.h>
int print_FileInfo(struct stat stBuf);
2. print_fileinfo.c
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include "print_fileinfo.h"
int print_FileInfo(struct stat stBuf)
{
printf("File type : ");
switch (stBuf.st_mode & S_IFMT)
{
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("soket\n"); break;
default: printf("unknown?\n");
break;
}
printf("I-node number: %ld\n", (long)stBuf.st_ino);
printf("Mode: %lo (octal)\n", (unsigned long)stBuf.st_mode);
printf("Link count: %ld\n, (long)stBuf.st_nlink");
printf("Ownership: UID=%ld GID=%ld\n", (long)stBuf.st_uid, (long)stBuf.st_gid);
printf("Preferred 1/0 block size: %ld bytes\n", (long)stBuf.st_blksize);
printf("File size: %lld bytes\n", (long long)stBuf.st_size);
printf("Blocks allocated: %lld\n", (long long)stBuf.st_blocks);
printf("Last status change: %s", ctime(&stBuf.st_ctime));
printf("Last file access: %s", ctime(&stBuf.st_ctime));
printf("Last file modification: %s", ctime(&stBuf.st_ctime));
printf("\n");
return 0;
}
}
3. stat.c
}
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "print_fileinfo.h"
int main(int argc, char* argv[])
{
struct stat stBuf;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
return -1;
}
if (stat(argv[1], &stBuf) == -1)
{
fprintf(stderr, "lstat");
return -1;
}
print_FileInfo(stBuf);
return 0;
}
4. lstat.c
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "print_fileinfo.h"
int main(int argc, char* argv[])
{
struct stat stBuf;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
return -1;
}
if (lstat(argv[1], &stBuf) == -1)
{
fprintf(stderr, "lstat");
return -1;
}
print_FileInfo(stBuf);
return 0;
}
결과 화면
'linux (with kali)' 카테고리의 다른 글
버퍼링과 논 버퍼링 (0) | 2021.09.27 |
---|---|
운영체제 - 파일시스템_파일접근권한 (0) | 2021.09.27 |
리눅스 - 파일 접근 위치 이동(fseek, lseek, ftell, getopt) (0) | 2021.09.27 |
리눅스 - 작업 경로 전환(chdir, fchdir, getcwd) (0) | 2021.09.27 |
리눅스 - 디렉토리 관리(mkdir, readdir, rename, rmdir) (0) | 2021.09.27 |
댓글