开发过*nix程序的人都知道当一个系统调用返回错误时,需要查看头文件
errno.h中的errno。但是,errno只给出一个数字。这个数字很难直接看到API
错误的直接原因。
所以就有了一下的程序:在Ubuntu上编译通过
- #include <errno.h>
-
#include <stdio.h>
-
#include <string.h>
-
-
#define show_erro(name) show(#name,name)
-
-
int show(char *,int);
-
-
int main(int argc,char* argv[])
-
{
-
int err = 0;
-
-
if(argc <= 1){
-
printf("usage: %s error value\n"
-
"eg: %s 1\n",argv[0],argv[0]);
-
return -1;
-
}
-
err = atoi(argv[1]);
-
switch(err){
-
case EPERM:/* 1 Operation not permitted */
-
show_erro(EPERM);
-
break;
-
case ENOENT:/* 2 No such file or directory */
-
show_erro(ENOENT);
-
break;
-
case ESRCH:/* 3 No such process */
-
show_erro(ESRCH);
-
break;
-
case EINTR:/* 4 Interrupted system call */
-
show_erro(EINTR);
-
break;
-
case EIO:/* 5 I/O error */
-
show_erro(EIO);
-
break;
-
case ENXIO:/* 6 No such device or address */
-
show_erro(ENXIO);
-
break;
-
case E2BIG:/* 7 Argument list too long */
-
show_erro(E2BIG);
-
break;
-
case ENOEXEC:/* 8 Exec format error */
-
show_erro(ENOEXEC);
-
break;
-
case EBADF:/* 9 Bad file number */
-
show_erro(EBADF);
-
break;
-
case ECHILD:/*10 No child processes */
-
show_erro(ECHILD);
-
break;
-
case EAGAIN:/*11 Try again */
-
show_erro(EAGAIN);
-
break;
-
case ENOMEM:/*12 Out of memory */
-
show_erro(ENOMEM);
-
break;
-
case EACCES:/*13 Permission denied */
-
show_erro(EACCES);
-
break;
-
case EFAULT:/*14 Bad address */
-
show_erro(EFAULT);
-
break;
-
case ENOTBLK:/*15 Block device required */
-
show_erro(ENOTBLK);
-
break;
-
case EBUSY:/*16 Device or resource busy */
-
show_erro(EBUSY);
-
break;
-
case EEXIST:/*17 File exists */
-
show_erro(EEXIST);
-
break;
-
case EXDEV:/*18 Cross-device link */
-
show_erro(EXDEV);
-
break;
-
case ENODEV:/*19 No such device */
-
show_erro(ENODEV);
-
break;
-
case ENOTDIR:/*20 Not a directory */
-
show_erro(ENOTDIR);
-
break;
-
case EISDIR:/*21 Is a directory */
-
show_erro(EISDIR);
-
break;
-
case EINVAL:/*22 Invalid argument */
-
show_erro(EINVAL);
-
break;
-
case ENFILE:/*23 File table overflow */
-
show_erro(ENFILE);
-
break;
-
case EMFILE:/*24 Too many open files */
-
show_erro(EMFILE);
-
break;
-
case ENOTTY:/*25 Not a typewriter */
-
show_erro(ENOTTY);
-
break;
-
case ETXTBSY:/*26 Text file busy */
-
show_erro(ETXTBSY);
-
break;
-
case EFBIG:/*27 File too large */
-
show_erro(EFBIG);
-
break;
-
case ENOSPC:/*28 No space left on device */
-
show_erro(ENOSPC);
-
break;
-
case ESPIPE:/*29 Illegal seek */
-
show_erro(ESPIPE);
-
break;
-
case EROFS:/*30 Read-only file system */
-
show_erro(EROFS);
-
break;
-
case EMLINK:/*31 Too many links */
-
show_erro(EMLINK);
-
break;
-
case EPIPE:/*32 Broken pipe */
-
show_erro(EPIPE);
-
break;
-
case EDOM:/*33 Math argument out of domain of func */
-
show_erro(EDOM);
-
break;
-
case ERANGE:/*34 Math result not representable */
-
show_erro(ERANGE);
-
break;
-
case EDEADLK:/*35 Resource deadlock would occur */
-
show_erro(EDEADLK);
-
break;
-
case ENAMETOOLONG:/*36 File name too long */
-
show_erro(ENAMETOOLONG);
-
break;
-
case ENOLCK:/*37 No record locks available */
-
show_erro(ENOLCK);
-
break;
-
case ENOSYS:/*38 Function not implemented */
-
show_erro(ENOSYS);
-
break;
-
case ENOTEMPTY:/*39 Directory not empty */
-
show_erro(ENOTEMPTY);
-
break;
-
case ELOOP:/*40 Too many symbolic links encountered */
-
show_erro(ELOOP);
-
break;
-
#ifndef EAGAIN
-
case EWOULDBLOCK:/*EAGAIN Operation would block */
-
show_erro(EWOULDBLOCK);
-
break;
-
#endif
-
case ENOMSG:/*42 No message of desired type */
-
show_erro(ENOMSG);
-
break;
-
case EIDRM:/*43 Identifier removed */
-
show_erro(EIDRM);
-
break;
-
case ECHRNG:/*44 Channel number out of range */
-
show_erro(ECHRNG);
-
break;
-
case EL2NSYNC:/*45 Level 2 not synchronized */
-
show_erro(EL2NSYNC);
-
break;
-
case EL3HLT:/*46 Level 3 halted */
-
show_erro(EL3HLT);
-
break;
-
case EL3RST:/*47 Level 3 reset */
-
show_erro(EL3RST);
-
break;
-
case ELNRNG:/*48 Link number out of range */
-
show_erro(ELNRNG);
-
break;
-
case EUNATCH:/*49 Protocol driver not attached */
-
show_erro(EUNATCH);
-
break;
-
case ENOCSI:/*50 No CSI structure available */
-
show_erro(ENOCSI);
-
break;
-
case EL2HLT:/*51 Level 2 halted */
-
show_erro(EL2HLT);
-
break;
-
case EBADE:/*52 Invalid exchange */
-
show_erro(EBADE);
-
break;
-
case EBADR:/*53 Invalid request descriptor */
-
show_erro(EBADR);
-
break;
-
case EXFULL:/*54 Exchange full */
-
show_erro(EXFULL);
-
break;
-
case ENOANO:/*55 No anode */
-
show_erro(ENOANO);
-
break;
-
case EBADRQC:/*56 Invalid request code */
-
show_erro(EBADRQC);
-
break;
-
case EBADSLT:/*57 Invalid slot */
-
show_erro(EBADSLT);
-
break;
-
#ifndef EDEADLK
-
case EDEADLOCK:/*EDEADLK*/
-
show_erro(EDEADLOCK);
-
break;
-
#endif
-
case EBFONT:/*59 Bad font file format */
-
show_erro(EBFONT);
-
break;
-
case ENOSTR:/*60 Device not a stream */
-
show_erro(ENOSTR);
-
break;
-
case ENODATA:/*61 No data available */
-
show_erro(ENODATA);
-
break;
-
case ETIME:/*62 Timer expired */
-
show_erro(ETIME);
-
break;
-
case ENOSR:/*63 Out of streams resources */
-
show_erro(ENOSR);
-
break;
-
case ENONET:/*64 Machine is not on the network */
-
show_erro(ENONET);
-
break;
-
case ENOPKG:/*65 Package not installed */
-
show_erro(ENOPKG);
-
break;
-
case EREMOTE:/*66 Object is remote */
-
show_erro(EREMOTE);
-
break;
-
case ENOLINK:/*67 Link has been severed */
-
show_erro(ENOLINK);
-
break;
-
case EADV:/*68 Advertise error */
-
show_erro(EADV);
-
break;
-
case ESRMNT:/*69 Srmount error */
-
show_erro(ESRMNT);
-
break;
-
case ECOMM:/*70 Communication error on send */
-
show_erro(ECOMM);
-
break;
-
case EPROTO:/*71 Protocol error */
-
show_erro(EPROTO);
-
break;
-
case EMULTIHOP:/*72 Multihop attempted */
-
show_erro(EMULTIHOP);
-
break;
-
case EDOTDOT:/*73 RFS specific error */
-
show_erro(EDOTDOT);
-
break;
-
case EBADMSG:/*74 Not a data message */
-
show_erro(EBADMSG);
-
break;
-
case EOVERFLOW:/*75 Value too large for defined data type */
-
show_erro(EOVERFLOW);
-
break;
-
case ENOTUNIQ:/*76 Name not unique on network */
-
show_erro(ENOTUNIQ);
-
break;
-
case EBADFD:/*77 File descriptor in bad state */
-
show_erro(EBADFD);
-
break;
-
case EREMCHG:/*78 Remote address changed */
-
show_erro(EREMCHG);
-
break;
-
case ELIBACC:/*79 Can not access a needed shared library */
-
show_erro(ELIBACC);
-
break;
-
case ELIBBAD:/*80 Accessing a corrupted shared library */
-
show_erro(ELIBBAD);
-
break;
-
case ELIBSCN:/*81 .lib section in a.out corrupted */
-
show_erro(ELIBSCN);
-
break;
-
case ELIBMAX:/*82 Attempting to link in too many shared libraries */
-
show_erro(ELIBMAX);
-
break;
-
case ELIBEXEC:/*83 Cannot exec a shared library directly */
-
show_erro(ELIBEXEC);
-
break;
-
case EILSEQ:/*84 Illegal byte sequence */
-
show_erro(EILSEQ);
-
break;
-
case ERESTART:/*85 Interrupted system call should be restarted */
-
show_erro(ERESTART);
-
break;
-
case ESTRPIPE:/*86 Streams pipe error */
-
show_erro(ESTRPIPE);
-
break;
-
case EUSERS:/*87 Too many users */
-
show_erro(EUSERS);
-
break;
-
case ENOTSOCK:/*88 Socket operation on non-socket */
-
show_erro(ENOTSOCK);
-
break;
-
case EDESTADDRREQ:/*89 Destination address required */
-
show_erro(EDESTADDRREQ);
-
break;
-
case EMSGSIZE:/*90 Message too long */
-
show_erro(EMSGSIZE);
-
break;
-
case EPROTOTYPE:/*91 Protocol wrong type for socket */
-
show_erro(EPROTOTYPE);
-
break;
-
case ENOPROTOOPT:/*92 Protocol not available */
-
show_erro(ENOPROTOOPT);
-
break;
-
case EPROTONOSUPPORT:/*93 Protocol not supported */
-
show_erro(EPROTONOSUPPORT);
-
break;
-
case ESOCKTNOSUPPORT:/*94 Socket type not supported */
-
show_erro(ESOCKTNOSUPPORT);
-
break;
-
case EOPNOTSUPP:/*95 Operation not supported on transport endpoint */
-
show_erro(EOPNOTSUPP);
-
break;
-
case EPFNOSUPPORT:/*96 Protocol family not supported */
-
show_erro(EPFNOSUPPORT);
-
break;
-
case EAFNOSUPPORT:/*97 Address family not supported by protocol */
-
show_erro(EAFNOSUPPORT);
-
break;
-
case EADDRINUSE:/*98 Address already in use */
-
show_erro(EADDRINUSE);
-
break;
-
case EADDRNOTAVAIL:/*99 Cannot assign requested address */
-
show_erro(EADDRNOTAVAIL);
-
break;
-
case ENETDOWN:/*100 Network is down */
-
show_erro(ENETDOWN);
-
break;
-
case ENETUNREACH:/*101 Network is unreachable */
-
show_erro(ENETUNREACH);
-
break;
-
case ENETRESET:/*102 Network dropped connection because of reset */
-
show_erro(ENETRESET);
-
break;
-
case ECONNABORTED:/*103 Software caused connection abort */
-
show_erro(ECONNABORTED);
-
break;
-
case ECONNRESET:/*104 Connection reset by peer */
-
show_erro(ECONNRESET);
-
break;
-
case ENOBUFS:/*105 No buffer space available */
-
show_erro(ENOBUFS);
-
break;
-
case EISCONN:/*106 Transport endpoint is already connected */
-
show_erro(EISCONN);
-
break;
-
case ENOTCONN:/*107 Transport endpoint is not connected */
-
show_erro(ENOTCONN);
-
break;
-
case ESHUTDOWN:/*108 Cannot send after transport endpoint shutdown */
-
show_erro(ESHUTDOWN);
-
break;
-
case ETOOMANYREFS:/*109 Too many references: cannot splice */
-
show_erro(ETOOMANYREFS);
-
break;
-
case ETIMEDOUT:/*110 Connection timed out */
-
show_erro(ETIMEDOUT);
-
break;
-
case ECONNREFUSED:/*111 Connection refused */
-
show_erro(ECONNREFUSED);
-
break;
-
case EHOSTDOWN:/*112 Host is down */
-
show_erro(EHOSTDOWN);
-
break;
-
case EHOSTUNREACH:/*113 No route to host */
-
show_erro(EHOSTUNREACH);
-
break;
-
case EALREADY:/*114 Operation already in progress */
-
show_erro(EALREADY);
-
break;
-
case EINPROGRESS:/*115 Operation now in progress */
-
show_erro(EINPROGRESS);
-
break;
-
case ESTALE:/*116 Stale NFS file handle */
-
show_erro(ESTALE);
-
break;
-
case EUCLEAN:/*117 Structure needs cleaning */
-
show_erro(EUCLEAN);
-
break;
-
case ENOTNAM:/*118 Not a XENIX named type file */
-
show_erro(ENOTNAM);
-
break;
-
case ENAVAIL:/*119 No XENIX semaphores available */
-
show_erro(ENAVAIL);
-
break;
-
case EISNAM:/*120 Is a named type file */
-
show_erro(EISNAM);
-
break;
-
case EREMOTEIO:/*121 Remote I/O error */
-
show_erro(EREMOTEIO);
-
break;
-
case EDQUOT:/*122 Quota exceeded */
-
show_erro(EDQUOT);
-
break;
-
-
case ENOMEDIUM:/*123 No medium found */
-
show_erro(ENOMEDIUM);
-
break;
-
case EMEDIUMTYPE:/*124 Wrong medium type */
-
show_erro(EMEDIUMTYPE);
-
break;
-
case ECANCELED:/*125 Operation Canceled */
-
show_erro(ECANCELED);
-
break;
-
case ENOKEY:/*126 Required key not available */
-
show_erro(ENOKEY);
-
break;
-
case EKEYEXPIRED:/*127 Key has expired */
-
show_erro(EKEYEXPIRED);
-
break;
-
case EKEYREVOKED:/*128 Key has been revoked */
-
show_erro(EKEYREVOKED);
-
break;
-
case EKEYREJECTED:/*129 Key was rejected by service */
-
show_erro(EKEYREJECTED);
-
break;
-
-
/* for robust mutexes */
-
case EOWNERDEAD:/*130 Owner died */
-
show_erro(EOWNERDEAD);
-
break;
-
case ENOTRECOVERABLE:/*131 State not recoverable */
-
show_erro(ENOTRECOVERABLE);
-
break;
-
-
case ERFKILL:/*132 Operation not possible due to RF-kill */
-
show_erro(ERFKILL);
-
break;
-
default:
-
printf("value = %d message=%s\n",err,strerror(err));
-
break;
-
}
-
return 0;
-
}
-
-
int show(char *name,int value)
-
{
-
if(name == NULL){
-
return -1;
-
}
-
printf("%s(%d) = %s\n",name,value,strerror(value));
-
return 0;
-
}
其中的函数strerror是将错误码转换为对错误码解释的函数。
阅读(4077) | 评论(1) | 转发(0) |