add errmsg API

master
leitner 21 years ago
parent f26451c086
commit a6a00edd75

@ -1,3 +1,6 @@
0.20:
add errmsg API
0.19.2:
for some reason, a botched dependency slipped in the the Makefile

@ -0,0 +1,29 @@
#include <stdarg.h>
#include <sys/uio.h>
#include "errmsg.h"
#include "str.h"
/* the people who defined stdarg.h need to be taken behind the barn and shot */
int errmsg_cvt(struct iovec* x,const char* message, va_list a) {
int i,j;
j=0;
if (argv0) {
x[0].iov_base=(char*)argv0;
x[0].iov_len=str_len(argv0);
x[1].iov_base=": ";
x[1].iov_len=2;
j=2;
}
x[j].iov_base=(char*)message; x[j].iov_len=str_len(message); ++j;
for (i=0; j<22; ++i) {
const char* t=va_arg(a,const char*);
if (!t) break;
x[j].iov_base=(char*)t;
x[j].iov_len=str_len(t);
++j;
}
x[j].iov_base="\n";
x[j].iov_len=1;
return j+1;
}

@ -0,0 +1,8 @@
#include "errmsg.h"
const char* argv0;
void errmsg_iam(const char* who) {
argv0=who;
}

@ -0,0 +1,14 @@
#include <stdarg.h>
#include <sys/uio.h>
#include "errmsg.h"
#include "str.h"
extern int errmsg_cvt(struct iovec* x,const char* message, va_list a);
void errmsg_warn(const char* message, ...) {
struct iovec x[23];
va_list a;
va_start(a,message);
writev(2,x,errmsg_cvt(x,message,a));
va_end(a);
}

@ -0,0 +1,28 @@
#include <stdarg.h>
#include <sys/uio.h>
#include "errmsg.h"
#include "str.h"
#include <string.h>
#include <errno.h>
extern int errmsg_cvt(struct iovec* x,const char* message, va_list a);
void errmsg_warnsys(const char* message, ...) {
struct iovec x[25];
va_list a;
int i;
va_start(a,message);
i=errmsg_cvt(x,message,a);
x[i-1].iov_base=": ";
x[i-1].iov_len=2;
x[i].iov_base=strerror(errno);
x[i].iov_len=str_len(x[i].iov_base);
x[i+1].iov_base="\n";
x[i+1].iov_len=1;
writev(2,x,i+2);
va_end(a);
}

@ -0,0 +1,27 @@
#ifndef ERRMSG_H
#define ERRMSG_H
#ifdef __dietlibc__
#include <sys/cdefs.h>
#else
#define __attribute__(x)
#endif
/* These use file descriptor 2, not buffer_2!
* Call buffer_flush(buffer_2) before calling these! */
extern const char* argv0;
void errmsg_iam(const char* who); /* set argv0 */
/* terminate with NULL. */
/* newline is appended automatically. */
void errmsg_warn(const char* message, ...);
void errmsg_warnsys(const char* message, ...);
#define carp(...) errmsg_warn(__VA_ARGS__,0)
#define carpsys(...) errmsg_warnsys(__VA_ARGS__,0)
#define die(n,...) { errmsg_warn(__VA_ARGS__,0); exit(n); }
#define diesys(n,...) { errmsg_warnsys(__VA_ARGS__,0); exit(n); }
#endif

12
t.c

@ -19,17 +19,29 @@
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <errmsg.h>
#define rdtscl(low) \
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
int main(int argc,char* argv[]) {
char* c="fnord";
int fd=open_read(c);
errmsg_iam(argv[0]);
carp("could not open file `",c,"'");
diesys(23,"could not open file `",c,"'");
#if 0
errmsg_warn("could not open file `",c,"'",0);
errmsg_warnsys("could not open file `",c,"'",0);
#endif
#if 0
char buf[100]="/usr/bin/sh";
int len=str_len(buf);
assert(byte_rchr(buf,len,'/')==8);
assert(byte_rchr(buf,len,'@')==len);
assert(byte_rchr(buf,len,'h')==len-1);
printf("%d\n",byte_rchr("x",1,'x'));
#endif
#if 0
char buf[IP6_FMT+100];
int i;

Loading…
Cancel
Save