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