• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

neomutt / neomutt / 20704611763

04 Jan 2026 11:45AM UTC coverage: 45.271% (+1.1%) from 44.194%
20704611763

push

github

flatcap
notmuch: restore virtual-mailboxes

`virtual-mailboxes` has been dropped from the docs.

The preferred commands are:

- `mailboxes -label LABEL MAILBOX-PATH`
- `named-mailboxes LABEL MAILBOX-PATH`

11367 of 25109 relevant lines covered (45.27%)

291.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.68
/mutt/string.c
1
/**
2
 * @file
3
 * String manipulation functions
4
 *
5
 * @authors
6
 * Copyright (C) 2017-2025 Richard Russon <rich@flatcap.org>
7
 * Copyright (C) 2018-2020 Pietro Cerutti <gahr@gahr.ch>
8
 * Copyright (C) 2021 Austin Ray <austin@austinray.io>
9
 * Copyright (C) 2022 Claes Nästén <pekdon@gmail.com>
10
 * Copyright (C) 2025 Dennis Schön <mail@dennis-schoen.de>
11
 * Copyright (C) 2025 Alejandro Colomar <alx@kernel.org>
12
 * Copyright (C) 2025 Thomas Klausner <wiz@gatalith.at>
13
 *
14
 * @copyright
15
 * This program is free software: you can redistribute it and/or modify it under
16
 * the terms of the GNU General Public License as published by the Free Software
17
 * Foundation, either version 2 of the License, or (at your option) any later
18
 * version.
19
 *
20
 * This program is distributed in the hope that it will be useful, but WITHOUT
21
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
23
 * details.
24
 *
25
 * You should have received a copy of the GNU General Public License along with
26
 * this program.  If not, see <http://www.gnu.org/licenses/>.
27
 */
28

29
/**
30
 * @page mutt_string String manipulation functions
31
 *
32
 * Lots of commonly-used string manipulation routines.
33
 */
34

35
#include "config.h"
36
#include <errno.h>
37
#include <stdarg.h> // IWYU pragma: keep
38
#include <stdbool.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <strings.h>
43
#include "array.h"
44
#include "ctype2.h"
45
#include "exit.h"
46
#include "logging2.h"
47
#include "memory.h"
48
#include "string2.h"
49
#ifdef HAVE_SYSEXITS_H
50
#include <sysexits.h>
51
#endif
52

53
#ifndef HAVE_STRCASESTR
54
/**
55
 * strcasestr - Find the first occurrence of needle in haystack, ignoring case
56
 * @param haystack String to search
57
 * @param needle   String to find
58
 * @retval ptr Matched string, or NULL on failure
59
 */
60
static char *strcasestr(const char *haystack, const char *needle)
61
{
62
  size_t haystackn = strlen(haystack);
63
  size_t needlen = strlen(needle);
64

65
  const char *p = haystack;
66
  while (haystackn >= needlen)
67
  {
68
    if (strncasecmp(p, needle, needlen) == 0)
69
      return (char *) p;
70
    p++;
71
    haystackn--;
72
  }
73
  return NULL;
74
}
75
#endif /* HAVE_STRCASESTR */
76

77
#ifndef HAVE_STRSEP
78
/**
79
 * strsep - Extract a token from a string
80
 * @param stringp String to be split up
81
 * @param delim   Characters to split stringp at
82
 * @retval ptr Next token, or NULL if the no more tokens
83
 *
84
 * @note The pointer stringp will be moved and NULs inserted into it
85
 */
86
static char *strsep(char **stringp, const char *delim)
87
{
88
  if (!*stringp)
89
    return NULL;
90

91
  char *start = *stringp;
92
  for (char *p = *stringp; *p != '\0'; p++)
93
  {
94
    for (const char *s = delim; *s != '\0'; s++)
95
    {
96
      if (*p == *s)
97
      {
98
        *p = '\0';
99
        *stringp = p + 1;
100
        return start;
101
      }
102
    }
103
  }
104
  *stringp = NULL;
105
  return start;
106
}
107
#endif /* HAVE_STRSEP */
108

109
/**
110
 * struct SysExits - Lookup table of error messages
111
 */
