Annotation of embedaddon/pimd/libite/lite.h, revision 1.1
1.1 ! misho 1: /* Collection of frog DNA
! 2: *
! 3: * Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
! 4: * Copyright (c) 2008-2016 Joachim Nilsson <troglobit@gmail.com>
! 5: *
! 6: * Permission is hereby granted, free of charge, to any person obtaining a copy
! 7: * of this software and associated documentation files (the "Software"), to deal
! 8: * in the Software without restriction, including without limitation the rights
! 9: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! 10: * copies of the Software, and to permit persons to whom the Software is
! 11: * furnished to do so, subject to the following conditions:
! 12: *
! 13: * The above copyright notice and this permission notice shall be included in
! 14: * all copies or substantial portions of the Software.
! 15: *
! 16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! 17: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! 18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! 19: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! 20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! 21: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
! 22: * THE SOFTWARE.
! 23: */
! 24:
! 25: #ifndef LITE_H_
! 26: #define LITE_H_
! 27:
! 28: #include <err.h>
! 29: #include <stdio.h>
! 30: #include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
! 31: #include <string.h>
! 32: #include <sys/stat.h>
! 33: #include <sys/types.h>
! 34: #include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
! 35: #include <unistd.h>
! 36:
! 37: typedef struct lfile lfile_t;
! 38:
! 39: char *chomp (char *str);
! 40:
! 41: int fexist (char *file);
! 42: int fisdir (char *file);
! 43: mode_t fmode (char *file);
! 44:
! 45: FILE *tempfile (void);
! 46: ssize_t copyfile (char *src, char *dst, int len, int sym);
! 47: int movefile (char *src, char *dst);
! 48: int fcopyfile (FILE *src, FILE *dst);
! 49: size_t fsendfile (FILE *src, FILE *dst, size_t len);
! 50:
! 51: int ifconfig (char *ifname, char *addr, char *mask, int up);
! 52:
! 53: lfile_t*lfopen (char *file, char *sep);
! 54: void lfclose (lfile_t *lf);
! 55: char *lftok (lfile_t *lf);
! 56: char *lfgetkey (lfile_t *lf, char *key);
! 57: int lfgetint (lfile_t *lf, char *key);
! 58: int fgetint (char *file, char *sep, char *key);
! 59:
! 60: int mkpath (char *dir, mode_t mode);
! 61: int makepath (char *dir);
! 62:
! 63: int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
! 64: int rsync (char *src, char *dst, int delete, int (*filter) (const char *file));
! 65:
! 66: int pidfile (const char *basename);
! 67: int pidfile_signal(const char *pidfile, int signal);
! 68: pid_t pidfile_read (const char *pidfile);
! 69: pid_t pidfile_poll (const char *pidfile);
! 70:
! 71: #ifndef strlcpy
! 72: size_t strlcpy (char *dst, const char *src, size_t siz);
! 73: #endif
! 74: #ifndef strlcat
! 75: size_t strlcat (char *dst, const char *src, size_t siz);
! 76: #endif
! 77: #ifndef strtonum
! 78: long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
! 79: #endif
! 80:
! 81: int tree(char *path, int show_perms);
! 82:
! 83: #ifndef touch
! 84: # define touch(x) do { if (mknod((x), S_IFREG|0644, 0) && errno != EEXIST) warn("Failed creating %s", x); } while (0)
! 85: #endif
! 86: #ifndef makedir
! 87: # define makedir(x, p) do { if (mkdir(x, p) && errno != EEXIST) warn("Failed creating directory %s", x); } while (0)
! 88: #endif
! 89: #ifndef makefifo
! 90: # define makefifo(x, p) do { if (mkfifo(x, p) && errno != EEXIST) warn("Failed creating FIFO %s", x); } while (0)
! 91: #endif
! 92: #ifndef erase
! 93: # define erase(x) do { if (remove(x) && errno != ENOENT) warn("Failed removing %s", x); } while (0)
! 94: #endif
! 95: #ifndef chardev
! 96: # define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
! 97: #endif
! 98: #ifndef blkdev
! 99: # define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min)))
! 100: #endif
! 101:
! 102: #ifndef S_ISEXEC
! 103: # define S_ISEXEC(m) (((m) & S_IXUSR) == S_IXUSR)
! 104: #endif
! 105:
! 106: /* Unline isset(), setbit() et al, these work with integers/shorts/longwords/etc. */
! 107: #ifndef ISCLR
! 108: #define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1))
! 109: #endif
! 110: #ifndef ISSET
! 111: #define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0))
! 112: #endif
! 113: #ifndef ISOTHER
! 114: #define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0)) /* Is any other bit set? */
! 115: #endif
! 116: #ifndef SETBIT
! 117: #define SETBIT(word,bit) (word |= (1 << (bit)))
! 118: #endif
! 119: #ifndef CLRBIT
! 120: #define CLRBIT(word,bit) (word &= ~(1 << (bit)))
! 121: #endif
! 122:
! 123: /* From The Practice of Programming, by Kernighan and Pike */
! 124: #ifndef NELEMS
! 125: #define NELEMS(array) (sizeof(array) / sizeof(array[0]))
! 126: #endif
! 127:
! 128: /* Mark a function variable as unused, useful for generic callbacks */
! 129: #ifndef UNUSED
! 130: #define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
! 131: #endif
! 132:
! 133: /* Does directory end with a slash? */
! 134: static inline int fisslashdir(char *dir)
! 135: {
! 136: if (!dir)
! 137: return 0;
! 138:
! 139: if (strlen(dir) > 0)
! 140: return dir[strlen(dir) - 1] == '/';
! 141:
! 142: return 0;
! 143: }
! 144:
! 145: /* Convert string to natural number (0-2147483647), returns -1 on error. */
! 146: static inline int atonum(const char *str)
! 147: {
! 148: int val = -1;
! 149: const char *errstr;
! 150:
! 151: if (str) {
! 152: val = strtonum(str, 0, INT32_MAX, &errstr);
! 153: if (errstr)
! 154: return -1;
! 155: }
! 156:
! 157: return val;
! 158: }
! 159:
! 160: /* Validate string, non NULL and not zero length */
! 161: static inline int string_valid(const char *s)
! 162: {
! 163: return s && strlen(s);
! 164: }
! 165:
! 166: /* Relaxed comparison, e.g., sys_string_match("small", "smaller") => TRUE */
! 167: static inline int string_match(const char *a, const char *b)
! 168: {
! 169: size_t min = MIN(strlen(a), strlen(b));
! 170:
! 171: return !strncasecmp(a, b, min);
! 172: }
! 173:
! 174: /* Strict comparison, e.g., sys_string_match("small", "smaller") => FALSE */
! 175: static inline int string_compare (const char *a, const char *b)
! 176: {
! 177: return strlen(a) == strlen(b) && !strcmp(a, b);
! 178: }
! 179:
! 180: /* Strict comparison, like sys_string_compare(), but case insensitive,
! 181: * e.g., sys_string_match("small", "SmAlL") => TRUE
! 182: */
! 183: static inline int string_case_compare (const char *a, const char *b)
! 184: {
! 185: return strlen (a) == strlen (b) && !strcasecmp (a, b);
! 186: }
! 187:
! 188: #define min(a,b) \
! 189: ({ \
! 190: __typeof__ (a) _a = (a); \
! 191: __typeof__ (b) _b = (b); \
! 192: _a < _b ? _a : _b; \
! 193: })
! 194:
! 195: #define max(a,b) \
! 196: ({ \
! 197: __typeof__ (a) _a = (a); \
! 198: __typeof__ (b) _b = (b); \
! 199: _a > _b ? _a : _b; \
! 200: })
! 201:
! 202: /* Compat */
! 203: #define copy_filep(src,dst) fcopyfile(src,dst)
! 204: #define pidfile_read_pid(file) pifile_read(file)
! 205: #define signal_pidfile(file,signo) pidfile_signal(file,signo)
! 206:
! 207: #endif /* LITE_H_ */
! 208:
! 209: /**
! 210: * Local Variables:
! 211: * version-control: t
! 212: * indent-tabs-mode: t
! 213: * c-file-style: "linux"
! 214: * End:
! 215: */
! 216:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>