LINUX TIPS AND TRICKS --- March 09, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________________________

Error Handling
By Danny Kalev

This week, I will discuss the C errno facility and its interfaces.

Each system call returns a status code. A zero value indicates success 
whereas small negative numbers represent error codes. The 
implementation automatically stores a system call's return value in a 
global variable called errno, and each invocation of the system call 
overwrites the previous errno value (note that in multithreaded 
environments, each thread has a distinct errno). errno and its 
associated functions are declared in the standard header file 
errno.h. 

The perror() function has the following prototype:

    void perror(const char * msg);

perror() takes an error description string as an argument and prints it 
to stderr (typically, the console). In addition to the user-supplied 
string, perror() adds a colon and a verbal description of the current 
errno value. The common approach passes the program's filename as an 
argument. For example:

    if (open("db_file.dat", O_RDONLY)0)
      perror ("database.c"); /*pass current filename*/

The output may look as follows:

    database.c: No such file or directory

Remember, do not append a newline character to the argument; perror() 
will add one after the error description.

Translating an error code to its verbal description may be useful if, 
for example, you wish to log the error messages or format them before 
printing. Use the function strerror() for this purpose. strerror() has 
the following prototype:

    char * strerror(int errcode);

The function takes an error code and returns a statically allocated 
string describing that code. Remember that subsequent calls to strerror
() overwrite the description string. Unless you intend to use it 
immediately, you should copy it to a local string. Using strerror(), 
the following example translates the current errno code to a string 
that is printed to the user's screen:

    printf("error: %s", strerror(errno));

In addition to these functions, errno.h defines an array of sys_nerr 
pointers to error messages called sys_errlist. The elements of 
sys_errlist are const strings, so you cannot modify them. The following 
example retrieves the error description associated with the current 
errno value by using sys_errlist and errno as the index:

    printf("error: %s", sys_errlist[errno]);

sys_errlist use is deprecated and non-portable; however, it may exist 
in legacy code. The strerror() function remains the preferred 
alternative for retrieving the error description associated with an 
errno code.


About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com.
________________________________________________________________________________

ADDITIONAL RESOURCES

Errno Libretto 
The system call return value is supplemented by the error number, or 
errno value

http://www.unixinsider.com/jsw/lintps_nl/swol-10-1995/
swol-10-sysadmin.html

Understanding Unix shells and environment variables, Part 2 
Examine and customize your Unix environment

http://www.unixinsider.com/jsw/lintps_nl/swol-06-2000/
swol-0623-unix101.html

Solaris 8 threads attributes 
New features in the Solaris 8 user thread's libraries

http://www.unixinsider.com/jsw/lintps_nl/swol-12-2000/
swol-1218-insidesolaris.html
________________________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/261!skip=207

Ask the Geek
Got a question about Linux setup, tuning, security, or maintenance? 
Hackers, newbies, and all the geeks in between knock heads and devise 
solutions.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6c981/375!skip=314
________________________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________________________

PRIVACY POLICY

http://www2.itworld.com/CDA/ITW_Privacy_Policy

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
