20140603
For ERROR.C to analyze source code
Keep looking error.c this feature
The buyer will now assemble their own code and data for example, below:
1. #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <pthread.h>
6 #include "Error.h"
7 #include "Dxyh_thread.h"
8 #include "Record.h"
9 #include "dxyh.h"
10
One-to-one static void Err_handle (int errnoflg, int thread_err_code,
N-const char *FMT, va_list AP);
13
14/*
* Err_sys--Print system error MSG
16 */
+ void Err_sys (const char *cause, ...)
18 {
Va_list ap;
20
Va_start (AP, cause);
Err_handle (1, 0, cause, AP);
Va_end (AP);
return;
/* End Err_sys */
26
27/*
* Err_msg--Print normal Err Msg
29 */
void err_msg (const char *cause, ...)
31 {
Va_list ap;
33
Va_start (AP, cause);
Err_handle (0, 0, cause, AP);
Va_end (AP);
PNS return;
*/* End ERR_MSG */
39
40/*
* T_err_sys--Thread Print system error MSG, the Quit
42 */
T_err_sys void (int thread_err_code, const char *cause, ...)
44 {
Va_list ap;
46
Va_start (AP, cause);
Err_handle (2, Thread_err_code, cause, AP);
Va_end (AP);
Exit (Exit_failure);
*/* End T_err_sys */
52
53/*
* Err_handle--Error handle function
* @errnoflg: If none zero would show sys err, otherwise not
* @thread_err_code: Error code in thread
* @fmt: Err string format wants printing
* @ap: Handle argument
59 */
The static void Err_handle (int errnoflg, int thread_err_code,
*fmt const CHAR, va_list AP)
62 {
Errno_save int, n;
(+ char buf[maxline];
65
vsnprintf (buf, sizeof (BUF), FMT, AP);
n = strlen (BUF);
/*if want to show system error msg*/
if (1 = = ERRNOFLG) {
/*save errno, because ' strerror ' may modify it*/
Errno_save = errno;
snprintf (buf+n, sizeof (BUF)-N,
":%s", Strerror (Errno_save));
74}
All else if (2 = = ERRNOFLG)
snprintf (Buf+n,
(BUF)-N, ":%s", strerror (Thread_err_cod e));
strcat (buf, "\ n");
79
/*output the final error msg*/
Bayi Fflush (stdout); /*in case stdout and stderr is the same*/
My_lock_mutex_wait ();
Fputs (buf, stderr);
My_lock_mutex_release ();
Fflush (stderr);
The return of the;
*/* End Err_handle */
~
Errno
Errno-number of last Error
errno the last error code of the recording system.
The code is a value of type int. Defined in Errno.h
if (somecall () = =-1) {
printf ("Somecall () failed\n");
if (errno = = ...) { ... }
}
This example does not get the error code generated by the execution of the Somecall function. Because it is very likely that the function of printf is generated.
if (somecall () = =-1) {
int ERRSV = errno;
printf ("Somecall () failed\n");
if (ERRSV = = ...) { ... }
This talent really gets the error code that executes the Somecall function more.
}
Note: errno is only set when a library function fails. When the function executes successfully, the value of the errno is not changed. This means that we cannot infer the existence of an error by testing the value of the errno. Instead. It only makes sense to check the value of errno when the called function tip has an error.
Viewing error code errno is an important way to debug a program.
When an exception occurs for a Linux C API function, the errno variable (include errno.h) is usually assigned an integer value, different values represent different meanings, and the reason for the error can be guessed by looking at the value. In the actual programming with this trick to overcome a lot of the original seemingly inexplicable problems.
Some error definitions for 2errno
Edit
Below is the main self-2.6.32 kernel code in the/usr/include/asm-generic/errno.h and errno-base.h. The output error causes a definition of inductive finishing such as the following:
#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 Erfkill */Operation not possible due to Rf-kill * *
#define EHWPOISON 133/* Memory page has hardware error */
Fflush
Fflush (stdin) refreshes the standard input buffer and discards things in the input buffer [non-standard]
Fflush (STDOUT) refreshes the standard output buffer to print the output buffer to the standard output device.
_vsnprintf
Edit
One of the _vsnprintf,c language library functions, which belongs to a variable parameter. Used to print data to a string, the data format is defined by the user itself.
Header file:
#include <stdarg.h>
function declaration:
int_vsnprintf (CHAR*STR,SIZE_TSIZE,CONSTCHAR*FORMAT,VA_LISTAP);
Description of the parameters:
Char *str [out], storing the generated formatted string here.
size_t size [in], the maximum number of bytes that STR can accept to prevent an array from being out of bounds.
const char *format [in], which specifies the output format of the string. It determines the type, number, and order of the variable arguments you need to provide.
Va_list ap [in], va_list variable. Va:variable-argument: variable number of parameters
function function: Formats the variable parameters into an array of characters.
The use method is similar to vsprintf, except that the size limit is added. Prevents memory overflow (size is the amount of storage space referred to by STR).
Return value: Run successfully, returns the number of characters written to the character array str (not including The Terminator). The maximum size does not exceed. The run failed with a negative value returned. Collocated errno. [1]
int snprintf (char *str, size_t size, const char *format, ...);
A variable number of parameters (...) Format the string according to format and copy it to Str
(1) Assuming the formatted string length < size, all copies of this string are copied to Str. And then add a string terminator (' + ') to it;
(2) Assume that the formatted string length >= size. Only the (size-1) characters are copied to Str. And then add a string terminator (' + ') to the following. The return value is the length of the formatted string.
Char a[20];
i = snprintf (A, 9, "%012d", 12345);
printf ("I =%d, a =%s", I, a);
The output is: i = 00000001
3 Required Header Files
Edit
#include <stdio.h>
4 function return value
Edit
Returns the length of the string to be written if successful, or a negative value if an error occurs.
5 description
Edit
strcpy () sprintf () strcat () has security implications. The corresponding security version is:
strncpy () snprintf () Strncat ()
1
snprintf (S, +, "%.*s", 3, "ABCD");
The value of S is ABC
%.*s indicates that there are two items, the first item specifies the length, and the second item is the contents of%s. So take the top three bits
#if _msc_ver
#define SNPRINTF _snprintf
#endif
Universal ANSI UNICODE Common definition
1
_sntprintf
6 examples
#include <stdio.h>
#include <stdlib.h>
int main ()
{
Char str[10]={0};
snprintf (str, sizeof (STR), "0123456789012345678");
printf ("str=%s \ n", str); return 0;
}
Execution Result: str=012345678
Header file
<STDIO.H>[1]
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
20140603 pairs of error.c for analyzing source code