| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: mem1.c,v 1.60 2022/02/27 08:31:26 rillig Exp $ */ | | 1 | /* $NetBSD: mem1.c,v 1.61 2022/02/27 17:12:06 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1994, 1995 Jochen Pohl | | 4 | * Copyright (c) 1994, 1995 Jochen Pohl |
5 | * All Rights Reserved. | | 5 | * All Rights Reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -27,47 +27,48 @@ | | | @@ -27,47 +27,48 @@ |
27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | */ | | 32 | */ |
33 | | | 33 | |
34 | #if HAVE_NBTOOL_CONFIG_H | | 34 | #if HAVE_NBTOOL_CONFIG_H |
35 | #include "nbtool_config.h" | | 35 | #include "nbtool_config.h" |
36 | #endif | | 36 | #endif |
37 | | | 37 | |
38 | #include <sys/cdefs.h> | | 38 | #include <sys/cdefs.h> |
39 | #if defined(__RCSID) && !defined(lint) | | 39 | #if defined(__RCSID) && !defined(lint) |
40 | __RCSID("$NetBSD: mem1.c,v 1.60 2022/02/27 08:31:26 rillig Exp $"); | | 40 | __RCSID("$NetBSD: mem1.c,v 1.61 2022/02/27 17:12:06 rillig Exp $"); |
41 | #endif | | 41 | #endif |
42 | | | 42 | |
43 | #include <sys/param.h> | | 43 | #include <sys/param.h> |
44 | #include <stdlib.h> | | 44 | #include <stdlib.h> |
45 | #include <string.h> | | 45 | #include <string.h> |
46 | | | 46 | |
47 | #include "lint1.h" | | 47 | #include "lint1.h" |
48 | | | 48 | |
49 | /* | | 49 | /* |
50 | * Filenames allocated by record_filename are shared and have unlimited | | 50 | * Filenames allocated by record_filename are shared and have unlimited |
51 | * lifetime. | | 51 | * lifetime. |
52 | */ | | 52 | */ |
53 | struct filename { | | 53 | struct filename { |
54 | const char *fn_name; | | 54 | const char *fn_name; |
55 | size_t fn_len; | | 55 | size_t fn_len; |
56 | int fn_id; | | 56 | int fn_id; |
57 | struct filename *fn_next; | | 57 | struct filename *fn_next; |
58 | }; | | 58 | }; |
59 | | | 59 | |
60 | static struct filename *filenames; /* null-terminated array */ | | 60 | static struct filename *filenames; /* null-terminated array */ |
| | | 61 | static int next_filename_id; |
61 | | | 62 | |
62 | /* Find the given filename, or return NULL. */ | | 63 | /* Find the given filename, or return NULL. */ |
63 | static const struct filename * | | 64 | static const struct filename * |
64 | search_filename(const char *s, size_t len) | | 65 | search_filename(const char *s, size_t len) |
65 | { | | 66 | { |
66 | const struct filename *fn; | | 67 | const struct filename *fn; |
67 | | | 68 | |
68 | for (fn = filenames; fn != NULL; fn = fn->fn_next) { | | 69 | for (fn = filenames; fn != NULL; fn = fn->fn_next) { |
69 | if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0) | | 70 | if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0) |
70 | break; | | 71 | break; |
71 | } | | 72 | } |
72 | return fn; | | 73 | return fn; |
73 | } | | 74 | } |
| @@ -104,60 +105,48 @@ transform_filename(const char *name, siz | | | @@ -104,60 +105,48 @@ transform_filename(const char *name, siz |
104 | static char buf[MAXPATHLEN]; | | 105 | static char buf[MAXPATHLEN]; |
105 | const struct filename_replacement *r; | | 106 | const struct filename_replacement *r; |
106 | | | 107 | |
107 | for (r = filename_replacements; r != NULL; r = r->next) | | 108 | for (r = filename_replacements; r != NULL; r = r->next) |
108 | if (r->orig_len < len && | | 109 | if (r->orig_len < len && |
109 | memcmp(name, r->orig, r->orig_len) == 0) | | 110 | memcmp(name, r->orig, r->orig_len) == 0) |
110 | break; | | 111 | break; |
111 | if (r == NULL) | | 112 | if (r == NULL) |
112 | return name; | | 113 | return name; |
113 | (void)snprintf(buf, sizeof(buf), "%s%s", r->repl, name + r->orig_len); | | 114 | (void)snprintf(buf, sizeof(buf), "%s%s", r->repl, name + r->orig_len); |
114 | return buf; | | 115 | return buf; |
115 | } | | 116 | } |
116 | | | 117 | |
117 | static int | | | |
118 | next_filename_id(void) | | | |
119 | { | | | |
120 | static int next_id = 0; | | | |
121 | | | | |
122 | return next_id++; | | | |
123 | } | | | |
124 | | | | |
125 | /* | | 118 | /* |
126 | * Return a copy of the filename s with unlimited lifetime. | | 119 | * Return a copy of the filename s with unlimited lifetime. |
127 | * If the filename is new, write it to the output file. | | 120 | * If the filename is new, write it to the output file. |
128 | */ | | 121 | */ |
129 | const char * | | 122 | const char * |
130 | record_filename(const char *s, size_t slen) | | 123 | record_filename(const char *s, size_t slen) |
131 | { | | 124 | { |
132 | const struct filename *existing_fn; | | 125 | const struct filename *existing_fn; |
133 | struct filename *fn; | | 126 | struct filename *fn; |
134 | char *name; | | 127 | char *name; |
135 | | | 128 | |
136 | if (s == NULL) | | | |
137 | return NULL; | | | |
138 | | | | |
139 | if ((existing_fn = search_filename(s, slen)) != NULL) | | 129 | if ((existing_fn = search_filename(s, slen)) != NULL) |
140 | return existing_fn->fn_name; | | 130 | return existing_fn->fn_name; |
141 | | | 131 | |
142 | /* Do not use strdup() because s is not NUL-terminated.*/ | | | |
143 | name = xmalloc(slen + 1); | | 132 | name = xmalloc(slen + 1); |
144 | (void)memcpy(name, s, slen); | | 133 | (void)memcpy(name, s, slen); |
145 | name[slen] = '\0'; | | 134 | name[slen] = '\0'; |
146 | | | 135 | |
147 | fn = xmalloc(sizeof(*fn)); | | 136 | fn = xmalloc(sizeof(*fn)); |
148 | fn->fn_name = name; | | 137 | fn->fn_name = name; |
149 | fn->fn_len = slen; | | 138 | fn->fn_len = slen; |
150 | fn->fn_id = next_filename_id(); | | 139 | fn->fn_id = next_filename_id++; |
151 | fn->fn_next = filenames; | | 140 | fn->fn_next = filenames; |
152 | filenames = fn; | | 141 | filenames = fn; |
153 | | | 142 | |
154 | /* Write the ID of this filename to the output file. */ | | 143 | /* Write the ID of this filename to the output file. */ |
155 | outclr(); | | 144 | outclr(); |
156 | outint(fn->fn_id); | | 145 | outint(fn->fn_id); |
157 | outchar('s'); | | 146 | outchar('s'); |
158 | outstrg(transform_filename(fn->fn_name, fn->fn_len)); | | 147 | outstrg(transform_filename(fn->fn_name, fn->fn_len)); |
159 | | | 148 | |
160 | return fn->fn_name; | | 149 | return fn->fn_name; |
161 | } | | 150 | } |
162 | | | 151 | |
163 | /* Get the ID of a filename. */ | | 152 | /* Get the ID of a filename. */ |