libite 2.6.1
lite.h
Go to the documentation of this file.
1/* Collection of frog DNA
2 *
3 * Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
4 * Copyright (c) 2008-2023 Joachim Wiberg <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
35
36#ifdef __cplusplus
37extern "C"
38{
39#endif
40
41#ifndef LIBITE_LITE_H_
42#define LIBITE_LITE_H_
43
44#include <errno.h>
45#include <fcntl.h> /* Definition of AT_* constants for utimensat() */
46#include <stdio.h>
47#include <stdarg.h>
48#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
49#include <sys/stat.h>
50#include <sys/types.h>
51#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
52#include <unistd.h>
53#include "strlite.h"
54
55/*
56 * fparseln() specific operation flags.
57 */
58#define FPARSELN_UNESCESC 0x01
59#define FPARSELN_UNESCCONT 0x02
60#define FPARSELN_UNESCCOMM 0x04
61#define FPARSELN_UNESCREST 0x08
62#define FPARSELN_UNESCALL 0x0f
63
64/*
65 * copyfile() and rsync() option flags
66 */
67#define LITE_FOPT_RSYNC_DELETE 0x01
68#define LITE_FOPT_COPYFILE_SYM 0x01
69#define LITE_FOPT_KEEP_MTIME 0x02
70
71typedef struct lfile lfile_t;
72typedef struct sdbuf sdbuf_t;
73
74char *chomp (char *str);
75
76int erasef (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
77
78char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
79
80int fexist (const char *file);
81int fexistf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));;
82int fisdir (const char *path);
83
84FILE *vfopenf (const char *mode, const char *fmt, va_list ap);
85FILE *fopenf (const char *mode, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
86int fremove (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
87FILE *tempfile (void);
88ssize_t copyfile (const char *src, const char *dst, int len, int opt);
89int movefile (const char *src, const char *dst);
90int fcopyfile (FILE *src, FILE *dst);
91ssize_t fsendfile (FILE *src, FILE *dst, size_t len);
92int truncatef (off_t length, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
93
94char *vreadsnf (char *line, size_t len, const char *fmt, va_list ap);
95char *readsnf (char *line, size_t len, const char *fmt, ...);
96int writesf (const char *str, const char *mode, const char *fmt, ...);
97
98int vreadllf (long long *value, const char *fmt, va_list ap);
99int readllf (long long *value, const char *fmt, ...);
100int readdf (int *value, const char *fmt, ...);
101int writellf (long long value, const char *mode, const char *fmt, ...);
102int writedf (int value, const char *mode, const char *fmt, ...);
103
104int ifconfig (const char *ifname, const char *addr, const char *mask, int up);
105
106lfile_t*lfopen (const char *file, const char *sep);
107void lfclose (lfile_t *lf);
108char *lftok (lfile_t *lf);
109char *lfgetkey (lfile_t *lf, const char *key);
110int lfgetint (lfile_t *lf, const char *key);
111int fgetint (const char *file, const char *sep, const char *key);
112
113int fmkpath (mode_t mode, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
114int mkpath (const char *dir, mode_t mode);
115int makepath (const char *dir);
116
117int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
118int rsync (char *src, char *dst, int opt, int (*filter) (const char *file));
119
120FILE *popenf (const char *type, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
121
122int pidfile (const char *basename);
123int pidfile_signal(const char *pidfile, int signal);
124pid_t pidfile_read (const char *pidfile);
125pid_t pidfile_poll (const char *pidfile);
126
127void *reallocarray(void *optr, size_t nmemb, size_t size);
128
129void progress (int percent, int max_width);
130void progress_simple(int percent);
131
132int runbg (char *const cmd[], int delay);
133int systemf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
134
135sdbuf_t*telnet_open (int addr, short port);
136int telnet_close (sdbuf_t *ctx);
137int telnet_expect (sdbuf_t *ctx, char *script[], FILE *output);
138int telnet_session (int addr, short port, char *script[], FILE *output);
139
140int touchf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
141
142int yorn (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
143
144char *which (const char *cmd);
145int whichp (const char *cmd);
146
147#ifndef touch
148#include <sys/stat.h> /* utimensat() */
149#include <sys/time.h> /* utimensat() on *BSD */
156static inline int touch(const char *path)
157{
158 int fd;
159
160 if (utimensat(AT_FDCWD, path, NULL, 0)) {
161 if (errno != ENOENT)
162 return -1;
163 fd = creat(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
164 if (fd < 0)
165 return -1;
166 close(fd);
167 }
168 return 0;
169}
170#endif
171#ifndef makedir
178static inline int makedir(const char *path, mode_t mode)
179{
180 if (mkdir(path, mode) && errno != EEXIST)
181 return -1;
182 return 0;
183}
184#endif
185#ifndef makefifo
192static inline int makefifo(const char *path, mode_t mode)
193{
194 if (mkfifo(path, mode) && errno != EEXIST)
195 return -1;
196 return 0;
197}
198#endif
199#ifndef erase
206static inline int erase(const char *path)
207{
208 if (remove(path) && errno != ENOENT)
209 return -1;
210 return 0;
211}
212#endif
213
214#ifndef chardev
215# define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
216#endif
217#ifndef blkdev
218# define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min)))
219#endif
220
221#ifndef S_ISEXEC
222# define S_ISEXEC(m) (((m) & S_IXUSR) == S_IXUSR)
223#endif
224
225/* Unline isset(), setbit() et al, these work with integers/shorts/longwords/etc. */
226#ifndef ISCLR
227#define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1))
228#endif
229#ifndef ISSET
230#define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0))
231#endif
232#ifndef ISOTHER
233#define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0))
234#endif
235#ifndef SETBIT
236#define SETBIT(word,bit) (word |= (1 << (bit)))
237#endif
238#ifndef CLRBIT
239#define CLRBIT(word,bit) (word &= ~(1 << (bit)))
240#endif
241
242/* From The Practice of Programming, by Kernighan and Pike */
243#ifndef NELEMS
244#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
245#endif
246
253static inline int fisslashdir(const char *dir)
254{
255 if (!dir)
256 return 0;
257
258 if (strlen(dir) > 0)
259 return dir[strlen(dir) - 1] == '/';
260
261 return 0;
262}
263
264/* Compat */
265#define copy_filep(src, dst) fcopyfile(src, dst)
266#define pidfile_read_pid(file) pidfile_read(file)
267#define signal_pidfile(file, signo) pidfile_signal(file, signo)
268
269#endif /* LIBITE_LITE_H_ */
270
271#ifdef __cplusplus
272}
273#endif
274
281
int movefile(const char *src, const char *dst)
Definition copyfile.c:244
int fcopyfile(FILE *src, FILE *dst)
Definition copyfile.c:278
ssize_t copyfile(const char *src, const char *dst, int len, int opt)
Definition copyfile.c:144
int dir(const char *dir, const char *type, int(*filter)(const char *file), char ***list, int strip)
Definition dir.c:82
int erasef(const char *fmt,...)
Definition erasef.c:44
int fexistf(const char *fmt,...)
Definition fexist.c:68
int fexist(const char *file)
Definition fexist.c:45
int fisdir(const char *path)
Definition fisdir.c:40
FILE * fopenf(const char *mode, const char *fmt,...)
Definition fopenf.c:75
FILE * vfopenf(const char *mode, const char *fmt, va_list ap)
Definition fopenf.c:40
char * fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
Definition fparseln.c:94
int fremove(const char *fmt,...)
Definition fremove.c:40
ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
Definition fsendfile.c:51
int ifconfig(const char *ifname, const char *addr, const char *mask, int up)
Definition ifconfig.c:53
char * lftok(lfile_t *lf)
Definition lfile.c:92
int lfgetint(lfile_t *lf, const char *key)
Definition lfile.c:156
void lfclose(lfile_t *lf)
Definition lfile.c:75
lfile_t * lfopen(const char *file, const char *sep)
Definition lfile.c:46
int fgetint(const char *file, const char *sep, const char *key)
Definition lfile.c:177
char * lfgetkey(lfile_t *lf, const char *key)
Definition lfile.c:131
int telnet_session(int addr, short port, char *script[], FILE *output)
Definition telnet.c:295
FILE int pidfile(const char *basename)
Definition pidfile.c:71
int telnet_expect(sdbuf_t *ctx, char *script[], FILE *output)
Definition telnet.c:174
pid_t pidfile_poll(const char *pidfile)
Definition pidfilefn.c:89
FILE int FILE * tempfile(void)
Definition tempfile.c:59
void progress(int percent, int max_width)
Definition progress.c:66
int int char * which(const char *cmd)
Definition which.c:54
void progress_simple(int percent)
Definition progress.c:99
int char * vreadsnf(char *line, size_t len, const char *fmt, va_list ap)
Definition procval.c:44
int writesf(const char *str, const char *mode, const char *fmt,...)
Definition procval.c:94
int pidfile_signal(const char *pidfile, int signal)
Definition pidfilefn.c:114
int sdbuf_t * telnet_open(int addr, short port)
Definition telnet.c:56
int whichp(const char *cmd)
Definition which.c:122
int readdf(int *value, const char *fmt,...)
Definition procval.c:154
int readllf(long long *value, const char *fmt,...)
Definition procval.c:133
pid_t pidfile_read(const char *pidfile)
Definition pidfilefn.c:49
char * readsnf(char *line, size_t len, const char *fmt,...)
Definition procval.c:74
int writellf(long long value, const char *mode, const char *fmt,...)
Definition procval.c:184
int telnet_close(sdbuf_t *ctx)
Definition telnet.c:116
int makepath(const char *dir)
Definition makepath.c:105
void * reallocarray(void *optr, size_t nmemb, size_t size)
int int mkpath(const char *dir, mode_t mode)
Definition makepath.c:40
int writedf(int value, const char *mode, const char *fmt,...)
Definition procval.c:208
int rsync(char *src, char *dst, int opt, int(*filter)(const char *file))
Definition rsync.c:70
int runbg(char *const cmd[], int delay)
Definition runbg.c:46
int vreadllf(long long *value, const char *fmt, va_list ap)
Definition procval.c:112
struct sdbuf sdbuf_t
Definition lite.h:72
char * chomp(char *str)
Definition chomp.c:38