summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorThomas Gellekum <tg@FreeBSD.org>2000-01-04 10:20:43 +0000
committerThomas Gellekum <tg@FreeBSD.org>2000-01-04 10:20:43 +0000
commitfe7f9ec6c12c67b4427cadef89ed7429245a1f0d (patch)
tree94213a51e532791c45d834626c8dc709b97c8d0c /lib/libc
parenta812a566d0fa4f78faf81b0af181127df626f326 (diff)
Notes
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/gen/directory.320
-rw-r--r--lib/libc/gen/readdir.c50
-rw-r--r--lib/libc/stdlib/Makefile.inc2
-rw-r--r--lib/libc/stdlib/rand.327
-rw-r--r--lib/libc/stdlib/rand.c55
5 files changed, 148 insertions, 6 deletions
diff --git a/lib/libc/gen/directory.3 b/lib/libc/gen/directory.3
index a1ca34774020..6334d384ae21 100644
--- a/lib/libc/gen/directory.3
+++ b/lib/libc/gen/directory.3
@@ -30,6 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)directory.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
.\"
.Dd June 4, 1993
.Dt DIRECTORY 3
@@ -37,6 +38,7 @@
.Sh NAME
.Nm opendir ,
.Nm readdir ,
+.Nm readdir_r ,
.Nm telldir ,
.Nm seekdir ,
.Nm rewinddir ,
@@ -50,6 +52,8 @@
.Fn opendir "const char *filename"
.Ft struct dirent *
.Fn readdir "DIR *dirp"
+.Ft int
+.Fn readdir_r "DIR *dirp" "struct dirent *entry" "struct dirent **result"
.Ft long
.Fn telldir "const DIR *dirp"
.Ft void
@@ -89,6 +93,22 @@ upon reaching the end of the directory or detecting an invalid
.Fn seekdir
operation.
.Pp
+.Fn readdir_r
+provides the same functionality as
+.Fn readdir ,
+but the caller must provide a directory
+.Fa entry
+buffer to store the results in. If the read succeeds,
+.Fa result
+is pointed at the
+.Fa entry ;
+upon reaching the end of the directory
+.Fa result
+is set to
+.Dv NULL .
+.Fn readdir_r
+returns 0 on success or an error number to indicate failure.
+.Pp
The
.Fn telldir
function
diff --git a/lib/libc/gen/readdir.c b/lib/libc/gen/readdir.c
index ab3a404095f4..2e91a9ac2448 100644
--- a/lib/libc/gen/readdir.c
+++ b/lib/libc/gen/readdir.c
@@ -29,6 +29,9 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
*/
#if defined(LIBC_SCCS) && !defined(lint)
@@ -37,6 +40,11 @@ static char sccsid[] = "@(#)readdir.c 8.3 (Berkeley) 9/29/94";
#include <sys/param.h>
#include <dirent.h>
+#include <errno.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+#endif _THREAD_SAFE
/*
* get next entry in a directory.
@@ -73,3 +81,45 @@ readdir(dirp)
return (dp);
}
}
+
+int
+readdir_r(dirp, entry, result)
+ DIR *dirp;
+ struct dirent *entry;
+ struct dirent **result;
+{
+ struct dirent *dp;
+ int ret, saved_errno;
+
+#ifdef _THREAD_SAFE
+ if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0)
+ return (ret);
+#endif
+
+ saved_errno = errno;
+ errno = 0;
+ dp = readdir(dirp);
+ if (errno != 0) {
+ if (dp == NULL) {
+#ifdef _THREAD_SAFE
+ _FD_UNLOCK(dirp->dd_fd, FD_READ);
+#endif
+ return (errno);
+ }
+ } else
+ errno = saved_errno;
+
+ if (dp != NULL)
+ memcpy(entry, dp, sizeof *entry);
+
+#ifdef _THREAD_SAFE
+ _FD_UNLOCK(dirp->dd_fd, FD_READ);
+#endif
+
+ if (dp != NULL)
+ *result = entry;
+ else
+ *result = NULL;
+
+ return (0);
+}
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index d1b4b2abc892..cb1d91f86759 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -30,7 +30,7 @@ MAN3+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \
MLINKS+=getenv.3 putenv.3 getenv.3 setenv.3 getenv.3 unsetenv.3
MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
-MLINKS+=rand.3 srand.3
+MLINKS+=rand.3 rand_r.3 rand.3 srand.3
MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 \
random.3 srandomdev.3
MLINKS+=strtol.3 strtoq.3
diff --git a/lib/libc/stdlib/rand.3 b/lib/libc/stdlib/rand.3
index f2e37421b6ac..1330cc76b1ae 100644
--- a/lib/libc/stdlib/rand.3
+++ b/lib/libc/stdlib/rand.3
@@ -34,13 +34,15 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)rand.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
.\"
-.Dd June 4, 1993
+.Dd May 25, 1999
.Dt RAND 3
.Os
.Sh NAME
.Nm rand ,
-.Nm srand
+.Nm srand ,
+.Nm rand_r
.Nd bad random number generator
.Sh SYNOPSIS
.Fd #include <stdlib.h>
@@ -48,6 +50,8 @@
.Fn srand "unsigned seed"
.Ft int
.Fn rand void
+.Ft int
+.Fn rand_r "unsigned *ctx"
.Sh DESCRIPTION
.Bf -symbolic
These interfaces are obsoleted by random(3).
@@ -63,15 +67,26 @@ of 0 to
.Pp
The
.Fn srand
-function sets its argument as the seed for a new sequence of
+function sets its argument
+.Fa seed
+as the seed for a new sequence of
pseudo-random numbers to be returned by
.Fn rand .
These sequences are repeatable by calling
.Fn srand
with the same seed value.
.Pp
-If no seed value is provided, the functions are automatically
+If no
+.Fa seed
+value is provided, the functions are automatically
seeded with a value of 1.
+.Pp
+.Fn rand_r
+provides the same functionality as
+.Fn rand .
+A pointer to the context value
+.Fa ctx
+must be supplied by the caller.
.Sh SEE ALSO
.Xr random 3
.Sh STANDARDS
@@ -82,3 +97,7 @@ and
functions
conform to
.St -ansiC .
+.Pp
+The
+.Fn rand_r
+function is as proposed in the POSIX.4a Draft #6 document.
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index 12846c343037..8c28a354350f 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -29,6 +29,10 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
+ *
+ * Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>.
+ *
+ * $FreeBSD$
*/
#if defined(LIBC_SCCS) && !defined(lint)
@@ -38,12 +42,32 @@ static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93";
#include <sys/types.h>
#include <stdlib.h>
+#ifdef TEST
+#include <stdio.h>
+#endif /* TEST */
+
+static int
+do_rand(unsigned long *ctx)
+{
+ return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
+}
+
+
+int
+rand_r(unsigned int *ctx)
+{
+ u_long val = (u_long) *ctx;
+ *ctx = do_rand(&val);
+ return (int) *ctx;
+}
+
+
static u_long next = 1;
int
rand()
{
- return ((next = next * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
+ return do_rand(&next);
}
void
@@ -52,3 +76,32 @@ u_int seed;
{
next = seed;
}
+
+#ifdef TEST
+
+main()
+{
+ int i;
+ unsigned myseed;
+
+ printf("seeding rand with 0x19610910: \n");
+ srand(0x19610910);
+
+ printf("generating three pseudo-random numbers:\n");
+ for (i = 0; i < 3; i++)
+ {
+ printf("next random number = %d\n", rand());
+ }
+
+ printf("generating the same sequence with rand_r:\n");
+ myseed = 0x19610910;
+ for (i = 0; i < 3; i++)
+ {
+ printf("next random number = %d\n", rand_r(&myseed));
+ }
+
+ return 0;
+}
+
+#endif /* TEST */
+