3.3 _doserrno, errno, sys_errlist, sys_nerr

The _doserrno, errno, sys_errlist, and sys_nerr variables contain error codes and are used by the perror and strerror routines to print error information.

These variables are declared in the STDLIB.H include file. Manifest constants for the errno variables are declared in the ERRNO.H include file. The declarations are as follows:

extern int _doserrno;

extern int errno;

extern char *sys_errlist[ ];

extern int sys_nerr;

The errno variable is set to an integer value to reflect the type of error that has occurred in a system-level call. Each errno value is associated with an error message, which can be printed with the perror routine or stored in a string with the strerror routine.

Note that only some routines set the errno variable. If a routine sets errno, the description of the routine in the reference section says so explicitly.

The value of errno reflects the error value for the last call that set errno. However, this value is not necessarily reset by later successful calls. To avoid confusion, test for errors immediately after a call.

The include file ERRNO.H contains the definitions of the errno values. However, not all of the definitions given in ERRNO.H are used in DOS. Some of the values in ERRNO.H are present to maintain compatibility with the UNIX (and XENIX) operating system.

The errno values in DOS are a subset of the values for errno in XENIX systems. Thus, the errno value is not necessarily the same as the actual error code returned by a DOS system call. To access the actual DOS error code, use the _doserrno variable, which contains this value.

In general, you should use _doserrno only for error detection in operations involving input and output, since the errno values for input and output errors have DOS error-code equivalents. In other cases, the value of _doserrno is undefined.

The sys_errlist variable is an array; the perror and strerror routines use it to process error information. The sys_nerr variable tells how many elements the sys_errlist array contains.

Table 3.1 gives the errno values for DOS, the system error message for each value, and the value of each constant. Note that only the ERANGE and EDOM constants are specified in the ANSI standard.

Table 3.1 errno Values and Their Meanings

Constant Meaning Value

E2BIG Argument list too long 7
EACCES Permission denied 13
EBADF Bad file number 9
EDEADLOCK Resource deadlock would occur 36
EDOM Math argument 33
EEXIST File exists 17
EINVAL Invalid argument 22
EMFILE Too many open files 24
ENOENT No such file or directory 2
ENOEXEC Exec format error 8
ENOMEM Not enough memory 12
ENOSPC No space left on device 28
ERANGE Result too large 34
EXDEV Cross-device link 18