File: util.c

package info (click to toggle)
fetchmail 6.6.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,888 kB
  • sloc: ansic: 19,454; sh: 7,111; python: 2,395; perl: 564; yacc: 447; lex: 286; makefile: 261; awk: 124; lisp: 84; exp: 43; sed: 17
file content (72 lines) | stat: -rw-r--r-- 2,509 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/** \file util.c -- utility functions
 * \copyright 2025 Matthias Andree, GNU GPL v2+
 */

#define _XOPEN_SOURCE 600
#include "util.h"
#include <string.h>

size_t str_split(char *str, const char *delim, char *outarray[], const size_t maxsplit, char **remainder)
{
        char *saveptr = NULL;
        char *startptr = str;
        char *curptr = NULL;
        size_t i;
        for (i = 0; i < maxsplit; ++i) {
                curptr = strtok_r(startptr, delim, &saveptr);
                outarray[i] = curptr;
                if (!curptr) break;
                startptr = NULL; // null this for subsequent strtok_r calls, which will look at saveptr
        }
        if (maxsplit == i && remainder && curptr) {
                *remainder = strtok_r(NULL, "", &saveptr);
        }
        return i;
}

bool str_startswith(const char *str /** string to check for \a prefix */, 
        const char *prefix /** prefix of string to check */)
{
        return 0 == strncmp(str, prefix, strlen(prefix));
}

#if defined(TEST)
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "xmalloc.h"

const char *program_name = "util";

static int test_split(int argc, char **argv) {
        int rc = EXIT_SUCCESS;
        for (int i = 0; i+2 < argc; i+=3) {
                char *endptr = NULL;
                errno = 0;
                unsigned long maxsplit = strtoul(argv[i+2], &endptr, 0);
                if (!maxsplit || ULONG_MAX == maxsplit || errno || *endptr || !*argv[i+2]) {
                        fprintf(stderr, "maxsplit invalid, skipping...\n");
                        rc = EXIT_FAILURE;
                        continue;
                }
                char **ary = (char **)xmalloc(sizeof(char *) * maxsplit);
                char *remainder = 0;
                size_t got = str_split(argv[i], argv[i+1], ary, maxsplit, &remainder);
                printf("str_split(\"%s\", \"%s\", ARRAY, %zu) returned %zu, remainder \"%s\"\n",
                        argv[i], argv[i+1], maxsplit, got, remainder ? remainder : "(NULL)");
                for (size_t j = 0; j < got; j++) printf("  %3zu %s\n", j, ary[j]);
                xfree(ary);
        }
        return rc;
}

int main(int argc, char **argv) {
        if (argc < 2) {
usage:
                fprintf(stderr, "Usage: %s SPLIT string delim num [...]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        if (!strcmp("SPLIT", argv[1])) return test_split(argc-2, argv+2); else goto usage;
}
#endif