summaryrefslogtreecommitdiffstats
path: root/src/utils.c
blob: 2607fc6cf32d73a4b5f66edfc983ebdab9577430 (plain)
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
/*
 * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */


#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include "utils.h"

void str_toupper(char *str)
{
       char *c = str;

       while (*c) {
               *c = toupper(*c);
               c++;
       }
}

void str_tolower(char *str)
{
       char *c = str;

       while (*c) {
               *c = tolower(*c);
               c++;
       }
}

int safe_strtoull(const char *str, uint64_t *out_value, int base, uint64_t max)
{
        char *endptr = NULL;
        uint64_t result;
        errno = 0;

        assert(str != NULL && out_value != NULL);

        if (str[0] == '-')
                return -1;

        result = strtoull(str, &endptr, base);

        if (endptr == str ||
            *endptr != '\0' ||
            (errno == ERANGE && result == ULLONG_MAX) ||
            result > max)
                return -1;

        *out_value = result;
        return 0;
}