NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
string.c File Reference

String manipulation functions. More...

#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "exit.h"
#include "logging2.h"
#include "memory.h"
#include "string2.h"
+ Include dependency graph for string.c:

Go to the source code of this file.

Data Structures

struct  SysExits
 Lookup table of error messages. More...
 

Macros

#define IS_INBOX(s)   (mutt_istrn_equal(s, "inbox", 5) && !isalnum((s)[5]))
 
#define CMP_INBOX(a, b)   (IS_INBOX(b) - IS_INBOX(a))
 

Functions

static char * strcasestr (const char *haystack, const char *needle)
 Find the first occurrence of needle in haystack, ignoring case.
 
static char * strsep (char **stringp, const char *delim)
 Extract a token from a string.
 
const char * mutt_str_sysexit (int err_num)
 Return a string matching an error code.
 
char * mutt_str_sep (char **stringp, const char *delim)
 Find first occurrence of any of delim characters in *stringp.
 
static size_t startswith (const char *str, const char *prefix, bool match_case)
 Check whether a string starts with a prefix.
 
size_t mutt_str_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix.
 
size_t mutt_istr_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix, ignoring case.
 
char * mutt_str_dup (const char *str)
 Copy a string, safely.
 
char * mutt_str_replace (char **p, const char *s)
 Replace one string with another.
 
void mutt_str_adjust (char **ptr)
 Shrink-to-fit a string.
 
char * mutt_str_lower (char *str)
 Convert all characters in the string to lowercase.
 
char * mutt_str_upper (char *str)
 Convert all characters in the string to uppercase.
 
char * mutt_strn_copy (char *dest, const char *src, size_t len, size_t dsize)
 Copy a sub-string into a buffer.
 
char * mutt_strn_dup (const char *begin, size_t len)
 Duplicate a sub-string.
 
int mutt_str_cmp (const char *a, const char *b)
 Compare two strings, safely.
 
int mutt_istr_cmp (const char *a, const char *b)
 Compare two strings ignoring case, safely.
 
bool mutt_strn_equal (const char *a, const char *b, size_t num)
 Check for equality of two strings (to a maximum), safely.
 
int mutt_istrn_cmp (const char *a, const char *b, size_t num)
 Compare two strings ignoring case (to a maximum), safely.
 
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.
 
const char * mutt_istrn_rfind (const char *haystack, size_t haystack_length, const char *needle)
 Find last instance of a substring, ignoring case.
 
size_t mutt_str_len (const char *a)
 Calculate the length of a string, safely.
 
int mutt_str_coll (const char *a, const char *b)
 Collate two strings (compare using locale), safely.
 
const char * mutt_istr_find (const char *haystack, const char *needle)
 Find first occurrence of string (ignoring case)
 
char * mutt_str_skip_whitespace (const char *p)
 Find the first non-whitespace character in a string.
 
void mutt_str_remove_trailing_ws (char *s)
 Trim trailing whitespace from a string.
 
size_t mutt_str_copy (char *dest, const char *src, size_t dsize)
 Copy a string into a buffer (guaranteeing NUL-termination)
 
char * mutt_str_skip_email_wsp (const char *s)
 Skip over whitespace as defined by RFC5322.
 
size_t mutt_str_lws_len (const char *s, size_t n)
 Measure the linear-white-space at the beginning of a string.
 
bool mutt_str_equal (const char *a, const char *b)
 Compare two strings.
 
bool mutt_istr_equal (const char *a, const char *b)
 Compare two strings, ignoring case.
 
bool mutt_str_is_ascii (const char *str, size_t len)
 Is a string ASCII (7-bit)?
 
const char * mutt_str_find_word (const char *src)
 Find the end of a word (non-space)
 
const char * mutt_str_getenv (const char *name)
 Get an environment variable.
 
int mutt_istr_remall (char *str, const char *target)
 Remove all occurrences of substring, ignoring case.
 
int mutt_str_asprintf (char **strp, const char *fmt,...)
 
void mutt_str_hyphenate (char *buf, size_t buflen, const char *str)
 Hyphenate a snake-case string.
 
int mutt_str_inbox_cmp (const char *a, const char *b)
 Do two folders share the same path and one is an inbox -.
 

Variables

static const struct SysExits SysExits []
 Lookup table of error messages.
 

