blob: 9a12537440f8b186ced2314c104d7559dc3ee653 [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
Radek Krejcica376bd2020-06-11 16:04:06 +020017#ifndef HAVE_GET_CURRENT_DIR_NAME
18# include <limits.h>
19# include <unistd.h>
20#endif
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <string.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022
23#include "config.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024
25#ifndef HAVE_STRNSTR
Radek Krejcie7b95092019-05-15 11:03:07 +020026char *
27strnstr(const char *s, const char *find, size_t slen)
28{
29 char c, sc;
30 size_t len;
31
32 if ((c = *find++) != '\0') {
33 len = strlen(find);
34 do {
35 do {
36 if (slen-- < 1 || (sc = *s++) == '\0')
37 return (NULL);
38 } while (sc != c);
39 if (len > slen)
40 return (NULL);
41 } while (strncmp(s, find, len) != 0);
42 s--;
43 }
44 return ((char *)s);
45}
Radek Krejcia9dc4ab2019-05-16 11:13:17 +020046#endif
Radek Krejcie7b95092019-05-15 11:03:07 +020047
Radek Krejcia9dc4ab2019-05-16 11:13:17 +020048#ifndef HAVE_GET_CURRENT_DIR_NAME
49char *
50get_current_dir_name(void)
51{
52 char tmp[PATH_MAX];
53 char *retval;
54
55 if (getcwd(tmp, sizeof(tmp))) {
56 retval = strdup(tmp);
57 LY_CHECK_ERR_RET(!retval, LOGMEM(NULL), NULL);
58 return retval;
59 }
60 return NULL;
61}
Radek Krejcie7b95092019-05-15 11:03:07 +020062#endif