Deleted Added
full compact
strerror.c (77298) strerror.c (89857)
1/* Extended support for using errno values.
2 Written by Fred Fish. fnf@cygnus.com
3 This file is in the public domain. --Per Bothner. */
4
5#include "ansidecl.h"
6#include "libiberty.h"
7
8#include "config.h"
9
10#ifdef HAVE_SYS_ERRLIST
11/* Note that errno.h (not sure what OS) or stdio.h (BSD 4.4, at least)
12 might declare sys_errlist in a way that the compiler might consider
13 incompatible with our later declaration, perhaps by using const
14 attributes. So we hide the declaration in errno.h (if any) using a
15 macro. */
1/* Extended support for using errno values.
2 Written by Fred Fish. fnf@cygnus.com
3 This file is in the public domain. --Per Bothner. */
4
5#include "ansidecl.h"
6#include "libiberty.h"
7
8#include "config.h"
9
10#ifdef HAVE_SYS_ERRLIST
11/* Note that errno.h (not sure what OS) or stdio.h (BSD 4.4, at least)
12 might declare sys_errlist in a way that the compiler might consider
13 incompatible with our later declaration, perhaps by using const
14 attributes. So we hide the declaration in errno.h (if any) using a
15 macro. */
16#define sys_nerr sys_nerr__
16#define sys_errlist sys_errlist__
17#endif
18
19#include <stdio.h>
20#include <errno.h>
21
22#ifdef HAVE_SYS_ERRLIST
17#define sys_errlist sys_errlist__
18#endif
19
20#include <stdio.h>
21#include <errno.h>
22
23#ifdef HAVE_SYS_ERRLIST
24#undef sys_nerr
23#undef sys_errlist
24#endif
25
26/* Routines imported from standard C runtime libraries. */
27
28#ifdef HAVE_STDLIB_H
29#include <stdlib.h>
30#else

--- 20 unchanged lines hidden (view full) ---

