Messi anlcuni nuovi esempi. E messa la GPL negli esempi.
[gapil.git] / sources / ErrCode.c
1 /****************************************************************
2  *
3  * Program ErrCode.c: 
4  * Print error code MACRO and messages
5  *
6  * Author: Simone Piccardi
7  * Sep. 2001
8  *
9  * Usage: errcode -h give all info's
10  *
11  * $Id: ErrCode.c,v 1.1 2001/09/09 17:39:15 piccardi Exp $ 
12  *
13  ****************************************************************/
14 /* 
15  * Include needed headers
16  */
17 #include <errno.h>       /* error definitions and routines */ 
18 #include <stdlib.h>      /* C standard library */
19 #include <unistd.h>      /* unix standard library */
20 #include <stdio.h>       /* standard I/O library */
21 #include <string.h>      /* string functions */
22
23 /* Help printing routine */
24 void usage(void);
25
26 /* Array for code number <=> code macro name */
27 char * err_code[] = {
28 "SUCCESS        ",  /*  0   Success */
29 "EPERM          ",  /*  1   Operation not permitted */
30 "ENOENT         ",  /*  2   No such file or directory */
31 "ESRCH          ",  /*  3   No such process */
32 "EINTR          ",  /*  4   Interrupted system call */
33 "EIO            ",  /*  5   I/O error */
34 "ENXIO          ",  /*  6   No such device or address */
35 "E2BIG          ",  /*  7   Arg list too long */
36 "ENOEXEC        ",  /*  8   Exec format error */
37 "EBADF          ",  /*  9   Bad file number */
38 "ECHILD         ",  /* 10   No child processes */
39 "EAGAIN         ",  /* 11   Try again */
40 "ENOMEM         ",  /* 12   Out of memory */
41 "EACCES         ",  /* 13   Permission denied */
42 "EFAULT         ",  /* 14   Bad address */
43 "ENOTBLK        ",  /* 15   Block device required */
44 "EBUSY          ",  /* 16   Device or resource busy */
45 "EEXIST         ",  /* 17   File exists */
46 "EXDEV          ",  /* 18   Cross-device link */
47 "ENODEV         ",  /* 19   No such device */
48 "ENOTDIR        ",  /* 20   Not a directory */
49 "EISDIR         ",  /* 21   Is a directory */
50 "EINVAL         ",  /* 22   Invalid argument */
51 "ENFILE         ",  /* 23   File table overflow */
52 "EMFILE         ",  /* 24   Too many open files */
53 "ENOTTY         ",  /* 25   Not a typewriter */
54 "ETXTBSY        ",  /* 26   Text file busy */
55 "EFBIG          ",  /* 27   File too large */
56 "ENOSPC         ",  /* 28   No space left on device */
57 "ESPIPE         ",  /* 29   Illegal seek */
58 "EROFS          ",  /* 30   Read-only file system */
59 "EMLINK         ",  /* 31   Too many links */
60 "EPIPE          ",  /* 32   Broken pipe */
61 "EDOM           ",  /* 33   Math argument out of domain of func */
62 "ERANGE         ",  /* 34   Math result not representable */
63 "EDEADLK        ",  /* 35   Resource deadlock would occur */
64 "ENAMETOOLONG   ",  /* 36   File name too long */
65 "ENOLCK         ",  /* 37   No record locks available */
66 "ENOSYS         ",  /* 38   Function not implemented */
67 "ENOTEMPTY      ",  /* 39   Directory not empty */
68 "ELOOP          ",  /* 40   Too many symbolic links encountered */
69 "EWOULDBLOCK    ",  /* EAGAIN Operation would block */
70 "ENOMSG         ",  /* 42   No message of desired type */
71 "EIDRM          ",  /* 43   Identifier removed */
72 "ECHRNG         ",  /* 44   Channel number out of range */
73 "EL2NSYNC       ",  /* 45   Level 2 not synchronized */
74 "EL3HLT         ",  /* 46   Level 3 halted */
75 "EL3RST         ",  /* 47   Level 3 reset */
76 "ELNRNG         ",  /* 48   Link number out of range */
77 "EUNATCH        ",  /* 49   Protocol driver not attached */
78 "ENOCSI         ",  /* 50   No CSI structure available */
79 "EL2HLT         ",  /* 51   Level 2 halted */
80 "EBADE          ",  /* 52   Invalid exchange */
81 "EBADR          ",  /* 53   Invalid request descriptor */
82 "EXFULL         ",  /* 54   Exchange full */
83 "ENOANO         ",  /* 55   No anode */
84 "EBADRQC        ",  /* 56   Invalid request code */
85 "EBADSLT        ",  /* 57   Invalid slot */
86 "EDEADLOCK      ",  /* EDEADLK */
87 "EBFONT         ",  /* 59   Bad font file format */
88 "ENOSTR         ",  /* 60   Device not a stream */
89 "ENODATA        ",  /* 61   No data available */
90 "ETIME          ",  /* 62   Timer expired */
91 "ENOSR          ",  /* 63   Out of streams resources */
92 "ENONET         ",  /* 64   Machine is not on the network */
93 "ENOPKG         ",  /* 65   Package not installed */
94 "EREMOTE        ",  /* 66   Object is remote */
95 "ENOLINK        ",  /* 67   Link has been severed */
96 "EADV           ",  /* 68   Advertise error */
97 "ESRMNT         ",  /* 69   Srmount error */
98 "ECOMM          ",  /* 70   Communication error on send */
99 "EPROTO         ",  /* 71   Protocol error */
100 "EMULTIHOP      ",  /* 72   Multihop attempted */
101 "EDOTDOT        ",  /* 73   RFS specific error */
102 "EBADMSG        ",  /* 74   Not a data message */
103 "EOVERFLOW      ",  /* 75   Value too large for defined data type */
104 "ENOTUNIQ       ",  /* 76   Name not unique on network */
105 "EBADFD         ",  /* 77   File descriptor in bad state */
106 "EREMCHG        ",  /* 78   Remote address changed */
107 "ELIBACC        ",  /* 79   Can not access a needed shared library */
108 "ELIBBAD        ",  /* 80   Accessing a corrupted shared library */
109 "ELIBSCN        ",  /* 81   .lib section in a.out corrupted */
110 "ELIBMAX        ",  /* 82   Attempting to link in too many shared libraries */
111 "ELIBEXEC       ",  /* 83   Cannot exec a shared library directly */
112 "EILSEQ         ",  /* 84   Illegal byte sequence */
113 "ERESTART       ",  /* 85   Interrupted system call should be restarted */
114 "ESTRPIPE       ",  /* 86   Streams pipe error */
115 "EUSERS         ",  /* 87   Too many users */
116 "ENOTSOCK       ",  /* 88   Socket operation on non-socket */
117 "EDESTADDRREQ   ",  /* 89   Destination address required */
118 "EMSGSIZE       ",  /* 90   Message too long */
119 "EPROTOTYPE     ",  /* 91   Protocol wrong type for socket */
120 "ENOPROTOOPT    ",  /* 92   Protocol not available */
121 "EPROTONOSUPPORT",  /* 93   Protocol not supported */
122 "ESOCKTNOSUPPORT",  /* 94   Socket type not supported */
123 "EOPNOTSUPP     ",  /* 95   Operation not supported on transport endpoint */
124 "EPFNOSUPPORT   ",  /* 96   Protocol family not supported */
125 "EAFNOSUPPORT   ",  /* 97   Address family not supported by protocol */
126 "EADDRINUSE     ",  /* 98   Address already in use */
127 "EADDRNOTAVAIL  ",  /* 99   Cannot assign requested address */
128 "ENETDOWN       ",  /* 100  Network is down */
129 "ENETUNREACH    ",  /* 101  Network is unreachable */
130 "ENETRESET      ",  /* 102  Network dropped connection because of reset */
131 "ECONNABORTED   ",  /* 103  Software caused connection abort */
132 "ECONNRESET     ",  /* 104  Connection reset by peer */
133 "ENOBUFS        ",  /* 105  No buffer space available */
134 "EISCONN        ",  /* 106  Transport endpoint is already connected */
135 "ENOTCONN       ",  /* 107  Transport endpoint is not connected */
136 "ESHUTDOWN      ",  /* 108  Cannot send after transport endpoint shutdown */
137 "ETOOMANYREFS   ",  /* 109  Too many references: cannot splice */
138 "ETIMEDOUT      ",  /* 110  Connection timed out */
139 "ECONNREFUSED   ",  /* 111  Connection refused */
140 "EHOSTDOWN      ",  /* 112  Host is down */
141 "EHOSTUNREACH   ",  /* 113  No route to host */
142 "EALREADY       ",  /* 114  Operation already in progress */
143 "EINPROGRESS    ",  /* 115  Operation now in progress */
144 "ESTALE         ",  /* 116  Stale NFS file handle */
145 "EUCLEAN        ",  /* 117  Structure needs cleaning */
146 "ENOTNAM        ",  /* 118  Not a XENIX named type file */
147 "ENAVAIL        ",  /* 119  No XENIX semaphores available */
148 "EISNAM         ",  /* 120  Is a named type file */
149 "EREMOTEIO      ",  /* 121  Remote I/O error */
150 "EDQUOT         ",  /* 122  Quota exceeded */
151 "ENOMEDIUM      ",  /* 123  No medium found */
152 "EMEDIUMTYPE    "   /* 124  Wrong medium type */
153 };
154 int main(int argc, char *argv[])
155 {
156 /* 
157  * Variables definition  
158  */
159     int i, err;
160     int label = 0;
161     int message = 0;
162     /*
163      * Input section: decode parameters passed in the calling 
164      * Use getopt function
165      */
166     opterr = 0;  /* don't want writing to stderr */
167     while ( (i = getopt(argc, argv, "hlm")) != -1) {
168         switch (i) {
169         /* 
170          * Handling options 
171          */ 
172         case 'h':  
173             printf("Wrong -h option use\n");
174             usage();
175             return(0);
176             break;
177         case 'l':   /* unrecognized options */
178             printf("Label printing\n");
179             label = 1;
180             break;
181         case 'm':   /* unrecognized options */
182             printf("Message printing\n");
183             message = 1;
184             break;
185         case '?':   /* unrecognized options */
186             printf("Unrecognized options -%c\n",optopt);
187             usage();
188         default:    /* should not reached */
189             usage();
190         }
191     }
192     /* ***********************************************************
193      * 
194      *           Options processing completed
195      *
196      *                Main code beginning
197      * 
198      * ***********************************************************/
199     if (optind == argc) {
200         usage();
201     }
202     err = atoi(argv[optind]);
203     if (message) {
204         printf("Error message for %d is %s\n", err, strerror(err));
205     }
206     if (label) {
207         printf("Error label for %d is %s\n", err, err_code[err]);
208     }
209     if ( (!label)&&(!message) ) {
210         usage();
211     }
212     /* normal exit */
213     return 0;
214 }
215 /*
216  * routine to print usage info and exit
217  */
218 void usage(void) {
219     printf("Print error code message or macro label \n");
220     printf("Usage:\n");
221     printf("  daytime [-h] [-l] [-m] error code \n");
222     printf("  -m           print error messages\n");
223     printf("  -l           print error code label\n");
224     printf("  -h           print this help\n");
225     exit(1);
226 }