xref: /freebsd/contrib/tcp_wrappers/percent_m.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1  /*
2   * Replace %m by system error message.
3   *
4   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
5   */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#) percent_m.c 1.1 94/12/28 17:42:37";
9 #endif
10 
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 
15 extern int errno;
16 #ifndef SYS_ERRLIST_DEFINED
17 extern char *sys_errlist[];
18 extern int sys_nerr;
19 #endif
20 
21 #include "mystdarg.h"
22 
23 char   *percent_m(obuf, ibuf)
24 char   *obuf;
25 char   *ibuf;
26 {
27     char   *bp = obuf;
28     char   *cp = ibuf;
29 
30     while (*bp = *cp)
31 	if (*cp == '%' && cp[1] == 'm') {
32 	    if (errno < sys_nerr && errno > 0) {
33 		strcpy(bp, sys_errlist[errno]);
34 	    } else {
35 		sprintf(bp, "Unknown error %d", errno);
36 	    }
37 	    bp += strlen(bp);
38 	    cp += 2;
39 	} else {
40 	    bp++, cp++;
41 	}
42     return (obuf);
43 }
44