Usermode/libc - C++ wrapping in headers, time rework
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / errno.c
1 /*
2  * Acess2 C Library
3  * - By John Hodge (thePowersGang)
4  * 
5  * errno.c
6  * - errno and strerror
7  */
8 #include "lib.h"
9 #include <stdio.h>
10 #include <errno.h>
11 #include <acess/sys.h>
12 #include <string.h>
13
14 EXPORT int *libc_geterrno()
15 {
16         return &_errno;
17 }
18
19 EXPORT char *strerror(int errnum)
20 {
21         switch((enum libc_eErrorNumbers)errnum)
22         {
23         case EOK:       return "Success";
24         case ENOSYS:    return "Invalid instruction/syscall";
25         case EINVAL:    return "Bad argument(s)";
26         case EBADF:     return "Invalid file";
27         case ENOMEM:    return "No free memory";
28         case EACCES:    return "Not permitted";
29         case EBUSY:     return "Resource is busy";
30         case ERANGE:    return "Value out of range";
31         case ENOTFOUND: return "Item not found";
32         case EROFS:     return "Read only filesystem";
33         case ENOTIMPL:  return "Not implimented";
34         case ENOENT:    return "No such file or directory";
35         case EEXIST:    return "Already exists";
36         case ENFILE:    return "Too many open files";
37         case ENOTDIR:   return "Not a directory";
38         case EISDIR:    return "Is a directory";
39         case EIO:       return "IO Error";
40         case EINTR:     return "Interrupted";
41         case EWOULDBLOCK:       return "Operation would have blocked";
42         case ENODEV:    return "No such device";
43         case EADDRNOTAVAIL:     return "Address not avaliable";
44         case EINPROGRESS:       return "Operation in process";
45         case EPERM:     return "Operation not permitted";
46         case ENOTTY:    return "Not a TTY";
47         case EAGAIN:    return "Try again";
48         case EFBIG:     return "File too big";
49         case EALREADY:  return "Operation was no-op";
50         case EAFNOSUPPORT:      return "Address family not supported";
51         case EINTERNAL: return "Internal error";
52         }
53         _SysDebug("strerror: errnum=%i unk", errnum);
54         errno = EINVAL;
55         return "unknown error";
56 }
57
58 EXPORT int strerror_r(int errnum, char *buf, size_t bufsiz)
59 {
60         const char *str = strerror(errnum);
61         if(!str)
62                 return -1;
63         
64         strncpy(buf, str, bufsiz);
65         return 0;
66 }
67
68 // stdio.h
69 EXPORT void perror(const char *s)
70 {
71         int err = errno;
72         if( s && s[0] ) {
73                 fprintf(stderr, "%s: (%i) %s\n", s, err, strerror(err));
74         }
75         else {
76                 fprintf(stderr, "(%i) %s\n", err, strerror(err));
77         }
78         _SysDebug("perror('%s'): %s (%i)", s, strerror(err), err);
79 }
80

UCC git Repository :: git.ucc.asn.au