| 1 | /* Adapted from src/utils-string.c from the libinput distribution */ |
| 2 | /* src/utils-string.c 1.23.0 |
| 3 | * Copyright © 2008 Kristian Høgsberg |
| 4 | * Copyright © 2013-2015 Red Hat, Inc. |
| 5 | * Modifications: Copyright © 2024 Bill Allombert <[email protected]> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the next |
| 15 | * paragraph) shall be included in all copies or substantial portions of the |
| 16 | * Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include "util-strings.h" |
| 28 | |
| 29 | /** |
| 30 | * Return the next word in a string pointed to by state before the first |
| 31 | * separator character. Call repeatedly to tokenize a whole string. |
| 32 | * |
| 33 | * @param state Current state |
| 34 | * @param len String length of the word returned |
| 35 | * @param separators List of separator characters |
| 36 | * |
| 37 | * @return The first word in *state, NOT null-terminated |
| 38 | */ |
| 39 | static const char * |
| 40 | next_word(const char **state, size_t *len, const char *separators) |
| 41 | { |
| 42 | const char *next = *state; |
| 43 | size_t l; |
| 44 | |
| 45 | if (!*next) |
| 46 | return NULL; |
| 47 | |
| 48 | next += strspn(next, separators); |
| 49 | if (!*next) { |
| 50 | *state = next; |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | l = strcspn(next, separators); |
| 55 | *state = next + l; |
| 56 | *len = l; |
| 57 | |
| 58 | return next; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Return a null-terminated string array with the contents of argv |
| 63 | * duplicated. |
| 64 | * |
| 65 | * Use strv_free() to free the array. |
| 66 | * |
| 67 | * @return A null-terminated string array or NULL on errors |
| 68 | */ |
| 69 | char** |
| 70 | strv_from_argv(int argc, char **argv) |
| 71 | { |
| 72 | char **strv = NULL; |
| 73 | |
| 74 | assert(argc >= 0); |
| 75 | |
| 76 | if (argc == 0) |
| 77 | return NULL; |
| 78 | |
| 79 | strv = zalloc((argc + 1) * sizeof *strv); |
| 80 | for (int i = 0; i < argc; i++) { |
| 81 | char *copy = safe_strdup(argv[i]); |
| 82 | if (!copy) { |
| 83 | strv_free(strv); |
| 84 | return NULL; |
| 85 | } |
| 86 | strv[i] = copy; |
| 87 | } |
| 88 | return strv; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return a null-terminated string array with the tokens in the input |
| 93 | * string, e.g. "one two\tthree" with a separator list of " \t" will return |
| 94 | * an array [ "one", "two", "three", NULL ] and num elements 3. |
| 95 | * |
| 96 | * Use strv_free() to free the array. |
| 97 | * |
| 98 | * Another example: |
| 99 | * result = strv_from_string("+1-2++3--4++-+5-+-", "+-", &nelem) |
| 100 | * result == [ "1", "2", "3", "4", "5", NULL ] and nelem == 5 |
| 101 | * |
| 102 | * @param in Input string |
| 103 | * @param separators List of separator characters |
| 104 | * @param num_elements Number of elements found in the input string |
| 105 | * |
| 106 | * @return A null-terminated string array or NULL on errors |
| 107 | */ |
| 108 | char ** |
| 109 | strv_from_string(const char *in, const char *separators, size_t *num_elements) |
| 110 | { |
| 111 | assert(in != NULL); |
| 112 | |
| 113 | const char *s = in; |
| 114 | size_t l, nelems = 0; |
| 115 | while (next_word(&s, &l, separators) != NULL) |
| 116 | nelems++; |
| 117 | |
| 118 | if (nelems == 0) { |
| 119 | *num_elements = 0; |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | size_t strv_len = nelems + 1; /* NULL-terminated */ |
| 124 | char **strv = zalloc(strv_len * sizeof *strv); |
| 125 | |
| 126 | size_t idx = 0; |
| 127 | const char *word; |
| 128 | s = in; |
| 129 | while ((word = next_word(&s, &l, separators)) != NULL) { |
| 130 | char *copy = strndup(word, l); |
| 131 | if (!copy) { |
| 132 | strv_free(strv); |
| 133 | *num_elements = 0; |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | strv[idx++] = copy; |
| 138 | } |
| 139 | |
| 140 | *num_elements = nelems; |
| 141 | |
| 142 | return strv; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Return a newly allocated string with all elements joined by the |
| 147 | * joiner, same as Python's string.join() basically. |
| 148 | * A strv of ["one", "two", "three", NULL] with a joiner of ", " results |
| 149 | * in "one, two, three". |
| 150 | * |
| 151 | * An empty strv ([NULL]) returns NULL, same for passing NULL as either |
| 152 | * argument. |
| 153 | * |
| 154 | * @param strv Input string array |
| 155 | * @param joiner Joiner between the elements in the final string |
| 156 | * |
| 157 | * @return A null-terminated string joining all elements |
| 158 | */ |
| 159 | char * |
| 160 | strv_join(char **strv, const char *joiner) |
| 161 | { |
| 162 | char **s; |
| 163 | char *str; |
| 164 | size_t slen = 0; |
| 165 | size_t count = 0; |
| 166 | |
| 167 | if (!strv || !joiner) |
| 168 | return NULL; |
| 169 | |
| 170 | if (strv[0] == NULL) |
| 171 | return NULL; |
| 172 | |
| 173 | for (s = strv, count = 0; *s; s++, count++) { |
| 174 | slen += strlen(*s); |
| 175 | } |
| 176 | |
| 177 | assert(slen < 1000); |
| 178 | assert(strlen(joiner) < 1000); |
| 179 | assert(count > 0); |
| 180 | assert(count < 100); |
| 181 | |
| 182 | slen += (count - 1) * strlen(joiner); |
| 183 | |
| 184 | str = zalloc(slen + 1); /* trailing \0 */ |
| 185 | for (s = strv; *s; s++) { |
| 186 | strcat(str, *s); |
| 187 | --count; |
| 188 | if (count > 0) |
| 189 | strcat(str, joiner); |
| 190 | } |
| 191 | |
| 192 | return str; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Return a pointer to the basename within filename. |
| 197 | * If the filename the empty string or a directory (i.e. the last char of |
| 198 | * filename is '/') NULL is returned. |
| 199 | */ |
| 200 | const char * |
| 201 | safe_basename(const char *filename) |
| 202 | { |
| 203 | const char *basename; |
| 204 | |
| 205 | if (*filename == '\0') |
| 206 | return NULL; |
| 207 | |
| 208 | basename = strrchr(filename, '/'); |
| 209 | if (basename == NULL) |
| 210 | return filename; |
| 211 | |
| 212 | if (*(basename + 1) == '\0') |
| 213 | return NULL; |
| 214 | |
| 215 | return basename + 1; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Similar to basename() but returns the trunk only without the (last) |
| 220 | * trailing suffix, so that: |
| 221 | * |
| 222 | * - foo.c returns foo |
| 223 | * - foo.a.b returns foo.a |
| 224 | * - foo returns foo |
| 225 | * - foo/ returns "" |
| 226 | * |
| 227 | * @return an allocated string representing the trunk name of the file |
| 228 | */ |
| 229 | char * |
| 230 | trunkname(const char *filename) |
| 231 | { |
| 232 | const char *base = safe_basename(filename); |
| 233 | char *suffix; |
| 234 | |
| 235 | if (base == NULL) |
| 236 | return safe_strdup(""); |
| 237 | |
| 238 | suffix = rindex(base, '.'); |
| 239 | if (suffix == NULL) |
| 240 | return safe_strdup(base); |
| 241 | else |
| 242 | return strndup(base, suffix-base); |
| 243 | } |