1 /* 2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors 3 * Copyright (C) 2004-2021 Kim Woelders 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to 7 * deal in the Software without restriction, including without limitation the 8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 * sell copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies of the Software, its documentation and marketing & publicity 14 * materials, and acknowledgment shall be given in the documentation, materials 15 * and software packages that this Software was used. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 #ifndef _UTIL_H_ 25 #define _UTIL_H_ 26 27 #include "config.h" 28 29 #include <stdarg.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 34 #ifndef M_PI 35 #define M_PI 3.14159265358979323846 36 #endif 37 38 #define VERS(maj, min) (1000 * (maj) + (min)) 39 40 #define INT2PTR(i) ((void*)(long)(i)) 41 #define PTR2INT(p) ((int)(long)(p)) 42 43 #if HAVE___ATTRIBUTE__ 44 #define __PRINTF_N__(no) __attribute__((__format__(__printf__, (no), (no)+1))) 45 #define __NORETURN__ __attribute__((noreturn)) 46 #else 47 #define __PRINTF_N__(no) 48 #define __NORETURN__ 49 #endif 50 #define __PRINTF__ __PRINTF_N__(1) 51 #define __PRINTF_2__ __PRINTF_N__(2) 52 #define __PRINTF_5__ __PRINTF_N__(5) 53 54 #if HAVE_STRDUP 55 #define USE_LIBC_STRDUP 1 /* Use libc strdup if present */ 56 #endif 57 #if HAVE_STRNDUP 58 #define USE_LIBC_STRNDUP 1 /* Use libc strndup if present */ 59 #endif 60 61 #ifdef HAVE_STRCASECMP 62 #define Estrcasecmp(s1, s2) strcasecmp(s1, s2) 63 #else 64 int Estrcasecmp(const char *s1, const char *s2); 65 #endif 66 #ifdef HAVE_STRCASESTR 67 #define Estrcasestr(haystack, needle) strcasestr(haystack, needle) 68 #else 69 const char *Estrcasestr(const char *haystack, const char *needle); 70 #endif 71 72 /* memory.c */ 73 #define Ecalloc calloc 74 #define Emalloc malloc 75 #define Erealloc realloc 76 #if HAVE_FREE_NULL_BUG 77 #define Efree(p) if (p) free(p) 78 #else 79 #define Efree free 80 #endif 81 82 #define ECALLOC(type, num) (type*)Ecalloc(num, sizeof(type)) 83 #define EMALLOC(type, num) (type*)Emalloc((num)*sizeof(type)) 84 #define EREALLOC(type, ptr, num) (type*)Erealloc(ptr, (num)*sizeof(type)) 85 86 void EfreeNull(void **p); 87 void EfreeSet(void **p, void *s); 88 void EfreeDup(char **p, const char *s); 89 90 #define EFREE_NULL(p) EfreeNull((void**)(&p)) 91 #define EFREE_SET(p, s) EfreeSet((void**)(&p), s) 92 #define EFREE_DUP(p, s) EfreeDup(&p, s) 93 94 #define STRCPY(dst, src) do { src[sizeof(dst)-1] = '\0'; strcpy(dst, src); } while(0) 95 96 char *Estrtrim(char *s); 97 98 char *Estrdup(const char *s); 99 char *Estrndup(const char *s, size_t n); 100 char *Estrdupcat2(char *ss, const char *s1, const char *s2); 101 102 char **StrlistDup(char **lst, int num); 103 void StrlistFree(char **lst, int num); 104 char *StrlistJoin(char **lst, int num); 105 char *StrlistEncodeEscaped(char *buf, int len, char **lst, 106 int num); 107 char **StrlistDecodeEscaped(const char *str, int *pnum); 108 char **StrlistFromString(const char *str, int delim, int *num); 109 110 void StrlistSort(char **lst, int num); 111 112 void Esetenv(const char *name, const char *value); 113 114 /* misc.c */ 115 void __PRINTF__ Eprintf(const char *fmt, ...); 116 117 #define Evsnprintf vsnprintf 118 #define Esnprintf snprintf 119 120 #define EXEC_SET_LANG 0x01 121 #define EXEC_SET_STARTUP_ID 0x02 122 123 void Eexec(const char *cmd); 124 int EspawnApplication(const char *params, int flags); 125 void __PRINTF__ Espawn(const char *fmt, ...); 126 int __PRINTF__ Esystem(const char *fmt, ...); 127 128 #if USE_MODULES 129 /* Dynamic loading */ 130 const void *ModLoadSym(const char *lib, const char *sym, 131 const char *name); 132 #endif 133 134 unsigned int GetTimeMs(void); 135 unsigned int GetTimeUs(void); 136 137 void SleepUs(unsigned int tus); 138 139 #define E_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 140 141 #endif /* _UTIL_H_ */ 142