
📌 매뉴얼 (Linux)
NAME
strerror - return string describing error number
: 오류 번호를 설명하는 문자열을 반환
SYNOPSIS
#include <string.h>
char *strerror(int errnum);
DESCRIPTION
The strerror() function returns a pointer to a string that describes
the error code passed in the argument errnum, possibly using the
LC_MESSAGES part of the current locale to select the appropriate lan‐
guage. (For example, if errnum is EINVAL, the returned description
will be "Invalid argument".) This string must not be modified by the
application, but may be modified by a subsequent call to strerror() or
strerror_l(). No other library function, including perror(3), will
modify this string.
: strererror() 함수는 인수 errnum에 전달된 오류 코드를 설명하는 문자열에 포인터를 반환하며,
현재 로케일의 LC_MESSAGES 부분을 사용하여 적절한 언어를 선택할 수 있습니다.
(예를 들어 errnum이 EINVAL인 경우 반환되는 설명은 "Invalid argument"입니다.)
이 문자열은 응용 프로그램에서 수정할 수 없지만 strererror() 또는 strererror_l()로
후속 호출하면 수정할 수 있습니다. errror(3)을 포함한 다른 라이브러리 함수는
이 문자열을 수정하지 않습니다.
RETURN VALUE
The strerror() functions return the appropriate error description string,
or an "Unknown error nnn" message if the error number is unknown.
POSIX.1-2001 and POSIX.1-2008 require that a successful call to str‐
error() or strerror_l() shall leave errno unchanged, and note that,
since no function return value is reserved to indicate an error, an ap‐
plication that wishes to check for errors should initialize errno to
zero before the call, and then check errno after the call.
: strererror() 함수는 적절한 오류 설명 문자열을 반환하거나 오류 번호를 알 수 없는 경우
"Unknown error nnn" 메시지를 반환합니다.
POSIX.1-2001 및 POSIX.1-2008은 strererror() 또는 strererror_l()에 대한 호출이 성공하면
errno가 변경되지 않고 유지되어야 하며, 함수 반환 값이 오류를 나타내기 위해 예약되지 않으므로
오류를 확인하려는 응용 프로그램은 호출 전에 errno를 0으로 초기화한 다음 호출 후에 errno를 확인해야 합니다.
📌 함수 설명
strerror() 함수는 오류 번호인 errno에 대해 해당 에러를 설명하는 문자열을 반환해주는 함수이다.
인자는 에러 번호인 errno이고, 반환값은 에러에 대한 설명 문자열이다.
perror가 errno를 따로 인자로 받아오지 않고, 자체적으로 처리해준다면, strerror는 인자로 직접 받아오기 때문에,
errno를 넣지 않고 특정 숫자 또는 에러문자를 넣어주면 그에 대응하는 문자열을 반환한다.
리턴 값 : 해당 errno에 대응되는 에러 메시지
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main()
{
int file = 10000;
if(close(file) == -1)
printf("에러 발생: %s, 에러 변수: %d\n", strerror(errno), errno);
return (0);
}
결과)
에러 발생: Bad file descriptor, 에러 변수: 9 |
에러 발생시, errno가 바뀌어, strerror가 해당 errno에 대응되는 문자열을 출력해준다.
여기서는 Bad file descriptor 가 반환된 문자열이다.
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
errno = 5;
printf("에러 발생: %s, 에러 변수: %d\n", strerror(errno), errno);
return (0);
}
결과)
에러 발생: Input/output error, 에러 변수: 5 |
이와 같이, 에러 변수 errno 를 수정하여 strerror의 결과값을 바꿀 수 있다.
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
errno = 5;
printf("에러 발생: %s, 에러 변수: %d\n", strerror(0), errno);
return (0);
}
결과)
에러 발생: Success, 에러 변수: 5 |
당연히 인자를 errno로 하지 않고 직접 숫자를 대응해서 넣으면 결과가 바뀐다.
[ errno list ]
1 | 0x01 | EPERM | Operation not permitted |
2 | 0x02 | ENOENT | No such file or directory |
3 | 0x03 | ESRCH | No such process |
4 | 0x04 | EINTR | Interrupted system call |
5 | 0x05 | EIO | Input/output error |
6 | 0x06 | ENXIO | No such device or address |
7 | 0x07 | E2BIG | Argument list too long |
8 | 0x08 | ENOEXEC | Exec format error |
9 | 0x09 | EBADF | Bad file descriptor |
10 | 0x0a | ECHILD | No child processes |
11 | 0x0b | EAGAIN | Resource temporarily unavailable |
11 | 0x0b | EWOULDBLOCK | (Same value as EAGAIN) Resource temporarily unavailable |
12 | 0x0c | ENOMEM | Cannot allocate memory |
13 | 0x0d | EACCES | Permission denied |
14 | 0x0e | EFAULT | Bad address |
15 | 0x0f | ENOTBLK | Block device required |
16 | 0x10 | EBUSY | Device or resource busy |
17 | 0x11 | EEXIST | File exists |
18 | 0x12 | EXDEV | Invalid cross-device link |
19 | 0x13 | ENODEV | No such device |
20 | 0x14 | ENOTDIR | Not a directory |
21 | 0x15 | EISDIR | Is a directory |
22 | 0x16 | EINVAL | Invalid argument |
23 | 0x17 | ENFILE | Too many open files in system |
24 | 0x18 | EMFILE | Too many open files |
25 | 0x19 | ENOTTY | Inappropriate ioctl for device |
26 | 0x1a | ETXTBSY | Text file busy |
27 | 0x1b | EFBIG | File too large |
28 | 0x1c | ENOSPC | No space left on device |
29 | 0x1d | ESPIPE | Illegal seek |
30 | 0x1e | EROFS | Read-only file system |
31 | 0x1f | EMLINK | Too many links |
32 | 0x20 | EPIPE | Broken pipe |
33 | 0x21 | EDOM | Numerical argument out of domain |
34 | 0x22 | ERANGE | Numerical result out of range |
35 | 0x23 | EDEADLK | Resource deadlock avoided |
35 | 0x23 | EDEADLOCK | (Same value as EDEADLK) Resource deadlock avoided |
36 | 0x24 | ENAMETOOLONG | File name too long |
37 | 0x25 | ENOLCK | No locks available |
38 | 0x26 | ENOSYS | Function not implemented |
39 | 0x27 | ENOTEMPTY | Directory not empty |
40 | 0x28 | ELOOP | Too many levels of symbolic links |
41 | 0x29 | not implemented | |
42 | 0x2a | ENOMSG | No message of desired type |
43 | 0x2b | EIDRM | Identifier removed |
44 | 0x2c | ECHRNG | Channel number out of range |
45 | 0x2d | EL2NSYNC | Level 2 not synchronized |
46 | 0x2e | EL3HLT | Level 3 halted |
47 | 0x2f | EL3RST | Level 3 reset |
48 | 0x30 | ELNRNG | Link number out of range |
49 | 0x31 | EUNATCH | Protocol driver not attached |
50 | 0x32 | ENOCSI | No CSI structure available |
51 | 0x33 | EL2HLT | Level 2 halted |
52 | 0x34 | EBADE | Invalid exchange |
53 | 0x35 | EBADR | Invalid request descriptor |
54 | 0x36 | EXFULL | Exchange full |
55 | 0x37 | ENOANO | No anode |
56 | 0x38 | EBADRQC | Invalid request code |
57 | 0x39 | EBADSLT | Invalid slot |
58 | 0x3a | not implemented | |
59 | 0x3b | EBFONT | Bad font file format |
60 | 0x3c | ENOSTR | Device not a stream |
61 | 0x3d | ENODATA | No data available |
62 | 0x3e | ETIME | Timer expired |
63 | 0x3f | ENOSR | Out of streams resources |
64 | 0x40 | ENONET | Machine is not on the network |
65 | 0x41 | ENOPKG | Package not installed |
66 | 0x42 | EREMOTE | Object is remote |
67 | 0x43 | ENOLINK | Link has been severed |
68 | 0x44 | EADV | Advertise error |
69 | 0x45 | ESRMNT | Srmount error |
70 | 0x46 | ECOMM | Communication error on send |
71 | 0x47 | EPROTO | Protocol error |
72 | 0x48 | EMULTIHOP | Multihop attempted |
73 | 0x49 | EDOTDOT | RFS specific error |
74 | 0x4a | EBADMSG | Bad message |
75 | 0x4b | EOVERFLOW | Value too large for defined data type |
76 | 0x4c | ENOTUNIQ | Name not unique on network |
77 | 0x4d | EBADFD | File descriptor in bad state |
78 | 0x4e | EREMCHG | Remote address changed |
79 | 0x4f | ELIBACC | Can not access a needed shared library |
80 | 0x50 | ELIBBAD | Accessing a corrupted shared library |
81 | 0x51 | ELIBSCN | .lib section in a.out corrupted |
82 | 0x52 | ELIBMAX | Attempting to link in too many shared libraries |
83 | 0x53 | ELIBEXEC | Cannot exec a shared library directly |
84 | 0x54 | EILSEQ | Invalid or incomplete multibyte or wide character |
85 | 0x55 | ERESTART | Interrupted system call should be restarted |
86 | 0x56 | ESTRPIPE | Streams pipe error |
87 | 0x57 | EUSERS | Too many users |
88 | 0x58 | ENOTSOCK | Socket operation on non-socket |
89 | 0x59 | EDESTADDRREQ | Destination address required |
90 | 0x5a | EMSGSIZE | Message too long |
91 | 0x5b | EPROTOTYPE | Protocol wrong type for socket |
92 | 0x5c | ENOPROTOOPT | Protocol not available |
93 | 0x5d | EPROTONOSUPPORT | Protocol not supported |
94 | 0x5e | ESOCKTNOSUPPORT | Socket type not supported |
95 | 0x5f | EOPNOTSUPP | Operation not supported |
95 | 0x5f | ENOTSUP | (Same value as EOPNOTSUPP) Operation not supported |
96 | 0x60 | EPFNOSUPPORT | Protocol family not supported |
97 | 0x61 | EAFNOSUPPORT | Address family not supported by protocol |
98 | 0x62 | EADDRINUSE | Address already in use |
99 | 0x63 | EADDRNOTAVAIL | Cannot assign requested address |
100 | 0x64 | ENETDOWN | Network is down |
101 | 0x65 | ENETUNREACH | Network is unreachable |
102 | 0x66 | ENETRESET | Network dropped connection on reset |
103 | 0x67 | ECONNABORTED | Software caused connection abort |
104 | 0x68 | ECONNRESET | Connection reset by peer |
105 | 0x69 | ENOBUFS | No buffer space available |
106 | 0x6a | EISCONN | Transport endpoint is already connected |
107 | 0x6b | ENOTCONN | Transport endpoint is not connected |
108 | 0x6c | ESHUTDOWN | Cannot send after transport endpoint shutdown |
109 | 0x6d | ETOOMANYREFS | Too many references: cannot splice |
110 | 0x6e | ETIMEDOUT | Connection timed out |
111 | 0x6f | ECONNREFUSED | Connection refused |
112 | 0x70 | EHOSTDOWN | Host is down |
113 | 0x71 | EHOSTUNREACH | No route to host |
114 | 0x72 | EALREADY | Operation already in progress |
115 | 0x73 | EINPROGRESS | Operation now in progress |
116 | 0x74 | ESTALE | Stale file handle |
117 | 0x75 | EUCLEAN | Structure needs cleaning |
118 | 0x76 | ENOTNAM | Not a XENIX named type file |
119 | 0x77 | ENAVAIL | No XENIX semaphores available |
120 | 0x78 | EISNAM | Is a named type file |
121 | 0x79 | EREMOTEIO | Remote I/O error |
122 | 0x7a | EDQUOT | Disk quota exceeded |
123 | 0x7b | ENOMEDIUM | No medium found |
124 | 0x7c | EMEDIUMTYPE | Wrong medium type |
125 | 0x7d | ECANCELED | Operation canceled |
126 | 0x7e | ENOKEY | Required key not available |
127 | 0x7f | EKEYEXPIRED | Key has expired |
128 | 0x80 | EKEYREVOKED | Key has been revoked |
129 | 0x81 | EKEYREJECTED | Key was rejected by service |
130 | 0x82 | EOWNERDEAD | Owner died |
131 | 0x83 | ENOTRECOVERABLE | State not recoverable |
132 | 0x84 | ERFKILL | Operation not possible due to RF-kill |
133 | 0x85 | EHWPOISON | Memory page has hardware error |
'프로그래밍 > C' 카테고리의 다른 글
[function] dup 함수 알아보기 (0) | 2024.05.27 |
---|---|
[function] access 함수 알아보기 (0) | 2024.05.24 |
[function] perror 함수 알아보기 (2) | 2024.05.20 |
[library] tolower 구현하기 (0) | 2024.05.13 |
[library] toupper 구현하기 (0) | 2024.05.13 |