diff options
author | Roland McGrath <roland@gnu.org> | 1995-09-17 20:23:15 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 1995-09-17 20:23:15 +0000 |
commit | 60478656fad8d8a487e9bc52d025f69767c3262b (patch) | |
tree | 0ae0836023f1c2f80064e7611f91d19c482208d9 | |
parent | 9fd18b6c1b397e1af82a0b544f10f946c73864b6 (diff) | |
download | glibc-60478656fad8d8a487e9bc52d025f69767c3262b.zip glibc-60478656fad8d8a487e9bc52d025f69767c3262b.tar.gz glibc-60478656fad8d8a487e9bc52d025f69767c3262b.tar.bz2 |
Sat Sep 16 17:47:19 1995 Ulrich Drepper <drepper@ipd.info.uni-karlsruhe.de>
* elf/elf.h (AT_GID): Fix typo: Read -> Real.
* misc/efgvt_r.c: New file. Reentrant version of [efg]cvt functions.
* misc/efgcvt.c: Rewrite to use reentrant functions.
* misc/hsearch_r.c: New file. Reentrant version of functions from
hsearch family.
* misc/hsearch.c, misc/tsearch.c: New files.
* misc/Makefile (routines): Add efgcvt_r, hsearch_r, hsearch, tsearch.
* posix/unistd.h (ttyname_r): Add prototype for new function.
* stdlib/drand48_r.c, stdlib/erand48_r.c, stdlib/jrand48_r.c,
stdlib/lrand48_r.c, stdlib/mrand48_r.c, stdlib/nrand48_r.c,
stdlib/seed48_r.c, stdlib/srand48_r.c, stdlib/lcong48_r.c,
stdlib/drand48-iter.c: New files implementing reentrant versions
of functions from drand48 family.
* stdlib/seed48.c, stdlib/drand48.c, stdlib/erand48.c,
stdlib/jrand48.c, stdlib/lrand48.c, stdlib/mrand48.c,
stdlib/nrand48.c, stdlib/srand48.c, stdlib/lcong48.c:
Rewrite to use reentrant versions.
* stdlib/a64l.c, stdlib/l64a.c: New files. Implement a64l()
and l64a() functions from SysV library.
* stdlib/Makefile (routines): Add drand48_r, erand48_r, lrand48_r,
nrand48_r, mrand48_r, jrand48_r, srand48_r, seed48_r, lcong48_r,
drand48-iter, a64l, l64a.
* stdlib/stdlib.h: Declare them.
* stdlib/random_r.c: New file. Reentrant version of functions
from random family.
* stdlib/stdlib.h: Declare them.
* stdlib/random.c: Rewrite to use reentrant functions.
* string/strerror_r.c: New file. Reentrant version.
* string/strerror.c: Change for new _strerror_internal form.
* string/Makefile (routines): Add strerror_r.
* sysdeps/generic/dl-sysdep.c (_dl_sysdep_start): Set default
value of user_entry to `_start'.
Close AT_ENTRY case with `break'.
* sysdeps/generic/strstr.c: New and much faster implementation
by Stephen R. van den Berg.
* sysdeps/generic/_strerror.c: _strerror_internal now takes
three argument and has and explicit buffer length.
* sysdeps/mach/_strerror.c: Change for new interface with three
arguments.
* stdio/perror.c, stdio/vfprintf.c: Callers changed.
* sysdeps/mach/hurd/ttyname_r.c: New file. Reentrant version.
* sysdeps/posix/ttyname_r.c: New file. Reentrant version.
* sysdeps/stub/ttyname_r: New file. Define as dummy function.
* sysdeps/posix/utimes.c: Include <utime.h> for prototype.
(utimes): First parameter to utime must be file, not path.
* sysdeps/posix/sysconf.c (__sysconf): Test for CLK_TCK in case
_SC_CLK_TCK and return it when available.
Test for STREAM_MAX in case _SC_STREAM_MAX and return it when
available.
Add case for _SC_2_LOCALEDEF which is now available.
* posix/sys/types.h [__USE_SVID] (key_t): New type.
* sysvipc/Makefile, sysvipc/ftok.c, sysvipc/sys/ipc.h,
sysvipc/sys/msg.h, sysvipc/sys/sem.h, sysvipc/sys/shm.h,
sysdeps/stub/sys/msq_buf.h, sysdeps/stub/sys/sem_buf.h,
sysdeps/stub/sys/shm_buf.h, sysdeps/stub/sys/ipc_buf.h,
sysdeps/stub/semctl.c, sysdeps/stub/semget.c, sysdeps/stub/semop.c,
sysdeps/stub/shmat.c, sysdeps/stub/shmctl.c, sysdeps/stub/shmdt.c,
sysdeps/stub/shmget.c, sysdeps/stub/msgctl.c, sysdeps/stub/msgget.c,
sysdeps/stub/msgrcv.c, sysdeps/stub/msgsnd.c: New files.
Add implementation of System V IPC.
71 files changed, 3279 insertions, 235 deletions
@@ -1,3 +1,77 @@ +Sat Sep 16 17:47:19 1995 Ulrich Drepper <drepper@ipd.info.uni-karlsruhe.de> + + * elf/elf.h (AT_GID): Fix typo: Read -> Real. + + * misc/efgvt_r.c: New file. Reentrant version of [efg]cvt functions. + * misc/efgcvt.c: Rewrite to use reentrant functions. + * misc/hsearch_r.c: New file. Reentrant version of functions from + hsearch family. + * misc/hsearch.c, misc/tsearch.c: New files. + * misc/Makefile (routines): Add efgcvt_r, hsearch_r, hsearch, tsearch. + + * posix/unistd.h (ttyname_r): Add prototype for new function. + + * stdlib/drand48_r.c, stdlib/erand48_r.c, stdlib/jrand48_r.c, + stdlib/lrand48_r.c, stdlib/mrand48_r.c, stdlib/nrand48_r.c, + stdlib/seed48_r.c, stdlib/srand48_r.c, stdlib/lcong48_r.c, + stdlib/drand48-iter.c: New files implementing reentrant versions + of functions from drand48 family. + * stdlib/seed48.c, stdlib/drand48.c, stdlib/erand48.c, + stdlib/jrand48.c, stdlib/lrand48.c, stdlib/mrand48.c, + stdlib/nrand48.c, stdlib/srand48.c, stdlib/lcong48.c: + Rewrite to use reentrant versions. + * stdlib/a64l.c, stdlib/l64a.c: New files. Implement a64l() + and l64a() functions from SysV library. + * stdlib/Makefile (routines): Add drand48_r, erand48_r, lrand48_r, + nrand48_r, mrand48_r, jrand48_r, srand48_r, seed48_r, lcong48_r, + drand48-iter, a64l, l64a. + * stdlib/stdlib.h: Declare them. + + * stdlib/random_r.c: New file. Reentrant version of functions + from random family. + * stdlib/stdlib.h: Declare them. + * stdlib/random.c: Rewrite to use reentrant functions. + + * string/strerror_r.c: New file. Reentrant version. + * string/strerror.c: Change for new _strerror_internal form. + * string/Makefile (routines): Add strerror_r. + + * sysdeps/generic/dl-sysdep.c (_dl_sysdep_start): Set default + value of user_entry to `_start'. + Close AT_ENTRY case with `break'. + + * sysdeps/generic/strstr.c: New and much faster implementation + by Stephen R. van den Berg. + + * sysdeps/generic/_strerror.c: _strerror_internal now takes + three argument and has and explicit buffer length. + * sysdeps/mach/_strerror.c: Change for new interface with three + arguments. + * stdio/perror.c, stdio/vfprintf.c: Callers changed. + + * sysdeps/mach/hurd/ttyname_r.c: New file. Reentrant version. + * sysdeps/posix/ttyname_r.c: New file. Reentrant version. + * sysdeps/stub/ttyname_r: New file. Define as dummy function. + + * sysdeps/posix/utimes.c: Include <utime.h> for prototype. + (utimes): First parameter to utime must be file, not path. + + * sysdeps/posix/sysconf.c (__sysconf): Test for CLK_TCK in case + _SC_CLK_TCK and return it when available. + Test for STREAM_MAX in case _SC_STREAM_MAX and return it when + available. + Add case for _SC_2_LOCALEDEF which is now available. + + * posix/sys/types.h [__USE_SVID] (key_t): New type. + * sysvipc/Makefile, sysvipc/ftok.c, sysvipc/sys/ipc.h, + sysvipc/sys/msg.h, sysvipc/sys/sem.h, sysvipc/sys/shm.h, + sysdeps/stub/sys/msq_buf.h, sysdeps/stub/sys/sem_buf.h, + sysdeps/stub/sys/shm_buf.h, sysdeps/stub/sys/ipc_buf.h, + sysdeps/stub/semctl.c, sysdeps/stub/semget.c, sysdeps/stub/semop.c, + sysdeps/stub/shmat.c, sysdeps/stub/shmctl.c, sysdeps/stub/shmdt.c, + sysdeps/stub/shmget.c, sysdeps/stub/msgctl.c, sysdeps/stub/msgget.c, + sysdeps/stub/msgrcv.c, sysdeps/stub/msgsnd.c: New files. + Add implementation of System V IPC. Fri Sep 15 21:34:28 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu> * hurd/hurdsig.c: Include <hurd/crash.h> in place of <hurd/core.h>. @@ -430,7 +430,7 @@ typedef struct #define AT_NOTELF 10 /* Program is not ELF */ #define AT_UID 11 /* Real uid */ #define AT_EUID 12 /* Effective uid */ -#define AT_GID 13 /* Read gid */ +#define AT_GID 13 /* Real gid */ #define AT_EGID 14 /* Effective gid */ /* Intel 80386 specific definitions. */ diff --git a/misc/Makefile b/misc/Makefile index c626dd7..213ca9f 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -25,7 +25,7 @@ subdir := misc headers := sys/uio.h sys/ioctl.h sys/ptrace.h ioctls.h sys/file.h \ a.out.h nlist.h stab.h stab.def sgtty.h sys/dir.h sys/cdefs.h \ ttyent.h syscall.h syslog.h sys/syslog.h paths.h sys/reboot.h \ - sys/mman.h sys/param.h fstab.h mntent.h search.h utmp.h + sys/mman.h sys/param.h fstab.h search.h utmp.h routines := brk sbrk sstk ioctl \ readv writev \ @@ -42,14 +42,15 @@ routines := brk sbrk sstk ioctl \ ualarm usleep \ gtty stty \ ptrace \ - nlist fstab mntent \ + nlist fstab \ utimes \ truncate ftruncate \ chflags fchflags \ insremque getttyent getusershell getpass ttyslot \ syslog syscall daemon \ mmap munmap mprotect msync madvise \ - efgcvt + efgcvt efgcvt_r \ + hsearch hsearch_r tsearch aux := progname init-misc distribute := bsd-compat.c extra-objs := bsd-compat.o diff --git a/misc/efgcvt.c b/misc/efgcvt.c index 6473475..95b0b0d 100644 --- a/misc/efgcvt.c +++ b/misc/efgcvt.c @@ -1,4 +1,4 @@ -/* [efg]cvt -- compatibility functions for floating point formatting +/* [efg]cvt -- compatibility functions for floating point formatting. Copyright (C) 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -18,9 +18,7 @@ not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <stdio.h> -#include <string.h> -#include <ctype.h> -#include <math.h> +#include <stdlib.h> char * fcvt (value, ndigit, decpt, sign) @@ -28,24 +26,8 @@ fcvt (value, ndigit, decpt, sign) int ndigit, *decpt, *sign; { static char buf[100]; - int n, i; - *sign = value < 0.0; - if (*sign) - value = - value; - - n = snprintf (buf, sizeof buf, "%.*f", ndigit, value); - if (n < 0) - return NULL; - - i = 0; - while (i < n && isdigit (buf[i])) - ++i; - *decpt = i; - do - ++i; - while (! isdigit (buf[i])); - memmove (&buf[i - *decpt], buf, n - (i - *decpt)); + (void) fcvt_r (value, ndigit, decpt, sign, buf, sizeof buf); return buf; } @@ -55,10 +37,11 @@ ecvt (value, ndigit, decpt, sign) double value; int ndigit, *decpt, *sign; { - ndigit -= (int) floor (log10 (value)); - if (ndigit < 0) - ndigit = 0; - return fcvt (value, ndigit, decpt, sign); + static char buf[100]; + + (void) ecvt_r (value, ndigit, decpt, sign, buf, sizeof buf); + + return buf; } char * diff --git a/misc/efgcvt_r.c b/misc/efgcvt_r.c new file mode 100644 index 0000000..fb0d591 --- /dev/null +++ b/misc/efgcvt_r.c @@ -0,0 +1,73 @@ +/* [efg]cvt -- compatibility functions for floating point formatting, + reentrent versions. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <ctype.h> +#include <math.h> + +int +fcvt_r (value, ndigit, decpt, sign, buf, len) + double value; + int ndigit, *decpt, *sign; + char *buf; + int len; +{ + int n, i; + + if (buf == NULL) + { + errno = EINVAL; + return -1; + } + + *sign = value < 0.0; + if (*sign) + value = - value; + + n = snprintf (buf, len, "%.*f", ndigit, value); + if (n < 0) + return -1; + + i = 0; + while (i < n && isdigit (buf[i])) + ++i; + *decpt = i; + do + ++i; + while (! isdigit (buf[i])); + memmove (&buf[i - *decpt], buf, n - (i - *decpt)); + + return 0; +} + +int +ecvt_r (value, ndigit, decpt, sign, buf, len) + double value; + int ndigit, *decpt, *sign; + char *buf; + int len; +{ + ndigit -= (int) floor (log10 (value)); + if (ndigit < 0) + ndigit = 0; + return fcvt_r (value, ndigit, decpt, sign, buf, len); +} diff --git a/misc/hsearch.c b/misc/hsearch.c new file mode 100644 index 0000000..7df8686 --- /dev/null +++ b/misc/hsearch.c @@ -0,0 +1,52 @@ +/* Copyright (C) 1993, 1995 Free Software Foundation, Inc. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu> +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <search.h> + +/* The non-reenttrent version use a global space for storing the table. */ +static struct hsearch_data htab; + + +/* Define the non-reentrent function using the reentrent counterparts. */ +ENTRY * +hsearch (item, action) + ENTRY item; + ACTION action; +{ + ENTRY *result; + + (void) hsearch_r (item, action, &result, &htab); + + return result; +} + + +int +hcreate (nel) + unsigned int nel; +{ + return hcreate_r (nel, &htab); +} + + +void +hdestroy () +{ + hdestroy_r (&htab); +} diff --git a/misc/hsearch_r.c b/misc/hsearch_r.c new file mode 100644 index 0000000..5ea1d5e --- /dev/null +++ b/misc/hsearch_r.c @@ -0,0 +1,229 @@ +/* Copyright (C) 1993, 1995 Free Software Foundation, Inc. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu> +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <errno.h> +#include <malloc.h> +#include <string.h> + +#include <search.h> + +/* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986 + [Knuth] The Art of Computer Programming, part 3 (6.4) */ + + +/* The reentrent version has no static variables to maintain the state. + Instead the interface of all functions is extended to take an argument + which describes the current status. */ +typedef struct _ENTRY +{ + int used; + ENTRY entry; +} +_ENTRY; + + +/* For the used double hash method the table size has to be a prime. To + correct the user given table size we need a prime test. This trivial + algorithm is adequate because + a) the code is (most probably) called a few times per program run and + b) the number is small because the table must fit in the core */ +static int +isprime (number) + unsigned int number; +{ + /* no even number will be passed */ + unsigned int div = 3; + + while (div * div < number && number % div != 0) + div += 2; + + return number % div != 0; +} + + +/* Before using the hash table we must allocate memory for it. + Test for an existing table are done. We allocate one element + more as the found prime number says. This is done for more effective + indexing as explained in the comment for the hsearch function. + The contents of the table is zeroed, especially the field used + becomes zero. */ +int +hcreate_r (nel, htab) + unsigned int nel; + struct hsearch_data *htab; +{ + /* Test for correct arguments. */ + if (htab == NULL) + { + errno = EINVAL; + return 0; + } + + /* There is still another table active. Return with error. */ + if (htab->table != NULL) + return 0; + + /* Change nel to the first prime number not smaller as nel. */ + nel |= 1; /* make odd */ + while (!isprime (nel)) + nel += 2; + + htab->size = nel; + htab->filled = 0; + + /* allocate memory and zero out */ + htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY)); + if (htab->table == NULL) + return 0; + + /* everything went alright */ + return 1; +} + + +/* After using the hash table it has to be destroyed. The used memory can + be freed and the local static variable can be marked as not used. */ +void +hdestroy_r (htab) + struct hsearch_data *htab; +{ + /* Test for correct arguments. */ + if (htab == NULL) + { + errno = EINVAL; + return; + } + + if (htab->table != NULL) + /* free used memory */ + free (htab->table); + + /* the sign for an existing table is an value != NULL in htable */ + htab->table = NULL; +} + + +/* This is the search function. It uses double hashing with open adressing. + The argument item.key has to be a pointer to an zero terminated, most + probably strings of chars. The function for generating a number of the + strings is simple but fast. It can be replaced by a more complex function + like ajw (see [Aho,Sethi,Ullman]) if the needs are shown. + + We use an trick to speed up the lookup. The table is created by hcreate + with one more element available. This enables us to use the index zero + special. This index will never be used because we store the first hash + index in the field used where zero means not used. Every other value + means used. The used field can be used as a first fast comparison for + equality of the stored and the parameter value. This helps to prevent + unnecessary expensive calls of strcmp. */ +int +hsearch_r (item, action, retval, htab) + ENTRY item; + ACTION action; + ENTRY **retval; + struct hsearch_data *htab; +{ + unsigned int hval; + unsigned int count; + unsigned int len = strlen (item.key); + unsigned int idx; + + /* If table is full and another entry should be entered return with + error. */ + if (action == ENTER && htab->filled == htab->size) + { + errno = ENOMEM; + *retval = NULL; + return 0; + } + + /* Compute an value for the given string. Perhaps use a better method. */ + hval = len; + count = len; + while (count-- > 0) + { + hval <<= 4; + hval += item.key[count]; + } + + /* First hash function: simply take the modul but prevent zero. */ + hval %= htab->size; + if (hval == 0) + ++hval; + + /* The first index tried. */ + idx = hval; + + if (htab->table[idx].used) + { + /* Further action might be required according to the action value. */ + unsigned hval2; + + if (htab->table[idx].used == hval + && strcmp (item.key, htab->table[idx].entry.key) == 0) + { + if (action == ENTER) + htab->table[idx].entry.data = item.data; + + *retval = &htab->table[idx].entry; + return 1; + } + + /* Second hash function, as suggested in [Knuth] */ + hval2 = 1 + hval % (htab->size - 2); + + do + { + /* Because SIZE is prime this guarantees to step through all + available indeces. */ + if (idx <= hval2) + idx = htab->size + idx - hval2; + else + idx -= hval2; + + /* If entry is found use it. */ + if (htab->table[idx].used == hval + && strcmp (item.key, htab->table[idx].entry.key) == 0) + { + if (action == ENTER) + htab->table[idx].entry.data = item.data; + + *retval = &htab->table[idx].entry; + return 1; + } + } + while (htab->table[idx].used); + } + + /* An empty bucket has been found. */ + if (action == ENTER) + { + htab->table[idx].used = hval; + htab->table[idx].entry = item; + + ++htab->filled; + + *retval = &htab->table[idx].entry; + return 1; + } + + errno = ESRCH; + *retval = NULL; + return 0; +} diff --git a/misc/tsearch.c b/misc/tsearch.c new file mode 100644 index 0000000..cb06b7d --- /dev/null +++ b/misc/tsearch.c @@ -0,0 +1,215 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +/* Tree search generalized from Knuth (6.2.2) Algorithm T just like + the AT&T man page says. + + The node_t structure is for internal use only, lint doesn't grok it. + + Written by reading the System V Interface Definition, not the code. + + Totally public domain. */ +/*LINTLIBRARY*/ + +#include <stdlib.h> +#include <search.h> + +/* This routine is not very bad. It makes many assumptions about + the compiler. It assumpts that the first field in node must be + the "key" field, which points to the datum. It is a very trick + stuff. H.J. */ + +typedef struct node_t +{ + const void *key; + struct node_t *left; + struct node_t *right; +} +node; + +/* Prototype fpr local function. */ +static void trecurse __P ((const void *vroot, __action_fn_t action, int level)); + + +/* find or insert datum into search tree. +char *key; key to be located +node **rootp; address of tree root +int (*compar)(); ordering function +*/ +void * +tsearch (key, vrootp, compar) + const void *key; + void **vrootp; + __compar_fn_t compar; +{ + node *q; + node **rootp = (node **) vrootp; + + if (rootp == NULL) + return NULL; + + while (*rootp != NULL) /* Knuth's T1: */ + { + int r; + + r = (*compar) (key, (*rootp)->key); + if (r == 0) /* T2: */ + return *rootp; /* we found it! */ + rootp = (r < 0) + ? &(*rootp)->left /* T3: follow left branch */ + : &(*rootp)->right; /* T4: follow right branch */ + } + + q = (node *) malloc (sizeof (node)); /* T5: key not found */ + if (q != NULL) /* make new node */ + { + *rootp = q; /* link new node to old */ + q->key = key; /* initialize new node */ + q->left = q->right = NULL; + } + + return q; +} + + +void * +tfind (key, vrootp, compar) + const void *key; + const void **vrootp; + __compar_fn_t compar; +{ + node **rootp = (node **) vrootp; + + if (rootp == NULL) + return NULL; + + while (*rootp != NULL) /* Knuth's T1: */ + { + int r; + + r = (*compar)(key, (*rootp)->key); + if (r == 0) /* T2: */ + return *rootp; /* we found it! */ + + rootp = (r < 0) + ? &(*rootp)->left /* T3: follow left branch */ + : &(*rootp)->right; /* T4: follow right branch */ + } + return NULL; +} + + +/* delete node with given key +char *key; key to be deleted +node **rootp; address of the root of tree +int (*compar)(); comparison function +*/ +void * +tdelete (key, vrootp, compar) + const void *key; + void **vrootp; + __compar_fn_t compar; +{ + node *p; + node *q; + node *r; + int cmp; + node **rootp = (node **) vrootp; + + if (rootp == NULL || (p = *rootp) == NULL) + return NULL; + + while ((cmp = (*compar) (key, (*rootp)->key)) != 0) + { + p = *rootp; + rootp = (cmp < 0) + ? &(*rootp)->left /* follow left branch */ + : &(*rootp)->right; /* follow right branch */ + if (*rootp == NULL) + return NULL; /* key not found */ + } + + r = (*rootp)->right; /* D1: */ + q = (*rootp)->left; + if (q == NULL) /* Left NULL? */ + q = r; + else if (r != NULL) /* Right link is NULL? */ + { + if (r->left == NULL) /* D2: Find successor */ + { + r->left = q; + q = r; + } + else + { /* D3: Find (struct node_t *)0 link */ + for (q = r->left; q->left != NULL; q = r->left) + r = q; + r->left = q->right; + q->left = (*rootp)->left; + q->right = (*rootp)->right; + } + } + free ((struct node_t *) *rootp); /* D4: Free node */ + *rootp = q; /* link parent to new node */ + return p; +} + + +/* Walk the nodes of a tree +node *root; Root of the tree to be walked +void (*action)(); Function to be called at each node +int level; +*/ +static void +trecurse (vroot, action, level) + const void *vroot; + __action_fn_t action; + int level; +{ + node *root = (node *) vroot; + + if (root->left == NULL && root->right == NULL) + (*action) (root, leaf, level); + else + { + (*action) (root, preorder, level); + if (root->left != NULL) + trecurse (root->left, action, level + 1); + (*action) (root, postorder, level); + if (root->right != NULL) + trecurse (root->right, action, level + 1); + (*action) (root, endorder, level); + } +} + + +/* void twalk(root, action) Walk the nodes of a tree +node *root; Root of the tree to be walked +void (*action)(); Function to be called at each node +PTR +*/ +void +twalk (vroot, action) + const void *vroot; + __action_fn_t action; +{ + const node *root = (node *) vroot; + + if (root != NULL && action != NULL) + trecurse (root, action, 0); +} diff --git a/posix/sys/types.h b/posix/sys/types.h index 8178303..207ae8d 100644 --- a/posix/sys/types.h +++ b/posix/sys/types.h @@ -89,6 +89,12 @@ typedef int register_t __attribute__ ((__mode__ (word))); #endif +#ifdef __USE_SVID +/* Data type for key value used in System V IPC functions. */ +typedef long int key_t; +#endif + + #ifdef __USE_BSD #define FD_SETSIZE __FD_SETSIZE diff --git a/posix/unistd.h b/posix/unistd.h index 38ca4f0..eda44c9 100644 --- a/posix/unistd.h +++ b/posix/unistd.h @@ -419,6 +419,11 @@ extern __pid_t vfork __P ((void)); /* Return the pathname of the terminal FD is open on, or NULL on errors. The returned storage is good only until the next call to this function. */ extern char *ttyname __P ((int __fd)); +#ifdef __USE_REENTRANT +/* Store at most BUFLEN characters of the pathname of the terminal FD is + open on in BUF. Return 0 on success, -1 otherwise. */ +extern int ttyname_r __P ((int __fd, char *__buf, int __buflen)); +#endif /* Return 1 if FD is a valid descriptor associated with a terminal, zero if not. */ diff --git a/search.h b/search.h new file mode 100644 index 0000000..ff24508 --- /dev/null +++ b/search.h @@ -0,0 +1 @@ +#include <misc/search.h> diff --git a/stdio/perror.c b/stdio/perror.c index 718a6f2..1054aca 100644 --- a/stdio/perror.c +++ b/stdio/perror.c @@ -20,7 +20,7 @@ Cambridge, MA 02139, USA. */ #include <stdio.h> #include <errno.h> -extern char *_strerror_internal __P ((int, char buf[1024])); +extern char *_strerror_internal __P ((int, char *buf, size_t)); /* Print a line on stderr consisting of the text in S, a colon, a space, a message describing the meaning of the contents of `errno' and a newline. @@ -38,5 +38,5 @@ DEFUN(perror, (s), register CONST char *s) colon = ": "; (void) fprintf (stderr, "%s%s%s\n", - s, colon, _strerror_internal (errnum, buf)); + s, colon, _strerror_internal (errnum, buf, sizeof buf)); } diff --git a/stdio/vfprintf.c b/stdio/vfprintf.c index 06aa0a0..63a5148 100644 --- a/stdio/vfprintf.c +++ b/stdio/vfprintf.c @@ -534,7 +534,7 @@ vfprintf (s, format, ap) else if (specs[cnt].info.prec != -1) { /* Search for the end of the string, but don't search - past the length specified by the precision. */ + past the length specified by the precision. */ const char *end = memchr (str, '\0', specs[cnt].info.prec); if (end) len = end - str; @@ -600,8 +600,8 @@ vfprintf (s, format, ap) case 'm': { - extern char *_strerror_internal __P ((int, char buf[1024])); - str = _strerror_internal (errno, errorbuf); + extern char *_strerror_internal __P ((int, char *buf, size_t)); + str = _strerror_internal (errno, errorbuf, sizeof errorbuf); goto string; } diff --git a/stdlib/Makefile b/stdlib/Makefile index 3ea206f..a28d2a8 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -32,10 +32,16 @@ routines := \ abs labs \ div ldiv \ mblen mbstowcs mbtowc wcstombs wctomb \ - random rand \ + random random_r rand \ + drand48 erand48 lrand48 nrand48 mrand48 jrand48 \ + srand48 seed48 lcong48 \ + drand48_r erand48_r lrand48_r nrand48_r mrand48_r jrand48_r \ + srand48_r seed48_r lcong48_r \ + drand48-iter \ strtol strtoul strtoq strtouq \ strtof strtod strtold \ - system + system \ + a64l l64a distribute := exit.h grouping.h tests := tst-strtol tst-strtod testmb testrand testsort testdiv diff --git a/stdlib/a64l.c b/stdlib/a64l.c new file mode 100644 index 0000000..3fdaab5 --- /dev/null +++ b/stdlib/a64l.c @@ -0,0 +1,54 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +long +a64l (string) + const char *string; +{ + int cnt; + long result = 0l; + + for (cnt = 0; cnt < 6; ++cnt) + { + result <<= 6; + switch (string[cnt]) + { + case '.': + break; + case '/': + result |= 1; + break; + case '0' ... '9': + result |= 2 + string[cnt] - '0'; + break; + case 'A' ... 'Z': + result |= 12 + string[cnt] - 'A'; + break; + case 'a' ... 'z': + result |= 38 + string[cnt] - 'a'; + break; + default: + return result >> 6; + } + } + + return result; +} diff --git a/stdlib/drand48-iter.c b/stdlib/drand48-iter.c new file mode 100644 index 0000000..013dbe7 --- /dev/null +++ b/stdlib/drand48-iter.c @@ -0,0 +1,102 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <errno.h> +#include <stdlib.h> + + +/* Global state for non-reentrent functions. */ +struct drand48_data __libc_drand48_data; + + +int +__drand48_iterate (xsubi, buffer) + unsigned short int xsubi[3]; + struct drand48_data *buffer; +{ + /* Be generous for the arguments, detect some errors. */ + if (xsubi == NULL || buffer == NULL) + { + errno = EFAULT; + return -1; + } + + /* Initialize buffer, if not yet done. */ + if (!buffer->init) + { + if (sizeof (unsigned short int) == 2) + { + buffer->a[2] = 0x5; + buffer->a[1] = 0xdeec; + buffer->a[0] = 0xe66d; + } + else + { + buffer->a[2] = 0x5deec; + buffer->a[1] = 0xe66d0000; + buffer->a[0] = 0; + } + buffer->c = 0xb; + buffer->init = 1; + } + + /* Do the real work. We choose a data type which contains at least + 48 bits. Because we compute the modulus it does not care how + many bits really are computed. */ + + if (sizeof (long int) >= 6) + { + /* The `long' data type is sufficent. */ + unsigned long int X, a, result; + +#define ONE_STEP \ + if (sizeof (unsigned short int) == 2) \ + { \ + X = (xsubi[2] << 16 | xsubi[1]) << 16 | xsubi[0]; \ + a = (buffer->a[2] << 16 | buffer->a[1]) << 16 | buffer->a[0]; \ + \ + result = X * a + buffer->c; \ + \ + xsubi[0] = result & 0xffff; \ + result >>= 16; \ + xsubi[1] = result & 0xffff; \ + result >>= 16; \ + xsubi[2] = result & 0xffff; \ + } \ + else \ + { \ + X = xsubi[2] << 16 | xsubi[1] >> 16; \ + a = buffer->a[2] << 16 | buffer->a[1] >> 16; \ + \ + result = X * a + buffer->c; \ + \ + xsubi[0] = result >> 16 & 0xffffffffl; \ + xsubi[1] = result << 16 & 0xffff0000l; \ + } + ONE_STEP; + } + else + { + /* We have to use the `long long' data type. */ + unsigned long long int X, a, result; + ONE_STEP; + } + + return 0; +} diff --git a/stdlib/drand48.c b/stdlib/drand48.c new file mode 100644 index 0000000..e2d8450 --- /dev/null +++ b/stdlib/drand48.c @@ -0,0 +1,33 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +double +drand48 () +{ + double result; + + (void) erand48_r (__libc_drand48_data.X, &__libc_drand48_data, &result); + + return result; +} diff --git a/stdlib/drand48_r.c b/stdlib/drand48_r.c new file mode 100644 index 0000000..eaba057 --- /dev/null +++ b/stdlib/drand48_r.c @@ -0,0 +1,37 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <errno.h> +#include <math.h> +#include <stdlib.h> + +int +drand48_r (buffer, result) + struct drand48_data *buffer; + double *result; +{ + /* be generous for the arguments, detect some errors. */ + if (buffer == NULL) + { + errno = EFAULT; + return -1; + } + + return erand48_r (buffer->X, buffer, result); +} diff --git a/stdlib/erand48.c b/stdlib/erand48.c new file mode 100644 index 0000000..b63c3bd --- /dev/null +++ b/stdlib/erand48.c @@ -0,0 +1,34 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +double +erand48 (xsubi) + unsigned short int xsubi[3]; +{ + double result; + + (void) erand48_r (xsubi, &__libc_drand48_data, &result); + + return result; +} diff --git a/stdlib/erand48_r.c b/stdlib/erand48_r.c new file mode 100644 index 0000000..86d2f73 --- /dev/null +++ b/stdlib/erand48_r.c @@ -0,0 +1,52 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <errno.h> +#include <math.h> +#include <stdlib.h> + +int +erand48_r (xsubi, buffer, result) + unsigned short int xsubi[3]; + struct drand48_data *buffer; + double *result; +{ + int i; + + /* Be generous for the arguments, detect some errors. */ + if (result == NULL) + { + errno = EFAULT; + return -1; + } + + /* Compute next state. */ + if (__drand48_iterate (xsubi, buffer) < 0) + return -1; + + *result = 0.0; + for (i = 4 / sizeof (unsigned short int); i >= 0; --i) + { + double factor = ldexp (1.0, (i - 6) * sizeof (unsigned short int)); + + *result += factor * (double) xsubi[i]; + } + + return 0; +} diff --git a/stdlib/jrand48.c b/stdlib/jrand48.c new file mode 100644 index 0000000..bdd62fb --- /dev/null +++ b/stdlib/jrand48.c @@ -0,0 +1,34 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +long +jrand48 (xsubi) + unsigned short int xsubi[3]; +{ + long result; + + (void) jrand48_r (xsubi, &__libc_drand48_data, &result); + + return result; +} diff --git a/stdlib/jrand48_r.c b/stdlib/jrand48_r.c new file mode 100644 index 0000000..b1a4378 --- /dev/null +++ b/stdlib/jrand48_r.c @@ -0,0 +1,49 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +jrand48_r (xsubi, buffer, result) + unsigned short int xsubi[3]; + struct drand48_data *buffer; + long *result; +{ + /* Be generous for the arguments, detect some errors. */ + if (result == NULL) + { + errno = EFAULT; + return -1; + } + + /* Compute next state. */ + if (__drand48_iterate (xsubi, buffer) < 0) + return -1; + + /* Store the result. */ + if (sizeof (unsigned short int) == 2) + *result = (xsubi[2] & 0x7fff) | xsubi[1]; + else + *result = xsubi[2] & 0x7fffffffl; + + if (xsubi[2] & (1 << (sizeof (xsubi[2]) * 8 - 1))) + *result *= -1; + + return 0; +} diff --git a/stdlib/l64a.c b/stdlib/l64a.c new file mode 100644 index 0000000..ad19a3b --- /dev/null +++ b/stdlib/l64a.c @@ -0,0 +1,51 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Conversion table. */ +static const char conv_tab[64] = +{ + '.', '/', '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', + 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', + 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' +}; + +const char * +l64a (n) + long n; +{ + static char result[7]; + int cnt; + + result[6] = '\0'; + + for (cnt = 5; cnt >= 0; --cnt) + { + result[cnt] = n & 0x3f; + n >>= 6; + } + + return result; +} diff --git a/stdlib/lcong48.c b/stdlib/lcong48.c new file mode 100644 index 0000000..7e4d188 --- /dev/null +++ b/stdlib/lcong48.c @@ -0,0 +1,30 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +void +lcong48 (param) + unsigned short int param[7]; +{ + (void) lcong48_r (param, &__libc_drand48_data); +} diff --git a/stdlib/lcong48_r.c b/stdlib/lcong48_r.c new file mode 100644 index 0000000..cc38b4d --- /dev/null +++ b/stdlib/lcong48_r.c @@ -0,0 +1,41 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +lcong48_r (param, buffer) + unsigned short int param[7]; + struct drand48_data *buffer; +{ + /* Be generous for the arguments, detect some errors. */ + if (buffer == NULL) + { + errno = EFAULT; + return -1; + } + + /* Store the given values. */ + memcpy (buffer->X, ¶m[0], sizeof (buffer->X)); + memcpy (buffer->a, ¶m[3], sizeof (buffer->a)); + buffer->c = param[6]; + buffer->init = 1; + + return 0; +} diff --git a/stdlib/lrand48.c b/stdlib/lrand48.c new file mode 100644 index 0000000..a3e6ea1 --- /dev/null +++ b/stdlib/lrand48.c @@ -0,0 +1,33 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +long +lrand48 () +{ + long result; + + (void) nrand48_r (__libc_drand48_data.X, &__libc_drand48_data, &result); + + return result; +} diff --git a/stdlib/lrand48_r.c b/stdlib/lrand48_r.c new file mode 100644 index 0000000..696722a --- /dev/null +++ b/stdlib/lrand48_r.c @@ -0,0 +1,32 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +lrand48_r (buffer, result) + struct drand48_data *buffer; + long *result; +{ + /* Be generous for the arguments, detect some errors. */ + if (buffer == NULL) + return -1; + + return nrand48_r (buffer->X, buffer, result); +} diff --git a/stdlib/mrand48.c b/stdlib/mrand48.c new file mode 100644 index 0000000..e71b234 --- /dev/null +++ b/stdlib/mrand48.c @@ -0,0 +1,33 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +long +mrand48 () +{ + long result; + + (void) jrand48_r (__libc_drand48_data.X, &__libc_drand48_data, &result); + + return result; +} diff --git a/stdlib/mrand48_r.c b/stdlib/mrand48_r.c new file mode 100644 index 0000000..92d1b11 --- /dev/null +++ b/stdlib/mrand48_r.c @@ -0,0 +1,32 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +mrand48_r (buffer, result) + struct drand48_data *buffer; + long *result; +{ + /* Be generous for the arguments, detect some errors. */ + if (buffer == NULL) + return -1; + + return jrand48_r (buffer->X, buffer, result); +} diff --git a/stdlib/nrand48.c b/stdlib/nrand48.c new file mode 100644 index 0000000..0dd2c2b --- /dev/null +++ b/stdlib/nrand48.c @@ -0,0 +1,34 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +long +nrand48 (xsubi) + unsigned short int xsubi[3]; +{ + long result; + + (void) nrand48_r (xsubi, &__libc_drand48_data, &result); + + return result; +} diff --git a/stdlib/nrand48_r.c b/stdlib/nrand48_r.c new file mode 100644 index 0000000..1bbc29e --- /dev/null +++ b/stdlib/nrand48_r.c @@ -0,0 +1,46 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +nrand48_r (xsubi, buffer, result) + unsigned short int xsubi[3]; + struct drand48_data *buffer; + long *result; +{ + /* Be generous for the arguments, detect some errors. */ + if (result == NULL) + { + errno = EFAULT; + return -1; + } + + /* Compute next state. */ + if (__drand48_iterate (xsubi, buffer) < 0) + return -1; + + /* Store the result. */ + if (sizeof (unsigned short int) == 2) + *result = xsubi[2] << 15 | xsubi[1] >> 1; + else + *result = xsubi[2] >> 1; + + return 0; +} diff --git a/stdlib/random.c b/stdlib/random.c index 473a5b1..c3f8eaa 100644 --- a/stdlib/random.c +++ b/stdlib/random.c @@ -19,10 +19,9 @@ * This is derived from the Berkeley source: * @(#)random.c 5.5 (Berkeley) 7/6/88 * It was reworked for the GNU C Library by Roland McGrath. + * Rewritten to use reentrent functions by Ulrich Drepper, 1995. */ -#include <ansidecl.h> -#include <errno.h> #include <limits.h> #include <stddef.h> #include <stdlib.h> @@ -105,10 +104,6 @@ #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 }; - - /* Initially, everything is set up as if from: initstate(1, randtbl, 128); @@ -132,6 +127,9 @@ static long int randtbl[DEG_3 + 1] = -205601318, }; + +static struct random_data unsafe_state = + { /* FPTR and RPTR are two pointers into the state info, a front and a rear pointer. These two pointers are always rand_sep places aparts, as they cycle through the state information. (Yes, this does mean we could get @@ -142,10 +140,8 @@ static long int randtbl[DEG_3 + 1] = in the initialization of randtbl) because the state table pointer is set to point to randtbl[1] (as explained below).) */ -static long int *fptr = &randtbl[SEP_3 + 1]; -static long int *rptr = &randtbl[1]; - - + fptr : &randtbl[SEP_3 + 1], + rptr : &randtbl[1], /* The following things are the pointer to the state information table, the type of the current generator, the degree of the current polynomial @@ -157,13 +153,14 @@ static long int *rptr = &randtbl[1]; indexing every time to find the address of the last element to see if the front and rear pointers have wrapped. */ -static long int *state = &randtbl[1]; + state : &randtbl[1], -static int rand_type = TYPE_3; -static int rand_deg = DEG_3; -static int rand_sep = SEP_3; + rand_type : TYPE_3, + rand_deg : DEG_3, + rand_sep : SEP_3, -static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])]; + end_ptr : &randtbl[sizeof (randtbl) / sizeof (randtbl[0])] +}; /* Initialize the random number generator based on the given seed. If the type is the trivial no-state-information type, just remember the seed. @@ -174,27 +171,10 @@ static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])]; introduced by the L.C.R.N.G. Note that the initialization of randtbl[] for default usage relies on values produced by this routine. */ void -DEFUN(__srandom, (x), unsigned int x) +__srandom (x) + unsigned int x; { - state[0] = x; - if (rand_type != TYPE_0) - { - register long int i; - for (i = 1; i < rand_deg; ++i) - { - /* This does: - state[i] = (16807 * state[i - 1]) % 2147483647; - but avoids overflowing 31 bits. */ - long int hi = state[i - 1] / 127773; - long int lo = state[i - 1] % 127773; - long int test = 16807 * lo - 2836 * hi; - state[i] = test + (test < 0 ? 2147483647 : 0); - } - fptr = &state[rand_sep]; - rptr = &state[0]; - for (i = 0; i < 10 * rand_deg; ++i) - (void) __random (); - } + (void) __srandom_r (x, &unsafe_state); } weak_alias (__srandom, srandom) @@ -211,60 +191,15 @@ weak_alias (__srandom, srand) Note: The first thing we do is save the current state, if any, just like setstate so that it doesn't matter when initstate is called. Returns a pointer to the old state. */ -PTR -DEFUN(__initstate, (seed, arg_state, n), - unsigned int seed AND PTR arg_state AND size_t n) +void * +__initstate (seed, arg_state, n) + unsigned int seed; + void *arg_state; + size_t n; { - PTR ostate = (PTR) &state[-1]; - - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = (MAX_TYPES * (rptr - state)) + rand_type; - if (n < BREAK_1) - { - if (n < BREAK_0) - { - errno = EINVAL; - return NULL; - } - rand_type = TYPE_0; - rand_deg = DEG_0; - rand_sep = SEP_0; - } - else if (n < BREAK_2) - { - rand_type = TYPE_1; - rand_deg = DEG_1; - rand_sep = SEP_1; - } - else if (n < BREAK_3) - { - rand_type = TYPE_2; - rand_deg = DEG_2; - rand_sep = SEP_2; - } - else if (n < BREAK_4) - { - rand_type = TYPE_3; - rand_deg = DEG_3; - rand_sep = SEP_3; - } - else - { - rand_type = TYPE_4; - rand_deg = DEG_4; - rand_sep = SEP_4; - } - - state = &((long int *) arg_state)[1]; /* First location. */ - /* Must set END_PTR before srandom. */ - end_ptr = &state[rand_deg]; - __srandom(seed); - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = (MAX_TYPES * (rptr - state)) + rand_type; + void *ostate = (void *) &unsafe_state.state[-1]; + + __initstate_r (seed, arg_state, n, &unsafe_state); return ostate; } @@ -279,44 +214,14 @@ weak_alias (__initstate, initstate) to the order in which things are done, it is OK to call setstate with the same state as the current state Returns a pointer to the old state information. */ -PTR -DEFUN(__setstate, (arg_state), PTR arg_state) +void * +__setstate (arg_state) + void *arg_state; { - register long int *new_state = (long int *) arg_state; - register int type = new_state[0] % MAX_TYPES; - register int rear = new_state[0] / MAX_TYPES; - PTR ostate = (PTR) &state[-1]; - - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = (MAX_TYPES * (rptr - state)) + rand_type; - - switch (type) - { - case TYPE_0: - case TYPE_1: - case TYPE_2: - case TYPE_3: - case TYPE_4: - rand_type = type; - rand_deg = degrees[type]; - rand_sep = seps[type]; - break; - default: - /* State info munged. */ - errno = EINVAL; - return NULL; - } - - state = &new_state[1]; - if (rand_type != TYPE_0) - { - rptr = &state[rear]; - fptr = &state[(rear + rand_sep) % rand_deg]; - } - /* Set end_ptr too. */ - end_ptr = &state[rand_deg]; + void *ostate = (void *) &unsafe_state.state[-1]; + + if (__setstate_r (arg_state, &unsafe_state) < 0) + return NULL; return ostate; } @@ -335,33 +240,13 @@ weak_alias (__setstate, setstate) pointer if the front one has wrapped. Returns a 31-bit random number. */ long int -DEFUN_VOID(__random) +__random () { - if (rand_type == TYPE_0) - { - state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX; - return state[0]; - } - else - { - long int i; - *fptr += *rptr; - /* Chucking least random bit. */ - i = (*fptr >> 1) & LONG_MAX; - ++fptr; - if (fptr >= end_ptr) - { - fptr = state; - ++rptr; - } - else - { - ++rptr; - if (rptr >= end_ptr) - rptr = state; - } - return i; - } + long int retval; + + (void) __random_r (&unsafe_state, &retval); + + return retval; } weak_alias (__random, random) diff --git a/stdlib/random_r.c b/stdlib/random_r.c new file mode 100644 index 0000000..aa7a33f --- /dev/null +++ b/stdlib/random_r.c @@ -0,0 +1,337 @@ +/* + * Copyright (c) 1983 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* + * This is derived from the Berkeley source: + * @(#)random.c 5.5 (Berkeley) 7/6/88 + * It was reworked for the GNU C Library by Roland McGrath. + * Rewritten to be reentrent by Ulrich Drepper, 1995 + */ + +#include <errno.h> +#include <limits.h> +#include <stddef.h> +#include <stdlib.h> + + +/* An improved random number generation package. In addition to the standard + rand()/srand() like interface, this package also has a special state info + interface. The initstate() routine is called with a seed, an array of + bytes, and a count of how many bytes are being passed in; this array is + then initialized to contain information for random number generation with + that much state information. Good sizes for the amount of state + information are 32, 64, 128, and 256 bytes. The state can be switched by + calling the setstate() function with the same array as was initiallized + with initstate(). 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. Internally, the + state information is treated as an array of longs; the zeroeth element of + the array is the type of R.N.G. being used (small integer); the remainder + of the array is the state information for the R.N.G. Thus, 32 bytes of + state information will give 7 longs worth of state information, which will + allow a degree seven polynomial. (Note: The zeroeth word of state + information also has some other information stored in it; see setstate + for details). The random number generation technique is a linear feedback + shift register approach, employing trinomials (since there are fewer terms + to sum up that way). In this approach, the least significant bit of all + the numbers in the state table will act as a linear feedback shift register, + and will have period 2^deg - 1 (where deg is the degree of the polynomial + being used, assuming that the polynomial is irreducible and primitive). + The higher order bits will have longer periods, since their values are + also influenced by pseudo-random carries out of the lower bits. The + total period of the generator is approximately deg*(2**deg - 1); thus + doubling the amount of state information has a vast influence on the + period of the generator. Note: The deg*(2**deg - 1) is an approximation + only good for 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. */ + + + +/* For each of the currently supported random number generators, we have a + break value on the amount of state information (you need at least thi + bytes of state info to support this random number generator), a degree for + the polynomial (actually a trinomial) that the R.N.G. is based on, and + separation between the two lower order coefficients of the trinomial. */ + +/* Linear congruential. */ +#define TYPE_0 0 +#define BREAK_0 8 +#define DEG_0 0 +#define SEP_0 0 + +/* x**7 + x**3 + 1. */ +#define TYPE_1 1 +#define BREAK_1 32 +#define DEG_1 7 +#define SEP_1 3 + +/* x**15 + x + 1. */ +#define TYPE_2 2 +#define BREAK_2 64 +#define DEG_2 15 +#define SEP_2 1 + +/* x**31 + x**3 + 1. */ +#define TYPE_3 3 +#define BREAK_3 128 +#define DEG_3 31 +#define SEP_3 3 + +/* x**63 + x + 1. */ +#define TYPE_4 4 +#define BREAK_4 256 +#define DEG_4 63 +#define SEP_4 1 + + +/* Array versions of the above information to make code run faster. + Relies on fact that TYPE_i == i. */ + +#define MAX_TYPES 5 /* Max number of types above. */ + +static const int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; +static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; + + + + +/* Initialize the random number generator based on the given seed. If the + type is the trivial no-state-information type, just remember the seed. + Otherwise, initializes state[] based on the given "seed" via a linear + congruential generator. Then, the pointers are set to known locations + that are exactly rand_sep places apart. Lastly, it cycles the state + information a given number of times to get rid of any initial dependencies + introduced by the L.C.R.N.G. Note that the initialization of randtbl[] + for default usage relies on values produced by this routine. */ +int +__srandom_r (x, buf) + unsigned int x; + struct random_data *buf; +{ + if (buf == NULL || buf->rand_type < TYPE_0 || buf->rand_type > TYPE_4) + return -1; + + buf->state[0] = x; + if (buf->rand_type != TYPE_0) + { + long int i; + for (i = 1; i < buf->rand_deg; ++i) + { + /* This does: + state[i] = (16807 * state[i - 1]) % 2147483647; + but avoids overflowing 31 bits. */ + long int hi = buf->state[i - 1] / 127773; + long int lo = buf->state[i - 1] % 127773; + long int test = 16807 * lo - 2836 * hi; + buf->state[i] = test + (test < 0 ? 2147483647 : 0); + } + buf->fptr = &buf->state[buf->rand_sep]; + buf->rptr = &buf->state[0]; + for (i = 0; i < 10 * buf->rand_deg; ++i) + { + long int discard; + (void) __random_r (buf, &discard); + } + } + + return 0; +} + +weak_alias (__srandom_r, srandom_r) +weak_alias (__srandom_r, srand_r) + +/* Initialize the state information in the given array of N bytes for + future random number generation. Based on the number of bytes we + are given, and the break values for the different R.N.G.'s, we choose + the best (largest) one we can and set things up for it. srandom is + then called to initialize the state information. Note that on return + from srandom, we set state[-1] to be the type multiplexed with the current + value of the rear pointer; this is so successive calls to initstate won't + lose this information and will be able to restart with setstate. + Note: The first thing we do is save the current state, if any, just like + setstate so that it doesn't matter when initstate is called. + Returns a pointer to the old state. */ +int +__initstate_r (seed, arg_state, n, buf) + unsigned int seed; + void *arg_state; + size_t n; + struct random_data *buf; +{ + if (buf == NULL) + return -1; + + if (buf->rand_type == TYPE_0) + buf->state[-1] = buf->rand_type; + else + buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type; + if (n < BREAK_1) + { + if (n < BREAK_0) + { + errno = EINVAL; + return -1; + } + buf->rand_type = TYPE_0; + buf->rand_deg = DEG_0; + buf->rand_sep = SEP_0; + } + else if (n < BREAK_2) + { + buf->rand_type = TYPE_1; + buf->rand_deg = DEG_1; + buf->rand_sep = SEP_1; + } + else if (n < BREAK_3) + { + buf->rand_type = TYPE_2; + buf->rand_deg = DEG_2; + buf->rand_sep = SEP_2; + } + else if (n < BREAK_4) + { + buf->rand_type = TYPE_3; + buf->rand_deg = DEG_3; + buf->rand_sep = SEP_3; + } + else + { + buf->rand_type = TYPE_4; + buf->rand_deg = DEG_4; + buf->rand_sep = SEP_4; + } + + buf->state = &((long int *) arg_state)[1]; /* First location. */ + /* Must set END_PTR before srandom. */ + buf->end_ptr = &buf->state[buf->rand_deg]; + + __srandom_r (seed, buf); + + if (buf->rand_type == TYPE_0) + buf->state[-1] = buf->rand_type; + else + buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type; + + return 0; +} + +weak_alias (__initstate_r, initstate_r) + +/* Restore the state from the given state array. + Note: It is important that we also remember the locations of the pointers + in the current state information, and restore the locations of the pointers + from the old state information. This is done by multiplexing the pointer + location into the zeroeth word of the state information. Note that due + to the order in which things are done, it is OK to call setstate with the + same state as the current state + Returns a pointer to the old state information. */ +int +__setstate_r (arg_state, buf) + void *arg_state; + struct random_data *buf; +{ + long int *new_state = (long int *) arg_state; + int type = new_state[0] % MAX_TYPES; + int rear = new_state[0] / MAX_TYPES; + + if (buf == NULL) + return -1; + + if (buf->rand_type == TYPE_0) + buf->state[-1] = buf->rand_type; + else + buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type; + + switch (type) + { + case TYPE_0: + case TYPE_1: + case TYPE_2: + case TYPE_3: + case TYPE_4: + buf->rand_type = type; + buf->rand_deg = degrees[type]; + buf->rand_sep = seps[type]; + break; + default: + /* State info munged. */ + errno = EINVAL; + return -1; + } + + buf->state = &new_state[1]; + if (buf->rand_type != TYPE_0) + { + buf->rptr = &buf->state[rear]; + buf->fptr = &buf->state[(rear + buf->rand_sep) % buf->rand_deg]; + } + /* Set end_ptr too. */ + buf->end_ptr = &buf->state[buf->rand_deg]; + + return 0; +} + +weak_alias (__setstate_r, setstate_r) + +/* If we are using the trivial TYPE_0 R.N.G., just do the old linear + congruential bit. Otherwise, we do our fancy trinomial stuff, which is the + same in all ther other cases due to all the global variables that have been + set up. The basic operation is to add the number at the rear pointer into + the one at the front pointer. Then both pointers are advanced to the next + location cyclically in the table. The value returned is the sum generated, + reduced to 31 bits by throwing away the "least random" low bit. + Note: The code takes advantage of the fact that both the front and + rear pointers can't wrap on the same call by not testing the rear + pointer if the front one has wrapped. Returns a 31-bit random number. */ + +int +__random_r (buf, result) + struct random_data *buf; + long int *result; +{ + if (buf == NULL || result == NULL) + return -1; + + if (buf->rand_type == TYPE_0) + { + buf->state[0] = ((buf->state[0] * 1103515245) + 12345) & LONG_MAX; + *result = buf->state[0]; + } + else + { + *buf->fptr += *buf->rptr; + /* Chucking least random bit. */ + *result = (*buf->fptr >> 1) & LONG_MAX; + ++buf->fptr; + if (buf->fptr >= buf->end_ptr) + { + buf->fptr = buf->state; + ++buf->rptr; + } + else + { + ++buf->rptr; + if (buf->rptr >= buf->end_ptr) + buf->rptr = buf->state; + } + } + return 0; +} + +weak_alias (__random_r, random_r) diff --git a/stdlib/seed48.c b/stdlib/seed48.c new file mode 100644 index 0000000..634f9a7 --- /dev/null +++ b/stdlib/seed48.c @@ -0,0 +1,32 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +unsigned short int * +seed48 (seed16v) + unsigned short int seed16v[3]; +{ + (void) seed48_r (seed16v, &__libc_drand48_data); + + return __libc_drand48_data.old_X; +} diff --git a/stdlib/seed48_r.c b/stdlib/seed48_r.c new file mode 100644 index 0000000..4d43507 --- /dev/null +++ b/stdlib/seed48_r.c @@ -0,0 +1,41 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +seed48_r (seed16v, buffer) + unsigned short int seed16v[3]; + struct drand48_data *buffer; +{ + /* Be generous for the arguments, detect some errors. */ + if (buffer == NULL) + { + errno = EFAULT; + return -1; + } + + /* Save old value at a private place to be used as return value. */ + memcpy (buffer->old_X, buffer->X, sizeof (buffer->X)); + + /* Install new state. */ + memcpy (buffer->X, seed16v, sizeof (buffer->X)); + + return 0; +} diff --git a/stdlib/srand48.c b/stdlib/srand48.c new file mode 100644 index 0000000..80ada31 --- /dev/null +++ b/stdlib/srand48.c @@ -0,0 +1,30 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +/* Global state for non-reentrent functions. Defined in drand48-iter.c. */ +extern struct drand48_data __libc_drand48_data; + +void +srand48 (seedval) + long seedval; +{ + (void) srand48_r (seedval, &__libc_drand48_data); +} diff --git a/stdlib/srand48_r.c b/stdlib/srand48_r.c new file mode 100644 index 0000000..7e77d16 --- /dev/null +++ b/stdlib/srand48_r.c @@ -0,0 +1,52 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdlib.h> + +int +srand48_r (seedval, buffer) + long seedval; + struct drand48_data *buffer; +{ + /* Be generous for the arguments, detect some errors. */ + if (buffer == NULL) + { + errno = EFAULT; + return -1; + } + + /* The standards say we only have 32 bits. */ + if (sizeof (long) > 4) + seedval &= 0xffffffffl; + + if (sizeof (unsigned short int) == 2) + { + buffer->X[2] = seedval >> 16; + buffer->X[1] = seedval & 0xffffl; + buffer->X[0] = 0x330e; + } + else + { + buffer->X[2] = seedval; + buffer->X[1] = 0x330e0000; + buffer->X[0] = 0; + } + + return 0; +} diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h index 2a3cf8e..c6c504a 100644 --- a/stdlib/stdlib.h +++ b/stdlib/stdlib.h @@ -198,7 +198,83 @@ extern __inline __ptr_t initstate (unsigned int __seed, extern __inline __ptr_t setstate (__ptr_t __statebuf) { return __setstate (__statebuf); } #endif /* Optimizing GCC >=2. */ -#endif /* Use BSD. */ + +#ifdef __USE_REENTRANT +/* Reentrant versions of the `random' family of functions. + These functions all use the following data structure to contain + state, rather than global state variables. */ + +struct random_data + { + long int *fptr; /* Front pointer. */ + long int *rptr; /* Rear pointer. */ + long int *state; /* Array of state values. */ + int rand_type; /* Type of random number generator. */ + int rand_deg; /* Degree of random number generator. */ + int rand_sep; /* Distance between front and rear. */ + long int *end_ptr; /* Pointer behind state table. */ + }; + +extern int __random_r __P ((struct random_data *__buf, long int *__result)); +extern int __srandom_r __P ((unsigned int __seed, struct random_data *__buf)); +extern int __initstate_r __P ((unsigned int __seed, __ptr_t __statebuf, + size_t __statelen, struct random_data *__buf)); +extern int __setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf)); + +extern int random_r __P ((struct random_data *__buf, long int *__result)); +extern int srandom_r __P ((unsigned int __seed, struct random_data *__buf)); +extern int initstate_r __P ((unsigned int __seed, __ptr_t __statebuf, + size_t __statelen, struct random_data *__buf)); +extern int setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf)); +#endif /* __USE_REENTRANT. */ +#endif /* Use BSD. */ + + +#ifdef __USE_SVID +/* System V style 48-bit random number generator functions. */ + +/* Data structure for communication with thread safe versions. */ +struct drand48_data + { + unsigned short int X[3]; /* Current state. */ + unsigned short int a[3]; /* Factor in congruential formula. */ + unsigned short int c; /* Additive const. in congruential formula. */ + unsigned short int old_X[3]; /* Old state. */ + int init; /* Flag for initializing. */ + }; + +/* Return non-negative, double-precision floating-point value in [0.0,1.0). */ +extern double drand48 __P ((void)); +extern int drand48_r __P ((struct drand48_data *__buffer, double *__result)); +extern double erand48 __P ((unsigned short int __xsubi[3])); +extern int erand48_r __P ((unsigned short int __xsubi[3], + struct drand48_data *__buffer, double *__result)); +/* Return non-negative, long integer in [0,2^31). */ +extern long lrand48 __P ((void)); +extern int lrand48_r __P ((struct drand48_data *__buffer, long *__result)); +extern long nrand48 __P ((unsigned short int __xsubi[3])); +extern int nrand48_r __P ((unsigned short int __xsubi[3], + struct drand48_data *__buffer, long *__result)); +/* Return signed, long integers in [-2^31,2^31). */ +extern long mrand48 __P ((void)); +extern int mrand48_r __P ((struct drand48_data *__buffer, long *__result)); +extern long jrand48 __P ((unsigned short int __xsubi[3])); +extern int jrand48_r __P ((unsigned short int __xsubi[3], + struct drand48_data *__buffer, long *__result)); +/* Seed random number generator. */ +extern void srand48 __P ((long __seedval)); +extern int srand48_r __P ((long __seedval, struct drand48_data *__buffer)); +extern unsigned short int *seed48 __P ((unsigned short int __seed16v[3])); +extern int seed48_r __P ((unsigned short int __seed16v[3], + struct drand48_data *__buffer)); +extern void lcong48 __P ((unsigned short int __param[7])); +extern int lcong48_r __P ((unsigned short int __param[7], + struct drand48_data *__buffer)); + +/* Internal function to compute next state of the generator. */ +extern int __drand48_iterate __P ((unsigned short int __xsubi[3], + struct drand48_data *__buffer)); +#endif /* __USE_SVID. */ /* Allocate SIZE bytes of memory. */ @@ -268,7 +344,10 @@ extern int system __P ((__const char *__command)); /* Shorthand for type of comparison functions. */ +#ifndef __COMPAR_FN_T +#define __COMPAR_FN_T typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t)); +#endif #ifdef __USE_GNU typedef __compar_fn_t comparison_fn_t; @@ -308,6 +387,34 @@ extern __CONSTVALUE div_t div __P ((int __numer, int __denom)); extern __CONSTVALUE ldiv_t ldiv __P ((long int __numer, long int __denom)); +#ifdef __USE_SVID +/* Convert floating point numbers to strings. The returned values are + valid only until another call to the same function. */ + +/* Convert VALUE to a string with NDIGIT digits and return a pointer to + this. Set *DECPT with the position of the decimal character and *SIGN + with the sign of the number. */ +char *ecvt __P ((double __value, int __ndigit, int *__decpt, int *sign)); + +/* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT + with the position of the decimal character and *SIGN with the sign of + the number. */ +char *fcvt __P ((double __value, int __ndigit, int *__decpt, int *sign)); + +/* If possible convert VALUE to a string with NDIGIT significant digits. + Otherwise use exponential representation. The resulting string will + be written to BUF. */ +char *gcvt __P ((double __value, int __ndigit, char *__buf)); + +/* Reentrant version of the functions above which provide their own + buffers. */ +int ecvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign, + char *__buf, int *__len)); +int fcvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign, + char *__buf, int *__len)); +#endif + + /* Return the length of the multibyte character in S, which is no longer than N. */ extern int mblen __P ((__const char *__s, size_t __n)); diff --git a/string/strerror.c b/string/strerror.c index 6775970..62a08a0 100644 --- a/string/strerror.c +++ b/string/strerror.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc. +/* Copyright (C) 1991, 1993, 1994, 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -13,21 +13,21 @@ Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ -#include <ansidecl.h> #include <stdio.h> #include <string.h> -extern char *_strerror_internal __P ((int, char buf[1024])); +extern char *_strerror_internal __P ((int, char *, size_t)); /* Return a string descibing the errno code in ERRNUM. The storage is good only until the next call to strerror. Writing to the storage causes undefined behavior. */ char * -DEFUN(strerror, (errnum), int errnum) +strerror (errnum) + int errnum; { static char buf[1024]; - return _strerror_internal (errnum, buf); + return _strerror_internal (errnum, buf, sizeof buf); } diff --git a/string/strerror_r.c b/string/strerror_r.c new file mode 100644 index 0000000..3b45996 --- /dev/null +++ b/string/strerror_r.c @@ -0,0 +1,30 @@ +/* Copyright (C) 1991, 1993, 1994, 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <stdio.h> +#include <string.h> + +extern char *_strerror_internal __P ((int, char *, size_t)); + +/* Return a string descibing the errno code in ERRNUM. At most BUFLEN + characters of the result will be placed in STRERRBUF. */ +char * +strerror_r (int errnum, char *buf, size_t buflen) +{ + return _strerror_internal (errnum, buf, buflen); +} diff --git a/string/string.h b/string/string.h index 905d727..564adcc 100644 --- a/string/string.h +++ b/string/string.h @@ -1,4 +1,4 @@ -/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc. +/* Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -120,8 +120,13 @@ extern __ptr_t memmem __P ((__const __ptr_t __haystack, size_t __haystacklen, /* Return the length of S. */ extern size_t strlen __P ((__const char *__s)); -/* Return a string describing the meaning of the errno code in ERRNUM. */ +/* Return a string describing the meaning of the `errno' code in ERRNUM. */ extern char *strerror __P ((int __errnum)); +#ifdef __USE_REENTRANT +/* Reentrant version of `strerror'. If a temporary buffer is required, at + most BUFLEN bytes of BUF will be used. */ +extern char *strerror_r __P ((int __errnum, char *__buf, int __buflen)); +#endif #ifdef __USE_BSD /* Find the first occurrence of C in S (same as strchr). */ diff --git a/sysdeps/generic/_strerror.c b/sysdeps/generic/_strerror.c index 455c8ff..0d069f8 100644 --- a/sysdeps/generic/_strerror.c +++ b/sysdeps/generic/_strerror.c @@ -1,4 +1,4 @@ -/* Copyright (C) 19911993 Free Software Foundation, Inc. +/* Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -16,7 +16,6 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <ansidecl.h> #include <stdio.h> #include <string.h> @@ -27,13 +26,16 @@ Cambridge, MA 02139, USA. */ /* Return a string describing the errno code in ERRNUM. */ char * -DEFUN(_strerror_internal, (errnum, buf), int errnum AND char buf[1024]) +_strerror_internal (errnum, buf, buflen) + int errnum; + char *buf; + size_t buflen; { if (errnum < 0 || errnum > _sys_nerr) { static char fmt[] = "Unknown error %d"; - size_t len = sprintf (buf, fmt, errnum); - if (len < sizeof(fmt) - 2) + size_t len = snprintf (buf, buflen, fmt, errnum); + if (len < sizeof (fmt) - 2) return NULL; buf[len - 1] = '\0'; return buf; diff --git a/sysdeps/generic/dl-sysdep.c b/sysdeps/generic/dl-sysdep.c index b18ac2b..c1cf37d 100644 --- a/sysdeps/generic/dl-sysdep.c +++ b/sysdeps/generic/dl-sysdep.c @@ -26,6 +26,7 @@ Cambridge, MA 02139, USA. */ extern int _dl_argc; extern char **_dl_argv; extern char **_environ; +extern void _start (void); Elf32_Addr _dl_sysdep_start (void **start_argptr, @@ -39,6 +40,7 @@ _dl_sysdep_start (void **start_argptr, uid_t uid, euid; gid_t gid, egid; + user_entry = (Elf32_Addr) &_start; _dl_argc = *(int *) start_argptr; _dl_argv = start_argptr + 1; _environ = &_dl_argv[_dl_argc + 1]; @@ -57,6 +59,7 @@ _dl_sysdep_start (void **start_argptr, break; case AT_ENTRY: user_entry = av->a_un.a_val; + break; case AT_UID: uid = av->a_un.a_val; break; diff --git a/sysdeps/generic/strstr.c b/sysdeps/generic/strstr.c index 06681de..9c0e818 100644 --- a/sysdeps/generic/strstr.c +++ b/sysdeps/generic/strstr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991, 1992 Free Software Foundation, Inc. +/* Copyright (C) 1994 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -16,41 +16,102 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <ansidecl.h> -#include <stddef.h> +/* + * My personal strstr() implementation that beats most other algorithms. + * Until someone tells me otherwise, I assume that this is the + * fastest implementation of strstr() in C. + * I deliberately chose not to comment it. You should have at least + * as much fun trying to understand it, as I had to write it :-). + * + * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ + #include <string.h> +#include <sys/types.h> + +typedef unsigned chartype; -/* Return the first ocurrence of NEEDLE in HAYSTACK. */ char * -DEFUN(strstr, (haystack, needle), - CONST char *CONST haystack AND - CONST char *CONST needle) +strstr (phaystack, pneedle) + const char *phaystack; + const char *pneedle; { - register CONST char *CONST needle_end = strchr(needle, '\0'); - register CONST char *CONST haystack_end = strchr(haystack, '\0'); - register CONST size_t needle_len = needle_end - needle; - register CONST size_t needle_last = needle_len - 1; - register CONST char *begin; - - if (needle_len == 0) - return (char *) haystack; /* ANSI 4.11.5.7, line 25. */ - if ((size_t) (haystack_end - haystack) < needle_len) - return NULL; - - for (begin = &haystack[needle_last]; begin < haystack_end; ++begin) - { - register CONST char *n = &needle[needle_last]; - register CONST char *h = begin; + register const unsigned char *haystack, *needle; + register chartype b, c; + + haystack = (const unsigned char *) phaystack; + needle = (const unsigned char *) pneedle; + b = *needle; + if (b != '\0') + { + haystack--; /* possible ANSI violation */ do - if (*h != *n) - goto loop; /* continue for loop */ - while (--n >= needle && --h >= haystack); + { + c = *++haystack; + if (c == '\0') + goto ret0; + } + while (c != b); - return (char *) h; + c = *++needle; + if (c == '\0') + goto foundneedle; + ++needle; + goto jin; - loop:; - } + for (;;) + { + register chartype a; + register const unsigned char *rhaystack, *rneedle; + + do + { + a = *++haystack; + if (a == '\0') + goto ret0; + if (a == b) + break; + a = *++haystack; + if (a == '\0') + goto ret0; +shloop: } + while (a != b); - return NULL; +jin: a = *++haystack; + if (a == '\0') + goto ret0; + + if (a != c) + goto shloop; + + rhaystack = haystack-- + 1; + rneedle = needle; + a = *rneedle; + + if (*rhaystack == a) + do + { + if (a == '\0') + goto foundneedle; + ++rhaystack; + a = *++needle; + if (*rhaystack != a) + break; + if (a == '\0') + goto foundneedle; + ++rhaystack; + a = *++needle; + } + while (*rhaystack == a); + + needle = rneedle; /* took the register-poor aproach */ + + if (a == '\0') + break; + } + } +foundneedle: + return (char*) haystack; +ret0: + return 0; } diff --git a/sysdeps/mach/_strerror.c b/sysdeps/mach/_strerror.c index eeebb9e..f241112 100644 --- a/sysdeps/mach/_strerror.c +++ b/sysdeps/mach/_strerror.c @@ -24,7 +24,7 @@ Cambridge, MA 02139, USA. */ /* Return a string describing the errno code in ERRNUM. */ char * -_strerror_internal (int errnum, char buf[1024]) +_strerror_internal (int errnum, char *buf, size_t buflen) { int system; int sub; @@ -41,7 +41,7 @@ _strerror_internal (int errnum, char buf[1024]) if (system > err_max_system || ! __mach_error_systems[system].bad_sub) { static const char unk[] = "Error in unknown error system: "; - char *p = buf + sizeof buf; + char *p = buf + buflen; *p-- = '\0'; p = _itoa (errnum, p, 16, 1); p -= sizeof unk - 1; @@ -56,7 +56,7 @@ _strerror_internal (int errnum, char buf[1024]) if (code >= es->subsystem[sub].max_code) { static const char unk[] = "Unknown error "; - char *p = buf + sizeof buf; + char *p = buf + buflen; size_t len = strlen (es->subsystem[sub].subsys_name); *p-- = '\0'; p = _itoa (errnum, p, 16, 1); diff --git a/sysdeps/mach/hurd/ttyname_r.c b/sysdeps/mach/hurd/ttyname_r.c new file mode 100644 index 0000000..67ff37d --- /dev/null +++ b/sysdeps/mach/hurd/ttyname_r.c @@ -0,0 +1,49 @@ +/* Copyright (C) 1994, 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <hurd.h> +#include <hurd/term.h> +#include <hurd/fd.h> + +/* Store at most BUFLEN characters of the pathname of the terminal FD is + open on in BUF. Return 0 on success, -1 otherwise. */ +int +ttyname_r (int fd, char *buf, int buflen) +{ + error_t err; + char nodename[1024]; /* XXX */ + char *cp; + int len; + + nodename[0] = '\0'; + if (err = HURD_DPORT_USE (fd, __term_get_nodename (port, nodename))) + return __hurd_dfail (fd, err), -1; + + len = (int) strlen (nodename) + 1; + if (len > buflen) + { + errno = EINVAL; + return -1; + } + + memcpy (buf, nodename, len); + return 0; +} diff --git a/sysdeps/posix/sysconf.c b/sysdeps/posix/sysconf.c index 7f4fbb7..485f9b3 100644 --- a/sysdeps/posix/sysconf.c +++ b/sysdeps/posix/sysconf.c @@ -52,7 +52,11 @@ DEFUN(__sysconf, (name), int name) #endif case _SC_CLK_TCK: +#ifdef CLK_TCK + return CLK_TCK; +#else return 60; +#endif case _SC_NGROUPS_MAX: #ifdef NGROUPS_MAX @@ -65,7 +69,11 @@ DEFUN(__sysconf, (name), int name) return __getdtablesize (); case _SC_STREAM_MAX: +#ifdef STREAM_MAX + return STREAM_MAX; +#else return FOPEN_MAX; +#endif case _SC_TZNAME_MAX: return __tzname_max (); @@ -171,6 +179,13 @@ DEFUN(__sysconf, (name), int name) return -1; #endif + case _SC_2_LOCALEDEF: +#ifdef _POSIX2_LOCALEDEF + return _POSIX2_LOCALEDEF; +#else + return -1; +#endif + case _SC_2_SW_DEV: #ifdef _POSIX2_SW_DEV return _POSIX2_SW_DEV; diff --git a/sysdeps/posix/ttyname_r.c b/sysdeps/posix/ttyname_r.c new file mode 100644 index 0000000..e225da2 --- /dev/null +++ b/sysdeps/posix/ttyname_r.c @@ -0,0 +1,91 @@ +/* Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <errno.h> +#include <limits.h> +#include <stddef.h> +#include <dirent.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> + +#ifndef MIN +# define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif + +/* Store at most BUFLEN character of the pathname of the terminal FD is + open on in BUF. Return 0 on success, -1 otherwise. */ +int +ttyname_r (fd, buf, buflen) + int fd; + char *buf; + int buflen; +{ + static const char dev[] = "/dev"; + struct stat st; + dev_t mydev; + ino_t myino; + DIR *dirstream; + struct dirent *d; + int save = errno; + + /* Test for the absolute minimal size. This makes life easier inside + the loop. */ + if (buflen < sizeof (dev) + 2) + { + errno = EINVAL; + return -1; + } + + if (fstat (fd, &st) < 0) + return -1; + mydev = st.st_dev; + myino = st.st_ino; + + dirstream = opendir (dev); + if (dirstream == NULL) + return -1; + + /* Prepare the result buffer. */ + memcpy (buf, dev, sizeof (dev)); + buf[sizeof (dev)] = '/'; + buflen -= sizeof (dev) + 1; + + while ((d = readdir (dirstream)) != NULL) + if (d->d_fileno == myino) + { + char *cp; + + cp = stpncpy (&buf[sizeof (dev) + 1], d->d_name, + MIN (d->d_namlen + 1, buflen)); + cp[0] = '\0'; + + if (stat (buf, &st) == 0 && st.st_dev == mydev) + { + (void) closedir (dirstream); + errno = save; + return 0; + } + } + + (void) closedir (dirstream); + errno = save; + return -1; +} diff --git a/sysdeps/posix/utimes.c b/sysdeps/posix/utimes.c index ca542c6..2c92803 100644 --- a/sysdeps/posix/utimes.c +++ b/sysdeps/posix/utimes.c @@ -16,6 +16,7 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include <utime.h> #include <sys/time.h> #include <errno.h> #include <stddef.h> @@ -36,7 +37,7 @@ __utimes (const char *file, struct timeval tvp[2]) else times = NULL; - return utime (path, times); + return utime (file, times); } weak_alias (__utimes, utimes) diff --git a/sysdeps/stub/msgctl.c b/sysdeps/stub/msgctl.c new file mode 100644 index 0000000..8212f09 --- /dev/null +++ b/sysdeps/stub/msgctl.c @@ -0,0 +1,36 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/msg.h> +#include <errno.h> + +/* Allows to control internal state and destruction of message queue + objects. */ + +int +msgctl (msqid, cmd, buf) + int msqid; + int cmd; + struct msqid_ds *buf; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (msgctl) diff --git a/sysdeps/stub/msgget.c b/sysdeps/stub/msgget.c new file mode 100644 index 0000000..3ee2b3d --- /dev/null +++ b/sysdeps/stub/msgget.c @@ -0,0 +1,35 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/msg.h> +#include <errno.h> + +/* Return descriptor for message queue associated with KEY. The MSGFLG + parameter describes how to proceed with clashing of key values. */ + +int +msgget (key, msgflg) + key_t key; + int msgflg; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (msgget) diff --git a/sysdeps/stub/msgrcv.c b/sysdeps/stub/msgrcv.c new file mode 100644 index 0000000..3d5dfc5 --- /dev/null +++ b/sysdeps/stub/msgrcv.c @@ -0,0 +1,41 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/msg.h> +#include <errno.h> + +/* Read a message from the queue associated with the message queue + descriptor MSQID. At most MSGSZ bytes of the message are placed + in the buffer specified by the MSGP parameter. The MSGTYP parameter + describes which message is returned in MSGFLG describes the behaviour + in buffer overflow or queue underflow. */ + +int +msgrcv (msqid, msgp, msgsz, msgtyp, msgflg) + int msqid; + void *msgp; + size_t msgsz; + long msgtyp; + int msgflg; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (msgrcv) diff --git a/sysdeps/stub/msgsnd.c b/sysdeps/stub/msgsnd.c new file mode 100644 index 0000000..77d1880 --- /dev/null +++ b/sysdeps/stub/msgsnd.c @@ -0,0 +1,40 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/msg.h> +#include <errno.h> + +/* Send a message to the queue associated with the message queue + descriptor MSQID. The parameter MSGP points to a structure + describing messages where the parameter MSGSZ gives the length + of the text. The MSGFLG parameter describes the action taken + when the limit of the message queue length is reached. */ + +int +msgsnd (msqid, msgp, msgsz, msgflg) + int msqid; + void *msgp; + size_t msgsz; + int msgflg; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (msgsnd) diff --git a/sysdeps/stub/semctl.c b/sysdeps/stub/semctl.c new file mode 100644 index 0000000..8f2fe99 --- /dev/null +++ b/sysdeps/stub/semctl.c @@ -0,0 +1,37 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/sem.h> +#include <errno.h> + +/* Return identifier for array of NSEMS semaphores associated with + KEY. */ + +int +semctl (semid, semnum, cmd, arg) + int semid; + int semnum; + int cmd; + union semun arg; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (semctl) diff --git a/sysdeps/stub/semget.c b/sysdeps/stub/semget.c new file mode 100644 index 0000000..3ccf271 --- /dev/null +++ b/sysdeps/stub/semget.c @@ -0,0 +1,36 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/sem.h> +#include <errno.h> + +/* Return identifier for array of NSEMS semaphores associated with + KEY. */ + +int +semget (key, nsems, semflg) + key_t key; + int nsems; + int semflg; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (semget) diff --git a/sysdeps/stub/semop.c b/sysdeps/stub/semop.c new file mode 100644 index 0000000..2bba862 --- /dev/null +++ b/sysdeps/stub/semop.c @@ -0,0 +1,35 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/sem.h> +#include <errno.h> + +/* Perform user-defined atomical operation of array of semaphores. */ + +int +semop (semid, sops, nsops) + int semid; + struct sembuf *sops; + unsigned int nsops; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (semop) diff --git a/sysdeps/stub/shmat.c b/sysdeps/stub/shmat.c new file mode 100644 index 0000000..099f227 --- /dev/null +++ b/sysdeps/stub/shmat.c @@ -0,0 +1,37 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/shm.h> +#include <errno.h> + +/* Attach the shared memory segment associated with SHMID to the data + segment of the calling process. SHMADDR and SHMFLG determine how + and where the segment is attached. */ + +char * +shmat (shmid, shmaddr, shmflg) + int shmid; + char *shmaddr; + int shmflg; +{ + errno = ENOSYS; + return (char *) -1; +} + +stub_warning (shmat) diff --git a/sysdeps/stub/shmctl.c b/sysdeps/stub/shmctl.c new file mode 100644 index 0000000..75a4613 --- /dev/null +++ b/sysdeps/stub/shmctl.c @@ -0,0 +1,35 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/shm.h> +#include <errno.h> + +/* Provide operations to control over shared memory segments. */ + +int +shmctl (shmid, cmd, buf) + int shmid; + int cmd; + struct shmid_ds *buf; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (shmctl) diff --git a/sysdeps/stub/shmdt.c b/sysdeps/stub/shmdt.c new file mode 100644 index 0000000..8affa6c --- /dev/null +++ b/sysdeps/stub/shmdt.c @@ -0,0 +1,34 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/shm.h> +#include <errno.h> + +/* Detach shared memory segment starting at address specified by SHMADDR + from the caller's data segment. */ + +int +shmdt (shmaddr) + char *shmaddr; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (shmdt) diff --git a/sysdeps/stub/shmget.c b/sysdeps/stub/shmget.c new file mode 100644 index 0000000..d59ff90 --- /dev/null +++ b/sysdeps/stub/shmget.c @@ -0,0 +1,36 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/shm.h> +#include <errno.h> + +/* Return an identifier for an shared memory segment of at least size SIZE + which is associated with KEY. */ + +int +shmget (key, size, shmflg) + key_t key; + int size; + int shmflg; +{ + errno = ENOSYS; + return -1; +} + +stub_warning (shmget) diff --git a/sysdeps/stub/sys/ipc_buf.h b/sysdeps/stub/sys/ipc_buf.h new file mode 100644 index 0000000..fc2ad03 --- /dev/null +++ b/sysdeps/stub/sys/ipc_buf.h @@ -0,0 +1,58 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_IPC_BUF_H +#define _SYS_IPC_BUF_H + +#include <features.h> +#include <sys/types.h> + +/* Mode bits for `msgget', `semget', and `shmget'. */ +#define IPC_CREAT 01000 /* create key if key does not exist */ +#define IPC_EXCL 02000 /* fail if key exists */ +#define IPC_NOWAIT 04000 /* return error on wait */ + +/* Control commands for `msgctl', `semctl', and `shmctl'. */ +#define IPC_RMID 0 /* remove identifier */ +#define IPC_SET 1 /* set `ipc_perm' options */ +#define IPC_STAT 2 /* get `ipc_perm' options */ + + +__BEGIN_DECLS + +/* Data type for key value. */ +typedef int key_t; + +/* Special key values. */ +#define IPC_PRIVATE ((key_t) 0) /* private key */ + + +/* Data structure used to pass permission information to IPC operations. */ +struct ipc_perm +{ + __uid_t uid; /* owner's user ID */ + __gid_t gid; /* owner's group ID */ + __uid_t cuid; /* creator's user ID */ + __gid_t cgid; /* creator's group ID */ + __mode_t mode; /* read/write permission */ +}; + +__END_DECLS + +#endif /* sys/ipc_buf.h */ diff --git a/sysdeps/stub/sys/msq_buf.h b/sysdeps/stub/sys/msq_buf.h new file mode 100644 index 0000000..0222a65 --- /dev/null +++ b/sysdeps/stub/sys/msq_buf.h @@ -0,0 +1,47 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_MSQ_BUF_H +#define _SYS_MSQ_BUF_H + +#include <features.h> +#include <sys/types.h> + +/* Define options for message queue functions. */ +#define MSG_NOERROR 010000 /* no error if message is too big */ + +__BEGIN_DECLS + +/* Structure of record for one message inside the kernel. + The type `struct __msg' is opaque. */ +struct msqid_ds +{ + struct ipc_perm msg_perm; /* structure describing operation permission */ + __time_t msg_stime; /* time of last msgsnd command */ + __time_t msg_rtime; /* time of last msgrcv command */ + __time_t msg_ctime; /* time of last change */ + unsigned short int msg_qnum; /* number of messages currently on queue */ + unsigned short int msg_qbytes;/* max number of bytes allowed on queue */ + __pid_t msg_lspid; /* pid of last msgsnd() */ + __pid_t msg_lrpid; /* pid of last msgrcv() */ +}; + +__END_DECLS + +#endif /* sys/msq_buf.h */ diff --git a/sysdeps/stub/sys/sem_buf.h b/sysdeps/stub/sys/sem_buf.h new file mode 100644 index 0000000..b301525 --- /dev/null +++ b/sysdeps/stub/sys/sem_buf.h @@ -0,0 +1,52 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_SEM_BUF_H +#define _SYS_SEM_BUF_H + +#include <features.h> +#include <sys/types.h> + +/* Flags for `semop'. */ +#define SEM_UNDO 0x1000 /* undo the operation on exit */ + +/* Commands for `semctl'. */ +#define GETPID 11 /* get sempid */ +#define GETVAL 12 /* get semval */ +#define GETALL 13 /* get all semval's */ +#define GETNCNT 14 /* get semncnt */ +#define GETZCNT 15 /* get semzcnt */ +#define SETVAL 16 /* set semval */ +#define SETALL 17 /* set all semval's */ + + +__BEGIN_DECLS + +/* Data structure describing a set of semaphores. */ +struct semid_ds +{ + struct ipc_perm sem_perm; /* operation permisson struct */ + __time_t sem_otime; /* last semop() time */ + __time_t sem_ctime; /* last time changed by semctl() */ + unsigned short int sem_nsems; /* number of semaphores in set */ +}; + +__END_DECLS + +#endif /* sys/sem_buf.h */ diff --git a/sysdeps/stub/sys/shm_buf.h b/sysdeps/stub/sys/shm_buf.h new file mode 100644 index 0000000..c665d6c --- /dev/null +++ b/sysdeps/stub/sys/shm_buf.h @@ -0,0 +1,53 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_SHM_BUF_H +#define _SYS_SHM_BUF_H + +#include <features.h> +#include <sys/types.h> + +/* Flags for `shmat'. */ +#define SHM_RDONLY 010000 /* attach read-only else read-write */ +#define SHM_RND 020000 /* round attach address to SHMLBA */ +#define SHM_REMAP 040000 /* take-over region on attach */ + +/* Commands for `shmctl'. */ +#define SHM_LOCK 11 /* lock segment (root only) */ +#define SHM_UNLOCK 12 /* unlock segment (root only) */ + + +__BEGIN_DECLS + +/* Data structure describing a set of semaphores. */ +struct shmid_ds +{ + struct ipc_perm sem_perm; /* operation permisson struct */ + int shm_segsz; /* size of segment in bytes */ + __time_t sem_atime; /* time of last shmat() */ + __time_t sem_dtime; /* time of last shmdt() */ + __time_t sem_ctime; /* time of last change by shmctl() */ + __pid_t shm_cpid; /* pid of creator */ + __pid_t shm_lpid; /* pid of last shmop */ + unsigned short int shm_nattch; /* number of current attaches */ +}; + +__END_DECLS + +#endif /* sys/shm_buf.h */ diff --git a/sysdeps/stub/ttyname_r.c b/sysdeps/stub/ttyname_r.c new file mode 100644 index 0000000..875929e --- /dev/null +++ b/sysdeps/stub/ttyname_r.c @@ -0,0 +1,42 @@ +/* Copyright (C) 1991, 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <errno.h> +#include <unistd.h> + + +/* Store at most BUFLEN characters the pathname of the terminal FD is + open on in BUF. Return 0 on success, -1 otherwise. */ +int +ttyname_r (fd, buf, buflen) + int fd; + char *buf; + int buflen; +{ + errno = ENOSYS; + return -1; +} + + +#ifdef HAVE_GNU_LD + +#include <gnu-stabs.h> + +stub_warning(ttyname_r); + +#endif /* GNU stabs. */ diff --git a/sysvipc/Makefile b/sysvipc/Makefile new file mode 100644 index 0000000..7cedab6 --- /dev/null +++ b/sysvipc/Makefile @@ -0,0 +1,31 @@ +# Copyright (C) 1995 Free Software Foundation, Inc. +# This file is part of the GNU C Library. + +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. + +# The GNU C Library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. + +# You should have received a copy of the GNU Library General Public +# License along with the GNU C Library; see the file COPYING.LIB. If +# not, write to the Free Software Foundation, Inc., 675 Mass Ave, +# Cambridge, MA 02139, USA. + +# +# Sub-makefile for sysvipc portion of the library. +# +subdir := sysvipc + +headers := sys/ipc.h sys/msg.h sys/sem.h sys/shm.h \ + sys/ipc_buf.h sys/msq_buf.h sys/sem_buf.h sys/shm_buf.h + +routines := msgsnd msgrcv msgget msgctl \ + semop semget semctl \ + shmat shmdt shmget shmctl + +include ../Rules diff --git a/sysvipc/ftok.c b/sysvipc/ftok.c new file mode 100644 index 0000000..4ecc3e6 --- /dev/null +++ b/sysvipc/ftok.c @@ -0,0 +1,37 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include <sys/ipc.h> +#include <sys/stat.h> + +key_t +ftok (pathname, proj_id) + char *pathname; + char proj_id; +{ + struct stat st; + key_t key; + + if (__stat (pathname, &st) < 0) + return (key_t) -1; + + key = (st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | (id << 24); + + return key; +} diff --git a/sysvipc/sys/ipc.h b/sysvipc/sys/ipc.h new file mode 100644 index 0000000..d459be7 --- /dev/null +++ b/sysvipc/sys/ipc.h @@ -0,0 +1,33 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_IPC_H +#define _SYS_IPC_H + +/* Get system dependent definition of `struct ipc_perm' and more. */ +#include <sys/ipc_buf.h> + +__BEGIN_DECLS + +/* Generates key for System V style IPC. */ +extern key_t ftok __P ((char *__pathname, char __proj_id)); + +__END_DECLS + +#endif /* sys/ipc.h */ diff --git a/sysvipc/sys/msg.h b/sysvipc/sys/msg.h new file mode 100644 index 0000000..22df1ec --- /dev/null +++ b/sysvipc/sys/msg.h @@ -0,0 +1,61 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_MSG_H +#define _SYS_MSG_H + +#include <features.h> +#include <sys/types.h> + +/* Get common definition of System V style IPC. */ +#include <sys/ipc.h> + +/* Get system dependent definition of `struct msqid_ds' and more. */ +#include <sys/msq_buf.h> + +__BEGIN_DECLS + +/* The following System V style IPC functions implement a message queue + system. The definition is found in XPG2. */ + +/* Template for struct to be used as argument for `msgsnd' and `msgrcv'. */ +struct msgbuf +{ + long mtype; /* type of received/sent message */ + char mtext[1]; /* text of the message */ +}; + + +/* Message queue control operation. */ +extern int msgctl __P ((int __msqid, int __cmd, struct msqid_ds *__buf)); + +/* Get messages queue. */ +extern int msgget __P ((key_t __key, int __msgflg)); + +/* Receive message from message queue. */ +extern int msgrcv __P ((int __msqid, void *__msgp, size_t __msgsz, + long __msgtyp, int __msgflg)); + +/* Send message to message queue. */ +extern int msgsnd __P ((int __msqid, void *__msgp, size_t __msgsz, + int __msgflg)); + +__END_DECLS + +#endif /* sys/msg.h */ diff --git a/sysvipc/sys/sem.h b/sysvipc/sys/sem.h new file mode 100644 index 0000000..d937e6b --- /dev/null +++ b/sysvipc/sys/sem.h @@ -0,0 +1,59 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_SEM_H +#define _SYS_SEM_H + +#include <features.h> +#include <sys/types.h> + +/* Get common definition of System V style IPC. */ +#include <sys/ipc.h> + +/* Get system dependent definition of `struct semid_ds' and more. */ +#include <sys/sem_buf.h> + +__BEGIN_DECLS + +/* The following System V style IPC functions implement a semaphore + handling. The definition is found in XPG2. */ + +/* Structure used for argument to `semop' to describe operations. */ +struct sembuf +{ + short int sem_num; /* semaphore number */ + short int sem_op; /* semaphore operation */ + short int sem_flg; /* operation flag */ +}; + + +/* Semaphore control operation. */ +extern int semctl __P ((int __semid, int __semnum, int __cmd, + union semun __arg)); + +/* Get semaphore. */ +extern int semget __P ((key_t __key, int __nsems, int __semflg)); + +/* Operate on semaphore. */ +extern int semop __P ((int __semid, struct sembuf *__sops, + unsigned int __nsops)); + +__END_DECLS + +#endif /* sys/sem.h */ diff --git a/sysvipc/sys/shm.h b/sysvipc/sys/shm.h new file mode 100644 index 0000000..11f9e7a --- /dev/null +++ b/sysvipc/sys/shm.h @@ -0,0 +1,56 @@ +/* Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. +Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifndef _SYS_SHM_H +#define _SYS_SHM_H + +#include <features.h> +#include <unistd.h> /* for `getpagesize' declaration */ +#include <sys/types.h> + +/* Get common definition of System V style IPC. */ +#include <sys/ipc.h> + +/* Get system dependent definition of `struct shmid_ds' and more. */ +#include <sys/shm_buf.h> + + +/* Segment low boundary address multiple. */ +#define SHMLBA getpagesize () + +__BEGIN_DECLS + +/* The following System V style IPC functions implement a shared memory + facility. The definition is found in XPG2. */ + +/* Shared memory control operation. */ +extern int shmctl __P ((int __shmid, int __cmd, struct shmid_ds *__buf)); + +/* Get shared memory segment. */ +extern int shmget __P ((key_t __key, int __size, int __shmflg)); + +/* Attach shared memory segment. */ +extern int shmat __P ((int __shmid, char *__shmaddr, int __shmflg)); + +/* Detach shared memory segment. */ +extern int shmdt __P ((char *__shmaddr)); + +__END_DECLS + +#endif /* sys/shm.h */ |