1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#define _XOPEN_SOURCE /* tell sun we want popen, etc */
#include "libc.h"
#include "types.h"
#include "misc.h"
#include "args.h"
#ifndef __lint
static const char rcsid[] =
"$Id: misc.c,v 1.2 2000/06/05 22:48:19 florea Exp $";
#endif
/* fatal ---------------------------------------------- print message and die */
void fatal(const char *msg)
{
fflush(stdout);
fatalf("%s", msg);
exit(1);
}
/* fatalf --------------------------------- format message, print it, and die */
static void print_argv0(void)
{
if (argv0) {
char *p = strrchr(argv0, '/');
(void)fprintf(stderr, "%s: ", p ? p+1 : argv0);
}
}
void fatalf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fflush(stdout);
print_argv0();
(void)vfprintf(stderr, fmt, ap);
(void)fputc('\n', stderr);
va_end(ap);
exit(1);
}
void fatalfr(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fflush(stdout);
print_argv0();
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
exit(1);
}
int psublast_debug = 0;
void debugf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (psublast_debug) {
fflush(stdout);
print_argv0();
if (vfprintf(stderr, fmt, ap) < 0)
exit(1);
}
va_end(ap);
}
void debugff(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (psublast_debug) {
fflush(stdout);
print_argv0();
if (vfprintf(stderr, fmt, ap) < 0)
exit(1);
if (fflush(stderr) != 0)
exit(1);
}
va_end(ap);
}
/* ckopen -------------------------------------- open file; check for success */
FILE *ckopen(const char *name, const char *mode)
{
FILE *fp;
if ((fp = fopen(name, mode)) == NULL)
fatalfr("Cannot open %s.", name);
return fp;
}
/* ckalloc -------------------------------- allocate space; check for success */
void *ckalloc(size_t amount)
{
void *p;
assert((long)amount >= 0);
if (amount == 0)
amount = 1; /* ANSI portability hack */
if ((p = malloc(amount)) == NULL)
fatalf("Ran out of memory trying to allocate %lu.",
(unsigned long)amount);
#if 0
memset(p, 0, amount); /* XXX */
#endif
return p;
}
/* ckallocz -------------------- allocate space; zero fill; check for success */
void *ckallocz(size_t amount)
{
void *p = ckalloc(amount);
memset(p, 0, amount);
return p;
}
void *ckfree(void *p)
{
free(p);
return 0;
}
/* strsame --------------------------- tell whether two strings are identical */
bool same_string(const char *s, const char *t)
{
return (strcmp(s, t) == 0);
}
/* strsave -------------------------- save string s somewhere; return address */
char *copy_string(const char *s)
{
char *p;
p = ckalloc(strlen(s)+1); /* +1 to hold '\0' */
return strcpy(p, s);
}
char *copy_substring(const char *s, int n)
{
char *p = ckalloc((size_t)n+1); /* +1 to hold '\0' */
memcpy(p, s, (size_t)n);
p[n] = 0;
return p;
}
long ckftell(FILE *f)
{
long r = ftell(f);
if (r < 0)
fatalfr("bad ftell: %s");
return r;
}
int ckfseek(FILE *f, long i, int m)
{
int r = fseek(f, i, m);
if (r < 0)
fatalfr("bad fseek: %s");
return r;
}
void *ckrealloc(void * p, size_t size)
{
p = p ? realloc(p, size) : malloc(size);
if (!p)
fatal("ckrealloc failed");
return p;
}
FILE *ckpopen(const char *name, const char *mode)
{
FILE *fp;
if ((fp = popen(name, mode)) == NULL)
fatalfr("Cannot open %s.", name);
return fp;
}
void ckpclose(FILE *fp)
{
int r = pclose(fp);
if (r != 0)
fatalfr("pclose failed (status %d)", r);
}
|