diff options
-rw-r--r-- | lib/libc/stdlib/Makefile.inc | 6 | ||||
-rw-r--r-- | lib/libc/stdlib/atexit.c | 4 | ||||
-rw-r--r-- | lib/libc/stdlib/getopt.3 | 34 | ||||
-rw-r--r-- | lib/libc/stdlib/radixsort.c | 6 | ||||
-rw-r--r-- | lib/libc/stdlib/random.c | 98 |
5 files changed, 98 insertions, 50 deletions
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 81e8ed0..0f083b9 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 strhash.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 @@ -18,7 +18,7 @@ MAN3+= stdlib/abort.3 stdlib/abs.3 stdlib/alloca.3 stdlib/atexit.3 \ stdlib/getenv.3 stdlib/getopt.3 stdlib/getsubopt.3 stdlib/labs.3 \ stdlib/ldiv.3 stdlib/malloc.3 stdlib/memory.3 stdlib/qsort.3 \ stdlib/radixsort.3 stdlib/rand.3 stdlib/random.3 \ - stdlib/realpath.3 stdlib/strtod.3 stdlib/strtol.3 stdlib/strtoul.3 \ + stdlib/strtod.3 stdlib/strtol.3 stdlib/strtoul.3 \ stdlib/system.3 MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index ea2a7e2..a65dae5 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -35,7 +35,7 @@ */ #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> @@ -43,6 +43,8 @@ static char sccsid[] = "@(#)atexit.c 8.1 (Berkeley) 6/4/93"; #include <unistd.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/getopt.3 b/lib/libc/stdlib/getopt.3 index 3047183..12fdf19 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 .Ql ? if a non-recognized @@ -131,13 +130,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 @@ -179,7 +176,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; @@ -204,6 +201,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 @@ -221,8 +230,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. @@ -239,7 +247,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/radixsort.c b/lib/libc/stdlib/radixsort.c index af47fb0..0a583ac 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 d1ec971..9f5bd2a 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 */ #ifdef COMPAT_WEAK_SEEDING @@ -84,6 +84,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. */ /* @@ -124,8 +143,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: @@ -190,9 +209,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]; static inline long good_rand __P((long)); @@ -241,9 +260,9 @@ static inline long good_rand (x) */ void srandom(x) - unsigned int x; + unsigned long x; { - register int i; + register long i; if (rand_type == TYPE_0) state[0] = x; @@ -276,14 +295,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) - unsigned 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; @@ -291,7 +315,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) { @@ -315,13 +339,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); } @@ -339,14 +363,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) @@ -367,7 +395,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]; @@ -396,18 +424,28 @@ setstate(arg_state) long random() { - long i; + register long i; + register long *f, *r; - if (rand_type == TYPE_0) - i = state[0] = good_rand(state[0]) & 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 = (good_rand(i)) & 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); } |