当调用execl(…)时,我得到一个errno=2。这是什么意思?我怎么知道这个errno的意思?


当前回答

我使用以下脚本:

#!/usr/bin/python

import errno
import os
import sys

toname = dict((str(getattr(errno, x)), x) 
              for x in dir(errno) 
              if x.startswith("E"))
tocode = dict((x, getattr(errno, x)) 
              for x in dir(errno) 
              if x.startswith("E"))

for arg in sys.argv[1:]:
    if arg in tocode:
        print arg, tocode[arg], os.strerror(tocode[arg])
    elif arg in toname:
        print toname[arg], arg, os.strerror(int(arg))
    else:
        print "Unknown:", arg

其他回答

我使用以下脚本:

#!/usr/bin/python

import errno
import os
import sys

toname = dict((str(getattr(errno, x)), x) 
              for x in dir(errno) 
              if x.startswith("E"))
tocode = dict((x, getattr(errno, x)) 
              for x in dir(errno) 
              if x.startswith("E"))

for arg in sys.argv[1:]:
    if arg in tocode:
        print arg, tocode[arg], os.strerror(tocode[arg])
    elif arg in toname:
        print toname[arg], arg, os.strerror(int(arg))
    else:
        print "Unknown:", arg

这比在errno.h中查找代码要快,比这里发布的大多数解决方案都要短,并且不需要安装第三方工具:

perl -E 'say $!2 =转变'

收益率

没有这样的文件或目录

在Linux上也有一个非常简洁的工具,可以立即告诉每个错误代码的含义。Ubuntu: apt-get install errno。

然后,例如,如果您想获取错误类型2的描述,只需在终端中键入errno 2。

使用errno -l可以得到一个包含所有错误及其描述的列表。比之前的海报提到的其他方法简单多了。

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main(int i, char *c[]) { 
  if (i != 2)  
    fprintf(stderr, "Usage: perror errno\n"); 
  else { 
    errno = atoi(c[1]); 
    perror(""); 
  } 
  exit(0); 
}    

适用于Solaris。 Cc perror.c -o perror <<使用这一行来编译它

这里是文档。这应该会告诉你它的含义以及如何处理它们。您应该避免使用数值,并使用这里列出的常量,因为数字在不同的系统之间可能会发生变化。