summaryrefslogtreecommitdiff
path: root/libc/misc
diff options
context:
space:
mode:
Diffstat (limited to 'libc/misc')
-rw-r--r--libc/misc/fnmatch/fnmatch.c8
-rw-r--r--libc/misc/regex/regex_old.c6
-rw-r--r--libc/misc/utmp/utent.c91
3 files changed, 55 insertions, 50 deletions
diff --git a/libc/misc/fnmatch/fnmatch.c b/libc/misc/fnmatch/fnmatch.c
index d25619b34..0fa043bad 100644
--- a/libc/misc/fnmatch/fnmatch.c
+++ b/libc/misc/fnmatch/fnmatch.c
@@ -21,13 +21,7 @@
# include <config.h>
#endif
-/* include unistd.h before we undefine _LIBC
- * because smallint is defined in unistd.h based
- * on _LIBC. For architectures that dont define
- * smallint of there own and rely upon the definition
- * from unistd.h will not build this file otherwise
- */
-
+/* unistd.h must be included with _LIBC defined: we need smallint */
#include <unistd.h>
#include <features.h>
#ifdef __UCLIBC__
diff --git a/libc/misc/regex/regex_old.c b/libc/misc/regex/regex_old.c
index bc2ad6cb8..a3c30b51a 100644
--- a/libc/misc/regex/regex_old.c
+++ b/libc/misc/regex/regex_old.c
@@ -23,7 +23,8 @@
/* To exclude some unwanted junk.... */
#undef emacs
#include <features.h>
-
+/* unistd.h must be included with _LIBC defined: we need smallint */
+#include <unistd.h>
#ifdef __UCLIBC__
# undef _LIBC
# define _REGEX_RE_COMP
@@ -33,7 +34,6 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
-#include <unistd.h>
#include <stdio.h>
/* AIX requires this to be the first thing in the file. */
@@ -263,7 +263,7 @@ static void
init_syntax_once (void)
{
register int c;
- static int done = 0;
+ static smallint done = 0;
if (done)
return;
diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c
index a3f01b6e9..a678130a3 100644
--- a/libc/misc/utmp/utent.c
+++ b/libc/misc/utmp/utent.c
@@ -25,14 +25,26 @@
__UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
+/* Do not create extra unlocked functions if no locking is needed */
+#if defined __UCLIBC_HAS_THREADS__
+# define static_if_threaded static
+#else
+# define static_if_threaded /* nothing */
+# define __setutent setutent
+# define __getutent getutent
+# define __getutid getutid
+#endif
+
+
/* Some global crap */
static int static_fd = -1;
static struct utmp static_utmp;
static const char default_file_name[] = _PATH_UTMP;
static const char *static_ut_name = default_file_name;
+
/* This function must be called with the LOCK held */
-static void __setutent(void)
+static_if_threaded void __setutent(void)
{
if (static_fd < 0) {
static_fd = open_not_cancel_2(static_ut_name, O_RDWR | O_CLOEXEC);
@@ -50,20 +62,19 @@ static void __setutent(void)
}
lseek(static_fd, 0, SEEK_SET);
}
-
+#if defined __UCLIBC_HAS_THREADS__
void setutent(void)
{
__UCLIBC_MUTEX_LOCK(utmplock);
__setutent();
__UCLIBC_MUTEX_UNLOCK(utmplock);
}
+#endif
libc_hidden_def(setutent)
/* This function must be called with the LOCK held */
-static struct utmp *__getutent(void)
+static_if_threaded struct utmp *__getutent(void)
{
- struct utmp *ret = NULL;
-
if (static_fd < 0) {
__setutent();
if (static_fd < 0) {
@@ -72,11 +83,22 @@ static struct utmp *__getutent(void)
}
if (read_not_cancel(static_fd, &static_utmp, sizeof(static_utmp)) == sizeof(static_utmp)) {
- ret = &static_utmp;
+ return &static_utmp;
}
+ return NULL;
+}
+#if defined __UCLIBC_HAS_THREADS__
+struct utmp *getutent(void)
+{
+ struct utmp *ret;
+
+ __UCLIBC_MUTEX_LOCK(utmplock);
+ ret = __getutent();
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
return ret;
}
+#endif
void endutent(void)
{
@@ -87,63 +109,52 @@ void endutent(void)
__UCLIBC_MUTEX_UNLOCK(utmplock);
}
-struct utmp *getutent(void)
-{
- struct utmp *ret = NULL;
-
- __UCLIBC_MUTEX_LOCK(utmplock);
- ret = __getutent();
- __UCLIBC_MUTEX_UNLOCK(utmplock);
- return ret;
-}
-
/* This function must be called with the LOCK held */
-static struct utmp *__getutid(const struct utmp *utmp_entry)
+static_if_threaded struct utmp *__getutid(const struct utmp *utmp_entry)
{
struct utmp *lutmp;
+ unsigned type;
+
+ /* We use the fact that constants we are interested in are: */
+ /* RUN_LVL=1, ... OLD_TIME=4; INIT_PROCESS=5, ... USER_PROCESS=8 */
+ type = utmp_entry->ut_type - 1;
+ type /= 4;
while ((lutmp = __getutent()) != NULL) {
- if ( (utmp_entry->ut_type == RUN_LVL ||
- utmp_entry->ut_type == BOOT_TIME ||
- utmp_entry->ut_type == NEW_TIME ||
- utmp_entry->ut_type == OLD_TIME) &&
- lutmp->ut_type == utmp_entry->ut_type)
- {
- return lutmp;
- }
- if ( (utmp_entry->ut_type == INIT_PROCESS ||
- utmp_entry->ut_type == DEAD_PROCESS ||
- utmp_entry->ut_type == LOGIN_PROCESS ||
- utmp_entry->ut_type == USER_PROCESS) &&
- !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
- {
- return lutmp;
- }
+ if (type == 0 && lutmp->ut_type == utmp_entry->ut_type) {
+ /* one of RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME */
+ return lutmp;
+ }
+ if (type == 1 && strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)) == 0) {
+ /* INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, DEAD_PROCESS */
+ return lutmp;
+ }
}
return NULL;
}
-
+#if defined __UCLIBC_HAS_THREADS__
struct utmp *getutid(const struct utmp *utmp_entry)
{
- struct utmp *ret = NULL;
+ struct utmp *ret;
__UCLIBC_MUTEX_LOCK(utmplock);
ret = __getutid(utmp_entry);
__UCLIBC_MUTEX_UNLOCK(utmplock);
return ret;
}
-libc_hidden_def(getutid)
+#endif
struct utmp *getutline(const struct utmp *utmp_entry)
{
- struct utmp *lutmp = NULL;
+ struct utmp *lutmp;
__UCLIBC_MUTEX_LOCK(utmplock);
while ((lutmp = __getutent()) != NULL) {
- if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
- !strcmp(lutmp->ut_line, utmp_entry->ut_line)) {
- break;
+ if (lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) {
+ if (strncmp(lutmp->ut_line, utmp_entry->ut_line, sizeof(lutmp->ut_line)) == 0) {
+ break;
+ }
}
}
__UCLIBC_MUTEX_UNLOCK(utmplock);