Basic tutorial for errno in Linux

  
 

When an exception occurs in the C api function in linux, the errno variable (includes errno.h) is generally assigned an integer value. Different values ​​indicate different meanings. You can guess the cause of the error by looking at the value. In the actual programming, this trick has solved many problems that seem to be inexplicable. But errno is a number, the specific meaning of the errno.h to read the macro definition, and each review is a very tedious thing. There are several ways to easily get the error message. (1) void perror (const char * s) function description perror ( ) is used to output the error of the previous function to the standard error (stderr), the parameter s refers to The string will be printed first, followed by the error reason string. The cause of this error is the string to be output according to the value of the global variable errno. (2) char *strerror(int errno) converts the error code into a string error message, which can be combined with other information output to the user interface such as fprintf(stderr,"error in CreateProcess %s, Process ID % d ",strerror(errno), processID) Note: Assume that processID is an acquired plastic ID (3) printf ("%m", errno); in addition, not all places can go through error when an error occurs Get the error code, such as the following code snippet /* Note: The following header file uses "" instead of using angle brackets directly because the angle brackets in the blog bus are treated as html symbols, so the internal header file name will be directly Ignore */#include"stdio.h"#include "stdlib.h"#include "errno.h"#include "netdb.h"#include "sys/types.h"#include "netinet/In.h"int main (int argc, char *argv[]){struct hostent *h;if (argc != 2){fprintf (stderr ,"usage: getip address\ ");exit(1); }/* Get host information */if((h=gethostbyname(argv[1])) == NULL){/* If gethostbyname fails, the error message is given */herror(“gethostbyname”);exit(1);}/* The information obtained by the print program*/printf(“Host name : %s\ ”, h-> ;h_name);printf(“IP Address : %s\ ”, inet_ntoa (*(( struct in_addr *)h->h_addr)));return 0;}

/**** *********************************/You can see through the above code: using the gethostbyname() function, you can't Use perror() to output error messages (because the error code is stored in h_errno instead of errno. So, you need to call the herror() function. You simply pass to gethostbyname() a machine name (“bbs.tsinghua.edu.cn”), and then get other information such as IP from the returned structure struct hostent. The program that outputs the IP address in the program needs to explain: h->h_addr is a char*, but the inet_ntoa() function needs to pass a struct in_addr structure. So the above h->h_addr is cast to struct in_addr*, and then all the data is obtained through it.


The error code values ​​defined in errno.h are as follows:

Checking the error code errno is an important method of debugging a program. When an exception occurs in the linuc C api function, the errno variable (includes errno.h) is generally assigned an integer value. Different values ​​indicate different meanings. You can estimate the cause of the error by looking at the value. In the actual programming, this trick has solved many problems that seem to be inexplicable. More trouble is that every time you have to go to the Linux source code to find the meaning of the error code, now paste it out, you need to check it later. The following is from the kernel code of linux 2.4.20-18 /usr/include/asm/errno.h#ifndef _I386_ERRNO_H#define _I386_ERRNO_H#define EPERM 1 /* Operation not permitted */#define ENOENT 2 /* No such file or Directory */#define ESRCH 3 /* No such process */#define EINTR 4 /* Interrupted system call */#define EIO 5 /* I/O error */#define ENXIO 6 /* No such device or address */#define E2BIG 7 /* Arg list too long */#define ENOEXEC 8 /* Exec format error */#define EBADF 9 /* Bad file number */#define ECHILD 10 /* No child processes */#define EAGAIN 11 /* Try again */#define ENOMEM 12 /* Out of memory */#define EACCES 13 /* Permission denied */#define EFAULT 14 /* Bad address */#define ENOTBLK 15 /* Block device required */#define EBUSY 16 /* Device or resource busy */#define EEXIST 17 /* File exists */#define EXDEV 18 /* Cross-device link */#define ENODEV 19 /* No such device */#define ENOTDIR 20 /* Not a Directory */#define EISDIR 21 /* Is a directory */#define EINVAL 22 /* Invalid argument */#define ENFILE 23 /* File table overflow */#define EMFILE 24 /* Too many open files */#define ENOTTY 25 /* Not a typewriter */#define ETXTBSY 26 /* Text file busy */#define EFBIG 27 /* File Too large */#define ENOSPC 28 /* No space left on device */#define ESPIPE 29 /* Illegal seek */#define EROFS 30 /* Read-only file system */#define EMLINK 31 /* Too many links * /#define EPIPE 32 /* Broken pipe */#define EDOM 33 /* Math argument out of domain of func */#define ERANGE 34 /* Math result not representable */#define EDEADLK 35 /* Resource deadlock would occur */#define ENAMETOOLONG 36 /* File name too long */#define ENOLCK 37 /* No record locks available */#define ENOSYS 38 /* Function not implemented */#define ENOTEMPTY 39 /* Directory not empty */#define ELOOP 40 /* Too many symbolic links encountered */

Copyright © Windows knowledge All Rights Reserved