免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2872 | 回复: 8
打印 上一主题 下一主题

[C] C标准库中有errno吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-04 14:28 |只看该作者 |倒序浏览
C标准库中有errno吗?
其中的errno机制到底是如何的?

我看到Linux上有用到errno,但如果我在nucleus操作系统上是否也可以用errno呢?

论坛徽章:
0
2 [报告]
发表于 2008-05-04 14:48 |只看该作者
试一下就知道了
不需要问

论坛徽章:
0
3 [报告]
发表于 2008-05-04 15:23 |只看该作者

回复 #1 eNsEmpire 的帖子

标准库中有

论坛徽章:
0
4 [报告]
发表于 2008-05-04 17:09 |只看该作者

回复 #3 wantjutju 的帖子

我在nucleus操作系统中编写一个应用程序,那么
只要我在应用程序中加入<errno.h>头文件,那么如果某个系统调用出错,会把出错信息反映到errno变量上来吗?

Linux操作系统中是完全可以的,但我现在用nucleus操作系统,该系统资源比较少,而且我也是开始学习该操作系统的

即只要是标准C库中的,那么就可以与操作系统无关吗?

[ 本帖最后由 eNsEmpire 于 2008-5-4 17:10 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-05-04 17:21 |只看该作者
原帖由 eNsEmpire 于 2008-5-4 17:09 发表
我在nucleus操作系统中编写一个应用程序,那么
只要我在应用程序中加入头文件,那么如果某个系统调用出错,会把出错信息反映到errno变量上来吗?

Linux操作系统中是完全可以的,但我现在用nucleus操作系统, ...



NAME
       errno - number of last error

SYNOPSIS
       #include <errno.h>

DESCRIPTION
       The  <errno.h> header file defines the integer variable errno, which is
       set by system calls and some library functions in the event of an error
       to  indicate  what  went wrong.  Its value is significant only when the
       call returned an error (usually -1), and a function that  does  succeed
       is allowed to change errno.

       Sometimes,  when  -1 is also a valid successful return value one has to
       zero errno before the call in order to detect possible errors.

       errno is defined by the ISO C standard to be  a  modifiable  lvalue  of
       type  int,  and  must not be explicitly declared; errno may be a macro.
       errno is thread-local; setting it in one thread  does  not  affect  its
       value in any other thread.

     Valid  error numbers are all nonzero; errno is never set to zero by any
       library function.  All the error names specified by POSIX.1  must  have
       distinct  values,  with  the exception of EAGAIN and EWOULDBLOCK, which
       may be the same.

       Below is a list of the symbolic error names that are defined on  Linux.
       Some  of  these are marked POSIX.1, indicating that the name is defined
       by POSIX.1-2001, or C99, indicating that the name is defined by C99.


       E2BIG           Argument list too long (POSIX.1)

       EACCES          Permission denied (POSIX.1)

       EADDRINUSE      Address already in use (POSIX.1)

       EADDRNOTAVAIL   Address not available (POSIX.1)

       EAFNOSUPPORT    Address family not supported (POSIX.1)

       EAGAIN          Resource temporarily unavailable (may be the same value
                       as EWOULDBLOCK) (POSIX.1)

       EALREADY        Connection already in progress (POSIX.1)
  .................................
太多了,我就不帖了
请用man
不懂就找男人。。。
哈哈

[ 本帖最后由 lishizelibin 于 2008-5-4 17:23 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-05-04 17:24 |只看该作者
原帖由 eNsEmpire 于 2008-5-4 17:09 发表
我在nucleus操作系统中编写一个应用程序,那么
只要我在应用程序中加入头文件,那么如果某个系统调用出错,会把出错信息反映到errno变量上来吗?

Linux操作系统中是完全可以的,但我现在用nucleus操作系统, ...

实现了C标准库就应该有,而无论是什么操作系统

手头有一个简单的实现,以前随手写的,和linux实现差不多


#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>

static char *errmsg[100] = {
    "Success","1","2","3","4","5","6","7","8",
    "Bad file descriptor...",
    "10"
};

int myerrno;

int mywrite(int fd,char *buf,unsigned int size)
{
    int ret;
    asm volatile(
        "int $0x80\n\t":"=a"(ret):"0"(4),"b"(fd),"c"((long)buf),"d"(size)
    );
    if (ret)
        myerrno = -ret;
    return -!!ret;
}

char *mystrerror(int errno)
{
    return errmsg[errno];
}

void myperror()
{
    fprintf(stderr,"errstr: %s\n",mystrerror(myerrno));
}

int main()
{
    if (mywrite(100,"test\n",5) < 0)
        myperror();

    if (mywrite(1,NULL,5) < 0)
        myperror();
}


[ 本帖最后由 flw2 于 2008-5-4 17:26 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-05-04 21:28 |只看该作者

回复 #5 lishizelibin 的帖子

man中我看过,但因为不知道是否与平台无关!

论坛徽章:
0
8 [报告]
发表于 2008-05-05 09:04 |只看该作者
原帖由 eNsEmpire 于 2008-5-4 21:28 发表
man中我看过,但因为不知道是否与平台无关!

实现肯定是实现相关的,但是它们都符合C标准
比如fopen就是打开一个文件,如果失败了,errno就表示出错的原因

论坛徽章:
0
9 [报告]
发表于 2008-05-05 09:20 |只看该作者

回复 #8 flw2 的帖子

很感谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP