master xplshn/aruu / shared / util.h
  1/* See LICENSE file for copyright and license details. */
  2#include <sys/types.h>
  3
  4#include <regex.h>
  5#include <stdarg.h>
  6#include <stddef.h>
  7#include <stdio.h>
  8#include <string.h>
  9
 10#include "arg.h"
 11#include "compat.h"
 12
 13#define UTF8_POINT(c) (((c) & 0xc0) != 0x80)
 14
 15#undef MIN
 16#define MIN(x, y) ((x) < (y) ? (x) : (y))
 17#undef MAX
 18#define MAX(x, y) ((x) > (y) ? (x) : (y))
 19#undef LIMIT
 20#define LIMIT(x, a, b) (x) = (x)<(a) ? (a) : (x)>(b) ? (b) : (x)
 21
 22#define LEN(x) (sizeof(x) / sizeof *(x))
 23
 24extern char *argv0;
 25
 26void *ecalloc(size_t, size_t);
 27void *emalloc(size_t);
 28void *erealloc(void *, size_t);
 29#undef reallocarray
 30void *reallocarray(void *, size_t, size_t);
 31void *ereallocarray(void *, size_t, size_t);
 32char *estrdup(const char *);
 33char *estrndup(const char *, size_t);
 34void *encalloc(int, size_t, size_t);
 35void *enmalloc(int, size_t);
 36void *enrealloc(int, void *, size_t);
 37void *enreallocarray(int, void *, size_t, size_t);
 38char *enstrdup(int, const char *);
 39char *enstrndup(int, const char *, size_t);
 40
 41void enfshut(int, FILE *, const char *);
 42void efshut(FILE *, const char *);
 43int  fshut(FILE *, const char *);
 44
 45void enprintf(int, const char *, ...);
 46void eprintf(const char *, ...);
 47void weprintf(const char *, ...);
 48void xvprintf(const char *, va_list);
 49
 50int confirm(const char *, ...);
 51
 52double estrtod(const char *);
 53
 54#undef strcasestr
 55#define strcasestr xstrcasestr
 56char *strcasestr(const char *, const char *);
 57
 58#undef strlcat
 59#define strlcat xstrlcat
 60size_t strlcat(char *, const char *, size_t);
 61size_t estrlcat(char *, const char *, size_t);
 62#undef strlcpy
 63#define strlcpy xstrlcpy
 64size_t strlcpy(char *, const char *, size_t);
 65size_t estrlcpy(char *, const char *, size_t);
 66
 67#undef strsep
 68#define strsep xstrsep
 69char *strsep(char **, const char *);
 70
 71void strnsubst(char **, const char *, const char *, size_t);
 72
 73/* regex */
 74int enregcomp(int, regex_t *, const char *, int);
 75int eregcomp(regex_t *, const char *, int);
 76
 77/* io */
 78ssize_t writeall(int, const void *, size_t);
 79int     concat(int, const char *, int, const char *);
 80
 81/* misc */
 82void   enmasse(int, char **, int (*)(const char *, const char *, int));
 83void   fnck(const char *, const char *, int (*)(const char *, const char *, int), int);
 84mode_t getumask(void);
 85char  *humansize(off_t);
 86mode_t parsemode(const char *, mode_t, mode_t);
 87off_t  parseoffset(const char *);
 88void   putword(FILE *, const char *);
 89#undef strtonum
 90#define strtonum xstrtonum
 91long long strtonum(const char *, long long, long long, const char **);
 92long long enstrtonum(int, const char *, long long, long long);
 93long long estrtonum(const char *, long long, long long);
 94size_t    unescape(char *);
 95int       mkdirp(const char *, mode_t, mode_t);
 96#undef memmem
 97#define memmem xmemmem
 98void *memmem(const void *, size_t, const void *, size_t);
 99
100/* ubase functions */
101char         *agetcwd(void);
102void          apathmax(char **, long *);
103long          estrtol(const char *, int);
104unsigned long estrtoul(const char *, int);
105#undef explicit_bzero
106void explicit_bzero(void *, size_t);
107void recurse_dir(const char *, void (*)(const char *));
108void devtotty(int, int *, int *);
109int  ttytostr(int, int, char *, size_t);
110
111#include <netinet/in.h>
112#include <sys/socket.h>
113
114#ifndef IFNAMSIZ
115#define IFNAMSIZ 16
116#endif
117
118struct NetStats {
119  unsigned long long rx_bytes;
120  unsigned long long rx_packets;
121  unsigned long long rx_errs;
122  unsigned long long rx_drop;
123  unsigned long long tx_bytes;
124  unsigned long long tx_packets;
125  unsigned long long tx_errs;
126  unsigned long long tx_drop;
127};
128
129struct NetInterface {
130  char          name[IFNAMSIZ];
131  unsigned int  flags;
132  int           mtu;
133  int           metric;
134  unsigned char mac[6];
135  int           has_mac;
136
137  /* ipv4 addresses */
138  struct sockaddr_in ipv4_addr;
139  int                has_ipv4;
140  struct sockaddr_in ipv4_mask;
141  struct sockaddr_in ipv4_brd;
142
143  /* ipv6 addresses */
144  struct sockaddr_in6 ipv6_addr;
145  int                 has_ipv6;
146  unsigned int        ipv6_scope;
147  unsigned int        ipv6_prefix;
148};
149
150struct MemInfo {
151  unsigned long long total;
152  unsigned long long free;
153  unsigned long long shared;
154  unsigned long long buffers;
155  unsigned long long cached;
156  unsigned long long totalswap;
157  unsigned long long freeswap;
158};
159
160int net_get_interfaces(struct NetInterface **, int *);
161int net_get_stats(const char *, struct NetStats *);
162int net_set_flags(const char *, unsigned int, int);
163int net_set_mtu(const char *, int);
164int net_set_mac(const char *, const unsigned char *);
165int net_add_addr(const char *, const char *, int);
166int net_del_addr(const char *, const char *, int);
167int net_show_routes(void);
168int net_add_route(const char *, const char *, const char *, const char *, int);
169int net_del_route(const char *, const char *, const char *, const char *, int);
170int net_flush_addrs(const char *);
171int net_set_txqueuelen(const char *, int);
172int net_set_name(const char *, const char *);
173
174#if FEATURE_NOEXEC || FEATURE_NOFORK
175#include "wexec.h"
176#define exit(code)  wexec_exit(code)
177#define _exit(code) wexec_exit(code)
178#endif