Chapter Contents

Previous

Next
Introduction to the SAS/C Library

The errno Variable

The external int variable errno contains the number of the most recent error or warning condition detected by the run-time library. To use this value, include the header file <errno.h> .

If no error or warning condition is detected, the value of errno is 0. After program execution starts, errno is never reset to 0 by the library. Programs that use errno for information about unusual conditions must set it to 0 before calling a library routine that may detect such a condition.

The <errno.h> file contains declarations of the errno variable and definitions of symbolic names for the values that can be assigned. These names rather than numeric values should be used for errno .

SAS/C defines a number of general-use errno names. There are also many errno names associated with specific sublibraries, notably the SAS/C socket library and the SAS/C POSIX support. Socket errno names are documented in Chapter 15, "The BSD UNIX Socket Library," in SAS/C Library Reference, Volume 2, and errno names related to USS are documented in Chapter 19, "Introduction to POSIX," in SAS/C Library Reference, Volume 2. For a complete listing of all errno values, see SAS/C Compiler and Library Quick Reference Guide.

The following list defines the error names and meanings that are for general use, and thus not associated with a specialized API, such as sockets:
EARG undefined function argument value
EBADF file or socket not open or suitable (synonym for ENOTOPEN )
ECONV data conversion failure
ECORRUPT file is in a corrupt or unreadable state
EDEVICE physical device error
EDOM math function domain error
EDUPKEY attempt to add record with duplicate key
EEXIST file already exists
EFATTR file attribute conflict
EFFORM file format error
EFORBID function execution prevented by run-time options
EILSEQ error in multi-byte character sequence (reserved for future use)
EINTR function failed due to interruption by signal
EINUSE file to be opened was already in use
EINVAL invalid argument (synonym for EARG )
EIO physical I/O error (synonym for EDEVICE )
ELIBERR run-time system internal error
ELIMIT internal limit exceeded
EMFILE too many open files (synonym for ELIMIT ) .p EMVSSSAF2ERR -->
ENFILE too many open HFS files in system
ENFOUND file not found
ENOENT file or directory not found (synonym for ENFOUND )
ENOMEM insufficient memory
ENOSPC no space in file
ENOSYS function not implemented by system
ENOTOPEN synonym for EBADF
EPREV previous error not cleared
ERANGE math function range error
ESYS operating system interface failure
EUNSUPP unsupported I/O operation
EUSAGE incorrect function usage.

The variable errno is implemented as a macro. If you use errno without including <errno.h> , the correct data may not be accessed.

The only portable values for errno are EDOM and ERANGE . The following example illustrates the use of errno :

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

FILE *f;
char *filename;
if (!(f = fopen(filename, "r"))) {
      /* See if any file can be opened. */
   if (errno == ELIMIT) {
      printf("Too many open files\n");
      return(EOF);
   }
   else {
      printf("%s could not be opened, enter new filename\n",
             filename);
      getfname(filename);
   }
}


More Exact Error Information: _msgno Variable

The header file <lcdef.h> contains the declaration of a nonstandard external variable, _msgno . This variable contains the message number of the last SAS/C library diagnostic. For example, if the last message ID were LSCX503, _msgno would contain 503.

_msgno may provide more information about a failure than errno . For instance, trying to read a file that has not been created sets errno to ENFOUND, but you can use _msgno to distinguish the cases of an empty sequential file ( _msgno = 503) and a missing PDS member ( _msgno = 504). _msgno is not portable, so programs that must be portable should use only errno .

_msgno is implemented as a macro, so you should not use the name for any other purpose.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.