目录
  1. Ubuntu 中安装 ‘errno’ 命令
  2. 使用 ‘errno -ls’ 命令,查看错误码
  3. 错误码(error code)列表
  4. 错误码列表生成程序

1. Ubuntu 中安装 ‘errno’ 命令:
ubuntu@cuname:~$ sudo apt-get install moreutils
2. 使用 ‘errno -ls’ 命令,查看错误码:
ubuntu@cuname:~$ errno -ls
/* 列分别为:macro name , errno code, description */
/* 宏名称, 错误代码, 描述 */
EPERM 1 Operation not permitted
ENOENT 2 No such file or directory
ESRCH 3 No such process
EINTR 4 Interrupted system call
...

3. 错误码(error code)列表(总共 134 行)
  • 声明头文件 ‘#include <errno.h>’
宏名称错误代码描述
EPERM1Operation not permitted
ENOENT2No such file or directory
ESRCH3No such process
EINTR4Interrupted system call
EIO5Input/output error
ENXIO6No such device or address
E2BIG7Argument list too long
ENOEXEC8Exec format error
EBADF9Bad file descriptor
ECHILD10No child processes
EAGAIN11Resource temporarily unavailable
ENOMEM12Cannot allocate memory
EACCES13Permission denied
EFAULT14Bad address
ENOTBLK15Block device required
EBUSY16Device or resource busy
EEXIST17File exists
EXDEV18Invalid cross-device link
ENODEV19No such device
ENOTDIR20Not a directory
EISDIR21Is a directory
EINVAL22Invalid argument
ENFILE23Too many open files in system
EMFILE24Too many open files
ENOTTY25Inappropriate ioctl for device
ETXTBSY26Text file busy
EFBIG27File too large
ENOSPC28No space left on device
ESPIPE29Illegal seek
EROFS30Read-only file system
EMLINK31Too many links
EPIPE32Broken pipe
EDOM33Numerical argument out of domain
ERANGE34Numerical result out of range
EDEADLK35Resource deadlock avoided
ENAMETOOLONG36File name too long
ENOLCK37No locks available
ENOSYS38Function not implemented
ENOTEMPTY39Directory not empty
ELOOP40Too many levels of symbolic links
EWOULDBLOCK11Resource temporarily unavailable
ENOMSG42No message of desired type
EIDRM43Identifier removed
ECHRNG44Channel number out of range
EL2NSYNC45Level 2 not synchronized
EL3HLT46Level 3 halted
EL3RST47Level 3 reset
ELNRNG48Link number out of range
EUNATCH49Protocol driver not attached
ENOCSI50No CSI structure available
EL2HLT51Level 2 halted
EBADE52Invalid exchange
EBADR53Invalid request descriptor
EXFULL54Exchange full
ENOANO55No anode
EBADRQC56Invalid request code
EBADSLT57Invalid slot
EDEADLOCK35Resource deadlock avoided
EBFONT59Bad font file format
ENOSTR60Device not a stream
ENODATA61No data available
ETIME62Timer expired
ENOSR63Out of streams resources
ENONET64Machine is not on the network
ENOPKG65Package not installed
EREMOTE66Object is remote
ENOLINK67Link has been severed
EADV68Advertise error
ESRMNT69Srmount error
ECOMM70Communication error on send
EPROTO71Protocol error
EMULTIHOP72Multihop attempted
EDOTDOT73RFS specific error
EBADMSG74Bad message
EOVERFLOW75Value too large for defined data type
ENOTUNIQ76Name not unique on network
EBADFD77File descriptor in bad state
EREMCHG78Remote address changed
ELIBACC79Can not access a needed shared library
ELIBBAD80Accessing a corrupted shared library
ELIBSCN81.lib section in a.out corrupted
ELIBMAX82Attempting to link in too many shared libraries
ELIBEXEC83Cannot exec a shared library directly
EILSEQ84Invalid or incomplete multibyte or wide character
ERESTART85Interrupted system call should be restarted
ESTRPIPE86Streams pipe error
EUSERS87Too many users
ENOTSOCK88Socket operation on non-socket
EDESTADDRREQ89Destination address required
EMSGSIZE90Message too long
EPROTOTYPE91Protocol wrong type for socket
ENOPROTOOPT92Protocol not available
EPROTONOSUPPORT93Protocol not supported
ESOCKTNOSUPPORT94Socket type not supported
EOPNOTSUPP95Operation not supported
EPFNOSUPPORT96Protocol family not supported
EAFNOSUPPORT97Address family not supported by protocol
EADDRINUSE98Address already in use
EADDRNOTAVAIL99Cannot assign requested address
ENETDOWN100Network is down
ENETUNREACH101Network is unreachable
ENETRESET102Network dropped connection on reset
ECONNABORTED103Software caused connection abort
ECONNRESET104Connection reset by peer
ENOBUFS105No buffer space available
EISCONN106Transport endpoint is already connected
ENOTCONN107Transport endpoint is not connected
ESHUTDOWN108Cannot send after transport endpoint shutdown
ETOOMANYREFS109Too many references: cannot splice
ETIMEDOUT110Connection timed out
ECONNREFUSED111Connection refused
EHOSTDOWN112Host is down
EHOSTUNREACH113No route to host
EALREADY114Operation already in progress
EINPROGRESS115Operation now in progress
ESTALE116Stale file handle
EUCLEAN117Structure needs cleaning
ENOTNAM118Not a XENIX named type file
ENAVAIL119No XENIX semaphores available
EISNAM120Is a named type file
EREMOTEIO121Remote I/O error
EDQUOT122Disk quota exceeded
ENOMEDIUM123No medium found
EMEDIUMTYPE124Wrong medium type
ECANCELED125Operation canceled
ENOKEY126Required key not available
EKEYEXPIRED127Key has expired
EKEYREVOKED128Key has been revoked
EKEYREJECTED129Key was rejected by service
EOWNERDEAD130Owner died
ENOTRECOVERABLE131State not recoverable
ERFKILL132Operation not possible due to RF-kill
EHWPOISON133Memory page has hardware error
ENOTSUP95Operation not supported

