41 #ifndef LIBITE_LITE_H_ 42 #define LIBITE_LITE_H_ 50 #include <sys/types.h> 51 #include <sys/param.h> 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 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); 76 int erasef (
const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
78 char *
fparseln (FILE *,
size_t *,
size_t *, const
char[3],
int);
80 int fexist (const
char *file);
81 int fexistf (const
char *fmt, ...) __attribute__ ((format (printf, 1, 2)));;
82 int fisdir (const
char *path);
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)));
88 ssize_t
copyfile (const
char *src, const
char *dst,
int len,
int opt);
89 int movefile (const
char *src, const
char *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)));
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, ...);
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, ...);
104 int ifconfig (const
char *ifname, const
char *addr, const
char *mask,
int up);
106 lfile_t*
lfopen (const
char *file, const
char *sep);
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);
113 int fmkpath (mode_t mode, const
char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
114 int mkpath (const
char *
dir, mode_t mode);
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));
120 FILE *popenf (const
char *type, const
char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
122 int pidfile (const
char *basename);
127 void *
reallocarray(
void *optr,
size_t nmemb,
size_t size);
129 void progress (
int percent,
int max_width);
132 int runbg (
char *const cmd[],
int delay);
133 int systemf (const
char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
138 int telnet_session (
int addr,
short port,
char *script[], FILE *output);
140 int touchf (const
char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
142 int yorn (const
char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
144 char *
which (const
char *cmd);
145 int whichp (const
char *cmd);
148 #include <sys/stat.h> 149 #include <sys/time.h> 156 static inline int touch(
const char *path)
160 if (utimensat(AT_FDCWD, path, NULL, 0)) {
163 fd = creat(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
178 static inline int makedir(
const char *path, mode_t mode)
180 if (mkdir(path, mode) && errno != EEXIST)
192 static inline int makefifo(
const char *path, mode_t mode)
194 if (mkfifo(path, mode) && errno != EEXIST)
206 static inline int erase(
const char *path)
208 if (
remove(path) && errno != ENOENT)
215 # define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min))) 218 # define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min))) 222 # define S_ISEXEC(m) (((m) & S_IXUSR) == S_IXUSR) 227 #define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1)) 230 #define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0)) 233 #define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0)) 236 #define SETBIT(word,bit) (word |= (1 << (bit))) 239 #define CLRBIT(word,bit) (word &= ~(1 << (bit))) 244 #define NELEMS(array) (sizeof(array) / sizeof(array[0])) 253 static inline int fisslashdir(
const char *dir)
259 return dir[strlen(dir) - 1] ==
'/';
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) 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'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'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