Import upstream 2.3.14
[packages/xinetd.git] / libs / src / xlog / util.c
1 /*
2  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
3  * All rights reserved.  The file named COPYRIGHT specifies the terms 
4  * and conditions for redistribution.
5  */
6
7
8 #include "config.h"
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <errno.h>
13
14 #include "str.h"
15 #include "libportable.h"
16
17 /*
18  * Search the given buffer for an occurrence of "%m"
19  */
20 int __xlog_add_errno( const char *buf, int len )
21 {
22         int i;
23
24         for ( i = 0 ; i < len-1 ; i++ )
25                 if ( (buf[i] == '%') && (buf[i+1] == 'm') )
26                         return( i ) ;
27         return( -1 ) ;
28 }
29
30 /* __xlog_explain_errno:
31  * Description: generates a string to explain the value of errno.
32  * On entry: buf must point to space already allocated by the caller,
33  * and size points to the amount of memory buf is allocated.
34  * On exit: buf contains the NULL terminated string, size now points
35  * to the length of the string, and the return value is buf.
36  */
37 char *__xlog_explain_errno( char *buf, unsigned *size )
38 {
39    strx_print((int *)size, buf, *size, "%s (errno = %d)", 
40       strerror(errno), errno);
41    return( buf ) ;
42 }
43
44
45 char *__xlog_new_string( const char *s )
46 {
47    if ( s )
48       return strdup( s ) ;
49    else
50       return 0;
51 }
52
53