112
struct SysExits
113
{
114
  int err_num;         ///< Error number, see errno(3)
115
  const char *err_str; ///< Human-readable string for error
116
};
117

118
/// Lookup table of error messages
119
static const struct SysExits SysExits[] = {
120
#ifdef EX_USAGE
121
  { 0xff & EX_USAGE, "Bad usage." },
122
#endif
123
#ifdef EX_DATAERR
124
  { 0xff & EX_DATAERR, "Data format error." },
125
#endif
126
#ifdef EX_NOINPUT
127
  { 0xff & EX_NOINPUT, "Can't open input." },
128
#endif
129
#ifdef EX_NOUSER
130
  { 0xff & EX_NOUSER, "User unknown." },
131
#endif
132
#ifdef EX_NOHOST
133
  { 0xff & EX_NOHOST, "Host unknown." },
134
#endif
135
#ifdef EX_UNAVAILABLE
136
  { 0xff & EX_UNAVAILABLE, "Service unavailable." },
137
#endif
138
#ifdef EX_SOFTWARE
139
  { 0xff & EX_SOFTWARE, "Internal error." },
140
#endif
141
#ifdef EX_OSERR
142
  { 0xff & EX_OSERR, "Operating system error." },
143
#endif
144
#ifdef EX_OSFILE
145
  { 0xff & EX_OSFILE, "System file missing." },
146
#endif
147
#ifdef EX_CANTCREAT
148
  { 0xff & EX_CANTCREAT, "Can't create output." },
149
#endif
150
#ifdef EX_IOERR
151
  { 0xff & EX_IOERR, "I/O error." },
152
#endif
153
#ifdef EX_TEMPFAIL
154
  { 0xff & EX_TEMPFAIL, "Deferred." },
155
#endif
156
#ifdef EX_PROTOCOL
157
  { 0xff & EX_PROTOCOL, "Remote protocol error." },
158
#endif
159
#ifdef EX_NOPERM
160
  { 0xff & EX_NOPERM, "Insufficient permission." },
161
#endif
162
#ifdef EX_CONFIG
163
  { 0xff & EX_NOPERM, "Local configuration error." },
164
#endif
165
  { S_ERR, "Exec error." },
166
};
167

168
/**
169
 * mutt_str_sysexit - Return a string matching an error code
170
 * @param err_num Error code, e.g. EX_NOPERM
171
 * @retval ptr string representing the error code
172
 */
173
const char *mutt_str_sysexit(int err_num)
7✔
174
{
175
  for (size_t i = 0; i < countof(SysExits); i++)
74✔
176
  {
177
    if (err_num == SysExits[i].err_num)
72✔
178
      return SysExits[i].err_str;
5✔
179
  }
180

181
  return NULL;
182
}
183

184
/**
185
 * mutt_str_sep - Find first occurrence of any of delim characters in *stringp
186
 * @param stringp Pointer to string to search for delim, updated with position of after delim if found else NULL
187
 * @param delim   String with characters to search for in *stringp
188
 * @retval ptr Input value of *stringp
189
 */
190
char *mutt_str_sep(char **stringp, const char *delim)
18✔
191
{
192
  if (!stringp || !*stringp || !delim)
18✔
193
    return NULL;
194
  return strsep(stringp, delim);
10✔
195
}
196

197
/**
198
 * startswith - Check whether a string starts with a prefix
199
 * @param str String to check
200
 * @param prefix Prefix to match
201
 * @param match_case True if case needs to match
202
 * @retval num Length of prefix if str starts with prefix
203
 * @retval 0   str does not start with prefix
204
 */
205
static size_t startswith(const char *str, const char *prefix, bool match_case)
15,227✔
206
{
207
  if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
15,227✔
208
  {
209
    return 0;
210
  }
211

212
  const char *saved_prefix = prefix;
213
  for (; *str && *prefix; str++, prefix++)
67,282✔
214
  {
215
    if (*str == *prefix)
60,096✔
216
      continue;
50,766✔
217

218
    if (!match_case && mutt_tolower(*str) == mutt_tolower(*prefix))
9,330✔
219
      continue;
1,310✔
220

221
    return 0;
222
  }
223

224
  return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
7,186✔
225
}
226

