1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /***
18 **** CAUTION!!! KEEP DOC CONSISTENT---if you change text of a message
19 **** here, change two places:
20 **** 1) the leading doc section (alphabetized by macro)
21 **** 2) the real text inside switch(errnum)
22 ***/
23
24 /*
25 FUNCTION
26 <<strerror>>, <<strerror_l>>---convert error number to string
27
28 INDEX
29 strerror
30
31 INDEX
32 strerror_l
33
34 SYNOPSIS
35 #include <string.h>
36 char *strerror(int <[errnum]>);
37 char *strerror_l(int <[errnum]>, locale_t <[locale]>);
38 char *_strerror_r(struct _reent <[ptr]>, int <[errnum]>,
39 int <[internal]>, int *<[error]>);
40
41 DESCRIPTION
42 <<strerror>> converts the error number <[errnum]> into a
43 string. The value of <[errnum]> is usually a copy of <<errno>>.
44 If <<errnum>> is not a known error number, the result points to an
45 empty string.
46
47 <<strerror_l>> is like <<strerror>> but creates a string in a format
48 as expected in locale <[locale]>. If <[locale]> is LC_GLOBAL_LOCALE or
49 not a valid locale object, the behaviour is undefined.
50
51 This implementation of <<strerror>> prints out the following strings
52 for each of the values defined in `<<errno.h>>':
53
54 o+
55 o 0
56 Success
57
58 o E2BIG
59 Arg list too long
60
61 o EACCES
62 Permission denied
63
64 o EADDRINUSE
65 Address already in use
66
67 o EADDRNOTAVAIL
68 Address not available
69
70 o EADV
71 Advertise error
72
73 o EAFNOSUPPORT
74 Address family not supported by protocol family
75
76 o EAGAIN
77 No more processes
78
79 o EALREADY
80 Socket already connected
81
82 o EBADF
83 Bad file number
84
85 o EBADMSG
86 Bad message
87
88 o EBUSY
89 Device or resource busy
90
91 o ECANCELED
92 Operation canceled
93
94 o ECHILD
95 No children
96
97 o ECOMM
98 Communication error
99
100 o ECONNABORTED
101 Software caused connection abort
102
103 o ECONNREFUSED
104 Connection refused
105
106 o ECONNRESET
107 Connection reset by peer
108
109 o EDEADLK
110 Deadlock
111
112 o EDESTADDRREQ
113 Destination address required
114
115 o EEXIST
116 File exists
117
118 o EDOM
119 Mathematics argument out of domain of function
120
121 o EFAULT
122 Bad address
123
124 o EFBIG
125 File too large
126
127 o EHOSTDOWN
128 Host is down
129
130 o EHOSTUNREACH
131 Host is unreachable
132
133 o EIDRM
134 Identifier removed
135
136 o EILSEQ
137 Illegal byte sequence
138
139 o EINPROGRESS
140 Connection already in progress
141
142 o EINTR
143 Interrupted system call
144
145 o EINVAL
146 Invalid argument
147
148 o EIO
149 I/O error
150
151 o EISCONN
152 Socket is already connected
153
154 o EISDIR
155 Is a directory
156
157 o ELIBACC
158 Cannot access a needed shared library
159
160 o ELIBBAD
161 Accessing a corrupted shared library
162
163 o ELIBEXEC
164 Cannot exec a shared library directly
165
166 o ELIBMAX
167 Attempting to link in more shared libraries than system limit
168
169 o ELIBSCN
170 <<.lib>> section in a.out corrupted
171
172 o EMFILE
173 File descriptor value too large
174
175 o EMLINK
176 Too many links
177
178 o EMSGSIZE
179 Message too long
180
181 o EMULTIHOP
182 Multihop attempted
183
184 o ENAMETOOLONG
185 File or path name too long
186
187 o ENETDOWN
188 Network interface is not configured
189
190 o ENETRESET
191 Connection aborted by network
192
193 o ENETUNREACH
194 Network is unreachable
195
196 o ENFILE
197 Too many open files in system
198
199 o ENOBUFS
200 No buffer space available
201
202 o ENODATA
203 No data
204
205 o ENODEV
206 No such device
207
208 o ENOENT
209 No such file or directory
210
211 o ENOEXEC
212 Exec format error
213
214 o ENOLCK
215 No lock
216
217 o ENOLINK
218 Virtual circuit is gone
219
220 o ENOMEM
221 Not enough space
222
223 o ENOMSG
224 No message of desired type
225
226 o ENONET
227 Machine is not on the network
228
229 o ENOPKG
230 No package
231
232 o ENOPROTOOPT
233 Protocol not available
234
235 o ENOSPC
236 No space left on device
237
238 o ENOSR
239 No stream resources
240
241 o ENOSTR
242 Not a stream
243
244 o ENOSYS
245 Function not implemented
246
247 o ENOTBLK
248 Block device required
249
250 o ENOTCONN
251 Socket is not connected
252
253 o ENOTDIR
254 Not a directory
255
256 o ENOTEMPTY
257 Directory not empty
258
259 o ENOTRECOVERABLE
260 State not recoverable
261
262 o ENOTSOCK
263 Socket operation on non-socket
264
265 o ENOTSUP
266 Not supported
267
268 o ENOTTY
269 Not a character device
270
271 o ENXIO
272 No such device or address
273
274 o EOPNOTSUPP
275 Operation not supported on socket
276
277 o EOVERFLOW
278 Value too large for defined data type
279
280 o EOWNERDEAD
281 Previous owner died
282
283 o EPERM
284 Not owner
285
286 o EPIPE
287 Broken pipe
288
289 o EPROTO
290 Protocol error
291
292 o EPROTOTYPE
293 Protocol wrong type for socket
294
295 o EPROTONOSUPPORT
296 Unknown protocol
297
298 o ERANGE
299 Result too large
300
301 o EREMOTE
302 Resource is remote
303
304 o EROFS
305 Read-only file system
306
307 o ESHUTDOWN
308 Can't send after socket shutdown
309
310 o ESOCKTNOSUPPORT
311 Socket type not supported
312
313 o ESPIPE
314 Illegal seek
315
316 o ESRCH
317 No such process
318
319 o ESRMNT
320 Srmount error
321
322 o ESTRPIPE
323 Strings pipe error
324
325 o ETIME
326 Stream ioctl timeout
327
328 o ETIMEDOUT
329 Connection timed out
330
331 o ETXTBSY
332 Text file busy
333
334 o EWOULDBLOCK
335 Operation would block (usually same as EAGAIN)
336
337 o EXDEV
338 Cross-device link
339
340 o-
341
342 <<_strerror_r>> is a reentrant version of the above.
343
344 RETURNS
345 This function returns a pointer to a string. Your application must
346 not modify that string.
347
348 PORTABILITY
349 ANSI C requires <<strerror>>, but does not specify the strings used
350 for each error number.
351
352 <<strerror_l>> is POSIX-1.2008.
353
354 Although this implementation of <<strerror>> is reentrant (depending
355 on <<_user_strerror>>), ANSI C declares that subsequent calls to
356 <<strerror>> may overwrite the result string; therefore portable
357 code cannot depend on the reentrancy of this subroutine.
358
359 Although this implementation of <<strerror>> guarantees a non-null
360 result with a NUL-terminator, some implementations return <<NULL>>
361 on failure. Although POSIX allows <<strerror>> to set <<errno>>
362 to EINVAL on failure, this implementation does not do so (unless
363 you provide <<_user_strerror>>).
364
365 POSIX recommends that unknown <[errnum]> result in a message
366 including that value, however it is not a requirement and this
367 implementation does not provide that information (unless you
368 provide <<_user_strerror>>).
369
370 This implementation of <<strerror>> provides for user-defined
371 extensibility. <<errno.h>> defines <[__ELASTERROR]>, which can be
372 used as a base for user-defined error values. If the user supplies a
373 routine named <<_user_strerror>>, and <[errnum]> passed to
374 <<strerror>> does not match any of the supported values,
375 <<_user_strerror>> is called with three arguments. The first is of
376 type <[int]>, and is the <[errnum]> value unknown to <<strerror>>.
377 The second is of type <[int]>, and matches the <[internal]> argument
378 of <<_strerror_r>>; this should be zero if called from <<strerror>>
379 and non-zero if called from any other function; <<_user_strerror>> can
380 use this information to satisfy the POSIX rule that no other
381 standardized function can overwrite a static buffer reused by
382 <<strerror>>. The third is of type <[int *]>, and matches the
383 <[error]> argument of <<_strerror_r>>; if a non-zero value is stored
384 into that location (usually <[EINVAL]>), then <<strerror>> will set
385 <<errno>> to that value, and the XPG variant of <<strerror_r>> will
386 return that value instead of zero or <[ERANGE]>. <<_user_strerror>>
387 returns a <[char *]> value; returning <[NULL]> implies that the user
388 function did not choose to handle <[errnum]>. The default
389 <<_user_strerror>> returns <[NULL]> for all input values. Note that
390 <<_user_sterror>> must be thread-safe, and only denote errors via the
391 third argument rather than modifying <<errno>>, if <<strerror>> and
392 <<strerror_r>> are are to comply with POSIX.
393
394 <<strerror>> requires no supporting OS subroutines.
395
396 QUICKREF
397 strerror ansi pure
398 */
399
400 #define _DEFAULT_SOURCE
401 #include <errno.h>
402 #include <string.h>
403
404 char *
_strerror_r(int errnum,int internal,int * errptr)405 _strerror_r (
406 int errnum,
407 int internal,
408 int *errptr)
409 {
410 char *error;
411 extern char *_user_strerror (int, int, int *);
412
413 switch (errnum)
414 {
415 case 0:
416 error = "Success";
417 break;
418 /* go32 defines EPERM as EACCES */
419 #if defined (EPERM) && (!defined (EACCES) || (EPERM != EACCES))
420 case EPERM:
421 error = "Not owner";
422 break;
423 #endif
424 #ifdef ENOENT
425 case ENOENT:
426 error = "No such file or directory";
427 break;
428 #endif
429 #ifdef ESRCH
430 case ESRCH:
431 error = "No such process";
432 break;
433 #endif
434 #ifdef EINTR
435 case EINTR:
436 error = "Interrupted system call";
437 break;
438 #endif
439 #ifdef EIO
440 case EIO:
441 error = "I/O error";
442 break;
443 #endif
444 /* go32 defines ENXIO as ENODEV */
445 #if defined (ENXIO) && (!defined (ENODEV) || (ENXIO != ENODEV))
446 case ENXIO:
447 error = "No such device or address";
448 break;
449 #endif
450 #ifdef E2BIG
451 case E2BIG:
452 error = "Arg list too long";
453 break;
454 #endif
455 #ifdef ENOEXEC
456 case ENOEXEC:
457 error = "Exec format error";
458 break;
459 #endif
460 #ifdef EALREADY
461 case EALREADY:
462 error = "Socket already connected";
463 break;
464 #endif
465 #ifdef EBADF
466 case EBADF:
467 error = "Bad file number";
468 break;
469 #endif
470 #ifdef ECHILD
471 case ECHILD:
472 error = "No children";
473 break;
474 #endif
475 #ifdef EDESTADDRREQ
476 case EDESTADDRREQ:
477 error = "Destination address required";
478 break;
479 #endif
480 #ifdef EAGAIN
481 case EAGAIN:
482 error = "No more processes";
483 break;
484 #endif
485 #ifdef ENOMEM
486 case ENOMEM:
487 error = "Not enough space";
488 break;
489 #endif
490 #ifdef EACCES
491 case EACCES:
492 error = "Permission denied";
493 break;
494 #endif
495 #ifdef EFAULT
496 case EFAULT:
497 error = "Bad address";
498 break;
499 #endif
500 #ifdef ENOTBLK
501 case ENOTBLK:
502 error = "Block device required";
503 break;
504 #endif
505 #ifdef EBUSY
506 case EBUSY:
507 error = "Device or resource busy";
508 break;
509 #endif
510 #ifdef EEXIST
511 case EEXIST:
512 error = "File exists";
513 break;
514 #endif
515 #ifdef EXDEV
516 case EXDEV:
517 error = "Cross-device link";
518 break;
519 #endif
520 #ifdef ENODEV
521 case ENODEV:
522 error = "No such device";
523 break;
524 #endif
525 #ifdef ENOTDIR
526 case ENOTDIR:
527 error = "Not a directory";
528 break;
529 #endif
530 #ifdef EHOSTDOWN
531 case EHOSTDOWN:
532 error = "Host is down";
533 break;
534 #endif
535 #ifdef EINPROGRESS
536 case EINPROGRESS:
537 error = "Connection already in progress";
538 break;
539 #endif
540 #ifdef EISDIR
541 case EISDIR:
542 error = "Is a directory";
543 break;
544 #endif
545 #ifdef EINVAL
546 case EINVAL:
547 error = "Invalid argument";
548 break;
549 #endif
550 #ifdef ENETDOWN
551 case ENETDOWN:
552 error = "Network interface is not configured";
553 break;
554 #endif
555 #ifdef ENETRESET
556 case ENETRESET:
557 error = "Connection aborted by network";
558 break;
559 #endif
560 #ifdef ENFILE
561 case ENFILE:
562 error = "Too many open files in system";
563 break;
564 #endif
565 #ifdef EMFILE
566 case EMFILE:
567 error = "File descriptor value too large";
568 break;
569 #endif
570 #ifdef ENOTTY
571 case ENOTTY:
572 error = "Not a character device";
573 break;
574 #endif
575 #ifdef ETXTBSY
576 case ETXTBSY:
577 error = "Text file busy";
578 break;
579 #endif
580 #ifdef EFBIG
581 case EFBIG:
582 error = "File too large";
583 break;
584 #endif
585 #ifdef EHOSTUNREACH
586 case EHOSTUNREACH:
587 error = "Host is unreachable";
588 break;
589 #endif
590 #ifdef ENOSPC
591 case ENOSPC:
592 error = "No space left on device";
593 break;
594 #endif
595 #ifdef ENOTSUP
596 case ENOTSUP:
597 error = "Not supported";
598 break;
599 #endif
600 #ifdef ESPIPE
601 case ESPIPE:
602 error = "Illegal seek";
603 break;
604 #endif
605 #ifdef EROFS
606 case EROFS:
607 error = "Read-only file system";
608 break;
609 #endif
610 #ifdef EMLINK
611 case EMLINK:
612 error = "Too many links";
613 break;
614 #endif
615 #ifdef EPIPE
616 case EPIPE:
617 error = "Broken pipe";
618 break;
619 #endif
620 #ifdef EDOM
621 case EDOM:
622 error = "Mathematics argument out of domain of function";
623 break;
624 #endif
625 #ifdef ERANGE
626 case ERANGE:
627 error = "Result too large";
628 break;
629 #endif
630 #ifdef ENOMSG
631 case ENOMSG:
632 error = "No message of desired type";
633 break;
634 #endif
635 #ifdef EIDRM
636 case EIDRM:
637 error = "Identifier removed";
638 break;
639 #endif
640 #ifdef EILSEQ
641 case EILSEQ:
642 error = "Illegal byte sequence";
643 break;
644 #endif
645 #ifdef EDEADLK
646 case EDEADLK:
647 error = "Deadlock";
648 break;
649 #endif
650 #ifdef ENETUNREACH
651 case ENETUNREACH:
652 error = "Network is unreachable";
653 break;
654 #endif
655 #ifdef ENOLCK
656 case ENOLCK:
657 error = "No lock";
658 break;
659 #endif
660 #ifdef ENOSTR
661 case ENOSTR:
662 error = "Not a stream";
663 break;
664 #endif
665 #ifdef ETIME
666 case ETIME:
667 error = "Stream ioctl timeout";
668 break;
669 #endif
670 #ifdef ENOSR
671 case ENOSR:
672 error = "No stream resources";
673 break;
674 #endif
675 #ifdef ENONET
676 case ENONET:
677 error = "Machine is not on the network";
678 break;
679 #endif
680 #ifdef ENOPKG
681 case ENOPKG:
682 error = "No package";
683 break;
684 #endif
685 #ifdef EREMOTE
686 case EREMOTE:
687 error = "Resource is remote";
688 break;
689 #endif
690 #ifdef ENOLINK
691 case ENOLINK:
692 error = "Virtual circuit is gone";
693 break;
694 #endif
695 #ifdef EADV
696 case EADV:
697 error = "Advertise error";
698 break;
699 #endif
700 #ifdef ESRMNT
701 case ESRMNT:
702 error = "Srmount error";
703 break;
704 #endif
705 #ifdef ECOMM
706 case ECOMM:
707 error = "Communication error";
708 break;
709 #endif
710 #ifdef EPROTO
711 case EPROTO:
712 error = "Protocol error";
713 break;
714 #endif
715 #ifdef EPROTONOSUPPORT
716 case EPROTONOSUPPORT:
717 error = "Unknown protocol";
718 break;
719 #endif
720 #ifdef EMULTIHOP
721 case EMULTIHOP:
722 error = "Multihop attempted";
723 break;
724 #endif
725 #ifdef EBADMSG
726 case EBADMSG:
727 error = "Bad message";
728 break;
729 #endif
730 #ifdef ELIBACC
731 case ELIBACC:
732 error = "Cannot access a needed shared library";
733 break;
734 #endif
735 #ifdef ELIBBAD
736 case ELIBBAD:
737 error = "Accessing a corrupted shared library";
738 break;
739 #endif
740 #ifdef ELIBSCN
741 case ELIBSCN:
742 error = ".lib section in a.out corrupted";
743 break;
744 #endif
745 #ifdef ELIBMAX
746 case ELIBMAX:
747 error = "Attempting to link in more shared libraries than system limit";
748 break;
749 #endif
750 #ifdef ELIBEXEC
751 case ELIBEXEC:
752 error = "Cannot exec a shared library directly";
753 break;
754 #endif
755 #ifdef ENOSYS
756 case ENOSYS:
757 error = "Function not implemented";
758 break;
759 #endif
760 #ifdef ENMFILE
761 case ENMFILE:
762 error = "No more files";
763 break;
764 #endif
765 #ifdef ENOTEMPTY
766 case ENOTEMPTY:
767 error = "Directory not empty";
768 break;
769 #endif
770 #ifdef ENAMETOOLONG
771 case ENAMETOOLONG:
772 error = "File or path name too long";
773 break;
774 #endif
775 #ifdef ELOOP
776 case ELOOP:
777 error = "Too many symbolic links";
778 break;
779 #endif
780 #ifdef ENOBUFS
781 case ENOBUFS:
782 error = "No buffer space available";
783 break;
784 #endif
785 #ifdef ENODATA
786 case ENODATA:
787 error = "No data";
788 break;
789 #endif
790 #ifdef EAFNOSUPPORT
791 case EAFNOSUPPORT:
792 error = "Address family not supported by protocol family";
793 break;
794 #endif
795 #ifdef EPROTOTYPE
796 case EPROTOTYPE:
797 error = "Protocol wrong type for socket";
798 break;
799 #endif
800 #ifdef ENOTSOCK
801 case ENOTSOCK:
802 error = "Socket operation on non-socket";
803 break;
804 #endif
805 #ifdef ENOPROTOOPT
806 case ENOPROTOOPT:
807 error = "Protocol not available";
808 break;
809 #endif
810 #ifdef ESHUTDOWN
811 case ESHUTDOWN:
812 error = "Can't send after socket shutdown";
813 break;
814 #endif
815 #ifdef ECONNREFUSED
816 case ECONNREFUSED:
817 error = "Connection refused";
818 break;
819 #endif
820 #ifdef ECONNRESET
821 case ECONNRESET:
822 error = "Connection reset by peer";
823 break;
824 #endif
825 #ifdef EADDRINUSE
826 case EADDRINUSE:
827 error = "Address already in use";
828 break;
829 #endif
830 #ifdef EADDRNOTAVAIL
831 case EADDRNOTAVAIL:
832 error = "Address not available";
833 break;
834 #endif
835 #ifdef ECONNABORTED
836 case ECONNABORTED:
837 error = "Software caused connection abort";
838 break;
839 #endif
840 #if (defined(EWOULDBLOCK) && (!defined (EAGAIN) || (EWOULDBLOCK != EAGAIN)))
841 case EWOULDBLOCK:
842 error = "Operation would block";
843 break;
844 #endif
845 #ifdef ENOTCONN
846 case ENOTCONN:
847 error = "Socket is not connected";
848 break;
849 #endif
850 #ifdef ESOCKTNOSUPPORT
851 case ESOCKTNOSUPPORT:
852 error = "Socket type not supported";
853 break;
854 #endif
855 #ifdef EISCONN
856 case EISCONN:
857 error = "Socket is already connected";
858 break;
859 #endif
860 #ifdef ECANCELED
861 case ECANCELED:
862 error = "Operation canceled";
863 break;
864 #endif
865 #ifdef ENOTRECOVERABLE
866 case ENOTRECOVERABLE:
867 error = "State not recoverable";
868 break;
869 #endif
870 #ifdef EOWNERDEAD
871 case EOWNERDEAD:
872 error = "Previous owner died";
873 break;
874 #endif
875 #ifdef ESTRPIPE
876 case ESTRPIPE:
877 error = "Streams pipe error";
878 break;
879 #endif
880 #if defined(EOPNOTSUPP) && (!defined(ENOTSUP) || (ENOTSUP != EOPNOTSUPP))
881 case EOPNOTSUPP:
882 error = "Operation not supported on socket";
883 break;
884 #endif
885 #ifdef EOVERFLOW
886 case EOVERFLOW:
887 error = "Value too large for defined data type";
888 break;
889 #endif
890 #ifdef EMSGSIZE
891 case EMSGSIZE:
892 error = "Message too long";
893 break;
894 #endif
895 #ifdef ETIMEDOUT
896 case ETIMEDOUT:
897 error = "Connection timed out";
898 break;
899 #endif
900 default:
901 if (!errptr)
902 errptr = &_REENT_ERRNO(ptr);
903 if ((error = _user_strerror (errnum, internal, errptr)) == 0)
904 error = "";
905 break;
906 }
907
908 return error;
909 }
910
911 char *
strerror(int errnum)912 strerror (int errnum)
913 {
914 return _strerror_r (errnum, 0, NULL);
915 }
916
917 char *
strerror_l(int errnum,locale_t locale)918 strerror_l (int errnum, locale_t locale)
919 {
920 (void) locale;
921 /* We don't support per-locale error messages. */
922 return _strerror_r (errnum, 0, NULL);
923 }
924