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