Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | a9dc4ab | 2019-05-16 11:13:17 +0200 | [diff] [blame^] | 18 | #include <unistd.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | |
| 20 | #ifndef HAVE_STRNSTR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | char * |
| 22 | strnstr(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 Krejci | a9dc4ab | 2019-05-16 11:13:17 +0200 | [diff] [blame^] | 41 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 42 | |
Radek Krejci | a9dc4ab | 2019-05-16 11:13:17 +0200 | [diff] [blame^] | 43 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 44 | char * |
| 45 | get_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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 57 | #endif |