summaryrefslogtreecommitdiff
path: root/utils.c
blob: 82151740518bdadc8df93660f7a326c08b899a55 (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
/*
 * Created by yuuta on 3/19/23.
 */

#include "utils.h"

#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>

uint32_t tg_url_get_index(const char *url) {
    char *tok = strrchr(url, '/');

    if (!tok) {
        return 0;
    }

    char *endptr;
    uintmax_t unum = strtoumax(tok + 1, &endptr, 10);
    if (strcmp(endptr, "") != 0 || (unum == UINTMAX_MAX && errno == ERANGE)) {
        return 0;
    }
    if (unum > UINT32_MAX) {
        return 0;
    }
    return (uint32_t) unum;
}