expandm.c revision 1.7 1 /* $NetBSD: expandm.c,v 1.7 2019/01/13 06:10:34 kre 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.7 2019/01/13 06:10:34 kre 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 int err = errno;
52 const char *e = NULL;
53 char *buf, *m, *nbuf;
54 const char *ptr;
55
56 for (ptr = fmt, buf = NULL; (m = strstr(ptr, "%m")) != NULL;
57 ptr = m + 2)
58 {
59 size_t cnt = 0;
60
61 for (char *p = m; p >= ptr && *p == '%'; p--)
62 cnt++;
63
64 if (__predict_false((m - ptr) >= INT_MAX)) {
65 size_t blen = buf ? strlen(buf) : 0;
66 size_t nlen = (size_t)(m - ptr);
67
68 nbuf = realloc(buf, blen + nlen + 1);
69 if (nbuf == NULL)
70 goto out;
71
72 memcpy(nbuf + blen, ptr, nlen);
73 nbuf[blen + nlen] = '\0';
74 ptr += nlen;
75 buf = nbuf;
76 }
77
78 if (__predict_true(e == NULL && (cnt & 1) != 0))
79 e = strerror(err);
80 if (asprintf(&nbuf, "%s%.*s%s", buf ? buf : "",
81 (int)(m - ptr), ptr, (cnt & 1) ? e : "%m") == -1)
82 goto out;
83 free(buf);
84 buf = nbuf;
85 }
86
87 if (asprintf(&nbuf, "%s%s%s", buf ? buf : "", ptr, sf ? sf : "") == -1)
88 goto out;
89
90 free(buf);
91 if (rbuf)
92 *rbuf = nbuf;
93 errno = err;
94 return nbuf;
95 out:
96 free(buf);
97 if (rbuf)
98 *rbuf = NULL;
99 errno = err;
100 return fmt;
101 }
102
103 #ifdef TEST
104 int
105 main(int argc, char *argv[])
106 {
107 errno = ERANGE;
108 printf("%s\n", expandm(argc > 1 ? argv[1] : "Message %%m=%m: %%%m%%",
109 "...", NULL));
110 return 0;
111 }
112 #endif
113