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