Should I set errno?

后端 未结 6 855
盖世英雄少女心
盖世英雄少女心 2020-12-14 05:37

I\'m writing a module which exports an interface similar to send and recv.

Since those functions are supposed to return respectively the nu

6条回答
  •  一个人的身影
    2020-12-14 06:16

    Actually, you probably can do "proper" (as you put it) error management since you return an int.

    Just use non-negative values for the number of bytes read or written and negative values for error codes. You don't have to limit yourself to -1:

    enum myerrors {
        ERR_NO_MEMORY    = -1,
        ERR_BAD_ARGS     = -2,
        ERR_CPU_EXPLODED = -3,
        // and so on
    };
    

    However, setting errno in the fashion you want is valid. The standard states that errno expands to a modifiable lvalue, meaning you can set it. From C1x/n1425, 7.5 Errors :

    ... and errno which expands to a modifiable lvalue that has type int, the value of which is set to a positive error number by several library functions.

提交回复
热议问题