227
/**
228
 * mutt_str_startswith - Check whether a string starts with a prefix
229
 * @param str String to check
230
 * @param prefix Prefix to match
231
 * @retval num Length of prefix if str starts with prefix
232
 * @retval 0   str does not start with prefix
233
 */
234
size_t mutt_str_startswith(const char *str, const char *prefix)
13,245✔
235
{
236
  return startswith(str, prefix, true);
13,245✔
237
}
238

239
/**
240
 * mutt_istr_startswith - Check whether a string starts with a prefix, ignoring case
241
 * @param str String to check
242
 * @param prefix Prefix to match
243
 * @retval num Length of prefix if str starts with prefix
244
 * @retval 0   str does not start with prefix
245
 */
246
size_t mutt_istr_startswith(const char *str, const char *prefix)
1,982✔
247
{
248
  return startswith(str, prefix, false);
1,982✔
249
}
250

251
/**
252
 * mutt_str_dup - Copy a string, safely
253
 * @param str String to copy
254
 * @retval ptr  Copy of the string
255
 * @retval NULL str was NULL or empty
256
 */
257
char *mutt_str_dup(const char *str)
11,475✔
258
{
259
  if (!str || (*str == '\0'))
11,475✔
260
    return NULL;
261

262
  char *p = strdup(str);
10,573✔
263
  if (!p)
10,573✔
264
  {
265
    mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
266
    mutt_exit(1);                      // LCOV_EXCL_LINE
267
  }
268
  return p;
269
}
270

271
/**
272
 * mutt_str_replace - Replace one string with another
273
 * @param[out] p String to replace
274
 * @param[in]  s New string
275
 * @retval ptr Replaced string
276
 *
277
 * This function free()s the original string, strdup()s the new string and
278
 * overwrites the pointer to the first string.
279
 *
280
 * This function alters the pointer of the caller.
281
 *
282
 * @note Free *p afterwards to handle the case that *p and s reference the same memory
283
 */
284
char *mutt_str_replace(char **p, const char *s)
56✔
285
{
286
  if (!p)
56✔
287
    return NULL;
288
  const char *tmp = *p;
54✔
289
  *p = mutt_str_dup(s);
54✔
290
  FREE(&tmp);
54✔
291
  return *p;
54✔
292
}
293

294
/**
295
 * mutt_str_adjust - Shrink-to-fit a string
296
 * @param[out] ptr String to alter
297
 *
298
 * Take a string which is allocated on the heap, find its length and reallocate
299
 * the memory to be exactly the right size.
300
 *
301
 * This function alters the pointer of the caller.
302
 */
303
void mutt_str_adjust(char **ptr)
78✔
304
{
305
  if (!ptr || !*ptr)
78✔
306
    return;
307
  MUTT_MEM_REALLOC(ptr, strlen(*ptr) + 1, char);
76✔
308
}
309

310
/**
311
 * mutt_str_lower - Convert all characters in the string to lowercase
312
 * @param str String to lowercase
313
 * @retval ptr Lowercase string
314
 *
315
 * The string is transformed in place.
316
 */
317
char *mutt_str_lower(char *str)
37✔
318
{
319
  if (!str)
37✔
320
    return NULL;
321

322
  char *p = str;
323

324
  while (*p)
271✔
325
  {
326
    *p = mutt_tolower(*p);
235✔
327
    p++;
235✔
328
  }
329

330
  return str;
331
}
332

333
/**
334
 * mutt_str_upper - Convert all characters in the string to uppercase
335
 * @param str String to uppercase
336
 * @retval ptr Uppercase string
337
 *
338
 * The string is transformed in place.
339
 */
340
char *mutt_str_upper(char *str)
4✔
341
{
342
  if (!str)
4✔
343
    return NULL;
344

345
  char *p = str;
346

347
  while (*p)
13✔
348
  {
349
    *p = mutt_toupper(*p);
10✔
350
    p++;
10✔
351
  }
352

353
  return str;
354
}
355

