blob: c877cebd1c21eb215aec39d0b87891f3da711d04 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file compat.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Compatibility functions - implemented basic functions which are not available on all the platforms.
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <string.h>
Radek Krejcia9dc4ab2019-05-16 11:13:17 +020018#include <unistd.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019
20#ifndef HAVE_STRNSTR
Radek Krejcie7b95092019-05-15 11:03:07 +020021char *
22strnstr(const char *s, const char *find, size_t slen)
23{
24 char c, sc;
25 size_t len;
26
27 if ((c = *find++) != '\0') {
28 len = strlen(find);
29 do {
30 do {
31 if (slen-- < 1 || (sc = *s++) == '\0')
32 return (NULL);
33 } while (sc != c);
34 if (len > slen)
35 return (NULL);
36 } while (strncmp(s, find, len) != 0);
37 s--;
38 }
39 return ((char *)s);
40}
Radek Krejcia9dc4ab2019-05-16 11:13:17 +020041#endif
Radek Krejcie7b95092019-05-15 11:03:07 +020042
Radek Krejcia9dc4ab2019-05-16 11:13:17 +020043#ifndef HAVE_GET_CURRENT_DIR_NAME
44char *
45get_current_dir_name(void)
46{
47 char tmp[PATH_MAX];
48 char *retval;
49
50 if (getcwd(tmp, sizeof(tmp))) {
51 retval = strdup(tmp);
52 LY_CHECK_ERR_RET(!retval, LOGMEM(NULL), NULL);
53 return retval;
54 }
55 return NULL;
56}
Radek Krejcie7b95092019-05-15 11:03:07 +020057#endif