Add Book to My BookshelfPurchase This Book Online

Chapter 2 - Utility Routines

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Miscellaneous Functions
There are many more functions provided by the C library on most UNIX systems, especially on SVR4. This section describes a few of the more generally useful ones. For a complete list of all the functions provided by your system, read Chapter 3 of the UNIX Programmer's Manual, which describes the C library.
String to Number Conversion
There are several functions provided to convert character strings to numbers:
   #include <stdlib.h>
   int atoi(const char *str);
   long atol(const char *str);
   double atof(const char *str);
   long strtol(const char *str, char **ptr, int base);
   unsigned long strtoul(const char *str, char **ptr, int base);
   double strtod(const char *str, char **ptr);
Both strtol and strtoul scan str up to the first character inconsistent with a number in the given base. Leading white space is ignored while a leading minus sign produces a negative number. If ptr is non-null, then a pointer to the character in str that terminated the scan is placed into it. Legal inputs to strtol and strtoul are determined by the value of base. If base is 10, decimal numbers are assumed; if base is 16, hexadecimal numbers are assumed, and so forth. Following an optional minus sign, leading zeros are ignored and, if base is 16, a leading 0X or 0x is ignored, too. If base is zero, the string itself determines the base. Following an optional sign, a leading zero indicates octal (base 8), a leading 0X or 0x indicates hexadecimal, and anything else indicates decimal.
strtod scans str up to the first character inconsistent with a floating-point number. If ptr is non-null, then a pointer to the character in str that terminated the scan is placed into it. After ignoring leading white space, strtod accepts an optional sign, a string of digits optionally containing a decimal point, and then an optional exponent part including an E or e, followed by an optional sign, followed by an integer. Thus, the string 123.456 represents the number 123.456, while the string 987.654e-2 represents the number 9.87654. The decimal point character defaults to a period (.), but can vary with international custom (for example, many European countries use a comma).
The other three functions have been around much longer, and are generally provided only for backward compatibility. You can write all three of them in terms of the newer functions:
   #include <stdlib.h>
   int
   atoi(char *str)
   {
       return((int) strtol(str, (char **) 0, 10));
   }
   long
   atol(char *str)
   {
       return(strtol(str, (char **) 0, 10));
   }
   double
   atof(char *str)
   {
     return(strtod(str, (char **) 0));
   }
Printing Error Messages
Every UNIX system call, and many of the library routines, returns an error code when something goes wrong. This error code is stored as a small integer in the external variable errno. The values that can be placed in errno are defined in the include file errno.h, and the manual page for each system call describes the errors that it can return.
The errors defined in errno.h can vary between different versions of UNIX, although most versions have at least a subset of them in common. However, because the errors do vary, it is unwise for a program to interpret the numerical values of errno directly. Instead, only the constant names defined in errno.h should be used. Additionally, to provide some consistency between applications, programs should use a standard set of error messages to describe these errors. To do this, use the perror function:
   #include <stdio.h>
   void perror(const char *s);
perror prints the contents of the string s, followed by a colon, followed by a string describing the error in errno, followed by a newline character to the standard error output. For example, the following code prints out the string myprogram: systemcall:, followed by a specific error message describing the way in which systemcall failed:
   if (systemcall(...arguments...) < 0) {
       perror("myprogram: systemcall");
       exit(1);
   }
ANSI C defines another function, strerror:
   #include <string.h>
   char *strerror(int errnum);
This function takes the error number as an argument (simply pass in the value of errno) and returns a pointer to a character string that describes the error. This is often more flexible than perror, since the program has more control over what happens to the error message.
Porting Notes
perror is available on all UNIX systems, and you should use it whenever appropriate. strerror, unfortunately, is not as widely available. On many older systems, an external character array called sys_errlist is defined; you can use errno as an index into this array to achieve the same result:
     char *
     strerror(int errnum)
     {
         extern int sys_nerr;
         extern char *sys_errlist[];
         if (errnum < 0 || errnum >= sys_nerr)
             return(NULL);
         return(sys_errlist[errnum]);
     }
Pausing a Program
Sometimes a program needs to wait for something to happen by “sitting there” for a few seconds. To do this, use the sleep routine:
   #include <unistd.h>
   unsigned int sleep(unsigned int seconds);
When called, sleep causes the program to pause for seconds seconds; when the time expires, sleep returns.
Exiting a Program
To exit a program, use the exit function:
   #include <stdlib.h>
   void exit(int status);
The lower eight bits of the status argument are passed to the parent process when the program terminates; the parent can use this value to determine whether the program terminated normally or abnormally.
UNIX convention dictates that a zero exit status represents normal termination, while a non-zero status indicates abnormal termination. Some programs assign special meanings to their exit status values; for example, grep exits with status 0 if matches are found, status 1 if no matches are found, and status 2 if the command line contained syntax errors or one of the files it was told to search could not be opened. However, most programs exit with status 0 if everything went fine and status 1 if there was a problem.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback