expandm.c revision 1.6 1 /* $NetBSD: expandm.c,v 1.6 2019/01/13 01:32:51 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: expandm.c,v 1.6 2019/01/13 01:32:51 christos Exp $");
33
34 #include <limits.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <errno.h>
39
40 #include "expandm.h"
41
42 #ifdef TEST
43 #undef INT_MAX
44 #define INT_MAX 31
45 #endif
46
47
48 const char * __attribute__((__format_arg__(1)))
49 expandm(const char *fmt, const char *sf, char **rbuf)
50 {
51 const char *e = strerror(errno);
52 char *buf, *m, *nbuf;
53 const char *ptr;
54
55 for (ptr = fmt, buf = NULL; (m = strstr(ptr, "%m")) != NULL;
56 ptr = m + 2)
57 {
58 size_t cnt = 0;
59
60 for (char *p = m; p >= ptr && *p == '%'; p--)
61 cnt++;
62
63 if (__predict_false((m - ptr) >= INT_MAX)) {
64 size_t blen = buf ? strlen(buf) : 0;
65 size_t nlen = (size_t)(m - ptr);
66
67 nbuf = realloc(buf, blen + nlen + 1);
68 if (nbuf == NULL)
69 goto out;
70
71 memcpy(nbuf + blen, ptr, nlen);
72 nbuf[blen + nlen] = '\0';
73 ptr += nlen;
74 buf = nbuf;
75 }
76
77 if (asprintf(&nbuf, "%s%.*s%s", buf ? buf : "",
78 (int)(m - ptr), ptr, (cnt & 1) ? e : "%m") == -1)
79 goto out;
80 free(buf);
81 buf = nbuf;
82 }
83
84 if (asprintf(&nbuf, "%s%s%s", buf ? buf : "", ptr, sf ? sf : "") == -1)
85 goto out;
86
87 free(buf);
88 if (rbuf)
89 *rbuf = nbuf;
90 return nbuf;
91 out:
92 free(buf);
93 if (rbuf)
94 *rbuf = NULL;
95 return fmt;
96 }
97
98 #ifdef TEST
99 int
100 main(int argc, char *argv[])
101 {
102 errno = ERANGE;
103 printf("%s\n", expandm(argc > 1 ? argv[1] : "Message %%m=%m: %%%m%%",
104 "...", NULL));
105 return 0;
106 }
107 #endif
108