10 #include "forwarder.h"
12 const char *const *argv;
13 char programid[SYS_NMLN+sizeof(PROGRAM)+3];
15 void arg_assert_fail(const char *msg) {
16 fprintf(stderr, PROGRAM ": argument error (!`%s')\n",msg);
20 void sysfail(const char *msg) {
21 fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
25 void fail(const char *msg) {
26 fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
30 void sysdiag(const char *msg) {
31 fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
34 void diag(const char *msg) {
35 fprintf(stderr, "%s: %s\n", programid, msg);
40 if (time(&r) == (time_t)-1) sysfail("get time of day");
44 void *xmalloc(size_t sz) {
47 if (!r) sysfail("allocate memory");
51 void write_must(int fd, const void *p_in, int sz, const char *what) {
52 const unsigned char *p= p_in;
58 if (errno == EINTR) continue;
67 void read_must(int fd, void *p_in, int sz, const char *what) {
68 unsigned char *p= p_in;
74 if (errno == EINTR) continue;
84 void get_random(void *ptr, size_t sz) {
85 static FILE *randfile;
90 randfile= fopen("/dev/urandom","rb");
91 if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb");
92 if (!randfile) sysfail("open random number generator");
95 r= fread(ptr,1,sz,randfile);
97 (ferror(randfile) ? sysfail : fail)("cannot read random number generator");
100 const char *getarg_string(void) {
108 unsigned long getarg_ulong(void) {
112 ul= strtoul(getarg_string(),&ep,0);
117 void *buf_append(struct buffer *buf, size_t amount) {
120 p= buf->start + buf->size;
125 void *buf_prepend(struct buffer *buf, size_t amount) {
127 return buf->start -= amount;
130 void *buf_unappend(struct buffer *buf, size_t amount) {
131 if (buf->size < amount) return 0;
132 return buf->start + (buf->size -= amount);
135 void *buf_unprepend(struct buffer *buf, size_t amount) {
139 buf->start += amount;