Detailed Description

String manipulation functions.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Austin Ray
  • Claes Nästén
  • Dennis Schön

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file string.c.

Macro Definition Documentation

◆ IS_INBOX

#define IS_INBOX (   s)    (mutt_istrn_equal(s, "inbox", 5) && !isalnum((s)[5]))

◆ CMP_INBOX

#define CMP_INBOX (   a,
 
)    (IS_INBOX(b) - IS_INBOX(a))

Function Documentation

◆ strcasestr()

static char * strcasestr ( const char *  haystack,
const char *  needle 
)
static

Find the first occurrence of needle in haystack, ignoring case.

Parameters
haystackString to search
needleString to find
Return values
ptrMatched string, or NULL on failure

Definition at line 57 of file string.c.

58{
59 size_t haystackn = strlen(haystack);
60 size_t needlen = strlen(needle);
61
62 const char *p = haystack;
63 while (haystackn >= needlen)
64 {
65 if (strncasecmp(p, needle, needlen) == 0)
66 return (char *) p;
67 p++;
68 haystackn--;
69 }
70 return NULL;
71}
+ Here is the caller graph for this function:

◆ strsep()

static char * strsep ( char **  stringp,
const char *  delim 
)
static

Extract a token from a string.

Parameters
stringpString to be split up
delimCharacters to split stringp at
Return values
ptrNext token, or NULL if the no more tokens
Note
The pointer stringp will be moved and NULs inserted into it

Definition at line 83 of file string.c.

84{
85 if (!*stringp)
86 return NULL;
87
88 char *start = *stringp;
89 for (char *p = *stringp; *p != '\0'; p++)
90 {
91 for (const char *s = delim; *s != '\0'; s++)
92 {
93 if (*p == *s)
94 {
95 *p = '\0';
96 *stringp = p + 1;
97 return start;
98 }
99 }
100 }
101 *stringp = NULL;
102 return start;
103}
+ Here is the caller graph for this function:

◆ mutt_str_sysexit()

const char * mutt_str_sysexit ( int  err_num)

Return a string matching an error code.

Parameters
err_numError code, e.g. EX_NOPERM
Return values
ptrstring representing the error code

Definition at line 170 of file string.c.

171{
172 for (size_t i = 0; i < mutt_array_size(SysExits); i++)
173 {
174 if (err_num == SysExits[i].err_num)
175 return SysExits[i].err_str;
176 }
177
178 return NULL;
179}
#define mutt_array_size(x)
Definition: memory.h:38
Lookup table of error messages.
Definition: string.c:110
const char * err_str
Human-readable string for error.
Definition: string.c:112
+ Here is the caller graph for this function:

◆ mutt_str_sep()

char * mutt_str_sep ( char **  stringp,
const char *  delim 
)

Find first occurrence of any of delim characters in *stringp.

Parameters
stringpPointer to string to search for delim, updated with position of after delim if found else NULL
delimString with characters to search for in *stringp
Return values
ptrInput value of *stringp

Definition at line 187 of file string.c.

188{
189 if (!stringp || !*stringp || !delim)
190 return NULL;
191 return strsep(stringp, delim);
192}
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition: string.c:83
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ startswith()

static size_t startswith ( const char *  str,
const char *  prefix,
bool  match_case 
)
static

Check whether a string starts with a prefix.

Parameters
strString to check
prefixPrefix to match
match_caseTrue if case needs to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 202 of file string.c.

203{
204 if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
205 {
206 return 0;
207 }
208
209 const char *saved_prefix = prefix;
210 for (; *str && *prefix; str++, prefix++)
211 {
212 if (*str == *prefix)
213 continue;
214
215 if (!match_case && tolower(*str) == tolower(*prefix))
216 continue;
217
218 return 0;
219 }
220
221 return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
222}
+ Here is the caller graph for this function:

◆ mutt_str_startswith()

size_t mutt_str_startswith ( const char *  str,
const char *  prefix 
)

Check whether a string starts with a prefix.

Parameters
strString to check
prefixPrefix to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 231 of file string.c.

232{
233 return startswith(str, prefix, true);
234}
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:202
+ Here is the call graph for this function:

◆ mutt_istr_startswith()

size_t mutt_istr_startswith ( const char *  str,
const char *  prefix 
)