356
/**
357
 * mutt_strn_copy - Copy a sub-string into a buffer
358
 * @param dest   Buffer for the result
359
 * @param src    Start of the string to copy
360
 * @param len    Length of the string to copy
361
 * @param dsize  Destination buffer size
362
 * @retval ptr Destination buffer
363
 */
364
char *mutt_strn_copy(char *dest, const char *src, size_t len, size_t dsize)
7✔
365
{
366
  if (!src || !dest || (len == 0) || (dsize == 0))
7✔
367
    return dest;
368

369
  if (len > (dsize - 1))
2✔
370
    len = dsize - 1;
371
  memcpy(dest, src, len);
372
  dest[len] = '\0';
2✔
373
  return dest;
2✔
374
}
375

376
/**
377
 * mutt_strn_dup - Duplicate a sub-string
378
 * @param begin Start of the string to copy
379
 * @param len   Length of string to copy
380
 * @retval ptr New string
381
 *
382
 * The caller must free the returned string.
383
 */
384
char *mutt_strn_dup(const char *begin, size_t len)
246✔
385
{
386
  if (!begin)
246✔
387
    return NULL;
388

389
  char *p = MUTT_MEM_MALLOC(len + 1, char);
244✔
390
  memcpy(p, begin, len);
391
  p[len] = '\0';
244✔
392
  return p;
244✔
393
}
394

395
/**
396
 * mutt_str_cmp - Compare two strings, safely
397
 * @param a First string to compare
398
 * @param b Second string to compare
399
 * @retval -1 a precedes b
400
 * @retval  0 a and b are identical
401
 * @retval  1 b precedes a
402
 */
403
int mutt_str_cmp(const char *a, const char *b)
19,632✔
404
{
405
  return strcmp(NONULL(a), NONULL(b));
23,695✔
406
}
407

408
/**
409
 * mutt_istr_cmp - Compare two strings ignoring case, safely
410
 * @param a First string to compare
411
 * @param b Second string to compare
412
 * @retval -1 a precedes b
413
 * @retval  0 a and b are identical
414
 * @retval  1 b precedes a
415
 */
416
int mutt_istr_cmp(const char *a, const char *b)
6,189✔
417
{
418
  return strcasecmp(NONULL(a), NONULL(b));
6,201✔
419
}
420

421
/**
422
 * mutt_strn_equal - Check for equality of two strings (to a maximum), safely
423
 * @param a   First string to compare
424
 * @param b   Second string to compare
425
 * @param num Maximum number of bytes to compare
426
 * @retval true First num chars of both strings are equal
427
 * @retval false First num chars of both strings not equal
428
 */
429
bool mutt_strn_equal(const char *a, const char *b, size_t num)
4,955✔
430
{
431
  return strncmp(NONULL(a), NONULL(b), num) == 0;
4,957✔
432
}
433

434
/**
435
 * mutt_istrn_cmp - Compare two strings ignoring case (to a maximum), safely
436
 * @param a   First string to compare
437
 * @param b   Second string to compare
438
 * @param num Maximum number of bytes to compare
439
 * @retval -1 a precedes b
440
 * @retval  0 a and b are identical
441
 * @retval  1 b precedes a
442
 */
443
int mutt_istrn_cmp(const char *a, const char *b, size_t num)
16✔
444
{
445
  return strncasecmp(NONULL(a), NONULL(b), num);
18✔
446
}
447

448
/**
449
 * mutt_istrn_equal - Check for equality of two strings ignoring case (to a maximum), safely
450
 * @param a   First string to compare
451
 * @param b   Second string to compare
452
 * @param num Maximum number of bytes to compare
453
 * @retval -1 a precedes b
454
 * @retval true First num chars of both strings are equal, ignoring case
455
 * @retval false First num chars of both strings not equal, ignoring case
456
 */
457
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
2,949✔
458
{
459
  return strncasecmp(NONULL(a), NONULL(b), num) == 0;
2,951✔
460
}
461

462
/**
463
 * mutt_istrn_rfind - Find last instance of a substring, ignoring case
464
 * @param haystack        String to search through
465
 * @param haystack_length Length of the string
466
 * @param needle          String to find
467
 * @retval NULL String not found
468
 * @retval ptr  Location of string
469
 *
470
 * Return the last instance of needle in the haystack, or NULL.
471
 * Like strcasestr(), only backwards, and for a limited haystack length.
472
 */
