File: error.c

package info (click to toggle)
wmii 3.9.2%2Bdebian-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,464 kB
  • sloc: ansic: 24,885; python: 2,123; sh: 1,203; makefile: 403; ruby: 326
file content (103 lines) | stat: -rw-r--r-- 2,247 bytes parent folder | download | duplicates (3)
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
/* Public Domain --Kris Maglione */
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ixp_local.h"

static int
_vsnprint(char *buf, int n, const char *fmt, va_list ap) {
	return vsnprintf(buf, n, fmt, ap);
}

static char*
_vsmprint(const char *fmt, va_list ap) {
	va_list al;
	char *buf = "";
	int n;

	va_copy(al, ap);
	n = vsnprintf(buf, 0, fmt, al);
	va_end(al);

	buf = malloc(++n);
	if(buf)
		vsnprintf(buf, n, fmt, ap);
	return buf;
}

int (*ixp_vsnprint)(char*, int, const char*, va_list) = _vsnprint;
char* (*ixp_vsmprint)(const char*, va_list) = _vsmprint;

/* Approach to errno handling taken from Plan 9 Port. */
enum {
	EPLAN9 = 0x19283745,
};

/**
 * Function: ixp_errbuf
 * Function: ixp_errstr
 * Function: ixp_rerrstr
 * Function: ixp_werrstr
 *
 * Params:
 *	buf - The buffer to read and/or fill.
 *	n - The size of the buffer.
 *	fmt - A format string with which to write the *	errstr.
 *	... - Arguments to P<fmt>.
 *
 * These functions simulate Plan 9's errstr functionality.
 * They replace errno in libixp. Note that these functions
 * are not internationalized.
 *
 * F<ixp_errbuf> returns the errstr buffer for the current
 * thread. F<ixp_rerrstr> fills P<buf> with the data from
 * the current thread's error buffer, while F<ixp_errstr>
 * exchanges P<buf>'s contents with those of the current
 * thread's error buffer. F<ixp_werrstr> is takes a format
 * string from which to construct an errstr.
 *
 * Returns:
 *	F<ixp_errbuf> returns the current thread's error
 * string buffer.
 */
char*
ixp_errbuf() {
	char *errbuf;

	errbuf = thread->errbuf();
	if(errno == EINTR)
		strncpy(errbuf, "interrupted", IXP_ERRMAX);
	else if(errno != EPLAN9)
		strncpy(errbuf, strerror(errno), IXP_ERRMAX);
	return errbuf;
}

void
errstr(char *buf, int n) {
	char tmp[IXP_ERRMAX];

	strncpy(tmp, buf, sizeof tmp);
	rerrstr(buf, n);
	strncpy(thread->errbuf(), tmp, IXP_ERRMAX);
	errno = EPLAN9;
}

void
rerrstr(char *buf, int n) {
	strncpy(buf, ixp_errbuf(), n);
}

void
werrstr(const char *fmt, ...) {
	char tmp[IXP_ERRMAX];
	va_list ap;

	va_start(ap, fmt);
	ixp_vsnprint(tmp, sizeof tmp, fmt, ap);
	va_end(ap);
	strncpy(thread->errbuf(), tmp, IXP_ERRMAX);
	errno = EPLAN9;
}