summaryrefslogtreecommitdiff
path: root/lib/libc/gen
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/gen')
-rw-r--r--lib/libc/gen/Makefile.inc4
-rw-r--r--lib/libc/gen/isatty.c17
-rw-r--r--lib/libc/gen/sleep.c20
-rw-r--r--lib/libc/gen/ttyname.c111
-rw-r--r--lib/libc/gen/usleep.c19
5 files changed, 167 insertions, 4 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
index 69a434d85daa..3809f449f571 100644
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -1,5 +1,5 @@
# From: @(#)Makefile.inc 8.3 (Berkeley) 4/16/94
-# $Id: Makefile.inc,v 1.15 1994/12/18 14:06:38 guido Exp $
+# $Id: Makefile.inc,v 1.16 1995/05/30 05:40:08 rgrimes Exp $
# machine-independent gen sources
.PATH: ${.CURDIR}/${MACHINE}/gen ${.CURDIR}/gen
@@ -19,7 +19,7 @@ SRCS+= alarm.c assert.c clock.c closedir.c config.c confstr.c crypt.c \
sigsetops.c sleep.c sysconf.c sysctl.c syslog.c \
telldir.c termios.c time.c times.c timezone.c ttyname.c ttyslot.c \
ualarm.c uname.c unvis.c usleep.c utime.c valloc.c vis.c wait.c \
- wait3.c waitpid.c
+ wait3.c waitpid.c _thread_init.c
# *rand48 family, from 1.1.5
SRCS+= _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \
diff --git a/lib/libc/gen/isatty.c b/lib/libc/gen/isatty.c
index f6bb04b86316..c8356c710342 100644
--- a/lib/libc/gen/isatty.c
+++ b/lib/libc/gen/isatty.c
@@ -37,12 +37,27 @@ static char sccsid[] = "@(#)isatty.c 8.1 (Berkeley) 6/4/93";
#include <termios.h>
#include <unistd.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+#endif
int
isatty(fd)
int fd;
{
+ int retval;
struct termios t;
- return(tcgetattr(fd, &t) != -1);
+#ifdef _THREAD_SAFE
+ if (_thread_fd_lock(fd, FD_READ, NULL,__FILE__,__LINE__) == 0) {
+#endif
+ retval = (tcgetattr(fd, &t) != -1);
+#ifdef _THREAD_SAFE
+ _thread_fd_unlock(fd, FD_READ);
+ } else {
+ retval = 0;
+ }
+#endif
+ return(retval);
}
diff --git a/lib/libc/gen/sleep.c b/lib/libc/gen/sleep.c
index 98d997d8b046..b06475d392a5 100644
--- a/lib/libc/gen/sleep.c
+++ b/lib/libc/gen/sleep.c
@@ -38,16 +38,33 @@ static char sccsid[] = "@(#)sleep.c 8.1 (Berkeley) 6/4/93";
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+#else
#define setvec(vec, a) \
vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
static int ringring;
+#endif
unsigned int
sleep(seconds)
unsigned int seconds;
{
+#ifdef _THREAD_SAFE
+ struct timespec time_to_sleep;
+ struct timespec time_remaining;
+
+ if (seconds) {
+ time_to_sleep.ts_sec = seconds;
+ time_to_sleep.ts_nsec = 0;
+ nanosleep(&time_to_sleep,&time_remaining);
+ seconds = time_remaining.ts_sec;
+ }
+ return(seconds);
+#else
register struct itimerval *itp;
struct itimerval itv, oitv;
struct sigvec vec, ovec;
@@ -88,10 +105,13 @@ sleep(seconds)
(void) sigsetmask(omask);
(void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
return 0;
+#endif
}
+#ifndef _THREAD_SAFE
static void
sleephandler()
{
ringring = 1;
}
+#endif
diff --git a/lib/libc/gen/ttyname.c b/lib/libc/gen/ttyname.c
index f024f52e20ca..39c640ee5eae 100644
--- a/lib/libc/gen/ttyname.c
+++ b/lib/libc/gen/ttyname.c
@@ -39,11 +39,121 @@ static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94";
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
+#include <stdlib.h>
#include <termios.h>
+#include <unistd.h>
#include <db.h>
#include <string.h>
#include <paths.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_key_t ttyname_key;
+static int ttyname_init = 0;
+
+char *
+ttyname(int fd)
+{
+ char *ret;
+
+ if (_thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__) == 0) {
+ ret = __ttyname_basic(fd);
+ _thread_fd_unlock(fd, FD_READ);
+ } else {
+ ret = NULL;
+ }
+
+ return (ret);
+}
+
+char *
+__ttyname_r_basic(int fd, char *buf, size_t len)
+{
+ register struct dirent *dirp;
+ register DIR *dp;
+ struct stat dsb;
+ struct stat sb;
+ char *rval;
+ int minlen;
+
+ rval = NULL;
+
+ /* Must be a terminal. */
+ if (!isatty(fd))
+ return (rval);
+ /* Must be a character device. */
+ if (_thread_sys_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
+ return (rval);
+ /* Must have enough room */
+ if (len <= sizeof(_PATH_DEV))
+ return (rval);
+
+ if ((dp = opendir(_PATH_DEV)) != NULL) {
+ memcpy(buf, _PATH_DEV, sizeof(_PATH_DEV));
+ for (rval = NULL; (dirp = readdir(dp)) != NULL;) {
+ if (dirp->d_fileno != sb.st_ino)
+ continue;
+ minlen = (len - (sizeof(_PATH_DEV) - 1)) < (dirp->d_namlen + 1) ?
+ (len - (sizeof(_PATH_DEV) - 1)) : (dirp->d_namlen + 1);
+ memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, minlen);
+ if (stat(buf, &dsb) || sb.st_dev != dsb.st_dev ||
+ sb.st_ino != dsb.st_ino)
+ continue;
+ rval = buf;
+ break;
+ }
+ (void) closedir(dp);
+ }
+ return (rval);
+}
+
+char *
+__ttyname_basic(int fd)
+{
+ char *buf;
+
+ pthread_mutex_lock(&ttyname_lock);
+ if (ttyname_init == 0) {
+ if (pthread_keycreate(&ttyname_key, free)) {
+ pthread_mutex_unlock(&ttyname_lock);
+ return (NULL);
+ }
+ ttyname_init = 1;
+ }
+ pthread_mutex_unlock(&ttyname_lock);
+
+ /* Must have thread specific data field to put data */
+ if (pthread_getspecific(ttyname_key, (void **) &buf) != 0) {
+ return (NULL);
+ } else if (buf == NULL) {
+ if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
+ if (pthread_setspecific(ttyname_key, buf) != 0) {
+ free(buf);
+ return (NULL);
+ }
+ } else {
+ return (NULL);
+ }
+ }
+ return (__ttyname_r_basic(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
+}
+
+char *
+ttyname_r(int fd, char *buf, size_t len)
+{
+ char *ret;
+
+ if (_thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__) == 0) {
+ ret = __ttyname_r_basic(fd, buf, len);
+ _thread_fd_unlock(fd, FD_READ);
+ } else {
+ ret = NULL;
+ }
+ return (ret);
+}
+#else
static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
static char *oldttyname __P((int, struct stat *));
@@ -110,3 +220,4 @@ oldttyname(fd, sb)
(void)closedir(dp);
return (NULL);
}
+#endif
diff --git a/lib/libc/gen/usleep.c b/lib/libc/gen/usleep.c
index 3f4a7f538b45..9e29f314d959 100644
--- a/lib/libc/gen/usleep.c
+++ b/lib/libc/gen/usleep.c
@@ -38,7 +38,10 @@ static char sccsid[] = "@(#)usleep.c 8.1 (Berkeley) 6/4/93";
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
-
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+#else
#define TICK 10000 /* system clock resolution in microseconds */
#define USPS 1000000 /* number of microseconds in a second */
@@ -46,11 +49,22 @@ static char sccsid[] = "@(#)usleep.c 8.1 (Berkeley) 6/4/93";
vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
static int ringring;
+#endif
+
void
usleep(useconds)
unsigned int useconds;
{
+#ifdef _THREAD_SAFE
+ struct timespec time_to_sleep;
+
+ if (useconds) {
+ time_to_sleep.ts_nsec = (useconds % 1000000) * 1000;
+ time_to_sleep.ts_sec = useconds / 1000000;
+ nanosleep(&time_to_sleep,NULL);
+ }
+#else
register struct itimerval *itp;
struct itimerval itv, oitv;
struct sigvec vec, ovec;
@@ -90,10 +104,13 @@ usleep(useconds)
(void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
(void) sigsetmask(omask);
(void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
+#endif
}
+#ifndef _THREAD_SAFE
static void
sleephandler()
{
ringring = 1;
}
+#endif