473
const char *mutt_istrn_rfind(const char *haystack, size_t haystack_length, const char *needle)
42✔
474
{
475
  if (!haystack || (haystack_length == 0) || !needle)
42✔
476
    return NULL;
477

478
  int needle_length = strlen(needle);
35✔
479
  const char *haystack_end = haystack + haystack_length - needle_length;
35✔
480

481
  for (const char *p = haystack_end; p >= haystack; p--)
237✔
482
  {
483
    for (size_t i = 0; i < needle_length; i++)
539✔
484
    {
485
      if ((mutt_tolower(p[i]) != mutt_tolower(needle[i])))
517✔
486
        goto next;
202✔
487
    }
488
    return p;
489

490
  next:;
491
  }
492
  return NULL;
493
}
494

495
/**
496
 * mutt_str_len - Calculate the length of a string, safely
497
 * @param a String to measure
498
 * @retval num Length in bytes
499
 */
500
size_t mutt_str_len(const char *a)
13,273✔
501
{
502
  return a ? strlen(a) : 0;
13,273✔
503
}
504

505
/**
506
 * mutt_str_coll - Collate two strings (compare using locale), safely
507
 * @param a First string to compare
508
 * @param b Second string to compare
509
 * @retval <0 a precedes b
510
 * @retval  0 a and b are identical
511
 * @retval >0 b precedes a
512
 */
513
int mutt_str_coll(const char *a, const char *b)
22✔
514
{
515
  return strcoll(NONULL(a), NONULL(b));
24✔
516
}
517

518
/**
519
 * mutt_istr_find - Find first occurrence of string (ignoring case)
520
 * @param haystack String to search through
521
 * @param needle   String to find
522
 * @retval ptr  First match of the search string
523
 * @retval NULL No match, or an error
524
 */
525
const char *mutt_istr_find(const char *haystack, const char *needle)
12✔
526
{
527
  if (!haystack)
12✔
528
    return NULL;
529
  if (!needle)
11✔
530
    return haystack;
531

532
  const char *p = NULL, *q = NULL;
533

534
  while (*(p = haystack))
35✔
535
  {
536
    for (q = needle; *p && *q && (mutt_tolower(*p) == mutt_tolower(*q)); p++, q++)
79✔
537
    {
538
    }
539
    if ((*q == '\0'))
34✔
540
      return haystack;
9✔
541
    haystack++;
25✔
542
  }
543
  return NULL;
544
}
545

546
/**
547
 * mutt_str_skip_whitespace - Find the first non-whitespace character in a string
548
 * @param p String to search
549
 * @retval ptr
550
 * - First non-whitespace character
551
 * - Terminating NUL character, if the string was entirely whitespace
552
 */
553
char *mutt_str_skip_whitespace(const char *p)
12✔
554
{
555
  if (!p)
12✔
556
    return NULL;
557
  SKIPWS(p);
26✔
558
  return (char *) p;
559
}
560

561
/**
562
 * mutt_str_remove_trailing_ws - Trim trailing whitespace from a string
563
 * @param s String to trim
564
 *
565
 * The string is modified in place.
566
 */
567
void mutt_str_remove_trailing_ws(char *s)
11✔
568
{
569
  if (!s)
11✔
570
    return;
571

572
  for (char *p = s + mutt_str_len(s) - 1; (p >= s) && mutt_isspace(*p); p--)
31✔
573
    *p = '\0';
21✔
574
}
575

576
/**
577
 * mutt_str_copy - Copy a string into a buffer (guaranteeing NUL-termination)
578
 * @param dest  Buffer for the result
579
 * @param src   String to copy
580
 * @param dsize Destination buffer size
581
 * @retval num Destination string length
582
 */
583
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
2,477✔
584
{
585
  if (!dest || (dsize == 0))
2,477✔
586
    return 0;
587
  if (!src)
2,475✔
588
  {
589
    dest[0] = '\0';
4✔
590
    return 0;
4✔
591
  }
592

593
  char *dest0 = dest;
594
  while ((--dsize > 0) && (*src != '\0'))
20,280✔
595
    *dest++ = *src++;
17,809✔
596

597
  *dest = '\0';
2,471✔
598
  return dest - dest0;
2,471✔
599
}
600