Check whether a string starts with a prefix, ignoring case.

Parameters
strString to check
prefixPrefix to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 243 of file string.c.

244{
245 return startswith(str, prefix, false);
246}
+ Here is the call graph for this function:

◆ mutt_str_dup()

char * mutt_str_dup ( const char *  str)

Copy a string, safely.

Parameters
strString to copy
Return values
ptrCopy of the string
NULLstr was NULL or empty

Definition at line 254 of file string.c.

255{
256 if (!str || (*str == '\0'))
257 return NULL;
258
259 char *p = strdup(str);
260 if (!p)
261 {
262 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
263 mutt_exit(1); // LCOV_EXCL_LINE
264 }
265 return p;
266}
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: exit.c:41
#define mutt_error(...)
Definition: logging2.h:93
+ Here is the call graph for this function:

◆ mutt_str_replace()

char * mutt_str_replace ( char **  p,
const char *  s 
)

Replace one string with another.

Parameters
[out]pString to replace
[in]sNew string
Return values
ptrReplaced string

This function free()s the original string, strdup()s the new string and overwrites the pointer to the first string.

This function alters the pointer of the caller.

Note
Free *p afterwards to handle the case that *p and s reference the same memory

Definition at line 281 of file string.c.

282{
283 if (!p)
284 return NULL;
285 const char *tmp = *p;
286 *p = mutt_str_dup(s);
287 FREE(&tmp);
288 return *p;
289}
#define FREE(x)
Definition: memory.h:55
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
+ Here is the call graph for this function:

◆ mutt_str_adjust()

void mutt_str_adjust ( char **  ptr)

Shrink-to-fit a string.

Parameters
[out]ptrString to alter

Take a string which is allocated on the heap, find its length and reallocate the memory to be exactly the right size.

This function alters the pointer of the caller.

Definition at line 300 of file string.c.

301{
302 if (!ptr || !*ptr)
303 return;
304 MUTT_MEM_REALLOC(ptr, strlen(*ptr) + 1, char);
305}
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:43
+ Here is the caller graph for this function:

◆ mutt_str_lower()

char * mutt_str_lower ( char *  str)

Convert all characters in the string to lowercase.

Parameters
strString to lowercase
Return values
ptrLowercase string

The string is transformed in place.

Definition at line 314 of file string.c.

315{
316 if (!str)
317 return NULL;
318
319 char *p = str;
320
321 while (*p)
322 {
323 *p = tolower((unsigned char) *p);
324 p++;
325 }
326
327 return str;
328}
+ Here is the caller graph for this function:

◆ mutt_str_upper()

char * mutt_str_upper ( char *  str)

Convert all characters in the string to uppercase.

Parameters
strString to uppercase
Return values
ptrUppercase string

The string is transformed in place.

Definition at line 337 of file string.c.

338{
339 if (!str)
340 return NULL;
341
342 char *p = str;
343
344 while (*p)
345 {
346 *p = toupper((unsigned char) *p);
347 p++;
348 }
349
350 return str;
351}
+ Here is the caller graph for this function:

◆ mutt_strn_copy()

char * mutt_strn_copy ( char *  dest,
const char *  src,
size_t  len,
size_t  dsize 
)

Copy a sub-string into a buffer.

Parameters
destBuffer for the result
srcStart of the string to copy
lenLength of the string to copy
dsizeDestination buffer size
Return values
ptrDestination buffer

Definition at line 361 of file string.c.

362{
363 if (!src || !dest || (len == 0) || (dsize == 0))
364 return dest;
365
366 if (len > (dsize - 1))
367 len = dsize - 1;
368 memcpy(dest, src, len);
369 dest[len] = '\0';
370 return dest;
371}
+ Here is the caller graph for this function:

◆ mutt_strn_dup()

char * mutt_strn_dup ( const char *  begin,
size_t  len 
)

Duplicate a sub-string.

Parameters
beginStart of the string to copy
lenLength of string to copy
Return values
ptrNew string

The caller must free the returned string.

Definition at line 381 of file string.c.

382{
383 if (!begin)
384 return NULL;
385
386 char *p = MUTT_MEM_MALLOC(len + 1, char);
387 memcpy(p, begin, len);
388 p[len] = '\0';
389 return p;
390}
#define MUTT_MEM_MALLOC(n, type)
Definition: memory.h:41
+ Here is the caller graph for this function:

◆ mutt_str_cmp()

int mutt_str_cmp ( const char *  a,
const char *  b 
)

Compare two strings, safely.

Parameters
aFirst string to compare
bSecond string to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 400 of file string.c.

401{
402 return strcmp(NONULL(a), NONULL(b));
403}
#define NONULL(x)
Definition: string2.h:37
+ Here is the caller graph for this function:

◆ mutt_istr_cmp()

int mutt_istr_cmp ( const char *  a,
const char *  b 
)

Compare two strings ignoring case, safely.

Parameters
aFirst string to compare
bSecond string to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 413 of file string.c.

414{
415 return strcasecmp(NONULL(a), NONULL(b));
416}
+ Here is the caller graph for this function:

◆ mutt_strn_equal()

bool mutt_strn_equal ( const char *  a,
const char *  b,
size_t  num 
)

Check for equality of two strings (to a maximum), safely.

Parameters
aFirst string to compare
bSecond string to compare
numMaximum number of bytes to compare
Return values
trueFirst num chars of both strings are equal
falseFirst num chars of both strings not equal

Definition at line 426 of file string.c.

427{
428 return strncmp(NONULL(a), NONULL(b), num) == 0;
429}
+ Here is the caller graph for this function:

◆ mutt_istrn_cmp()

int mutt_istrn_cmp ( const char *  a,
const char *  b,
size_t  num 
)

Compare two strings ignoring case (to a maximum), safely.

Parameters
aFirst string to compare
bSecond string to compare
numMaximum number of bytes to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 440 of file string.c.

441{
442 return strncasecmp(NONULL(a), NONULL(b), num);
443}
+ Here is the caller graph for this function:

◆ mutt_istrn_equal()

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.

Parameters
aFirst string to compare
bSecond string to compare
numMaximum number of bytes to compare
Return values
-1a precedes b
trueFirst num chars of both strings are equal, ignoring case
falseFirst num chars of both strings not equal, ignoring case

Definition at line 454 of file string.c.

455{
456 return strncasecmp(NONULL(a), NONULL(b), num) == 0;
457}
+ Here is the caller graph for this function:

◆ mutt_istrn_rfind()

const char * mutt_istrn_rfind ( const char *  haystack,
size_t  haystack_length,
const char *  needle 
)

Find last instance of a substring, ignoring case.

Parameters
haystackString to search through
haystack_lengthLength of the string
needleString to find
Return values
NULLString not found
ptrLocation of string

Return the last instance of needle in the haystack, or NULL. Like strcasestr(), only backwards, and for a limited haystack length.

Definition at line 470 of file string.c.

471{
472 if (!haystack || (haystack_length == 0) || !needle)
473 return NULL;
474
475 int needle_length = strlen(needle);
476 const char *haystack_end = haystack + haystack_length - needle_length;
477
478 for (const char *p = haystack_end; p >= haystack; p--)
479 {
480 for (size_t i = 0; i < needle_length; i++)
481 {
482 if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
483 goto next;
484 }
485 return p;
486
487 next:;
488 }
489 return NULL;
490}
+ Here is the caller graph for this function:

◆ mutt_str_len()

size_t mutt_str_len ( const char *  a)

Calculate the length of a string, safely.

Parameters
aString to measure
Return values
numLength in bytes

Definition at line 497 of file string.c.

498{
499 return a ? strlen(a) : 0;
500}

◆ mutt_str_coll()

int mutt_str_coll ( const char *  a,
const char *  b 
)

Collate two strings (compare using locale), safely.

Parameters
aFirst string to compare
bSecond string to compare
Return values
<0a precedes b
0a and b are identical
>0b precedes a

Definition at line 510 of file string.c.

511{
512 return strcoll(NONULL(a), NONULL(b));
513}
+ Here is the caller graph for this function:

◆ mutt_istr_find()

const char * mutt_istr_find ( const char *  haystack,
const char *  needle 
)

Find first occurrence of string (ignoring case)

Parameters
haystackString to search through
needleString to find
Return values
ptrFirst match of the search string
NULLNo match, or an error

Definition at line 522 of file string.c.

