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 #include "string_private.h"
404 #include "local.h"
405
406 extern char *_user_strerror (int, int, int *) _ATTRIBUTE((__weak__));
407
408 char *
_strerror_r(int errnum,int internal,int * errptr)409 _strerror_r (
410 int errnum,
411 int internal,
412 int *errptr)
413 {
414 char *error;
415
416 switch (errnum)
417 {
418 case 0:
419 error = "Success";
420 break;
421 /* go32 defines EPERM as EACCES */
422 #if defined (EPERM) && (!defined (EACCES) || (EPERM != EACCES))
423 case EPERM:
424 error = "Not owner";
425 break;
426 #endif
427 #ifdef ENOENT
428 case ENOENT:
429 error = "No such file or directory";
430 break;
431 #endif
432 #ifdef ESRCH
433 case ESRCH:
434 error = "No such process";
435 break;
436 #endif
437 #ifdef EINTR
438 case EINTR:
439 error = "Interrupted system call";
440 break;
441 #endif
442 #ifdef EIO
443 case EIO:
444 error = "I/O error";
445 break;
446 #endif
447 /* go32 defines ENXIO as ENODEV */
448 #if defined (ENXIO) && (!defined (ENODEV) || (ENXIO != ENODEV))
449 case ENXIO:
450 error = "No such device or address";
451 break;
452 #endif
453 #ifdef E2BIG
454 case E2BIG:
455 error = "Arg list too long";
456 break;
457 #endif
458 #ifdef ENOEXEC
459 case ENOEXEC:
460 error = "Exec format error";
461 break;
462 #endif
463 #ifdef EALREADY
464 case EALREADY:
465 error = "Socket already connected";
466 break;
467 #endif
468 #ifdef EBADF
469 case EBADF:
470 error = "Bad file number";
471 break;
472 #endif
473 #ifdef ECHILD
474 case ECHILD:
475 error = "No children";
476 break;
477 #endif
478 #ifdef EDESTADDRREQ
479 case EDESTADDRREQ:
480 error = "Destination address required";
481 break;
482 #endif
483 #ifdef EAGAIN
484 case EAGAIN:
485 error = "No more processes";
486 break;
487 #endif
488 #ifdef ENOMEM
489 case ENOMEM:
490 error = "Not enough space";
491 break;
492 #endif
493 #ifdef EACCES
494 case EACCES:
495 error = "Permission denied";
496 break;
497 #endif
498 #ifdef EFAULT
499 case EFAULT:
500 error = "Bad address";
501 break;
502 #endif
503 #ifdef ENOTBLK
504 case ENOTBLK:
505 error = "Block device required";
506 break;
507 #endif
508 #ifdef EBUSY
509 case EBUSY:
510 error = "Device or resource busy";
511 break;
512 #endif
513 #ifdef EEXIST
514 case EEXIST:
515 error = "File exists";
516 break;
517 #endif
518 #ifdef EXDEV
519 case EXDEV:
520 error = "Cross-device link";
521 break;
522 #endif
523 #ifdef ENODEV
524 case ENODEV:
525 error = "No such device";
526 break;
527 #endif
528 #ifdef ENOTDIR
529 case ENOTDIR:
530 error = "Not a directory";
531 break;
532 #endif
533 #ifdef EHOSTDOWN
534 case EHOSTDOWN:
535 error = "Host is down";
536 break;
537 #endif
538 #ifdef EINPROGRESS
539 case EINPROGRESS:
540 error = "Connection already in progress";
541 break;
542 #endif
543 #ifdef EISDIR
544 case EISDIR:
545 error = "Is a directory";
546 break;
547 #endif
548 #ifdef EINVAL
549 case EINVAL:
550 error = "Invalid argument";
551 break;
552 #endif
553 #ifdef ENETDOWN
554 case ENETDOWN:
555 error = "Network interface is not configured";
556 break;
557 #endif
558 #ifdef ENETRESET
559 case ENETRESET:
560 error = "Connection aborted by network";
561 break;
562 #endif
563 #ifdef ENFILE
564 case ENFILE:
565 error = "Too many open files in system";
566 break;
567 #endif
568 #ifdef EMFILE
569 case EMFILE:
570 error = "File descriptor value too large";
571 break;
572 #endif
573 #ifdef ENOTTY
574 case ENOTTY:
575 error = "Not a character device";
576 break;
577 #endif
578 #ifdef ETXTBSY
579 case ETXTBSY:
580 error = "Text file busy";
581 break;
582 #endif
583 #ifdef EFBIG
584 case EFBIG:
585 error = "File too large";
586 break;
587 #endif
588 #ifdef EHOSTUNREACH
589 case EHOSTUNREACH:
590 error = "Host is unreachable";
591 break;
592 #endif
593 #ifdef ENOSPC
594 case ENOSPC:
595 error = "No space left on device";
596 break;
597 #endif
598 #ifdef ENOTSUP
599 case ENOTSUP:
600 error = "Not supported";
601 break;
602 #endif
603 #ifdef ESPIPE
604 case ESPIPE:
605 error = "Illegal seek";
606 break;
607 #endif
608 #ifdef EROFS
609 case EROFS:
610 error = "Read-only file system";
611 break;
612 #endif
613 #ifdef EMLINK
614 case EMLINK:
615 error = "Too many links";
616 break;
617 #endif
618 #ifdef EPIPE
619 case EPIPE:
620 error = "Broken pipe";
621 break;
622 #endif
623 #ifdef EDOM
624 case EDOM:
625 error = "Mathematics argument out of domain of function";
626 break;
627 #endif
628 #ifdef ERANGE
629 case ERANGE:
630 error = "Result too large";
631 break;
632 #endif
633 #ifdef ENOMSG
634 case ENOMSG:
635 error = "No message of desired type";
636 break;
637 #endif
638 #ifdef EIDRM
639 case EIDRM:
640 error = "Identifier removed";
641 break;
642 #endif
643 #ifdef EILSEQ
644 case EILSEQ:
645 error = "Illegal byte sequence";
646 break;
647 #endif
648 #ifdef EDEADLK
649 case EDEADLK:
650 error = "Deadlock";
651 break;
652 #endif
653 #ifdef ENETUNREACH
654 case ENETUNREACH:
655 error = "Network is unreachable";
656 break;
657 #endif
658 #ifdef ENOLCK
659 case ENOLCK:
660 error = "No lock";
661 break;
662 #endif
663 #ifdef ENOSTR
664 case ENOSTR:
665 error = "Not a stream";
666 break;
667 #endif
668 #ifdef ETIME
669 case ETIME:
670 error = "Stream ioctl timeout";
671 break;
672 #endif
673 #ifdef ENOSR
674 case ENOSR:
675 error = "No stream resources";
676 break;
677 #endif
678 #ifdef ENONET
679 case ENONET:
680 error = "Machine is not on the network";
681 break;
682 #endif
683 #ifdef ENOPKG
684 case ENOPKG:
685 error = "No package";
686 break;
687 #endif
688 #ifdef EREMOTE
689 case EREMOTE:
690 error = "Resource is remote";
691 break;
692 #endif
693 #ifdef ENOLINK
694 case ENOLINK:
695 error = "Virtual circuit is gone";
696 break;
697 #endif
698 #ifdef EADV
699 case EADV:
700 error = "Advertise error";
701 break;
702 #endif
703 #ifdef ESRMNT
704 case ESRMNT:
705 error = "Srmount error";
706 break;
707 #endif
708 #ifdef ECOMM
709 case ECOMM:
710 error = "Communication error";
711 break;
712 #endif
713 #ifdef EPROTO
714 case EPROTO:
715 error = "Protocol error";
716 break;
717 #endif
718 #ifdef EPROTONOSUPPORT
719 case EPROTONOSUPPORT:
720 error = "Unknown protocol";
721 break;
722 #endif
723 #ifdef EMULTIHOP
724 case EMULTIHOP:
725 error = "Multihop attempted";
726 break;
727 #endif
728 #ifdef EBADMSG
729 case EBADMSG:
730 error = "Bad message";
731 break;
732 #endif
733 #ifdef ELIBACC
734 case ELIBACC:
735 error = "Cannot access a needed shared library";
736 break;
737 #endif
738 #ifdef ELIBBAD
739 case ELIBBAD:
740 error = "Accessing a corrupted shared library";
741 break;
742 #endif
743 #ifdef ELIBSCN
744 case ELIBSCN:
745 error = ".lib section in a.out corrupted";
746 break;
747 #endif
748 #ifdef ELIBMAX
749 case ELIBMAX:
750 error = "Attempting to link in more shared libraries than system limit";
751 break;
752 #endif
753 #ifdef ELIBEXEC
754 case ELIBEXEC:
755 error = "Cannot exec a shared library directly";
756 break;
757 #endif
758 #ifdef ENOSYS
759 case ENOSYS:
760 error = "Function not implemented";
761 break;
762 #endif
763 #ifdef ENMFILE
764 case ENMFILE:
765 error = "No more files";
766 break;
767 #endif
768 #ifdef ENOTEMPTY
769 case ENOTEMPTY:
770 error = "Directory not empty";
771 break;
772 #endif
773 #ifdef ENAMETOOLONG
774 case ENAMETOOLONG:
775 error = "File or path name too long";
776 break;
777 #endif
778 #ifdef ELOOP
779 case ELOOP:
780 error = "Too many symbolic links";
781 break;
782 #endif
783 #ifdef ENOBUFS
784 case ENOBUFS:
785 error = "No buffer space available";
786 break;
787 #endif
788 #ifdef ENODATA
789 case ENODATA:
790 error = "No data";
791 break;
792 #endif
793 #ifdef EAFNOSUPPORT
794 case EAFNOSUPPORT:
795 error = "Address family not supported by protocol family";
796 break;
797 #endif
798 #ifdef EPROTOTYPE
799 case EPROTOTYPE:
800 error = "Protocol wrong type for socket";
801 break;
802 #endif
803 #ifdef ENOTSOCK
804 case ENOTSOCK:
805 error = "Socket operation on non-socket";
806 break;
807 #endif
808 #ifdef ENOPROTOOPT
809 case ENOPROTOOPT:
810 error = "Protocol not available";
811 break;
812 #endif
813 #ifdef ESHUTDOWN
814 case ESHUTDOWN:
815 error = "Can't send after socket shutdown";
816 break;
817 #endif
818 #ifdef ECONNREFUSED
819 case ECONNREFUSED:
820 error = "Connection refused";
821 break;
822 #endif
823 #ifdef ECONNRESET
824 case ECONNRESET:
825 error = "Connection reset by peer";
826 break;
827 #endif
828 #ifdef EADDRINUSE
829 case EADDRINUSE:
830 error = "Address already in use";
831 break;
832 #endif
833 #ifdef EADDRNOTAVAIL
834 case EADDRNOTAVAIL:
835 error = "Address not available";
836 break;
837 #endif
838 #ifdef ECONNABORTED
839 case ECONNABORTED:
840 error = "Software caused connection abort";
841 break;
842 #endif
843 #if (defined(EWOULDBLOCK) && (!defined (EAGAIN) || (EWOULDBLOCK != EAGAIN)))
844 case EWOULDBLOCK:
845 error = "Operation would block";
846 break;
847 #endif
848 #ifdef ENOTCONN
849 case ENOTCONN:
850 error = "Socket is not connected";
851 break;
852 #endif
853 #ifdef ESOCKTNOSUPPORT
854 case ESOCKTNOSUPPORT:
855 error = "Socket type not supported";
856 break;
857 #endif
858 #ifdef EISCONN
859 case EISCONN:
860 error = "Socket is already connected";
861 break;
862 #endif
863 #ifdef ECANCELED
864 case ECANCELED:
865 error = "Operation canceled";
866 break;
867 #endif
868 #ifdef ENOTRECOVERABLE
869 case ENOTRECOVERABLE:
870 error = "State not recoverable";
871 break;
872 #endif
873 #ifdef EOWNERDEAD
874 case EOWNERDEAD:
875 error = "Previous owner died";
876 break;
877 #endif
878 #ifdef ESTRPIPE
879 case ESTRPIPE:
880 error = "Streams pipe error";
881 break;
882 #endif
883 #if defined(EOPNOTSUPP) && (!defined(ENOTSUP) || (ENOTSUP != EOPNOTSUPP))
884 case EOPNOTSUPP:
885 error = "Operation not supported on socket";
886 break;
887 #endif
888 #ifdef EOVERFLOW
889 case EOVERFLOW:
890 error = "Value too large for defined data type";
891 break;
892 #endif
893 #ifdef EMSGSIZE
894 case EMSGSIZE:
895 error = "Message too long";
896 break;
897 #endif
898 #ifdef ETIMEDOUT
899 case ETIMEDOUT:
900 error = "Connection timed out";
901 break;
902 #endif
903 default:
904 if (!errptr)
905 errptr = &_REENT_ERRNO(ptr);
906 if (&_user_strerror == NULL || (error = _user_strerror (errnum, internal, errptr)) == 0)
907 error = "";
908 break;
909 }
910
911 return error;
912 }
913
914 char *
strerror(int errnum)915 strerror (int errnum)
916 {
917 return _strerror_r (errnum, 0, NULL);
918 }
919
920 char *
strerror_l(int errnum,locale_t locale)921 strerror_l (int errnum, locale_t locale)
922 {
923 (void) locale;
924 /* We don't support per-locale error messages. */
925 return _strerror_r (errnum, 0, NULL);
926 }
927