601
/**
602
 * mutt_str_skip_email_wsp - Skip over whitespace as defined by RFC5322
603
 * @param s String to search
604
 * @retval ptr
605
 * - First non-whitespace character
606
 * - Terminating NUL character, if the string was entirely whitespace
607
 *
608
 * This is used primarily for parsing header fields.
609
 */
610
char *mutt_str_skip_email_wsp(const char *s)
1,160✔
611
{
612
  if (!s)
1,160✔
613
    return NULL;
614

615
  for (; mutt_str_is_email_wsp(*s); s++)
1,312✔
616
    ; // Do nothing
617

618
  return (char *) s;
619
}
620

621
/**
622
 * mutt_str_lws_len - Measure the linear-white-space at the beginning of a string
623
 * @param s String to check
624
 * @param n Maximum number of characters to check
625
 * @retval num Count of whitespace characters
626
 *
627
 * Count the number of whitespace characters at the beginning of a string.
628
 * They can be `<space>`, `<tab>`, `<cr>` or `<lf>`.
629
 */
630
size_t mutt_str_lws_len(const char *s, size_t n)
16✔
631
{
632
  if (!s)
16✔
633
    return 0;
634

635
  const char *p = s;
636
  size_t len = n;
637

638
  if (n == 0)
15✔
639
    return 0;
640

641
  for (; p < (s + n); p++)
46✔
642
  {
643
    if (!strchr(" \t\r\n", *p))
40✔
644
    {
645
      len = p - s;
8✔
646
      break;
8✔
647
    }
648
  }
649

650
  if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
14✔
651
    len = 0;
652
  return len;
653
}
654

655
/**
656
 * mutt_str_equal - Compare two strings
657
 * @param a First string
658
 * @param b Second string
659
 * @retval true The strings are equal
660
 * @retval false The strings are not equal
661
 */
662
bool mutt_str_equal(const char *a, const char *b)
11,304✔
663
{
664
  return (a == b) || (mutt_str_cmp(a, b) == 0);
11,304✔
665
}
666

667
/**
668
 * mutt_istr_equal - Compare two strings, ignoring case
669
 * @param a First string
670
 * @param b Second string
671
 * @retval true The strings are equal
672
 * @retval false The strings are not equal
673
 */
674
bool mutt_istr_equal(const char *a, const char *b)
4,357✔
675
{
676
  return (a == b) || (mutt_istr_cmp(a, b) == 0);
4,357✔
677
}
678

679
/**
680
 * mutt_str_is_ascii - Is a string ASCII (7-bit)?
681
 * @param str String to examine
682
 * @param len Length of string to examine
683
 * @retval true There are no 8-bit chars
684
 */
685
bool mutt_str_is_ascii(const char *str, size_t len)
8✔
686
{
687
  if (!str)
8✔
688
    return true;
689

690
  for (; (*str != '\0') && (len > 0); str++, len--)
24✔
691
    if ((*str & 0x80) != 0)
20✔
692
      return false;
693

694
  return true;
695
}
696

697
/**
698
 * mutt_str_find_word - Find the end of a word (non-space)
699
 * @param src String to search
700
 * @retval ptr End of the word
701
 *
702
 * Skip to the end of the current word.
703
 * Skip past any whitespace characters.
704
 *
705
 * @note If there aren't any more words, this will return a pointer to the
706
 *       final NUL character.
707
 */
708
const char *mutt_str_find_word(const char *src)
11✔
709
{
710
  if (!src)
11✔
711
    return NULL;
712

713
  while (*src && strchr(" \t\n", *src))
21✔
714
    src++;
11✔
715
  while (*src && !strchr(" \t\n", *src))
58✔
716
    src++;
48✔
717
  return src;
718
}
719

720
/**
721
 * mutt_str_getenv - Get an environment variable
722
 * @param name Environment variable to get
723
 * @retval ptr Value of variable
724
 * @retval NULL Variable isn't set, or is empty
725
 *
726
 * @warning The caller must not free the returned pointer.
727
 */