523{
524 if (!haystack)
525 return NULL;
526 if (!needle)
527 return haystack;
528
529 const char *p = NULL, *q = NULL;
530
531 while (*(p = haystack))
532 {
533 for (q = needle;
534 *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
535 p++, q++)
536 {
537 }
538 if ((*q == '\0'))
539 return haystack;
540 haystack++;
541 }
542 return NULL;
543}
+ Here is the caller graph for this function:

◆ mutt_str_skip_whitespace()

char * mutt_str_skip_whitespace ( const char *  p)

Find the first non-whitespace character in a string.

Parameters
pString to search
Return values
ptr
  • First non-whitespace character
  • Terminating NUL character, if the string was entirely whitespace

Definition at line 552 of file string.c.

553{
554 if (!p)
555 return NULL;
556 SKIPWS(p);
557 return (char *) p;
558}
#define SKIPWS(ch)
Definition: string2.h:45
+ Here is the caller graph for this function:

◆ mutt_str_remove_trailing_ws()

void mutt_str_remove_trailing_ws ( char *  s)

Trim trailing whitespace from a string.

Parameters
sString to trim

The string is modified in place.

Definition at line 566 of file string.c.

567{
568 if (!s)
569 return;
570
571 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && isspace(*p); p--)
572 *p = '\0';
573}
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:497
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_copy()

size_t mutt_str_copy ( char *  dest,
const char *  src,
size_t  dsize 
)

Copy a string into a buffer (guaranteeing NUL-termination)

Parameters
destBuffer for the result
srcString to copy
dsizeDestination buffer size
Return values
numDestination string length

Definition at line 582 of file string.c.

583{
584 if (!dest || (dsize == 0))
585 return 0;
586 if (!src)
587 {
588 dest[0] = '\0';
589 return 0;
590 }
591
592 char *dest0 = dest;
593 while ((--dsize > 0) && (*src != '\0'))
594 *dest++ = *src++;
595
596 *dest = '\0';
597 return dest - dest0;
598}

◆ mutt_str_skip_email_wsp()

char * mutt_str_skip_email_wsp ( const char *  s)

Skip over whitespace as defined by RFC5322.

Parameters
sString to search
Return values
ptr
  • First non-whitespace character
  • Terminating NUL character, if the string was entirely whitespace

This is used primarily for parsing header fields.

Definition at line 609 of file string.c.

610{
611 if (!s)
612 return NULL;
613
614 for (; mutt_str_is_email_wsp(*s); s++)
615 ; // Do nothing
616
617 return (char *) s;
618}
static bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string2.h:104
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_lws_len()

size_t mutt_str_lws_len ( const char *  s,
size_t  n 
)

Measure the linear-white-space at the beginning of a string.

Parameters
sString to check
nMaximum number of characters to check
Return values
numCount of whitespace characters

Count the number of whitespace characters at the beginning of a string. They can be <space>, <tab>, <cr> or <lf>.

Definition at line 629 of file string.c.

630{
631 if (!s)
632 return 0;
633
634 const char *p = s;
635 size_t len = n;
636
637 if (n == 0)
638 return 0;
639
640 for (; p < (s + n); p++)
641 {
642 if (!strchr(" \t\r\n", *p))
643 {
644 len = p - s;
645 break;
646 }
647 }
648
649 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
650 len = 0;
651 return len;
652}
+ Here is the caller graph for this function:

◆ mutt_str_equal()

bool mutt_str_equal ( const char *  a,
const char *  b 
)

Compare two strings.

Parameters
aFirst string
bSecond string
Return values
trueThe strings are equal
falseThe strings are not equal

Definition at line 661 of file string.c.

662{
663 return (a == b) || (mutt_str_cmp(a, b) == 0);
664}
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:400
+ Here is the call graph for this function:

◆ mutt_istr_equal()

bool mutt_istr_equal ( const char *  a,
const char *  b 
)

Compare two strings, ignoring case.

Parameters
aFirst string
bSecond string
Return values
trueThe strings are equal
falseThe strings are not equal

Definition at line 673 of file string.c.

674{
675 return (a == b) || (mutt_istr_cmp(a, b) == 0);
676}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:413
+ Here is the call graph for this function:

◆ mutt_str_is_ascii()

bool mutt_str_is_ascii ( const char *  str,
size_t  len 
)

Is a string ASCII (7-bit)?

