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

neomutt / neomutt / 27930548110

15 Jun 2026 02:57PM UTC coverage: 42.995% (+0.03%) from 42.97%
27930548110

push

github

flatcap
fix coloured prompt truncation

An upgrade to the Buffer code exposed a bug in prompt-colouring code.

msgwin_add_text_n() cached the Buffer's dptr, before adding text.
This was done to track where the *new* text began.

buf_addstr_n() caused a Buffer reallocation, invalidating dptr.

12690 of 29515 relevant lines covered (43.0%)

5227.33 hits per line

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

90.8
/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)
83✔
191
{
192
  if (!stringp || !*stringp || !delim)
83✔
193
    return NULL;
194
  return strsep(stringp, delim);
51✔
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)
27,324✔
206
{
207
  if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
27,324✔
208
  {
209
    return 0;
210
  }
211

212
  const char *saved_prefix = prefix;
213
  for (; *str && *prefix; str++, prefix++)
79,880✔
214
  {
215
    if (*str == *prefix)
72,686✔
216
      continue;
51,267✔
217

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

221
    return 0;
222
  }
223

224
  return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
7,194✔
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)
20,096✔
235
{
236
  return startswith(str, prefix, true);
20,096✔
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)
7,228✔
247
{
248
  return startswith(str, prefix, false);
7,228✔
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)
226,428✔
258
{
259
  if (!str || (*str == '\0'))
226,428✔
260
    return NULL;
261

262
  char *p = strdup(str);
177,359✔
263
  if (!p)
177,359✔
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)
2,667✔
285
{
286
  if (!p)
2,667✔
287
    return NULL;
288
  const char *tmp = *p;
2,665✔
289
  *p = mutt_str_dup(s);
2,665✔
290
  FREE(&tmp);
2,665✔
291
  return *p;
2,665✔
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)
5,502✔
385
{
386
  if (!begin)
5,502✔
387
    return NULL;
388

389
  char *p = MUTT_MEM_MALLOC(len + 1, char);
5,500✔
390
  memcpy(p, begin, len);
391
  p[len] = '\0';
5,500✔
392
  return p;
5,500✔
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)
2,383,583✔
404
{
405
  return strcmp(NONULL(a), NONULL(b));
2,416,514✔
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)
2,022,311✔
417
{
418
  return strcasecmp(NONULL(a), NONULL(b));
2,022,323✔
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)
1,336,094✔
430
{
431
  return strncmp(NONULL(a), NONULL(b), num) == 0;
1,336,096✔
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)
12,356✔
458
{
459
  return strncasecmp(NONULL(a), NONULL(b), num) == 0;
12,358✔
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
  size_t needle_length = strlen(needle);
35✔
479
  if (needle_length > haystack_length)
35✔
480
    return NULL;
481

482
  const char *haystack_end = haystack + haystack_length - needle_length;
32✔
483

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

493
  next:;
494
  }
495
  return NULL;
496
}
497

498
/**
499
 * mutt_str_len - Calculate the length of a string, safely
500
 * @param a String to measure
501
 * @retval num Length in bytes
502
 */
503
size_t mutt_str_len(const char *a)
1,382,290✔
504
{
505
  return a ? strlen(a) : 0;
1,382,290✔
506
}
507

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

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

535
  const char *p = NULL;
536
  const char *q = NULL;
537

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

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

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

576
  for (char *p = s + mutt_str_len(s) - 1; (p >= s) && mutt_isspace(*p); p--)
31✔
577
    *p = '\0';
21✔
578
}
579

580
/**
581
 * mutt_str_copy - Copy a string into a buffer (guaranteeing NUL-termination)
582
 * @param dest  Buffer for the result
583
 * @param src   String to copy
584
 * @param dsize Destination buffer size
585
 * @retval num Destination string length
586
 */
587
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
27,690✔
588
{
589
  if (!dest || (dsize == 0))
27,690✔
590
    return 0;
591
  if (!src)
27,688✔
592
  {
593
    dest[0] = '\0';
657✔
594
    return 0;
657✔
595
  }
596

597
  char *dest0 = dest;
598
  while ((--dsize > 0) && (*src != '\0'))
226,141✔
599
    *dest++ = *src++;
199,110✔
600

601
  *dest = '\0';
27,031✔
602
  return dest - dest0;
27,031✔
603
}
604

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

619
  for (; mutt_str_is_email_wsp(*s); s++)
1,309✔
620
    ; // Do nothing
621

622
  return (char *) s;
623
}
624

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

639
  const char *p = s;
640
  size_t len = n;
641

642
  if (n == 0)
15✔
643
    return 0;
644

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

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

659
/**
660
 * mutt_str_equal - Compare two strings
661
 * @param a First string
662
 * @param b Second string
663
 * @retval true The strings are equal
664
 * @retval false The strings are not equal
665
 */
666
bool mutt_str_equal(const char *a, const char *b)
163,296✔
667
{
668
  return (a == b) || (mutt_str_cmp(a, b) == 0);
163,296✔
669
}
670

671
/**
672
 * mutt_istr_equal - Compare two strings, ignoring case
673
 * @param a First string
674
 * @param b Second string
675
 * @retval true The strings are equal
676
 * @retval false The strings are not equal
677
 */
678
bool mutt_istr_equal(const char *a, const char *b)
10,918✔
679
{
680
  return (a == b) || (mutt_istr_cmp(a, b) == 0);
10,918✔
681
}
682

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

694
  for (; (*str != '\0') && (len > 0); str++, len--)
24✔
695
    if ((*str & 0x80) != 0)
20✔
696
      return false;
697

698
  return true;
699
}
700

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

717
  while (*src && strchr(" \t\n", *src))
21✔
718
    src++;
11✔
719
  while (*src && !strchr(" \t\n", *src))
58✔
720
    src++;
48✔
721
  return src;
722
}
723

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

737
  const char *val = getenv(name);
7,303✔
738
  if (val && (val[0] != '\0'))
7,303✔
739
    return val;
740

741
  return NULL;
742
}
743

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

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

765
  return rc;
766
}
767

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

782
  va_list ap = { 0 };
42✔
783
  int n;
784

785
  va_start(ap, fmt);
42✔
786
  n = vasprintf(strp, fmt, ap);
787
  va_end(ap);
42✔
788

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

797
  if (n == 0)
42✔
798
  {
799
    /* NeoMutt convention is to use NULL for 0-length strings */
800
    FREE(strp); /* LCOV_EXCL_LINE */
801
  }
802

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

814
  int rlen = 256;
815

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

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

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

860
  mutt_str_copy(buf, str, buflen);
12✔
861
  for (; *buf != '\0'; buf++)
75✔
862
  {
863
    if (*buf == '_')
63✔
864
      *buf = '-';
16✔
865
  }
866
}
867

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

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

904
  const char *a_end = strrchr(a, '/');
×
905
  const char *b_end = strrchr(b, '/');
×
906

907
  /* If one path contains a '/', but not the other */
908
  if ((!a_end) ^ (!b_end))
×
909
    return 0;
910

911
  /* If neither path contains a '/' */
912
  if (!a_end)
×
913
    return 0;
914

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

922
  if (!same)
923
    return 0;
924

925
  return CMP_INBOX(a + 1 + min, b + 1 + min);
×
926

927
#undef CMP_INBOX
928
#undef IS_INBOX
929
}
930

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

945
  ARRAY_FREE(arr);
×
946
}
×
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