728
const char *mutt_str_getenv(const char *name)
1,389✔
729
{
730
  if (!name)
1,389✔
731
    return NULL;
732

733
  const char *val = getenv(name);
1,388✔
734
  if (val && (val[0] != '\0'))
1,388✔
735
    return val;
736

737
  return NULL;
738
}
739

740
/**
741
 * mutt_istr_remall - Remove all occurrences of substring, ignoring case
742
 * @param str     String containing the substring
743
 * @param target  Target substring for removal
744
 * @retval 0 String contained substring and substring was removed successfully
745
 * @retval 1 String did not contain substring
746
 */
747
int mutt_istr_remall(char *str, const char *target)
92✔
748
{
749
  int rc = 1;
750
  if (!str || !target)
92✔
751
    return rc;
752

753
  // Look through an ensure all instances of the substring are gone.
754
  while ((str = (char *) strcasestr(str, target)))
134✔
755
  {
756
    size_t target_len = mutt_str_len(target);
757
    memmove(str, str + target_len, 1 + strlen(str + target_len));
45✔
758
    rc = 0; // If we got here, then a substring existed and has been removed.
759
  }
760

761
  return rc;
762
}
763

764
#ifdef HAVE_VASPRINTF
765
/**
766
 * mutt_str_asprintf - Format a string, allocating space as necessary
767
 * @param[out] strp New string saved here
768
 * @param[in]  fmt  Format string
769
 * @param[in]  ...  Format arguments
770
 * @retval num Characters written
771
 * @retval -1  Error
772
 */
773
int mutt_str_asprintf(char **strp, const char *fmt, ...)
44✔
774
{
775
  if (!strp || !fmt)
44✔
776
    return -1;
777

778
  va_list ap;
779
  int n;
780

781
  va_start(ap, fmt);
42✔
782
  n = vasprintf(strp, fmt, ap);
783
  va_end(ap);
42✔
784

785
  /* GNU libc man page for vasprintf(3) states that the value of *strp
786
   * is undefined when the return code is -1.  */
787
  if (n < 0)
42✔
788
  {
789
    mutt_error("%s", strerror(errno)); /* LCOV_EXCL_LINE */
790
    mutt_exit(1);                      /* LCOV_EXCL_LINE */
791
  }
792

793
  if (n == 0)
42✔
794
  {
795
    /* NeoMutt convention is to use NULL for 0-length strings */
796
    FREE(strp); /* LCOV_EXCL_LINE */
797
  }
798

799
  return n;
800
}
801
#else
802
/* Allocate a C-string large enough to contain the formatted string.
803
 * This is essentially malloc+sprintf in one.
804
 */
805
int mutt_str_asprintf(char **strp, const char *fmt, ...)
806
{
807
  if (!strp || !fmt)
808
    return -1;
809

810
  int rlen = 256;
811

812
  *strp = MUTT_MEM_MALLOC(rlen, char);
813
  while (true)
814
  {
815
    va_list ap;
816
    va_start(ap, fmt);
817
    const int n = vsnprintf(*strp, rlen, fmt, ap);
818
    va_end(ap);
819
    if (n < 0)
820
    {
821
      FREE(strp);
822
      return n;
823
    }
824

825
    if (n < rlen)
826
    {
827
      /* reduce space to just that which was used.  note that 'n' does not
828
       * include the terminal nul char.  */
829
      if (n == 0) /* convention is to use NULL for zero-length strings. */
830
        FREE(strp);
831
      else if (n != rlen - 1)
832
        MUTT_MEM_REALLOC(strp, n + 1, char);
833
      return n;
834
    }
835
    /* increase size and try again */
836
    rlen = n + 1;
837
    MUTT_MEM_REALLOC(strp, rlen, char);
838
  }
839
  /* not reached */
840
}
841
#endif /* HAVE_ASPRINTF */
842

843
/**
844
 * mutt_str_hyphenate - Hyphenate a snake-case string
845
 * @param buf    Buffer for the result
846
 * @param buflen Length of the buffer
847
 * @param str    String to convert
848
 *
849
 * Replace underscores (`_`) with hyphens -`).
850
 */
