No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / percentm.c
blob34d4f32af4809ba808db8b85f46f8f5ffb43cc44
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* percentm 3
6 /* SUMMARY
7 /* expand %m embedded in string to system error text
8 /* SYNOPSIS
9 /* #include <percentm.h>
11 /* char *percentm(const char *src, int err)
12 /* DESCRIPTION
13 /* The percentm() routine makes a copy of the null-terminated string
14 /* given via the \fIsrc\fR argument, with %m sequences replaced by
15 /* the system error text corresponding to the \fIerr\fR argument.
16 /* The result is overwritten upon each successive call.
18 /* Arguments:
19 /* .IP src
20 /* A null-terminated input string with zero or more %m sequences.
21 /* .IP err
22 /* A legal \fIerrno\fR value. The text corresponding to this error
23 /* value is used when expanding %m sequences.
24 /* SEE ALSO
25 /* syslog(3) system logger library
26 /* HISTORY
27 /* .ad
28 /* .fi
29 /* A percentm() routine appears in the TCP Wrapper software
30 /* by Wietse Venema.
31 /* LICENSE
32 /* .ad
33 /* .fi
34 /* The Secure Mailer license must be distributed with this software.
35 /* AUTHOR(S)
36 /* Wietse Venema
37 /* IBM T.J. Watson Research
38 /* P.O. Box 704
39 /* Yorktown Heights, NY 10598, USA
40 /*--*/
42 /* System libraries. */
44 #include <sys_defs.h>
45 #include <string.h>
47 /* Utility library. */
49 #include "vstring.h"
50 #include "percentm.h"
52 /* percentm - replace %m by error message corresponding to value in err */
54 char *percentm(const char *str, int err)
56 static VSTRING *vp;
57 const unsigned char *ip = (const unsigned char *) str;
59 if (vp == 0)
60 vp = vstring_alloc(100); /* grows on demand */
61 VSTRING_RESET(vp);
63 while (*ip) {
64 switch (*ip) {
65 default:
66 VSTRING_ADDCH(vp, *ip++);
67 break;
68 case '%':
69 switch (ip[1]) {
70 default: /* leave %<any> alone */
71 VSTRING_ADDCH(vp, *ip++);
72 /* FALLTHROUGH */
73 case '\0': /* don't fall off end */
74 VSTRING_ADDCH(vp, *ip++);
75 break;
76 case 'm': /* replace %m */
77 vstring_strcat(vp, strerror(err));
78 ip += 2;
79 break;
83 VSTRING_TERMINATE(vp);
84 return (vstring_str(vp));