Repositories » libk0
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Added error handling

Changeset 44b9d8052cf8

Parent 57c05feedb89

by Rup Palchowdhury

Changes to 2 files · Browse files at 44b9d8052cf8 Showing diff from parent 57c05feedb89 Diff from another changeset...

Change 1 of 1 Show Entire File eprintf.c Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -0,0 +1,113 @@
+/* Copyright (C) 1999 Lucent Technologies */ +/* Excerpted from 'The Practice of Programming' */ +/* by Brian W. Kernighan and Rob Pike */ + +#include <stdio.h> +#include <stdlib.h> +#include "eprintf.h" +#include <stdarg.h> +#include <string.h> +#include <errno.h> + + +static char *name = NULL; /* program name for messages */ +static FILE *stream = NULL; /* output stream for messages */ + +/* eprintf: print error message and exit */ +void eprintf(char *fmt, ...) +{ + va_list args; + + if (stream == NULL) + stream = stderr; + + fflush(stdout); + if (eprogname() != NULL) + fprintf(stream, "%s: ", eprogname()); + + va_start(args, fmt); + vfprintf(stream, fmt, args); + va_end(args); + + if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') + fprintf(stream, " %s", strerror(errno)); + fprintf(stream, "\n"); + exit(2); /* conventional value for failed execution */ +} + +/* weprintf: print warning message */ +void weprintf(char *fmt, ...) +{ + va_list args; + + if (stream == NULL) + stream = stderr; + + fflush(stdout); + fprintf(stream, "warning: "); + if (eprogname() != NULL) + fprintf(stream, "%s: ", eprogname()); + va_start(args, fmt); + vfprintf(stream, fmt, args); + va_end(args); + if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') + fprintf(stream, " %s\n", strerror(errno)); + else + fprintf(stream, "\n"); +} + +/* emalloc: malloc and report if error */ +void *emalloc(size_t n) +{ + void *p; + + p = malloc(n); + if (p == NULL) + eprintf("malloc of %u bytes failed:", n); + return p; +} + +/* erealloc: realloc and report if error */ +void *erealloc(void *vp, size_t n) +{ + void *p; + + p = realloc(vp, n); + if (p == NULL) + eprintf("realloc of %u bytes failed:", n); + return p; +} + +/* estrdup: duplicate a string, report if error */ +char *estrdup(char *s) +{ + char *t; + + t = (char *) malloc(strlen(s)+1); + if (t == NULL) + eprintf("estrdup(\"%.20s\") failed:", s); + strcpy(t, s); + return t; +} + +/* eprogname: return stored name of program */ +char *eprogname(void) +{ + return name; +} + +/* esetprogname: set stored name of program */ +void esetprogname(char *str) +{ + name = estrdup(str); +} + +FILE *egetstream(void) +{ + return stream; +} + +void esetstream(FILE *stream_) +{ + stream = stream_; +}
Change 1 of 1 Show Entire File eprintf.h Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@@ -0,0 +1,16 @@
+/* Copyright (C) 1999 Lucent Technologies */ +/* Excerpted from 'The Practice of Programming' */ +/* by Brian W. Kernighan and Rob Pike */ + +/* eprintf.h: error wrapper functions */ +extern void eprintf(char *, ...); +extern void weprintf(char *, ...); +extern char *estrdup(char *); +extern void *emalloc(size_t); +extern void *erealloc(void *, size_t); +extern char *eprogname(void); +extern void esetprogname(char *); +extern FILE *egetstream(void); +extern void esetstream(FILE *); + +#define NELEMS(a) (sizeof(a) / sizeof(a[0]))