Chinaunix首页 | 论坛 | 博客
  • 博客访问: 320269
  • 博文数量: 43
  • 博客积分: 1044
  • 博客等级: 准尉
  • 技术积分: 658
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-20 14:56
个人简介

人法地,地法天,天法道,道法自然。

文章分类

全部博文(43)

文章存档

2019年(1)

2013年(3)

2012年(15)

2011年(24)

分类: LINUX

2011-02-09 10:24:27

    开发过*nix程序的人都知道当一个系统调用返回错误时,需要查看头文件
errno.h中的errno。但是,errno只给出一个数字。这个数字很难直接看到API
错误的直接原因。

    所以就有了一下的程序:在Ubuntu上编译通过
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <string.h>

  4. #define show_erro(name) show(#name,name)

  5. int show(char *,int);

  6. int main(int argc,char* argv[])
  7. {
  8.     int err = 0;

  9.     if(argc <= 1){
  10.         printf("usage: %s error value\n"
  11.                "eg: %s 1\n",argv[0],argv[0]);
  12.         return -1;
  13.     }
  14.     err = atoi(argv[1]);
  15.     switch(err){
  16.         case EPERM:/* 1 Operation not permitted */
  17.             show_erro(EPERM);
  18.             break;
  19.         case ENOENT:/* 2 No such file or directory */
  20.             show_erro(ENOENT);
  21.             break;
  22.         case ESRCH:/* 3 No such process */
  23.             show_erro(ESRCH);
  24.             break;
  25.         case EINTR:/* 4 Interrupted system call */
  26.             show_erro(EINTR);
  27.             break;
  28.         case EIO:/* 5 I/O error */
  29.             show_erro(EIO);
  30.             break;
  31.         case ENXIO:/* 6 No such device or address */
  32.             show_erro(ENXIO);
  33.             break;
  34.         case E2BIG:/* 7 Argument list too long */
  35.             show_erro(E2BIG);
  36.             break;
  37.         case ENOEXEC:/* 8 Exec format error */
  38.             show_erro(ENOEXEC);
  39.             break;
  40.         case EBADF:/* 9 Bad file number */
  41.             show_erro(EBADF);
  42.             break;
  43.         case ECHILD:/*10 No child processes */
  44.             show_erro(ECHILD);
  45.             break;
  46.         case EAGAIN:/*11 Try again */
  47.             show_erro(EAGAIN);
  48.             break;
  49.         case ENOMEM:/*12 Out of memory */
  50.             show_erro(ENOMEM);
  51.             break;
  52.         case EACCES:/*13 Permission denied */
  53.             show_erro(EACCES);
  54.             break;
  55.         case EFAULT:/*14 Bad address */
  56.             show_erro(EFAULT);
  57.             break;
  58.         case ENOTBLK:/*15 Block device required */
  59.             show_erro(ENOTBLK);
  60.             break;
  61.         case EBUSY:/*16 Device or resource busy */
  62.             show_erro(EBUSY);
  63.             break;
  64.         case EEXIST:/*17 File exists */
  65.             show_erro(EEXIST);
  66.             break;
  67.         case EXDEV:/*18 Cross-device link */
  68.             show_erro(EXDEV);
  69.             break;
  70.         case ENODEV:/*19 No such device */
  71.             show_erro(ENODEV);
  72.             break;
  73.         case ENOTDIR:/*20 Not a directory */
  74.             show_erro(ENOTDIR);
  75.             break;
  76.         case EISDIR:/*21 Is a directory */
  77.             show_erro(EISDIR);
  78.             break;
  79.         case EINVAL:/*22 Invalid argument */
  80.             show_erro(EINVAL);
  81.             break;
  82.         case ENFILE:/*23 File table overflow */
  83.             show_erro(ENFILE);
  84.             break;
  85.         case EMFILE:/*24 Too many open files */
  86.             show_erro(EMFILE);
  87.             break;
  88.         case ENOTTY:/*25 Not a typewriter */
  89.             show_erro(ENOTTY);
  90.             break;
  91.         case ETXTBSY:/*26 Text file busy */
  92.             show_erro(ETXTBSY);
  93.             break;
  94.         case EFBIG:/*27 File too large */
  95.             show_erro(EFBIG);
  96.             break;
  97.         case ENOSPC:/*28 No space left on device */
  98.             show_erro(ENOSPC);
  99.             break;
  100.         case ESPIPE:/*29 Illegal seek */
  101.             show_erro(ESPIPE);
  102.             break;
  103.         case EROFS:/*30 Read-only file system */
  104.             show_erro(EROFS);
  105.             break;
  106.         case EMLINK:/*31 Too many links */
  107.             show_erro(EMLINK);
  108.             break;
  109.         case EPIPE:/*32 Broken pipe */
  110.             show_erro(EPIPE);
  111.             break;
  112.         case EDOM:/*33 Math argument out of domain of func */
  113.             show_erro(EDOM);
  114.             break;
  115.         case ERANGE:/*34 Math result not representable */
  116.             show_erro(ERANGE);
  117.             break;
  118.         case EDEADLK:/*35 Resource deadlock would occur */
  119.             show_erro(EDEADLK);
  120.             break;
  121.         case ENAMETOOLONG:/*36 File name too long */
  122.             show_erro(ENAMETOOLONG);
  123.             break;
  124.         case ENOLCK:/*37 No record locks available */
  125.             show_erro(ENOLCK);
  126.             break;
  127.         case ENOSYS:/*38 Function not implemented */
  128.             show_erro(ENOSYS);
  129.             break;
  130.         case ENOTEMPTY:/*39 Directory not empty */
  131.             show_erro(ENOTEMPTY);
  132.             break;
  133.         case ELOOP:/*40 Too many symbolic links encountered */
  134.             show_erro(ELOOP);
  135.             break;
  136.         #ifndef EAGAIN
  137.             case EWOULDBLOCK:/*EAGAIN Operation would block */
  138.                 show_erro(EWOULDBLOCK);
  139.                 break;
  140.         #endif
  141.         case ENOMSG:/*42 No message of desired type */
  142.             show_erro(ENOMSG);
  143.             break;
  144.         case EIDRM:/*43 Identifier removed */
  145.             show_erro(EIDRM);
  146.             break;
  147.         case ECHRNG:/*44 Channel number out of range */
  148.             show_erro(ECHRNG);
  149.             break;
  150.         case EL2NSYNC:/*45 Level 2 not synchronized */
  151.             show_erro(EL2NSYNC);
  152.             break;
  153.         case EL3HLT:/*46 Level 3 halted */
  154.             show_erro(EL3HLT);
  155.             break;
  156.         case EL3RST:/*47 Level 3 reset */
  157.             show_erro(EL3RST);
  158.             break;
  159.         case ELNRNG:/*48 Link number out of range */
  160.             show_erro(ELNRNG);
  161.             break;
  162.         case EUNATCH:/*49 Protocol driver not attached */
  163.             show_erro(EUNATCH);
  164.             break;
  165.         case ENOCSI:/*50 No CSI structure available */
  166.             show_erro(ENOCSI);
  167.             break;
  168.         case EL2HLT:/*51 Level 2 halted */
  169.             show_erro(EL2HLT);
  170.             break;
  171.         case EBADE:/*52 Invalid exchange */
  172.             show_erro(EBADE);
  173.             break;
  174.         case EBADR:/*53 Invalid request descriptor */
  175.             show_erro(EBADR);
  176.             break;
  177.         case EXFULL:/*54 Exchange full */
  178.             show_erro(EXFULL);
  179.             break;
  180.         case ENOANO:/*55 No anode */
  181.             show_erro(ENOANO);
  182.             break;
  183.         case EBADRQC:/*56 Invalid request code */
  184.             show_erro(EBADRQC);
  185.             break;
  186.         case EBADSLT:/*57 Invalid slot */
  187.             show_erro(EBADSLT);
  188.             break;
  189.         #ifndef EDEADLK
  190.             case EDEADLOCK:/*EDEADLK*/
  191.                 show_erro(EDEADLOCK);
  192.                 break;
  193.         #endif
  194.         case EBFONT:/*59 Bad font file format */
  195.             show_erro(EBFONT);
  196.             break;
  197.         case ENOSTR:/*60 Device not a stream */
  198.             show_erro(ENOSTR);
  199.             break;
  200.         case ENODATA:/*61 No data available */
  201.             show_erro(ENODATA);
  202.             break;
  203.         case ETIME:/*62 Timer expired */
  204.             show_erro(ETIME);
  205.             break;
  206.         case ENOSR:/*63 Out of streams resources */
  207.             show_erro(ENOSR);
  208.             break;
  209.         case ENONET:/*64 Machine is not on the network */
  210.             show_erro(ENONET);
  211.             break;
  212.         case ENOPKG:/*65 Package not installed */
  213.             show_erro(ENOPKG);
  214.             break;
  215.         case EREMOTE:/*66 Object is remote */
  216.             show_erro(EREMOTE);
  217.             break;
  218.         case ENOLINK:/*67 Link has been severed */
  219.             show_erro(ENOLINK);
  220.             break;
  221.         case EADV:/*68 Advertise error */
  222.             show_erro(EADV);
  223.             break;
  224.         case ESRMNT:/*69 Srmount error */
  225.             show_erro(ESRMNT);
  226.             break;
  227.         case ECOMM:/*70 Communication error on send */
  228.             show_erro(ECOMM);
  229.             break;
  230.         case EPROTO:/*71 Protocol error */
  231.             show_erro(EPROTO);
  232.             break;
  233.         case EMULTIHOP:/*72 Multihop attempted */
  234.             show_erro(EMULTIHOP);
  235.             break;
  236.         case EDOTDOT:/*73 RFS specific error */
  237.             show_erro(EDOTDOT);
  238.             break;
  239.         case EBADMSG:/*74 Not a data message */
  240.             show_erro(EBADMSG);
  241.             break;
  242.         case EOVERFLOW:/*75 Value too large for defined data type */
  243.             show_erro(EOVERFLOW);
  244.             break;
  245.         case ENOTUNIQ:/*76 Name not unique on network */
  246.             show_erro(ENOTUNIQ);
  247.             break;
  248.         case EBADFD:/*77 File descriptor in bad state */
  249.             show_erro(EBADFD);
  250.             break;
  251.         case EREMCHG:/*78 Remote address changed */
  252.             show_erro(EREMCHG);
  253.             break;
  254.         case ELIBACC:/*79 Can not access a needed shared library */
  255.             show_erro(ELIBACC);
  256.             break;
  257.         case ELIBBAD:/*80 Accessing a corrupted shared library */
  258.             show_erro(ELIBBAD);
  259.             break;
  260.         case ELIBSCN:/*81 .lib section in a.out corrupted */
  261.             show_erro(ELIBSCN);
  262.             break;
  263.         case ELIBMAX:/*82 Attempting to link in too many shared libraries */
  264.             show_erro(ELIBMAX);
  265.             break;
  266.         case ELIBEXEC:/*83 Cannot exec a shared library directly */
  267.             show_erro(ELIBEXEC);
  268.             break;
  269.         case EILSEQ:/*84 Illegal byte sequence */
  270.             show_erro(EILSEQ);
  271.             break;
  272.         case ERESTART:/*85 Interrupted system call should be restarted */
  273.             show_erro(ERESTART);
  274.             break;
  275.         case ESTRPIPE:/*86 Streams pipe error */
  276.             show_erro(ESTRPIPE);
  277.             break;
  278.         case EUSERS:/*87 Too many users */
  279.             show_erro(EUSERS);
  280.             break;
  281.         case ENOTSOCK:/*88 Socket operation on non-socket */
  282.             show_erro(ENOTSOCK);
  283.             break;
  284.         case EDESTADDRREQ:/*89 Destination address required */
  285.             show_erro(EDESTADDRREQ);
  286.             break;
  287.         case EMSGSIZE:/*90 Message too long */
  288.             show_erro(EMSGSIZE);
  289.             break;
  290.         case EPROTOTYPE:/*91 Protocol wrong type for socket */
  291.             show_erro(EPROTOTYPE);
  292.             break;
  293.         case ENOPROTOOPT:/*92 Protocol not available */
  294.             show_erro(ENOPROTOOPT);
  295.             break;
  296.         case EPROTONOSUPPORT:/*93 Protocol not supported */
  297.             show_erro(EPROTONOSUPPORT);
  298.             break;
  299.         case ESOCKTNOSUPPORT:/*94 Socket type not supported */
  300.             show_erro(ESOCKTNOSUPPORT);
  301.             break;
  302.         case EOPNOTSUPP:/*95 Operation not supported on transport endpoint */
  303.             show_erro(EOPNOTSUPP);
  304.             break;
  305.         case EPFNOSUPPORT:/*96 Protocol family not supported */
  306.             show_erro(EPFNOSUPPORT);
  307.             break;
  308.         case EAFNOSUPPORT:/*97 Address family not supported by protocol */
  309.             show_erro(EAFNOSUPPORT);
  310.             break;
  311.         case EADDRINUSE:/*98 Address already in use */
  312.             show_erro(EADDRINUSE);
  313.             break;
  314.         case EADDRNOTAVAIL:/*99 Cannot assign requested address */
  315.             show_erro(EADDRNOTAVAIL);
  316.             break;
  317.         case ENETDOWN:/*100 Network is down */
  318.             show_erro(ENETDOWN);
  319.             break;
  320.         case ENETUNREACH:/*101 Network is unreachable */
  321.             show_erro(ENETUNREACH);
  322.             break;
  323.         case ENETRESET:/*102 Network dropped connection because of reset */
  324.             show_erro(ENETRESET);
  325.             break;
  326.         case ECONNABORTED:/*103 Software caused connection abort */
  327.             show_erro(ECONNABORTED);
  328.             break;
  329.         case ECONNRESET:/*104 Connection reset by peer */
  330.             show_erro(ECONNRESET);
  331.             break;
  332.         case ENOBUFS:/*105 No buffer space available */
  333.             show_erro(ENOBUFS);
  334.             break;
  335.         case EISCONN:/*106 Transport endpoint is already connected */
  336.             show_erro(EISCONN);
  337.             break;
  338.         case ENOTCONN:/*107 Transport endpoint is not connected */
  339.             show_erro(ENOTCONN);
  340.             break;
  341.         case ESHUTDOWN:/*108 Cannot send after transport endpoint shutdown */
  342.             show_erro(ESHUTDOWN);
  343.             break;
  344.         case ETOOMANYREFS:/*109 Too many references: cannot splice */
  345.             show_erro(ETOOMANYREFS);
  346.             break;
  347.         case ETIMEDOUT:/*110 Connection timed out */
  348.             show_erro(ETIMEDOUT);
  349.             break;
  350.         case ECONNREFUSED:/*111 Connection refused */
  351.             show_erro(ECONNREFUSED);
  352.             break;
  353.         case EHOSTDOWN:/*112 Host is down */
  354.             show_erro(EHOSTDOWN);
  355.             break;
  356.         case EHOSTUNREACH:/*113 No route to host */
  357.             show_erro(EHOSTUNREACH);
  358.             break;
  359.         case EALREADY:/*114 Operation already in progress */
  360.             show_erro(EALREADY);
  361.             break;
  362.         case EINPROGRESS:/*115 Operation now in progress */
  363.             show_erro(EINPROGRESS);
  364.             break;
  365.         case ESTALE:/*116 Stale NFS file handle */
  366.             show_erro(ESTALE);
  367.             break;
  368.         case EUCLEAN:/*117 Structure needs cleaning */
  369.             show_erro(EUCLEAN);
  370.             break;
  371.         case ENOTNAM:/*118 Not a XENIX named type file */
  372.             show_erro(ENOTNAM);
  373.             break;
  374.         case ENAVAIL:/*119 No XENIX semaphores available */
  375.             show_erro(ENAVAIL);
  376.             break;
  377.         case EISNAM:/*120 Is a named type file */
  378.             show_erro(EISNAM);
  379.             break;
  380.         case EREMOTEIO:/*121 Remote I/O error */
  381.             show_erro(EREMOTEIO);
  382.             break;
  383.         case EDQUOT:/*122 Quota exceeded */
  384.             show_erro(EDQUOT);
  385.             break;
  386.         
  387.         case ENOMEDIUM:/*123 No medium found */
  388.             show_erro(ENOMEDIUM);
  389.             break;
  390.         case EMEDIUMTYPE:/*124 Wrong medium type */
  391.            show_erro(EMEDIUMTYPE);
  392.            break;
  393.         case ECANCELED:/*125 Operation Canceled */
  394.             show_erro(ECANCELED);
  395.             break;
  396.         case ENOKEY:/*126 Required key not available */
  397.             show_erro(ENOKEY);
  398.             break;
  399.         case EKEYEXPIRED:/*127 Key has expired */
  400.             show_erro(EKEYEXPIRED);
  401.             break;
  402.         case EKEYREVOKED:/*128 Key has been revoked */
  403.             show_erro(EKEYREVOKED);
  404.             break;
  405.         case EKEYREJECTED:/*129 Key was rejected by service */
  406.             show_erro(EKEYREJECTED);
  407.             break;
  408.         
  409.         /* for robust mutexes */
  410.         case EOWNERDEAD:/*130 Owner died */
  411.             show_erro(EOWNERDEAD);
  412.             break;
  413.         case ENOTRECOVERABLE:/*131 State not recoverable */
  414.             show_erro(ENOTRECOVERABLE);
  415.             break;
  416.         
  417.         case ERFKILL:/*132 Operation not possible due to RF-kill */
  418.             show_erro(ERFKILL);
  419.             break;
  420.         default:
  421.             printf("value = %d message=%s\n",err,strerror(err));
  422.             break;
  423.     }
  424.     return 0;
  425. }

  426. int show(char *name,int value)
  427. {
  428.     if(name == NULL){
  429.         return -1;
  430.     }
  431.     printf("%s(%d) = %s\n",name,value,strerror(value));
  432.     return 0;
  433. }

    其中的函数strerror是将错误码转换为对错误码解释的函数。
阅读(4077) | 评论(1) | 转发(0) |
0

上一篇:没有了

下一篇:另一种形式的C递归

给主人留下些什么吧!~~

file32016-02-07 01:24:19

这个代码好无聊啊。