libite
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 
36 #ifdef __cplusplus
37 extern "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
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
71 typedef struct lfile lfile_t;
72 typedef struct sdbuf sdbuf_t;
74 char *chomp (char *str);
75 
76 int erasef (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
77 
78 char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
79 
80 int fexist (const char *file);
81 int fexistf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));;
82 int fisdir (const char *path);
83 
84 FILE *vfopenf (const char *mode, const char *fmt, va_list ap);
85 FILE *fopenf (const char *mode, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
86 int fremove (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
87 FILE *tempfile (void);
88 ssize_t copyfile (const char *src, const char *dst, int len, int opt);
89 int movefile (const char *src, const char *dst);
90 int fcopyfile (FILE *src, FILE *dst);
91 ssize_t fsendfile (FILE *src, FILE *dst, size_t len);
92 int truncatef (off_t length, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
93 
94 char *vreadsnf (char *line, size_t len, const char *fmt, va_list ap);
95 char *readsnf (char *line, size_t len, const char *fmt, ...);
96 int writesf (const char *str, const char *mode, const char *fmt, ...);
97 
98 int vreadllf (long long *value, const char *fmt, va_list ap);
99 int readllf (long long *value, const char *fmt, ...);
100 int readdf (int *value, const char *fmt, ...);
101 int writellf (long long value, const char *mode, const char *fmt, ...);
102 int writedf (int value, const char *mode, const char *fmt, ...);
103 
104 int ifconfig (const char *ifname, const char *addr, const char *mask, int up);
105 
106 lfile_t*lfopen (const char *file, const char *sep);
107 void lfclose (lfile_t *lf);
108 char *lftok (lfile_t *lf);
109 char *lfgetkey (lfile_t *lf, const char *key);
110 int lfgetint (lfile_t *lf, const char *key);
111 int fgetint (const char *file, const char *sep, const char *key);
112 
113 int fmkpath (mode_t mode, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
114 int mkpath (const char *dir, mode_t mode);
115 int makepath (const char *dir);
116 
117 int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
118 int rsync (char *src, char *dst, int opt, int (*filter) (const char *file));
119 
120 FILE *popenf (const char *type, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
121 
122 int pidfile (const char *basename);
123 int pidfile_signal(const char *pidfile, int signal);
124 pid_t pidfile_read (const char *pidfile);
125 pid_t pidfile_poll (const char *pidfile);
126 
127 void *reallocarray(void *optr, size_t nmemb, size_t size);
128 
129 void progress (int percent, int max_width);
130 void progress_simple(int percent);
131 
132 int runbg (char *const cmd[], int delay);
133 int systemf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
134 
135 sdbuf_t*telnet_open (int addr, short port);
136 int telnet_close (sdbuf_t *ctx);
137 int telnet_expect (sdbuf_t *ctx, char *script[], FILE *output);
138 int telnet_session (int addr, short port, char *script[], FILE *output);
139 
140 int touchf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
141 
142 int yorn (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
143 
144 char *which (const char *cmd);
145 int whichp (const char *cmd);
146 
147 #ifndef touch
148 #include <sys/stat.h> /* utimensat() */
149 #include <sys/time.h> /* utimensat() on *BSD */
156 static 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
172 
178 static 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
186 
192 static 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
200 
206 static 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 
253 static 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 
FILE * fopenf(const char *mode, const char *fmt,...)
Open a file based on the formatted string and optional arguments.
Definition: fopenf.c:75
FILE int FILE * tempfile(void)
A secure tmpfile() replacement.
Definition: tempfile.c:59
int telnet_close(sdbuf_t *ctx)
Close a telnet session previously opened with telnet_open()
Definition: telnet.c:116
int fremove(const char *fmt,...)
Remove a file based on the formatted string and optional arguments.
Definition: fremove.c:40
char * lfgetkey(lfile_t *lf, const char *key)
Find key in file.
Definition: lfile.c:131
lfile_t * lfopen(const char *file, const char *sep)
Open file and return parsing context.
Definition: lfile.c:46
FILE * vfopenf(const char *mode, const char *fmt, va_list ap)
Similar to fopenf() except it takes a va_list argument.
Definition: fopenf.c:40
int runbg(char *const cmd[], int delay)
Run command in the background.
Definition: runbg.c:46
int touchf(const char *fmt,...)
Like touch() but with formatted string support.
Definition: touchf.c:44
void progress_simple(int percent)
Alternative progress bar on systems where progress() doesn&#39;t work.
Definition: progress.c:99
int makepath(const char *dir)
Create all components of the specified directory.
Definition: makepath.c:105
int pidfile_signal(const char *pidfile, int signal)
Send signal to a PID and cleanup pidfile afterwards.
Definition: pidfilefn.c:114
char * lftok(lfile_t *lf)
Get next token in file.
Definition: lfile.c:92
int lfgetint(lfile_t *lf, const char *key)
Same as lfgetkey() but returns an integer.
Definition: lfile.c:156
void lfclose(lfile_t *lf)
Close a parser context.
Definition: lfile.c:75
int movefile(const char *src, const char *dst)
Move a file to another location.
Definition: copyfile.c:244
void progress(int percent, int max_width)
Advanced ASCII progress bar with spinner.
Definition: progress.c:66
int vreadllf(long long *value, const char *fmt, va_list ap)
Same as readllf() except it takes a va_list argument.
Definition: procval.c:112
char * readsnf(char *line, size_t len, const char *fmt,...)
Read first line from a file composed from fmt and optional args.
Definition: procval.c:74
int yorn(const char *fmt,...)
Pose a a Yes or No question and return answer.
Definition: yorn.c:74
void * reallocarray(void *optr, size_t nmemb, size_t size)
Similar to realloc() but for an array of items, like calloc()
Definition: reallocarray.c:44
int fgetint(const char *file, const char *sep, const char *key)
Find the integer value for key in a file.
Definition: lfile.c:177
int int char * which(const char *cmd)
Like which(1), or command -v foo
Definition: which.c:54
int int fisdir(const char *path)
Check if a path exists and is a directory.
Definition: fisdir.c:40
int writellf(long long value, const char *mode, const char *fmt,...)
Write 64-bit integer value to a file composed from fmt and optional args.
Definition: procval.c:184
FILE int pidfile(const char *basename)
Create or update mtime of process PID file.
Definition: pidfile.c:72
int sdbuf_t * telnet_open(int addr, short port)
Open telnet connection to addr:port and connect.
Definition: telnet.c:56
int writedf(int value, const char *mode, const char *fmt,...)
Write integer value to a file composed from fmt and optional args.
Definition: procval.c:208
int erasef(const char *fmt,...)
Like erase() but with formatted string support.
Definition: erasef.c:44
int ifconfig(const char *ifname, const char *addr, const char *mask, int up)
Basic ifconfig like operations on an interface.
Definition: ifconfig.c:53
int fmkpath(mode_t mode, const char *fmt,...)
Formatted version of mkpath().
Definition: makepath.c:66
int fcopyfile(FILE *src, FILE *dst)
Copy between FILE *fp.
Definition: copyfile.c:278
int char * fparseln(FILE *, size_t *, size_t *, const char[3], int)
Read a line from a file parsing continuations and trailing newlines.
Definition: fparseln.c:94
ssize_t copyfile(const char *src, const char *dst, int len, int opt)
Copy a file to another.
Definition: copyfile.c:144
int fexistf(const char *fmt,...)
Like fexist() but with formatted string support.
Definition: fexist.c:68
int rsync(char *src, char *dst, int opt, int(*filter)(const char *file))
Synchronize contents and optionally remove non-existing backups.
Definition: rsync.c:70
int systemf(const char *fmt,...)
Like system(), but takes a formatted string as argument.
Definition: systemf.c:44
int char * vreadsnf(char *line, size_t len, const char *fmt, va_list ap)
Similar to readsnf() except it takes a va_list argument.
Definition: procval.c:44
int readllf(long long *value, const char *fmt,...)
Read 64-bit integer value from a file composed from fmt and optional args.
Definition: procval.c:133
ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
Copy data between file streams.
Definition: fsendfile.c:51
struct sdbuf sdbuf_t
Opqaue context struct for telnet APIs.
Definition: lite.h:72
int truncatef(off_t length, const char *fmt,...)
Truncate a file based on the formatted string.
Definition: truncatef.c:42
int fexist(const char *file)
Check if a file exists in the file system.
Definition: fexist.c:45
int int mkpath(const char *dir, mode_t mode)
makepath() but takes a mode_t argument.
Definition: makepath.c:40
int writesf(const char *str, const char *mode, const char *fmt,...)
Write a string buffer to a file composed from fmt and optional args.
Definition: procval.c:94
pid_t pidfile_read(const char *pidfile)
Reads a PID value from a pidfile.
Definition: pidfilefn.c:49
int readdf(int *value, const char *fmt,...)
Read integer value from a file composed from fmt and optional args.
Definition: procval.c:154
int dir(const char *dir, const char *type, int(*filter)(const char *file), char ***list, int strip)
List all files of a certain type in the given directory.
Definition: dir.c:82
int whichp(const char *cmd)
Predicate variant of which()
Definition: which.c:122
pid_t pidfile_poll(const char *pidfile)
Poll for the existence of a pidfile and return PID.
Definition: pidfilefn.c:89
int telnet_expect(sdbuf_t *ctx, char *script[], FILE *output)
Poor man&#39;s telnet expect.
Definition: telnet.c:174
int telnet_session(int addr, short port, char *script[], FILE *output)
Very simple expect-like implementation for telnet.
Definition: telnet.c:295