diff options
author | peter <peter@FreeBSD.org> | 1997-03-11 11:29:42 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 1997-03-11 11:29:42 +0000 |
commit | e0e06d68d52707cbac25844a417ab6919613e9eb (patch) | |
tree | 21479b686e8fe46544082e319ecfa70f9bd25fc6 /lib/libc/stdlib | |
parent | 53b81178e2d0aa56f51f680543678a3d7ef8aecd (diff) | |
download | FreeBSD-src-e0e06d68d52707cbac25844a417ab6919613e9eb.zip FreeBSD-src-e0e06d68d52707cbac25844a417ab6919613e9eb.tar.gz |
Import CSRG 4.4BSD-Lite2 lib/libc onto vendor branch
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r-- | lib/libc/stdlib/Makefile.inc | 6 | ||||
-rw-r--r-- | lib/libc/stdlib/atexit.c | 4 | ||||
-rw-r--r-- | lib/libc/stdlib/atexit.h | 4 | ||||
-rw-r--r-- | lib/libc/stdlib/getopt.3 | 34 | ||||
-rw-r--r-- | lib/libc/stdlib/getopt.c | 10 | ||||
-rw-r--r-- | lib/libc/stdlib/radixsort.c | 6 | ||||
-rw-r--r-- | lib/libc/stdlib/random.c | 99 |
7 files changed, 105 insertions, 58 deletions
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 5298202..9982037 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -1,4 +1,4 @@ -# @(#)Makefile.inc 8.2 (Berkeley) 2/16/94 +# @(#)Makefile.inc 8.3 (Berkeley) 2/4/95 # machine-independent stdlib sources .PATH: ${.CURDIR}/${MACHINE}/stdlib ${.CURDIR}/stdlib @@ -6,7 +6,7 @@ SRCS+= abort.c atexit.c atof.c atoi.c atol.c bsearch.c calloc.c div.c \ exit.c getenv.c getopt.c getsubopt.c heapsort.c labs.c ldiv.c \ malloc.c merge.c putenv.c qsort.c radixsort.c rand.c random.c \ - realpath.c setenv.c strtod.c strtol.c strtoq.c strtoul.c \ + setenv.c strtod.c strtol.c strtoq.c strtoul.c \ strtouq.c system.c # machine-dependent stdlib sources @@ -15,7 +15,7 @@ SRCS+= abort.c atexit.c atof.c atoi.c atol.c bsearch.c calloc.c div.c \ MAN3+= abort.0 abs.0 alloca.0 atexit.0 atof.0 atoi.0 atol.0 bsearch.0 \ calloc.0 div.0 exit.0 free.0 getenv.0 getopt.0 getsubopt.0 labs.0 \ ldiv.0 malloc.0 memory.0 qsort.0 radixsort.0 rand.0 random.0 \ - realloc.0 realpath.0 strtod.0 strtol.0 strtoul.0 system.0 + realloc.0 strtol.0 strtoul.0 system.0 MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index 9c4caed..bbf374e 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -35,13 +35,15 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)atexit.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94"; #endif /* LIBC_SCCS and not lint */ #include <stddef.h> #include <stdlib.h> #include "atexit.h" +struct atexit *__atexit; /* points to head of LIFO stack */ + /* * Register a function to be performed at exit. */ diff --git a/lib/libc/stdlib/atexit.h b/lib/libc/stdlib/atexit.h index 3067e8d..819151e 100644 --- a/lib/libc/stdlib/atexit.h +++ b/lib/libc/stdlib/atexit.h @@ -30,7 +30,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)atexit.h 8.1 (Berkeley) 6/4/93 + * @(#)atexit.h 8.2 (Berkeley) 7/3/94 */ /* must be at least 32 to guarantee ANSI conformance */ @@ -42,4 +42,4 @@ struct atexit { void (*fns[ATEXIT_SIZE])(); /* the table itself */ }; -struct atexit *__atexit; /* points to head of LIFO stack */ +extern struct atexit *__atexit; /* points to head of LIFO stack */ diff --git a/lib/libc/stdlib/getopt.3 b/lib/libc/stdlib/getopt.3 index 95ff6e6..39cc5de 100644 --- a/lib/libc/stdlib/getopt.3 +++ b/lib/libc/stdlib/getopt.3 @@ -29,16 +29,16 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 +.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 .\" -.Dd April 19, 1994 +.Dd April 27, 1995 .Dt GETOPT 3 .Os BSD 4.3 .Sh NAME .Nm getopt .Nd get option character from command line argument list .Sh SYNOPSIS -.Fd #include <stdlib.h> +.Fd #include <unistd.h> .Vt extern char *optarg; .Vt extern int optind; .Vt extern int optopt; @@ -120,8 +120,7 @@ must be reinitialized. The .Fn getopt function -returns an -.Dv EOF +returns \-1 when the argument list is exhausted, or a non-recognized option is encountered. The interpretation of options in the argument list may be cancelled @@ -129,13 +128,11 @@ by the option .Ql -- (double dash) which causes .Fn getopt -to signal the end of argument processing and return an -.Dv EOF . +to signal the end of argument processing and returns \-1. When all options have been processed (i.e., up to the first non-option argument), .Fn getopt -returns -.Dv EOF . +returns \-1. .Sh DIAGNOSTICS If the .Fn getopt @@ -177,7 +174,7 @@ extern int optind; int bflag, ch, fd; bflag = 0; -while ((ch = getopt(argc, argv, "bf:")) != EOF) +while ((ch = getopt(argc, argv, "bf:")) != -1) switch(ch) { case 'b': bflag = 1; @@ -202,6 +199,18 @@ The function appeared .Bx 4.3 . .Sh BUGS +The +.Fn getopt +function was once specified to return +.Dv EOF +instead of \-1. +This was changed by +.St -p1003.2-92 +to decouple +.Fn getopt +from +.Pa <stdio.h> . +.Pp A single dash .Dq Li - may be specified as an character in @@ -219,8 +228,7 @@ It is provided for backward compatibility .Em only . By default, a single dash causes .Fn getopt -to return -.Dv EOF . +to return \-1. This is, we believe, compatible with System V. .Pp It is also possible to handle digits as option letters. @@ -237,7 +245,7 @@ The following code fragment works in most cases. int length; char *p; -while ((c = getopt(argc, argv, "0123456789")) != EOF) +while ((c = getopt(argc, argv, "0123456789")) != -1) switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': diff --git a/lib/libc/stdlib/getopt.c b/lib/libc/stdlib/getopt.c index 994d757..5dddf86 100644 --- a/lib/libc/stdlib/getopt.c +++ b/lib/libc/stdlib/getopt.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)getopt.c 8.2 (Berkeley) 4/2/94"; +static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -67,22 +67,22 @@ getopt(nargc, nargv, ostr) optreset = 0; if (optind >= nargc || *(place = nargv[optind]) != '-') { place = EMSG; - return (EOF); + return (-1); } if (place[1] && *++place == '-') { /* found "--" */ ++optind; place = EMSG; - return (EOF); + return (-1); } } /* option letter okay? */ if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr, optopt))) { /* * if the user didn't specify '-' as an option, - * assume it means EOF. + * assume it means -1. */ if (optopt == (int)'-') - return (EOF); + return (-1); if (!*place) ++optind; if (opterr && *ostr != ':') diff --git a/lib/libc/stdlib/radixsort.c b/lib/libc/stdlib/radixsort.c index d211f3d..b932bf5 100644 --- a/lib/libc/stdlib/radixsort.c +++ b/lib/libc/stdlib/radixsort.c @@ -35,7 +35,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)radixsort.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95"; #endif /* LIBC_SCCS and not lint */ /* @@ -132,7 +132,7 @@ sradixsort(a, n, tab, endch) #define swap(a, b, t) t = a, a = b, b = t /* Unstable, in-place sort. */ -void +static void r_sort_a(a, n, i, tr, endch) const u_char **a; int n, i; @@ -223,7 +223,7 @@ r_sort_a(a, n, i, tr, endch) } /* Stable sort, requiring additional memory. */ -void +static void r_sort_b(a, ta, n, i, tr, endch) const u_char **a, **ta; int n, i; diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c index 18babba..7c76158 100644 --- a/lib/libc/stdlib/random.c +++ b/lib/libc/stdlib/random.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)random.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -76,6 +76,25 @@ static char sccsid[] = "@(#)random.c 8.1 (Berkeley) 6/4/93"; * large deg, when the period of the shift register is the dominant factor. * With deg equal to seven, the period is actually much longer than the * 7*(2**7 - 1) predicted by this formula. + * + * Modified 28 December 1994 by Jacob S. Rosenberg. + * The following changes have been made: + * All references to the type u_int have been changed to unsigned long. + * All references to type int have been changed to type long. Other + * cleanups have been made as well. A warning for both initstate and + * setstate has been inserted to the effect that on Sparc platforms + * the 'arg_state' variable must be forced to begin on word boundaries. + * This can be easily done by casting a long integer array to char *. + * The overall logic has been left STRICTLY alone. This software was + * tested on both a VAX and Sun SpacsStation with exactly the same + * results. The new version and the original give IDENTICAL results. + * The new version is somewhat faster than the original. As the + * documentation says: "By default, the package runs with 128 bytes of + * state information and generates far better random numbers than a linear + * congruential generator. If the amount of state information is less than + * 32 bytes, a simple linear congruential R.N.G. is used." For a buffer of + * 128 bytes, this new version runs about 19 percent faster and for a 16 + * byte buffer it is about 5 percent faster. */ /* @@ -116,8 +135,8 @@ static char sccsid[] = "@(#)random.c 8.1 (Berkeley) 6/4/93"; */ #define MAX_TYPES 5 /* max number of types above */ -static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; -static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; +static long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; +static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; /* * Initially, everything is set up as if from: @@ -171,9 +190,9 @@ static long *rptr = &randtbl[1]; * the last element to see if the front and rear pointers have wrapped. */ static long *state = &randtbl[1]; -static int rand_type = TYPE_3; -static int rand_deg = DEG_3; -static int rand_sep = SEP_3; +static long rand_type = TYPE_3; +static long rand_deg = DEG_3; +static long rand_sep = SEP_3; static long *end_ptr = &randtbl[DEG_3 + 1]; /* @@ -190,14 +209,13 @@ static long *end_ptr = &randtbl[DEG_3 + 1]; */ void srandom(x) - u_int x; + unsigned long x; { - register int i, j; + register long i; if (rand_type == TYPE_0) state[0] = x; else { - j = 1; state[0] = x; for (i = 1; i < rand_deg; i++) state[i] = 1103515245 * state[i - 1] + 12345; @@ -226,14 +244,19 @@ srandom(x) * setstate() so that it doesn't matter when initstate is called. * * Returns a pointer to the old state. + * + * Note: The Sparc platform requires that arg_state begin on a long + * word boundary; otherwise a bus error will occur. Even so, lint will + * complain about mis-alignment, but you should disregard these messages. */ char * initstate(seed, arg_state, n) - u_int seed; /* seed for R.N.G. */ + unsigned long seed; /* seed for R.N.G. */ char *arg_state; /* pointer to state array */ - int n; /* # bytes of state info */ + long n; /* # bytes of state info */ { register char *ostate = (char *)(&state[-1]); + register long *long_arg_state = (long *) arg_state; if (rand_type == TYPE_0) state[-1] = rand_type; @@ -241,7 +264,7 @@ initstate(seed, arg_state, n) state[-1] = MAX_TYPES * (rptr - state) + rand_type; if (n < BREAK_0) { (void)fprintf(stderr, - "random: not enough state (%d bytes); ignored.\n", n); + "random: not enough state (%ld bytes); ignored.\n", n); return(0); } if (n < BREAK_1) { @@ -265,13 +288,13 @@ initstate(seed, arg_state, n) rand_deg = DEG_4; rand_sep = SEP_4; } - state = &(((long *)arg_state)[1]); /* first location */ + state = (long *) (long_arg_state + 1); /* first location */ end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ srandom(seed); if (rand_type == TYPE_0) - state[-1] = rand_type; + long_arg_state[0] = rand_type; else - state[-1] = MAX_TYPES*(rptr - state) + rand_type; + long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type; return(ostate); } @@ -289,14 +312,18 @@ initstate(seed, arg_state, n) * setstate() with the same state as the current state. * * Returns a pointer to the old state information. + * + * Note: The Sparc platform requires that arg_state begin on a long + * word boundary; otherwise a bus error will occur. Even so, lint will + * complain about mis-alignment, but you should disregard these messages. */ char * setstate(arg_state) - char *arg_state; + char *arg_state; /* pointer to state array */ { - register long *new_state = (long *)arg_state; - register int type = new_state[0] % MAX_TYPES; - register int rear = new_state[0] / MAX_TYPES; + register long *new_state = (long *) arg_state; + register long type = new_state[0] % MAX_TYPES; + register long rear = new_state[0] / MAX_TYPES; char *ostate = (char *)(&state[-1]); if (rand_type == TYPE_0) @@ -317,7 +344,7 @@ setstate(arg_state) (void)fprintf(stderr, "random: state info corrupted; not changed.\n"); } - state = &new_state[1]; + state = (long *) (new_state + 1); if (rand_type != TYPE_0) { rptr = &state[rear]; fptr = &state[(rear + rand_sep) % rand_deg]; @@ -346,18 +373,28 @@ setstate(arg_state) long random() { - long i; + register long i; + register long *f, *r; - if (rand_type == TYPE_0) - i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; - else { - *fptr += *rptr; - i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ - if (++fptr >= end_ptr) { - fptr = state; - ++rptr; - } else if (++rptr >= end_ptr) - rptr = state; + if (rand_type == TYPE_0) { + i = state[0]; + state[0] = i = (i * 1103515245 + 12345) & 0x7fffffff; + } else { + /* + * Use local variables rather than static variables for speed. + */ + f = fptr; r = rptr; + *f += *r; + i = (*f >> 1) & 0x7fffffff; /* chucking least random bit */ + if (++f >= end_ptr) { + f = state; + ++r; + } + else if (++r >= end_ptr) { + r = state; + } + + fptr = f; rptr = r; } return(i); } |