Linux programming-Get error information

Source: Internet
Author: User

After a library function or system call function is called, 0 is returned for successful execution in most cases. If the execution fails, 0 is returned.
Back to-1, and assign values to the external global variable error to indicate the specific error. All error codes are stored in errno. h.
File. The following is part of the/usr/include/ASM/errno. h file.
Come from/usr/include/ASM/errno. h
# Ifndef _ i1__errno_h
# DEFINE _ i1__errno_h

# Define eperm 1/* operation not permitted * // No operation permission
# Define enoent 2/* no such file or directory * // the file and directory do not exist
# Define esrch 3/* no such process * // this program does not exist
# Define eintr 4/* interrupted system call * // system call interruption
# Define EIO 5/* I/O Error * // I/O Error
......
To print the error message corresponding to the specific error value, the perror function is generally used. This function declaration

// Come from/usr/include/stdio. h
738 _ begin_namespace_std
739/* print a message describing the meaning of the value of errno. // print the error value
Description
740
741 this function is a possible cancellation point and therefore not
742 marked with _ Throw .*/
743 extern void perror (_ const char * _ s); // perror function declaration
744 _ end_namespace_std
In addition, the Linux system provides the following error handling functions:
# Include // string. h header file
Char * strerror (INT errnum); // strerror function declaration
# Include // errno. h header file
Extern char * sys_errlist []; // sys_errlist function declaration
Extern int sys_nerr; // sys_nerr function declaration
Perror () can write language-related messages to standard error output, which describes the problems encountered during the call of system functions or library functions.
. Perror () first outputs the parameter string s, followed by a colon, space, message, and line feed. For sending
The parameter string should include the name of the program that causes the error. The error code is taken from the symbol errno.
This symbol will be set, but it will not be cleared after no error call is executed. Message content and
The strerror () function returns the same content. If a null string is given, the perror () function only outputs messages and
Line Break.
The perror () function does not return values. If the errnum (form parameter of the sterror function) message number is valid, strerror ()
Returns a pointer to a language-related message string. The program should not modify the array to which it points, but will call this function later.
This array can be overwritten. If the valid errnum message number does not include messages related to the corresponding language, strerror () will
Errnum is used as the index to add sys_errlist to obtain the message string. If the errnum message number is invalid
Strerror () returns a pointer to a null string.
To format messages in various forms, the system provides the strerror () function and the message string array sys_errlist. Strerror ()
The function maps error numbers in errnum to language-related error message strings, and returns
Pointer. The returned message string does not contain line breaks. You can use errno as the index to add sys_errlist to obtain
The unconverted message string of the line break.
In addition, the environmental impact of external languages may affect the perror function. The environmental variable Lang environment variable specifies
The language type of the message output by strerror () and perror. If the language-related message is unavailable or Lang is not set,

Or set it as a null string, the default version of the message associated with the "c" language will be used.

 

This article was first launched by .com. Please keep this information for reprinting!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.