851
void mutt_str_hyphenate(char *buf, size_t buflen, const char *str)
15✔
852
{
853
  if (!buf || (buflen == 0) || !str)
15✔
854
    return;
855

856
  mutt_str_copy(buf, str, buflen);
12✔
857
  for (; *buf != '\0'; buf++)
75✔
858
  {
859
    if (*buf == '_')
63✔
860
      *buf = '-';
16✔
861
  }
862
}
863

864
/**
865
 * mutt_str_inbox_cmp - Do two folders share the same path and one is an inbox - @ingroup sort_api
866
 * @param a First path
867
 * @param b Second path
868
 * @retval -1 a is INBOX of b
869
 * @retval  0 None is INBOX
870
 * @retval  1 b is INBOX for a
871
 *
872
 * This function compares two folder paths. It first looks for the position of
873
 * the last common '/' character. If a valid position is found and it's not the
874
 * last character in any of the two paths, the remaining parts of the paths are
875
 * compared (case insensitively) with the string "INBOX" followed by a non
876
 * alpha character, e.g., '.' or '/'. If only one of the two paths matches,
877
 * it's reported as being less than the other and the function returns -1 (a <
878
 * b) or 1 (a > b).  If both or no paths match the requirements, the two paths
879
 * are considered equivalent and this function returns 0.
880
 *
881
 * Examples:
882
 * * mutt_str_inbox_cmp("/foo/bar",      "/foo/baz") --> 0
883
 * * mutt_str_inbox_cmp("/foo/bar/",     "/foo/bar/inbox") --> 0
884
 * * mutt_str_inbox_cmp("/foo/bar/sent", "/foo/bar/inbox") --> 1
885
 * * mutt_str_inbox_cmp("=INBOX",        "=Drafts") --> -1
886
 * * mutt_str_inbox_cmp("=INBOX",        "=INBOX.Foo") --> 0
887
 * * mutt_str_inbox_cmp("=INBOX.Foo",    "=Drafts") --> -1
888
 */
889
int mutt_str_inbox_cmp(const char *a, const char *b)
11✔
890
{
891
#define IS_INBOX(s) (mutt_istrn_equal(s, "inbox", 5) && !mutt_isalnum((s)[5]))
892
#define CMP_INBOX(a, b) (IS_INBOX(b) - IS_INBOX(a))
893

894
  /* fast-track in case the paths have been mutt_pretty_mailbox'ified */
895
  if ((a[0] == '+') && (b[0] == '+'))
11✔
896
  {
897
    return CMP_INBOX(a + 1, b + 1);
28✔
898
  }
899

900
  const char *a_end = strrchr(a, '/');
×
901
  const char *b_end = strrchr(b, '/');
×
902

903
  /* If one path contains a '/', but not the other */
904
  if ((!a_end) ^ (!b_end))
×
905
    return 0;
906

907
  /* If neither path contains a '/' */
908
  if (!a_end)
×
909
    return 0;
910

911
  /* Compare the subpaths */
912
  size_t a_len = a_end - a;
×
913
  size_t b_len = b_end - b;
×
914
  size_t min = MIN(a_len, b_len);
×
915
  int same = (a[min] == '/') && (b[min] == '/') && (a[min + 1] != '\0') &&
×
916
             (b[min + 1] != '\0') && mutt_istrn_equal(a, b, min);
×
917

918
  if (!same)
919
    return 0;
920

921
  return CMP_INBOX(a + 1 + min, b + 1 + min);
×
922

923
#undef CMP_INBOX
924
#undef IS_INBOX
925
}
926

927
/**
928
 * string_array_clear - Free all memory of a StringArray
929
 * @param arr Array of text to clear
930
 *
931
 * @note Array is emptied, but not freed
932
 */
933
void string_array_clear(struct StringArray *arr)
×
934
{
935
  const char **str = NULL;
936
  ARRAY_FOREACH(str, arr)
×
937
  {
938
    FREE(str);
×
939
  }
940

941
  ARRAY_FREE(arr);
×
942
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc