plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

errstr.c (1451B)


      1 #include "a.h"
      2 
      3 enum
      4 {
      5 	EPLAN9 = 0x19283745	/* see /usr/local/plan9/src/lib9/errstr.c */
      6 };
      7 
      8 typedef struct Error Error;
      9 struct Error
     10 {
     11 	char *text;
     12 	int err;
     13 	int len;
     14 };
     15 
     16 static Error errortab[] = {
     17 	{ "permitted", EPERM },
     18 	{ "permission", EACCES },
     19 	{ "access", EACCES },
     20 	{ "exists", EEXIST },
     21 	{ "exist", ENOENT },
     22 	{ "no such", ENOENT },
     23 	{ "not found", ENOENT },
     24 	{ "not implemented", ENOSYS},
     25 	{ "input/output", EIO },
     26 	{ "timeout", ETIMEDOUT },
     27 	{ "timed out", ETIMEDOUT },
     28 	{ "i/o", EIO },
     29 	{ "too long", E2BIG },
     30 	{ "interrupt", EINTR },
     31 	{ "no such", ENODEV },
     32 	{ "bad file", EBADF },
     33 	{ " fid ", EBADF },
     34 	{ "temporar", EAGAIN },
     35 	{ "memory", ENOMEM },
     36 	{ "is a directory", EISDIR },
     37 	{ "directory", ENOTDIR },
     38 	{ "argument", EINVAL },
     39 	{ "pipe", EPIPE },
     40 	{ "in use", EBUSY },
     41 	{ "busy", EBUSY },
     42 	{ "illegal", EINVAL },
     43 	{ "invalid", EINVAL },
     44 	{ "read-only", EROFS },
     45 	{ "read only", EROFS },
     46 	{ "stale ", ESTALE},
     47 #ifdef EPROTO
     48 	{ "proto", EPROTO },
     49 #else
     50 	{ "proto", EINVAL },
     51 #endif
     52 	{ "entry", ENOENT },
     53 };
     54 
     55 int
     56 errstr2errno(void)
     57 {
     58 	char e[ERRMAX];
     59 	int i, len;
     60 
     61 	if(errno != EPLAN9)
     62 		return errno;
     63 
     64 	if(errortab[0].len == 0)
     65 		for(i=0; i<nelem(errortab); i++)
     66 			errortab[i].len = strlen(errortab[i].text);
     67 
     68 	rerrstr(e, sizeof e);
     69 	len = strlen(e);
     70 	for(i=0; i<nelem(errortab); i++)
     71 		if(errortab[i].len <= len && cistrstr(e, errortab[i].text))
     72 			return errortab[i].err;
     73 	return ERANGE;	/* who knows - be blatantly wrong */
     74 }