Glibc common: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 1: | Line 1: | ||
== Overview == | == Overview == | ||
GNU C library 내용 정리 | GNU C library 내용 정리 | ||
== stat/fstat/lstat == | |||
파일의 속성을 확인한다. | |||
<source lang=c> | |||
#include <sys/types.h> | |||
#include <sys/stat.h> | |||
#include <unistd.h> | |||
int stat(const char *path, struct stat *buf); | |||
int fstat(int fd, struct stat *buf); | |||
int lstat(const char *path, struct stat *buf); | |||
</source> | |||
lstat 사용시, 다음의 사항들이 필요하다. | |||
<pre> | |||
lstat(): | |||
_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED | |||
|| /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L | |||
</pre> | |||
=== Macros === | |||
다음의 매크로 함수들을 사용할 수 있다. | |||
<pre> | |||
S_ISREG(m) is it a regular file? | |||
S_ISDIR(m) directory? | |||
S_ISCHR(m) character device? | |||
S_ISBLK(m) block device? | |||
S_ISFIFO(m) FIFO (named pipe)? | |||
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) | |||
S_ISSOCK(m) socket? (Not in POSIX.1-1996.) | |||
</pre> | |||
=== Example === | |||
<source lang=c> | |||
#include <sys/types.h> | |||
#include <sys/stat.h> | |||
#include <time.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
int main(int argc, char *argv[]) | |||
{ | |||
struct stat sb; | |||
if (argc != 2) { | |||
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); | |||
exit(EXIT_FAILURE); | |||
} | |||
if (stat(argv[1], &sb) == -1) { | |||
perror("stat"); | |||
exit(EXIT_FAILURE); | |||
} | |||
printf("File type: "); | |||
switch (sb.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("socket\n"); break; | |||
default: printf("unknown?\n"); break; | |||
} | |||
printf("I-node number: %ld\n", (long) sb.st_ino); | |||
printf("Mode: %lo (octal)\n", (unsigned long) sb.st_mode); | |||
printf("Link count: %ld\n", (long) sb.st_nlink); | |||
printf("Ownership: UID=%ld GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid); | |||
printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize); | |||
printf("File size: %lld bytes\n", (long long) sb.st_size); | |||
printf("Blocks allocated: %lld\n", (long long) sb.st_blocks); | |||
printf("Last status change: %s", ctime(&sb.st_ctime)); | |||
printf("Last file access: %s", ctime(&sb.st_atime)); | |||
printf("Last file modification: %s", ctime(&sb.st_mtime)); | |||
exit(EXIT_SUCCESS); | |||
} | |||
</source> | |||
== assert.h == | == assert.h == |
Revision as of 09:03, 4 August 2016
Overview
GNU C library 내용 정리
stat/fstat/lstat
파일의 속성을 확인한다. <source lang=c>
- include <sys/types.h>
- include <sys/stat.h>
- include <unistd.h>
int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf); </source>
lstat 사용시, 다음의 사항들이 필요하다.
lstat(): _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
Macros
다음의 매크로 함수들을 사용할 수 있다.
S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
Example
<source lang=c>
- include <sys/types.h>
- include <sys/stat.h>
- include <time.h>
- include <stdio.h>
- include <stdlib.h>
int main(int argc, char *argv[]) {
struct stat sb;
if (argc != 2) { fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); exit(EXIT_FAILURE); }
if (stat(argv[1], &sb) == -1) { perror("stat"); exit(EXIT_FAILURE); }
printf("File type: ");
switch (sb.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("socket\n"); break; default: printf("unknown?\n"); break; }
printf("I-node number: %ld\n", (long) sb.st_ino); printf("Mode: %lo (octal)\n", (unsigned long) sb.st_mode); printf("Link count: %ld\n", (long) sb.st_nlink); printf("Ownership: UID=%ld GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid); printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize); printf("File size: %lld bytes\n", (long long) sb.st_size); printf("Blocks allocated: %lld\n", (long long) sb.st_blocks); printf("Last status change: %s", ctime(&sb.st_ctime)); printf("Last file access: %s", ctime(&sb.st_atime)); printf("Last file modification: %s", ctime(&sb.st_mtime));
exit(EXIT_SUCCESS);
} </source>
assert.h
컴파일 시, -DNDEBUG 옵션을 함께 사용하면 assert() 관련 함수들이 동작하지 않는다. 조심할 것.
/* void assert (int expression); If NDEBUG is defined, do nothing. If not, and EXPRESSION is zero, print an error message and abort. */
mysql 라이브러리 연동시, mysql_config --cflag 옵션을 함께 사용하는 경우가 많다. 그런데 개발 환경에 따라서 -DNDEBUG 옵션이 추가되는 경우가 있다. 조심해야 한다. <source lang=bash> $ mysql_config --cflags -I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g -DNDEBUG </source>
assert()
<source lang=c>
- include <assert.h>
void assert(scalar expression); </source> Expression 의 내용이 false 일 경우, 코어 덤프 생성 후, 프로그램을 종료한다.