NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <ctype.h>
34#include <errno.h>
35#include <stdarg.h> // IWYU pragma: keep
36#include <stdbool.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <strings.h>
41#include "exit.h"
42#include "logging2.h"
43#include "memory.h"
44#include "string2.h"
45#ifdef HAVE_SYSEXITS_H
46#include <sysexits.h>
47#endif
48
49#ifndef HAVE_STRCASESTR
56static char *strcasestr(const char *haystack, const char *needle)
57{
58 size_t haystackn = strlen(haystack);
59 size_t needlen = strlen(needle);
60
61 const char *p = haystack;
62 while (haystackn >= needlen)
63 {
64 if (strncasecmp(p, needle, needlen) == 0)
65 return (char *) p;
66 p++;
67 haystackn--;
68 }
69 return NULL;
70}
71#endif /* HAVE_STRCASESTR */
72
73#ifndef HAVE_STRSEP
82static char *strsep(char **stringp, const char *delim)
83{
84 if (!*stringp)
85 return NULL;
86
87 char *start = *stringp;
88 for (char *p = *stringp; *p != '\0'; p++)
89 {
90 for (const char *s = delim; *s != '\0'; s++)
91 {
92 if (*p == *s)
93 {
94 *p = '\0';
95 *stringp = p + 1;
96 return start;
97 }
98 }
99 }
100 *stringp = NULL;
101 return start;
102}
103#endif /* HAVE_STRSEP */
104
109{
111 const char *err_str;
112};
113
115static const struct SysExits SysExits[] = {
116#ifdef EX_USAGE
117 { 0xff & EX_USAGE, "Bad usage." },
118#endif
119#ifdef EX_DATAERR
120 { 0xff & EX_DATAERR, "Data format error." },
121#endif
122#ifdef EX_NOINPUT
123 { 0xff & EX_NOINPUT, "Can't open input." },
124#endif
125#ifdef EX_NOUSER
126 { 0xff & EX_NOUSER, "User unknown." },
127#endif
128#ifdef EX_NOHOST
129 { 0xff & EX_NOHOST, "Host unknown." },
130#endif
131#ifdef EX_UNAVAILABLE
132 { 0xff & EX_UNAVAILABLE, "Service unavailable." },
133#endif
134#ifdef EX_SOFTWARE
135 { 0xff & EX_SOFTWARE, "Internal error." },
136#endif
137#ifdef EX_OSERR
138 { 0xff & EX_OSERR, "Operating system error." },
139#endif
140#ifdef EX_OSFILE
141 { 0xff & EX_OSFILE, "System file missing." },
142#endif
143#ifdef EX_CANTCREAT
144 { 0xff & EX_CANTCREAT, "Can't create output." },
145#endif
146#ifdef EX_IOERR
147 { 0xff & EX_IOERR, "I/O error." },
148#endif
149#ifdef EX_TEMPFAIL
150 { 0xff & EX_TEMPFAIL, "Deferred." },
151#endif
152#ifdef EX_PROTOCOL
153 { 0xff & EX_PROTOCOL, "Remote protocol error." },
154#endif
155#ifdef EX_NOPERM
156 { 0xff & EX_NOPERM, "Insufficient permission." },
157#endif
158#ifdef EX_CONFIG
159 { 0xff & EX_NOPERM, "Local configuration error." },
160#endif
161 { S_ERR, "Exec error." },
162};
163
169const char *mutt_str_sysexit(int err_num)
170{
171 for (size_t i = 0; i < mutt_array_size(SysExits); i++)
172 {
173 if (err_num == SysExits[i].err_num)
174 return SysExits[i].err_str;
175 }
176
177 return NULL;
178}
179
186char *mutt_str_sep(char **stringp, const char *delim)
187{
188 if (!stringp || !*stringp || !delim)
189 return NULL;
190 return strsep(stringp, delim);
191}
192
201static size_t startswith(const char *str, const char *prefix, bool match_case)
202{
203 if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
204 {
205 return 0;
206 }
207
208 const char *saved_prefix = prefix;
209 for (; *str && *prefix; str++, prefix++)
210 {
211 if (*str == *prefix)
212 continue;
213
214 if (!match_case && tolower(*str) == tolower(*prefix))
215 continue;
216
217 return 0;
218 }
219
220 return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
221}
222
230size_t mutt_str_startswith(const char *str, const char *prefix)
231{
232 return startswith(str, prefix, true);
233}
234
242size_t mutt_istr_startswith(const char *str, const char *prefix)
243{
244 return startswith(str, prefix, false);
245}
246
253char *mutt_str_dup(const char *str)
254{
255 if (!str || (*str == '\0'))
256 return NULL;
257
258 char *p = strdup(str);
259 if (!p)
260 {
261 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
262 mutt_exit(1); // LCOV_EXCL_LINE
263 }
264 return p;
265}
266
280char *mutt_str_replace(char **p, const char *s)
281{
282 if (!p)
283 return NULL;
284 const char *tmp = *p;
285 *p = mutt_str_dup(s);
286 FREE(&tmp);
287 return *p;
288}
289
299void mutt_str_adjust(char **ptr)
300{
301 if (!ptr || !*ptr)
302 return;
303 mutt_mem_realloc(ptr, strlen(*ptr) + 1);
304}
305
313char *mutt_str_lower(char *str)
314{
315 if (!str)
316 return NULL;
317
318 char *p = str;
319
320 while (*p)
321 {
322 *p = tolower((unsigned char) *p);
323 p++;
324 }
325
326 return str;
327}
328
336char *mutt_str_upper(char *str)
337{
338 if (!str)
339 return NULL;
340
341 char *p = str;
342
343 while (*p)
344 {
345 *p = toupper((unsigned char) *p);
346 p++;
347 }
348
349 return str;
350}
351
360char *mutt_strn_copy(char *dest, const char *src, size_t len, size_t dsize)
361{
362 if (!src || !dest || (len == 0) || (dsize == 0))
363 return dest;
364
365 if (len > (dsize - 1))
366 len = dsize - 1;
367 memcpy(dest, src, len);
368 dest[len] = '\0';
369 return dest;
370}
371
380char *mutt_strn_dup(const char *begin, size_t len)
381{
382 if (!begin)
383 return NULL;
384
385 char *p = mutt_mem_malloc(len + 1);
386 memcpy(p, begin, len);
387 p[len] = '\0';
388 return p;
389}
390
399int mutt_str_cmp(const char *a, const char *b)
400{
401 return strcmp(NONULL(a), NONULL(b));
402}
403
412int mutt_istr_cmp(const char *a, const char *b)
413{
414 return strcasecmp(NONULL(a), NONULL(b));
415}
416
425bool mutt_strn_equal(const char *a, const char *b, size_t num)
426{
427 return strncmp(NONULL(a), NONULL(b), num) == 0;
428}
429
439int mutt_istrn_cmp(const char *a, const char *b, size_t num)
440{
441 return strncasecmp(NONULL(a), NONULL(b), num);
442}
443
453bool mutt_istrn_equal(const char *a, const char *b, size_t num)
454{
455 return strncasecmp(NONULL(a), NONULL(b), num) == 0;
456}
457
469const char *mutt_istrn_rfind(const char *haystack, size_t haystack_length, const char *needle)
470{
471 if (!haystack || (haystack_length == 0) || !needle)
472 return NULL;
473
474 int needle_length = strlen(needle);
475 const char *haystack_end = haystack + haystack_length - needle_length;
476
477 for (const char *p = haystack_end; p >= haystack; --p)
478 {
479 for (size_t i = 0; i < needle_length; i++)
480 {
481 if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
482 goto next;
483 }
484 return p;
485
486 next:;
487 }
488 return NULL;
489}
490
496size_t mutt_str_len(const char *a)
497{
498 return a ? strlen(a) : 0;
499}
500
509int mutt_str_coll(const char *a, const char *b)
510{
511 return strcoll(NONULL(a), NONULL(b));
512}
513
521const char *mutt_istr_find(const char *haystack, const char *needle)
522{
523 if (!haystack)
524 return NULL;
525 if (!needle)
526 return haystack;
527
528 const char *p = NULL, *q = NULL;
529
530 while (*(p = haystack))
531 {
532 for (q = needle;
533 *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
534 p++, q++)
535 {
536 }
537 if ((*q == '\0'))
538 return haystack;
539 haystack++;
540 }
541 return NULL;
542}
543
551char *mutt_str_skip_whitespace(const char *p)
552{
553 if (!p)
554 return NULL;
555 SKIPWS(p);
556 return (char *) p;
557}
558
566{
567 if (!s)
568 return;
569
570 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && isspace(*p); p--)
571 *p = '\0';
572}
573
581size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
582{
583 if (!dest || (dsize == 0))
584 return 0;
585 if (!src)
586 {
587 dest[0] = '\0';
588 return 0;
589 }
590
591 char *dest0 = dest;
592 while ((--dsize > 0) && (*src != '\0'))
593 *dest++ = *src++;
594
595 *dest = '\0';
596 return dest - dest0;
597}
598
608char *mutt_str_skip_email_wsp(const char *s)
609{
610 if (!s)
611 return NULL;
612
613 for (; mutt_str_is_email_wsp(*s); s++)
614 ; // Do nothing
615
616 return (char *) s;
617}
618
628size_t mutt_str_lws_len(const char *s, size_t n)
629{
630 if (!s)
631 return 0;
632
633 const char *p = s;
634 size_t len = n;
635
636 if (n == 0)
637 return 0;
638
639 for (; p < (s + n); p++)
640 {
641 if (!strchr(" \t\r\n", *p))
642 {
643 len = p - s;
644 break;
645 }
646 }
647
648 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
649 len = 0;
650 return len;
651}
652
660bool mutt_str_equal(const char *a, const char *b)
661{
662 return (a == b) || (mutt_str_cmp(a, b) == 0);
663}
664
672bool mutt_istr_equal(const char *a, const char *b)
673{
674 return (a == b) || (mutt_istr_cmp(a, b) == 0);
675}
676
683bool mutt_str_is_ascii(const char *str, size_t len)
684{
685 if (!str)
686 return true;
687
688 for (; (*str != '\0') && (len > 0); str++, len--)
689 if ((*str & 0x80) != 0)
690 return false;
691
692 return true;
693}
694
706const char *mutt_str_find_word(const char *src)
707{
708 if (!src)
709 return NULL;
710
711 while (*src && strchr(" \t\n", *src))
712 src++;
713 while (*src && !strchr(" \t\n", *src))
714 src++;
715 return src;
716}
717
726const char *mutt_str_getenv(const char *name)
727{
728 if (!name)
729 return NULL;
730
731 const char *val = getenv(name);
732 if (val && (val[0] != '\0'))
733 return val;
734
735 return NULL;
736}
737
745int mutt_istr_remall(char *str, const char *target)
746{
747 int rc = 1;
748 if (!str || !target)
749 return rc;
750
751 // Look through an ensure all instances of the substring are gone.
752 while ((str = (char *) strcasestr(str, target)))
753 {
754 size_t target_len = mutt_str_len(target);
755 memmove(str, str + target_len, 1 + strlen(str + target_len));
756 rc = 0; // If we got here, then a substring existed and has been removed.
757 }
758
759 return rc;
760}
761
762#ifdef HAVE_VASPRINTF
771int mutt_str_asprintf(char **strp, const char *fmt, ...)
772{
773 if (!strp || !fmt)
774 return -1;
775
776 va_list ap;
777 int n;
778
779 va_start(ap, fmt);
780 n = vasprintf(strp, fmt, ap);
781 va_end(ap);
782
783 /* GNU libc man page for vasprintf(3) states that the value of *strp
784 * is undefined when the return code is -1. */
785 if (n < 0)
786 {
787 mutt_error("%s", strerror(errno)); /* LCOV_EXCL_LINE */
788 mutt_exit(1); /* LCOV_EXCL_LINE */
789 }
790
791 if (n == 0)
792 {
793 /* NeoMutt convention is to use NULL for 0-length strings */
794 FREE(strp); /* LCOV_EXCL_LINE */
795 }
796
797 return n;
798}
799#else
800/* Allocate a C-string large enough to contain the formatted string.
801 * This is essentially malloc+sprintf in one.
802 */
803int mutt_str_asprintf(char **strp, const char *fmt, ...)
804{
805 if (!strp || !fmt)
806 return -1;
807
808 int rlen = 256;
809
810 *strp = mutt_mem_malloc(rlen);
811 while (true)
812 {
813 va_list ap;
814 va_start(ap, fmt);
815 const int n = vsnprintf(*strp, rlen, fmt, ap);
816 va_end(ap);
817 if (n < 0)
818 {
819 FREE(strp);
820 return n;
821 }
822
823 if (n < rlen)
824 {
825 /* reduce space to just that which was used. note that 'n' does not
826 * include the terminal nul char. */
827 if (n == 0) /* convention is to use NULL for zero-length strings. */
828 FREE(strp);
829 else if (n != rlen - 1)
830 mutt_mem_realloc(strp, n + 1);
831 return n;
832 }
833 /* increase size and try again */
834 rlen = n + 1;
835 mutt_mem_realloc(strp, rlen);
836 }
837 /* not reached */
838}
839#endif /* HAVE_ASPRINTF */
840
849void mutt_str_hyphenate(char *buf, size_t buflen, const char *str)
850{
851 if (!buf || (buflen == 0) || !str)
852 return;
853
854 mutt_str_copy(buf, str, buflen);
855 for (; *buf != '\0'; buf++)
856 {
857 if (*buf == '_')
858 *buf = '-';
859 }
860}
861
887int mutt_str_inbox_cmp(const char *a, const char *b)
888{
889#define IS_INBOX(s) (mutt_istrn_equal(s, "inbox", 5) && !isalnum((s)[5]))
890#define CMP_INBOX(a, b) (IS_INBOX(b) - IS_INBOX(a))
891
892 /* fast-track in case the paths have been mutt_pretty_mailbox'ified */
893 if ((a[0] == '+') && (b[0] == '+'))
894 {
895 return CMP_INBOX(a + 1, b + 1);
896 }
897
898 const char *a_end = strrchr(a, '/');
899 const char *b_end = strrchr(b, '/');
900
901 /* If one path contains a '/', but not the other */
902 if ((!a_end) ^ (!b_end))
903 return 0;
904
905 /* If neither path contains a '/' */
906 if (!a_end)
907 return 0;
908
909 /* Compare the subpaths */
910 size_t a_len = a_end - a;
911 size_t b_len = b_end - b;
912 size_t min = MIN(a_len, b_len);
913 int same = (a[min] == '/') && (b[min] == '/') && (a[min + 1] != '\0') &&
914 (b[min + 1] != '\0') && mutt_istrn_equal(a, b, min);
915
916 if (!same)
917 return 0;
918
919 return CMP_INBOX(a + 1 + min, b + 1 + min);
920
921#undef CMP_INBOX
922#undef IS_INBOX
923}
Leave the program NOW.
#define mutt_error(...)
Definition: logging2.h:92
int mutt_str_inbox_cmp(const char *a, const char *b)
Do two folders share the same path and one is an inbox -.
Definition: string.c:887
Logging Dispatcher.
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:91
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:115
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
#define MIN(a, b)
Definition: memory.h:32
#define mutt_array_size(x)
Definition: memory.h:38
char * mutt_strn_dup(const char *begin, size_t len)
Duplicate a sub-string.
Definition: string.c:380
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:399
int mutt_istrn_cmp(const char *a, const char *b, size_t num)
Compare two strings ignoring case (to a maximum), safely.
Definition: string.c:439
void mutt_str_remove_trailing_ws(char *s)
Trim trailing whitespace from a string.
Definition: string.c:565
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:201
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:803
char * mutt_str_upper(char *str)
Convert all characters in the string to uppercase.
Definition: string.c:336
#define CMP_INBOX(a, b)
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:313
char * mutt_str_skip_email_wsp(const char *s)
Skip over whitespace as defined by RFC5322.
Definition: string.c:608
const char * mutt_istrn_rfind(const char *haystack, size_t haystack_length, const char *needle)
Find last instance of a substring, ignoring case.
Definition: string.c:469
size_t mutt_str_lws_len(const char *s, size_t n)
Measure the linear-white-space at the beginning of a string.
Definition: string.c:628
bool mutt_str_is_ascii(const char *str, size_t len)
Is a string ASCII (7-bit)?
Definition: string.c:683
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:412
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:425
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition: string.c:82
const char * mutt_str_find_word(const char *src)
Find the end of a word (non-space)
Definition: string.c:706
char * mutt_strn_copy(char *dest, const char *src, size_t len, size_t dsize)
Copy a sub-string into a buffer.
Definition: string.c:360
int mutt_istr_remall(char *str, const char *target)
Remove all occurrences of substring, ignoring case.
Definition: string.c:745
const char * mutt_str_getenv(const char *name)
Get an environment variable.
Definition: string.c:726
const char * mutt_istr_find(const char *haystack, const char *needle)
Find first occurrence of string (ignoring case)
Definition: string.c:521
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
void mutt_str_hyphenate(char *buf, size_t buflen, const char *str)
Hyphenate a snake-case string.
Definition: string.c:849
char * mutt_str_skip_whitespace(const char *p)
Find the first non-whitespace character in a string.
Definition: string.c:551
void mutt_str_adjust(char **ptr)
Shrink-to-fit a string.
Definition: string.c:299
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:581
static char * strcasestr(const char *haystack, const char *needle)
Find the first occurrence of needle in haystack, ignoring case.
Definition: string.c:56
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:242
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition: string.c:453
const char * mutt_str_sysexit(int err_num)
Return a string matching an error code.
Definition: string.c:169
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:509
char * mutt_str_sep(char **stringp, const char *delim)
Find first occurrence of any of delim characters in *stringp.
Definition: string.c:186
String manipulation functions.
#define S_ERR
Definition: string2.h:40
#define NONULL(x)
Definition: string2.h:37
static bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string2.h:104
#define SKIPWS(ch)
Definition: string2.h:45
Lookup table of error messages.
Definition: string.c:109
const char * err_str
Human-readable string for error.
Definition: string.c:111
int err_num
Error number, see errno(3)
Definition: string.c:110