summaryrefslogtreecommitdiff
path: root/lib/libwrap/percent_m.c
blob: c2aa835e2164780d9520158ed6e7ebeab07a77de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*	$OpenBSD: percent_m.c,v 1.4 2009/10/27 23:59:30 deraadt Exp $	*/

 /*
  * Replace %m by system error message.
  * 
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */

#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>

#ifndef SYS_ERRLIST_DEFINED
extern char *sys_errlist[];
extern int sys_nerr;
#endif

char   *percent_m(obuf, ibuf)
char   *obuf;
char   *ibuf;
{
    char   *bp = obuf;
    char   *cp = ibuf;
    int len = BUFSIZ;

    while ((*bp = *cp)) {
	if (*cp == '%' && cp[1] == 'm') {
	    len = BUFSIZ - (bp - obuf);
	    if (errno < sys_nerr && errno > 0) {
		strlcpy(bp, sys_errlist[errno], len);
	    } else {
		snprintf(bp, len, "Unknown error %d", errno);
	    }
	    bp += strlen(bp);
	    cp += 2;
	} else {
	    bp++, cp++;
	}
    }
    return (obuf);
}