Do You Coding?
[function] perror ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
perror - print a system error message
: ์์คํ
์๋ฌ ๋ฉ์์ง ์ถ๋ ฅํ๊ธฐ
SYNOPSIS
#include <stdio.h>
void perror(const char *s);
#include <errno.h>
const char * const sys_errlist[];
int sys_nerr;
int errno; /* Not really declared this way; see errno(3) */
DESCRIPTION
The perror() function produces a message on standard error describing
the last error encountered during a call to a system or library funcโ
tion.
First (if s is not NULL and *s is not a null byte ('\0')), the argument
string s is printed, followed by a colon and a blank. Then an error
message corresponding to the current value of errno and a new-line.
To be of most use, the argument string should include the name of the
function that incurred the error.
The global error list sys_errlist[], which can be indexed by errno, can
be used to obtain the error message without the newline. The largest
message number provided in the table is sys_nerr-1. Be careful when
directly accessing this list, because new error values may not have
been added to sys_errlist[]. The use of sys_errlist[] is nowadays depโ
recated; use strerror(3) instead.
When a system call fails, it usually returns -1 and sets the variable
errno to a value describing what went wrong. (These values can be
found in <errno.h>.) Many library functions do likewise. The function
perror() serves to translate this error code into human-readable form.
Note that errno is undefined after a successful system call or library
function call: this call may well change this variable, even though it
succeeds, for example because it internally used some other library
function that failed. Thus, if a failing call is not immediately folโ
lowed by a call to perror(), the value of errno should be saved.
: perror() ํจ์๋ ์์คํ
๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํจ์์ ๋ํ ํธ์ถ ์ค์ ๋ฐ์ํ ๋ง์ง๋ง ์ค๋ฅ๋ฅผ ์ค๋ช
ํ๋
ํ์ค ์ค๋ฅ์ ๋ํ ๋ฉ์์ง๋ฅผ ์์ฑํฉ๋๋ค.
๋จผ์ (s๊ฐ NULL์ด ์๋๊ณ *s๊ฐ NULL ๋ฐ์ดํธ๊ฐ ์๋ ๊ฒฝ์ฐ('\0')) ์ธ์ ๋ฌธ์์ด์ด ์ถ๋ ฅ๋๊ณ
์ฝ๋ก ๊ณผ ๋ธ๋ญํฌ๊ฐ ์ฐจ๋ก๋ก ์ถ๋ ฅ๋ฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ errno ๋ฐ new-line์ ํ์ฌ ๊ฐ์ ํด๋นํ๋
์ค๋ฅ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ๊ฒ ์ฌ์ฉํ๋ ค๋ฉด ์ธ์ ๋ฌธ์์ด์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ํจ์์ ์ด๋ฆ์ ํฌํจํด์ผ ํฉ๋๋ค.
errno๋ก ์ธ๋ฑ์ฑํ ์ ์๋ ์ ์ญ ์ค๋ฅ ๋ชฉ๋ก sys_errlist[]๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ค ์์ด ์ค๋ฅ ๋ฉ์์ง๋ฅผ ์ป์ ์ ์์ต๋๋ค.
ํ์ ์ ๊ณต๋ ๊ฐ์ฅ ํฐ ๋ฉ์์ง ๋ฒํธ๋ sys_nerr-1์ ๋๋ค. ์ ์ค๋ฅ ๊ฐ์ด sys_errlist[ ]์ ์ถ๊ฐ๋์ง ์์์ ์ ์์ผ๋ฏ๋ก
์ด ๋ชฉ๋ก์ ์ง์ ์ก์ธ์คํ ๋ ์ฃผ์ํ์ญ์์ค. ์์ฆ์๋ sys_errlist[]์ ์ฌ์ฉ์ด ๊ถ์ฅ๋์ง ์์ต๋๋ค.
๋์ strerror(3)๋ฅผ ์ฌ์ฉํ์ญ์์ค.
์์คํ
ํธ์ถ์ด ์คํจํ๋ฉด ๋ณดํต -1์ ๋ฐํํ๊ณ ๋ณ์ errno๋ฅผ ๋ฌด์์ด ์๋ชป๋์๋์ง๋ฅผ ์ค๋ช
ํ๋ ๊ฐ์ผ๋ก ์ค์ ํฉ๋๋ค.
(์ด ๊ฐ๋ค์ <errno.h>์์ ์ฐพ์ ์ ์์ต๋๋ค.) ๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํจ์๋ค๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค.
perror ํจ์๋ ์ด ์ค๋ฅ ์ฝ๋๋ฅผ ์ฌ๋์ด ์ฝ์ ์ ์๋ ํํ๋ก ๋ณํํ๋ ์ญํ ์ ํฉ๋๋ค.
errno๋ ์์คํ ํธ์ถ์ด๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํจ์ ํธ์ถ์ด ์ฑ๊ณตํ ํ์ ์ ์๋์ง ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, ์ด ํธ์ถ์ ์ฑ๊ณตํ๋๋ผ๋ ์ด ๋ณ์๋ฅผ ๋ณ๊ฒฝํ ์ ์๋๋ฐ, ์ด๋ ์คํจํ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํจ์๋ฅผ
๋ด๋ถ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ฐ๋ผ์ ์คํจํ ํธ์ถ์ด errno() ํธ์ถ๋ก ๋ฐ๋ก ๋ค๋ฐ๋ฅด์ง ์์ผ๋ฉด
errno ๊ฐ์ ์ ์ฅํด์ผ ํฉ๋๋ค.
๐ ํจ์ ์ค๋ช
perror() ํจ์๋ ์๋ฌ ๋ด์ฉ์ ํธ์ถํด์ฃผ๋ ํจ์์ด๋ค.
()์์๋ char *, ์ฆ ๋ฌธ์์ด์ด ๋ค์ด๊ฐ๋๋ฐ, ์ฌ๊ธฐ์ ์ ๋ ฅํ ๋ฌธ์์ด์ ์ถ๋ ฅํ ๋ค ์ถ๊ฐ๋ก ๊ธฐ์กด ์๋ฌ ์ฝ๋๋ฅผ ์ถ๋ ฅํด์ค๋ค.
๊ฒฐ๊ณผ ์ถ๋ ฅ ๊ฐ : ์ธ์๋ก ์ ๋ ฅํ ์๋ฌ๋ฉ์์ง : ์๋ ์๋ฌ ๋ฉ์์ง
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int file = 10000;
if(close(file) == -1)
perror("์๋ชป๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ");
return 0;
}
์์๋ก, ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ก readํ์ง ์์, ์ด๋ฏธ ๋ซํ์๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ 10000์ ๋ซ์ผ๋ คํ๋ ์ฝ๋์ด๋ค.
if๋ฌธ์ ์๋ฌ ์ํฉ์ด๊ณ , ์ด ๋ perror()๋ฅผ ๋ถ๋ฌ์ ์๋ฌ๋ฉ์์ง์ ํจ๊ป ์ค์ ํ ๋ฌธ์์ด์ ํจ๊ป ์ถ๋ ฅํด์ค๋ค.
๊ฒฐ๊ณผ)
| ์๋ชป๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ : Bad file descriptor |
๊ทธ๋ ๋ค๋ฉด, ์๋ฌ๊ฐ ์๋ ์ํฉ์์๋ ์ด๋ป๊ฒ ์ถ๋ ฅ์ด ๋ ๊น?
#include <stdio.h>
#include <unistd.h>
int main(void)
{
perror("์ ์์๋");
return 0;
}
๊ฒฐ๊ณผ)
| ์ ์์๋ : Success |
์๋ฌด๋ฐ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์์ ์ฝ๋์ด๋ค. ๊ทธ๋ผ perror์ ์ ๋ฌ๋๋ errno๊ฐ 0์ด๊ณ , ์ด๋ฅผ ๋ฐ์ผ๋ฉด error๊ฐ ์์ด
์๋ฌ ๋ฉ์์ง ๋์ Success๋ฅผ ์ถ๋ ฅํ๊ฒ ๋๋ค.
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(void)
{
errno = 4;
perror("์ ์์๋");
return 0;
}
๊ฒฐ๊ณผ)
| ์ ์์๋ : Interrupted system call |
errno๋ฅผ ์ง์ ์์ ํด์, error๊ฐ ์๋ ์ํฉ์๋ perror์ errno๋ฅผ ๋ค๋ฅด๊ฒ ์ ๋ฌํ๋ฉด ํด๋น ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ฒ ํ ์ ์๋ค.
errno๋ 1 ~ 133 ๊น์ง ์ซ์๋ก ์ ์๋์ด์๊ณ , ๋๋ ์๋ฌ์ฝ๋๋ก๋ ์์ฑํด๋ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
์์์์ฒ๋ผ, ์ด errno๊ฐ 0์ด๋ฉด ์๋ฌ๊ฐ ์๋ Success ๊ฐ ์ถ๋ ฅ๋๋ค.
[ 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 |
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] access ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.24 |
|---|---|
| [function] strerror ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.23 |
| [library] tolower ๊ตฌํํ๊ธฐ (0) | 2024.05.13 |
| [library] toupper ๊ตฌํํ๊ธฐ (0) | 2024.05.13 |
| [library] strdup ๊ตฌํํ๊ธฐ (0) | 2024.05.13 |