51 value.
52
53 Not all of these errnos will exist on all systems. This table is the only
54 thing that should have to be updated as new error numbers are introduced.
55 It's sort of ugly, but at least its portable. */
56
57struct error_info
58{
25#undef sys_errlist
26#endif
27
28/* Routines imported from standard C runtime libraries. */
29
30#ifdef HAVE_STDLIB_H
31#include <stdlib.h>
32#else

--- 20 unchanged lines hidden (view full) ---

53 value.
54
55 Not all of these errnos will exist on all systems. This table is the only
56 thing that should have to be updated as new error numbers are introduced.
57 It's sort of ugly, but at least its portable. */
58
59struct error_info
60{
59 int value; /* The numeric value from */
60 const char *name; /* The equivalent symbolic value */
61 const int value; /* The numeric value from <errno.h> */
62 const char *const name; /* The equivalent symbolic value */
61#ifndef HAVE_SYS_ERRLIST
63#ifndef HAVE_SYS_ERRLIST
62 const char *msg; /* Short message about this value */
64 const char *const msg; /* Short message about this value */
63#endif
64};
65
66#ifndef HAVE_SYS_ERRLIST
67# define ENTRY(value, name, msg) {value, name, msg}
68#else
69# define ENTRY(value, name, msg) {value, name}
70#endif

--- 484 unchanged lines hidden (view full) ---

555 }
556
557#endif
558
559}
560
561/*
562
65#endif
66};
67
68#ifndef HAVE_SYS_ERRLIST
69# define ENTRY(value, name, msg) {value, name, msg}
70#else
71# define ENTRY(value, name, msg) {value, name}
72#endif

--- 484 unchanged lines hidden (view full) ---

557 }
558
559#endif
560
561}
562
563/*
564
563NAME
564
565
565 errno_max -- return the max errno value
566@deftypefn Extension int errno_max (void)
566
567
567SYNOPSIS
568Returns the maximum @code{errno} value for which a corresponding
569symbolic name or message is available. Note that in the case where we
570use the @code{sys_errlist} supplied by the system, it is possible for
571there to be more symbolic names than messages, or vice versa. In
572fact, the manual page for @code{perror(3C)} explicitly warns that one
573should check the size of the table (@code{sys_nerr}) before indexing
574it, since new error codes may be added to the system before they are
575added to the table. Thus @code{sys_nerr} might be smaller than value
576implied by the largest @code{errno} value defined in @code{<errno.h>}.
568
577
569 int errno_max ();
578We return the maximum value that can be used to obtain a meaningful
579symbolic name or message.
570
580
571DESCRIPTION
581@end deftypefn
572
582
573 Returns the maximum errno value for which a corresponding symbolic
574 name or message is available. Note that in the case where
575 we use the sys_errlist supplied by the system, it is possible for
576 there to be more symbolic names than messages, or vice versa.
577 In fact, the manual page for perror(3C) explicitly warns that one
578 should check the size of the table (sys_nerr) before indexing it,
579 since new error codes may be added to the system before they are
580 added to the table. Thus sys_nerr might be smaller than value
581 implied by the largest errno value defined in <errno.h>.
582
583 We return the maximum value that can be used to obtain a meaningful
584 symbolic name or message.
585
586*/
587
588int
589errno_max ()
590{
591 int maxsize;
592
593 if (error_names == NULL)
594 {
595 init_error_tables ();
596 }
597 maxsize = MAX (sys_nerr, num_error_names);
598 return (maxsize - 1);
599}
600
601#ifndef HAVE_STRERROR
602
603/*
604
583*/
584
585int
586errno_max ()
587{
588 int maxsize;
589
590 if (error_names == NULL)
591 {
592 init_error_tables ();
593 }
594 maxsize = MAX (sys_nerr, num_error_names);
595 return (maxsize - 1);
596}
597
598#ifndef HAVE_STRERROR
599
600/*
601
605NAME
602@deftypefn Supplemental char* strerror (int @var{errnoval})
606
603
607 strerror -- map an error number to an error message string
604Maps an @code{errno} number to an error message string, the contents
605of which are implementation defined. On systems which have the
606external variables @code{sys_nerr} and @code{sys_errlist}, these
607strings will be the same as the ones used by @code{perror}.
608
608
609SYNOPSIS
609If the supplied error number is within the valid range of indices for
610the @code{sys_errlist}, but no message is available for the particular
611error number, then returns the string @samp{Error @var{num}}, where
612@var{num} is the error number.
610
613
611 char *strerror (int errnoval)
614If the supplied error number is not a valid index into
615@code{sys_errlist}, returns @code{NULL}.
612
616
613DESCRIPTION
617The returned string is only guaranteed to be valid only until the
618next call to @code{strerror}.
614
619
615 Maps an errno number to an error message string, the contents of
616 which are implementation defined. On systems which have the external
617 variables sys_nerr and sys_errlist, these strings will be the same
618 as the ones used by perror().
620@end deftypefn
619
621
620 If the supplied error number is within the valid range of indices
621 for the sys_errlist, but no message is available for the particular
622 error number, then returns the string "Error NUM", where NUM is the
623 error number.
624
625 If the supplied error number is not a valid index into sys_errlist,
626 returns NULL.
627
628 The returned string is only guaranteed to be valid only until the
629 next call to strerror.
630
631*/
632
633char *
634strerror (errnoval)
635 int errnoval;
636{
622*/
623
624char *
625strerror (errnoval)
626 int errnoval;
627{
637 char *msg;
628 const char *msg;
638 static char buf[32];
639
640#ifndef HAVE_SYS_ERRLIST
641
642 if (error_names == NULL)
643 {
644 init_error_tables ();
645 }

--- 25 unchanged lines hidden (view full) ---

671 return (msg);
672}
673
674#endif /* ! HAVE_STRERROR */
675
676
677/*
678
629 static char buf[32];
630
631#ifndef HAVE_SYS_ERRLIST
632
633 if (error_names == NULL)
634 {
635 init_error_tables ();
636 }

--- 25 unchanged lines hidden (view full) ---

662 return (msg);
663}
664
665#endif /* ! HAVE_STRERROR */
666
667
668/*
669
679NAME
670@deftypefn Replacement {const char*} strerrno (int @var{errnum})
680
671
681 strerrno -- map an error number to a symbolic name string
672Given an error number returned from a system call (typically returned
673in @code{errno}), returns a pointer to a string containing the
674symbolic name of that error number, as found in @code{<errno.h>}.
682
675
683SYNOPSIS
676If the supplied error number is within the valid range of indices for
677symbolic names, but no name is available for the particular error
678number, then returns the string @samp{Error @var{num}}, where @var{num}
679is the error number.
684
680
685 const char *strerrno (int errnoval)
681If the supplied error number is not within the range of valid
682indices, then returns @code{NULL}.
686
683
687DESCRIPTION
684The contents of the location pointed to are only guaranteed to be
685valid until the next call to @code{strerrno}.
688
686
689 Given an error number returned from a system call (typically
690 returned in errno), returns a pointer to a string containing the
691 symbolic name of that error number, as found in <errno.h>.
687@end deftypefn
692
688
693 If the supplied error number is within the valid range of indices
694 for symbolic names, but no name is available for the particular
695 error number, then returns the string "Error NUM", where NUM is
696 the error number.
697
698 If the supplied error number is not within the range of valid
699 indices, then returns NULL.
700
701BUGS
702
703 The contents of the location pointed to are only guaranteed to be
704 valid until the next call to strerrno.
705
706*/
707
708const char *
709strerrno (errnoval)
710 int errnoval;
711{
712 const char *name;
713 static char buf[32];

--- 25 unchanged lines hidden (view full) ---

739 name = error_names[errnoval];
740 }
741
742 return (name);
743}
744
745/*
746
689*/
690
691const char *
692strerrno (errnoval)
693 int errnoval;
694{
695 const char *name;
696 static char buf[32];

--- 25 unchanged lines hidden (view full) ---

722 name = error_names[errnoval];
723 }
724
725 return (name);
726}
727
728/*
729
747NAME
730@deftypefn Extension int strtoerrno (const char *@var{name})
748
731
749 strtoerrno -- map a symbolic errno name to a numeric value
732Given the symbolic name of a error number (e.g., @code{EACCES}), map it
733to an errno value. If no translation is found, returns 0.
750
734
751SYNOPSIS
735@end deftypefn
752
736
753 int strtoerrno (char *name)
754
755DESCRIPTION
756
757 Given the symbolic name of a error number, map it to an errno value.
758 If no translation is found, returns 0.
759
760*/
761
762int
763strtoerrno (name)
764 const char *name;
765{
766 int errnoval = 0;
767

--- 33 unchanged lines hidden (view full) ---

801#include <stdio.h>
802
803int
804main ()
805{
806 int errn;
807 int errnmax;
808 const char *name;
737*/
738
739int
740strtoerrno (name)
741 const char *name;
742{
743 int errnoval = 0;
744

--- 33 unchanged lines hidden (view full) ---

778#include <stdio.h>
779
780int
781main ()
782{
783 int errn;
784 int errnmax;
785 const char *name;
809 char *msg;
786 const char *msg;
810 char *strerror ();
811
812 errnmax = errno_max ();
813 printf ("%d entries in names table.\n", num_error_names);
814 printf ("%d entries in messages table.\n", sys_nerr);
815 printf ("%d is max useful index.\n", errnmax);
816
817 /* Keep printing values until we get to the end of *both* tables, not

--- 17 unchanged lines hidden ---
787 char *strerror ();
788
789 errnmax = errno_max ();
790 printf ("%d entries in names table.\n", num_error_names);
791 printf ("%d entries in messages table.\n", sys_nerr);
792 printf ("%d is max useful index.\n", errnmax);
793
794 /* Keep printing values until we get to the end of *both* tables, not

--- 17 unchanged lines hidden ---