1 /* 2 * SPDX-License-Identifier: pkgconf 3 * 4 * Copyright (c) 2012 Ariadne Conill <ariadne@dereferenced.org> 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * This software is provided 'as is' and without any warranty, express or 11 * implied. In no event shall the authors be liable for any damages arising 12 * from the use of this software. 13 */ 14 15 #include <libpkgconf/stdinc.h> 16 #include <libpkgconf/bsdstubs.h> 17 #include <libpkgconf/config.h> 18 19 #if HAVE_DECL_STRNDUP 20 # define pkgconf_strndup_impl strndup 21 #else 22 /* 23 * Creates a memory buffer and copies at most 'len' characters to it. 24 * If 'len' is less than the length of the source string, truncation occured. 25 */ 26 static inline char * 27 pkgconf_strndup_impl(const char *src, size_t len) 28 { 29 const char *end = memchr(src, '\0', len); 30 size_t n = end != NULL ? (size_t)(end - src) : len; 31 char *out = malloc(n + 1); 32 33 if (out == NULL) 34 return NULL; 35 36 memcpy(out, src, n); 37 out[n] = '\0'; 38 39 return out; 40 } 41 #endif 42 43 #if HAVE_DECL_PLEDGE 44 # define pkgconf_pledge_impl pledge 45 #else 46 static inline int 47 pkgconf_pledge_impl(const char *promises, const char *execpromises) 48 { 49 (void) promises; 50 (void) execpromises; 51 52 return 0; 53 } 54 #endif 55 56 #if HAVE_DECL_UNVEIL 57 # define pkgconf_unveil_impl unveil 58 #else 59 static inline int 60 pkgconf_unveil_impl(const char *path, const char *permissions) 61 { 62 (void) path; 63 (void) permissions; 64 65 return 0; 66 } 67 #endif 68 69 #if HAVE_DECL_REALLOCARRAY 70 # define pkgconf_reallocarray_impl reallocarray 71 #else 72 static inline void * 73 pkgconf_reallocarray_impl(void *ptr, size_t m, size_t n) 74 { 75 if (n && m > -1 / n) 76 { 77 errno = ENOMEM; 78 return 0; 79 } 80 81 return realloc(ptr, m * n); 82 } 83 #endif 84 85 char * 86 pkgconf_strndup(const char *src, size_t len) 87 { 88 return pkgconf_strndup_impl(src, len); 89 } 90 91 void * 92 pkgconf_reallocarray(void *ptr, size_t m, size_t n) 93 { 94 return pkgconf_reallocarray_impl(ptr, m, n); 95 } 96 97 int 98 pkgconf_pledge(const char *promises, const char *execpromises) 99 { 100 return pkgconf_pledge_impl(promises, execpromises); 101 } 102 103 int 104 pkgconf_unveil(const char *path, const char *permissions) 105 { 106 return pkgconf_unveil_impl(path, permissions); 107 } 108