4. 错误码列表生成程序(待优化/待注释)

代码如下(运行前提:必须安装了 ‘errno’ 命令,即上述的步骤 1 和 2 ):

/* test1.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>

int getALine(int, char *);
void handleALine(char *aLine, char *resStr, int *resStrLen);

void main(){
    char buf_ALine[512];
    char buf_HandledALine[512];
    int len;
    // 将命令‘errno -ls’输出重定向到文件‘data.txt’,
    system("errno -ls > data.txt");

    int so = open("data.txt", O_RDONLY);
    int de = open("error_code.txt", O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    char a[] = "|宏名称|错误代码|描述|\n";
    char b[] = "|:--|:--:|:--|\n";
    write(de, a, sizeof(a) - 1);
    write(de, b, sizeof(b) - 1);

    while(getALine(so, buf_ALine) > 0){
        handleALine(buf_ALine, buf_HandledALine, &len);
        // write to de
        if(write(de, buf_HandledALine, len) != len){
            printf("error: write file failed.\n");
            exit(EXIT_FAILURE);
        }
    }
    
    close(so);
    close(de);
}

void handleALine(char *aLine, char *resStr, int *resStrLen){
    // printf("%s", aLine);
    // init
    *resStrLen = 0;
    char *pstr = aLine;
    char *rstr = resStr;
    *rstr++ = '|';
    (*resStrLen)++;
    int count = 0; // max 2

    char pchar = -1;
    char cchar;

    while(*pstr != '\n'){
        cchar = *pstr;
        if(*resStrLen > 0){
            pchar = *(pstr - 1);
        }
        if(pchar == ' ' && cchar >= '0' && cchar <= '9' && count < 2){
            *rstr++ = '|';
            (*resStrLen)++;
            count++;
        }else if(pchar >= '0' && pchar <= '9' && cchar == ' ' && count < 2){
            *rstr++ = '|';
            (*resStrLen)++;
            count++;
        }

        *rstr++ = *pstr++;
        (*resStrLen)++;
    }
    *rstr++ = '|';
    *rstr = '\n';
    (*resStrLen) += 2;
}

int getALine(int fd, char *buf){
    int count = 0;
    char *ptr = buf;
    int res;
    char c;
    do{
        res = read(fd, &c, 1);
        if(res == 0){
            // EOF
            *ptr++ = '\n';
            break;
        }else if(res == 1){
            *ptr++ = c;
            count++;
        }else{
            // error
            printf("error: getALine failed.\n");
            exit(EXIT_FAILURE);
        }
    }while(c != '\n');
    // 
    *ptr = '\0';
    return count;
}

Logo

更多推荐