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