Parameters
strString to examine
lenLength of string to examine
Return values
trueThere are no 8-bit chars

Definition at line 684 of file string.c.

685{
686 if (!str)
687 return true;
688
689 for (; (*str != '\0') && (len > 0); str++, len--)
690 if ((*str & 0x80) != 0)
691 return false;
692
693 return true;
694}
+ Here is the caller graph for this function:

◆ mutt_str_find_word()

const char * mutt_str_find_word ( const char *  src)

Find the end of a word (non-space)

Parameters
srcString to search
Return values
ptrEnd of the word

Skip to the end of the current word. Skip past any whitespace characters.

Note
If there aren't any more words, this will return a pointer to the final NUL character.

Definition at line 707 of file string.c.

708{
709 if (!src)
710 return NULL;
711
712 while (*src && strchr(" \t\n", *src))
713 src++;
714 while (*src && !strchr(" \t\n", *src))
715 src++;
716 return src;
717}
+ Here is the caller graph for this function:

◆ mutt_str_getenv()

const char * mutt_str_getenv ( const char *  name)

Get an environment variable.

Parameters
nameEnvironment variable to get
Return values
ptrValue of variable
NULLVariable isn't set, or is empty
Warning
The caller must not free the returned pointer.

Definition at line 727 of file string.c.

728{
729 if (!name)
730 return NULL;
731
732 const char *val = getenv(name);
733 if (val && (val[0] != '\0'))
734 return val;
735
736 return NULL;
737}
+ Here is the caller graph for this function:

◆ mutt_istr_remall()

int mutt_istr_remall ( char *  str,
const char *  target 
)

Remove all occurrences of substring, ignoring case.

Parameters
strString containing the substring
targetTarget substring for removal
Return values
0String contained substring and substring was removed successfully
1String did not contain substring

Definition at line 746 of file string.c.

747{
748 int rc = 1;
749 if (!str || !target)
750 return rc;
751
752 // Look through an ensure all instances of the substring are gone.
753 while ((str = (char *) strcasestr(str, target)))
754 {
755 size_t target_len = mutt_str_len(target);
756 memmove(str, str + target_len, 1 + strlen(str + target_len));
757 rc = 0; // If we got here, then a substring existed and has been removed.
758 }
759
760 return rc;
761}
static char * strcasestr(const char *haystack, const char *needle)
Find the first occurrence of needle in haystack, ignoring case.
Definition: string.c:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_asprintf()

int mutt_str_asprintf ( char **  strp,
const char *  fmt,
  ... 
)

Definition at line 804 of file string.c.

805{
806 if (!strp || !fmt)
807 return -1;
808
809 int rlen = 256;
810
811 *strp = MUTT_MEM_MALLOC(rlen, char);
812 while (true)
813 {
814 va_list ap;
815 va_start(ap, fmt);
816 const int n = vsnprintf(*strp, rlen, fmt, ap);
817 va_end(ap);
818 if (n < 0)
819 {
820 FREE(strp);
821 return n;
822 }
823
824 if (n < rlen)
825 {
826 /* reduce space to just that which was used. note that 'n' does not
827 * include the terminal nul char. */
828 if (n == 0) /* convention is to use NULL for zero-length strings. */
829 FREE(strp);
830 else if (n != rlen - 1)
831 MUTT_MEM_REALLOC(strp, n + 1, char);
832 return n;
833 }
834 /* increase size and try again */
835 rlen = n + 1;
836 MUTT_MEM_REALLOC(strp, rlen, char);
837 }
838 /* not reached */
839}
+ Here is the caller graph for this function:

◆ mutt_str_hyphenate()

void mutt_str_hyphenate ( char *  buf,
size_t  buflen,
const char *  str 
)

Hyphenate a snake-case string.

Parameters
bufBuffer for the result
buflenLength of the buffer
strString to convert

Replace underscores (_) with hyphens -`).

Definition at line 850 of file string.c.

851{
852 if (!buf || (buflen == 0) || !str)
853 return;
854
855 mutt_str_copy(buf, str, buflen);
856 for (; *buf != '\0'; buf++)
857 {
858 if (*buf == '_')
859 *buf = '-';
860 }
861}
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:582
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ SysExits

const struct SysExits SysExits[]
static

Lookup table of error messages.

Definition at line 116 of file string.c.