Browse Source

Add some more stuff -- {get|set}mntent, getline, getdelim, etc.

Eric Andersen 23 years ago
parent
commit
cf4328b16e

+ 2 - 1
Makefile

@@ -26,7 +26,7 @@
 #
 #--------------------------------------------------------
 
-
+include Rules.mak
 
 DIRS = misc pwd_grp stdio string termios unistd net signal stdlib sysdeps
 
@@ -45,6 +45,7 @@ headers: dummy
 	@if [ ! -L "include/asm" ]; then ln -s /usr/include/asm include/asm ; fi
 	@if [ ! -L "include/net" ]; then ln -s /usr/include/net include/net ; fi
 	@if [ ! -L "include/linux" ]; then ln -s /usr/include/linux include/linux ; fi
+	@if [ ! -L "include/bits" ]; then ln -s ../sysdeps/linux/$(ARCH)/bits include/bits ; fi
 
 tags:
 	ctags -R

+ 3 - 0
Rules.mak

@@ -66,6 +66,9 @@ CROSS = #powerpc-linux-
 CC = $(CROSS)gcc
 STRIPTOOL = $(CROSS)strip
 
+# Figure out what arch to build...
+ARCH = $(shell uname -m | sed -e 's/i.86/i386/' -e 's/sparc.*/sparc/' -e 's/arm.*/arm/g')
+
 
 #--------------------------------------------------------
 # Nothing beyond this point should need be touched by mere 

+ 9 - 1
include/stdio.h

@@ -210,8 +210,16 @@ extern int vfscanf __P ((FILE *__restrict __s,
 /* Print a message describing the meaning of the value of errno.  */
 extern void perror __P ((__const char *__s));
 
+/* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
+   (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
+   NULL), pointing to *N characters of space.  It is realloc'd as
+   necessary.  Returns the number of characters read (not including the
+   null terminator), or -1 on error or EOF.  */
+extern size_t getdelim __P ((char **__restrict __lineptr,
+				  size_t *__restrict __n, int __delimiter,
+				  FILE *__restrict __stream));
 /* Like `getdelim', but reads up to a newline.  */
-extern int getline __P ((char **__restrict __lineptr,
+extern size_t getline __P ((char **__restrict __lineptr,
 				 size_t *__restrict __n,
 				 FILE *__restrict __stream));
 

+ 1 - 1
libc/misc/Makefile

@@ -21,7 +21,7 @@
 # respective copyright holders.
 
 
-DIRS = assert crypt fnmatch glob internals lsearch syslog regex shm time
+DIRS = assert crypt fnmatch glob internals lsearch mntent syslog regex shm time
 
 ifeq ($(USE_CTYPE_C_FUNCTIONS),true)
     DIRS+=ctype

+ 33 - 0
libc/misc/mntent/.indent.pro

@@ -0,0 +1,33 @@
+--blank-lines-after-declarations
+--blank-lines-after-procedures
+--break-before-boolean-operator
+--no-blank-lines-after-commas
+--braces-on-if-line
+--braces-on-struct-decl-line
+--comment-indentation25
+--declaration-comment-column25
+--no-comment-delimiters-on-blank-lines
+--cuddle-else
+--continuation-indentation4
+--case-indentation0
+--else-endif-column33
+--space-after-cast
+--line-comments-indentation0
+--declaration-indentation1
+--dont-format-first-column-comments
+--dont-format-comments
+--honour-newlines
+--indent-level4
+/* changed from 0 to 4 */
+--parameter-indentation4
+--line-length78 /* changed from 75 */
+--continue-at-parentheses
+--no-space-after-function-call-names
+--dont-break-procedure-type
+--dont-star-comments
+--leave-optional-blank-lines
+--dont-space-special-semicolon
+--tab-size4
+/* additions by Mark */
+--case-brace-indentation0
+--leave-preprocessor-space

+ 44 - 0
libc/misc/mntent/Makefile

@@ -0,0 +1,44 @@
+# Makefile for uCLibc
+#
+# Copyright (C) 2000 by Lineo, inc.
+#
+# This program 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.
+#
+# This program 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 General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+# Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# Derived in part from the Linux-8086 C library, the GNU C Library, and several
+# other sundry sources.  Files within this library are copyright by their
+# respective copyright holders.
+
+TOPDIR=../../
+include $(TOPDIR)Rules.mak
+LIBC=$(TOPDIR)libc.a
+
+CSRC=mntent.c
+COBJS=$(patsubst %.c,%.o, $(CSRC))
+OBJS=$(COBJS)
+
+all: $(OBJS) $(LIBC)
+
+$(LIBC): ar-target
+
+ar-target: $(OBJS)
+	$(AR) $(ARFLAGS) $(LIBC) $(OBJS)
+
+$(COBJS):
+	$(CC) $(CFLAGS) $< -c $*.c -o $*.o
+	$(STRIPTOOL) -x -R .note -R .comment $*.o
+
+clean:
+	rm -f *.[oa] *~ core
+

+ 79 - 0
libc/misc/mntent/mntent.c

@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mntent.h>
+
+
+
+struct mntent *getmntent(FILE * filep)
+{
+	char *cp, *sep = " \t\n";
+	static char buff[MNTMAXSTR];
+	static struct mntent mnt;
+
+	/* Loop on the file, skipping comment lines. - FvK 03/07/93 */
+	while ((cp = fgets(buff, sizeof buff, filep)) != NULL) {
+		if (buff[0] == '#' || buff[0] == '\n')
+			continue;
+		break;
+	}
+
+	/* At the EOF, the buffer should be unchanged. We should
+	 * check the return value from fgets ().
+	 */
+	if (cp == NULL)
+		return NULL;
+
+	mnt.mnt_fsname = strtok(buff, sep);
+	if (mnt.mnt_fsname == NULL)
+		return NULL;
+
+	mnt.mnt_dir = strtok(NULL, sep);
+	if (mnt.mnt_dir == NULL)
+		return NULL;
+
+	mnt.mnt_type = strtok(NULL, sep);
+	if (mnt.mnt_type == NULL)
+		return NULL;
+
+	mnt.mnt_opts = strtok(NULL, sep);
+	if (mnt.mnt_opts == NULL)
+		mnt.mnt_opts = "";
+
+	cp = strtok(NULL, sep);
+	mnt.mnt_freq = (cp != NULL) ? atoi(cp) : 0;
+
+	cp = strtok(NULL, sep);
+	mnt.mnt_passno = (cp != NULL) ? atoi(cp) : 0;
+
+	return &mnt;
+}
+
+int addmntent(FILE * filep, const struct mntent *mnt)
+{
+	if (fseek(filep, 0, SEEK_END) < 0)
+		return 1;
+
+	if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
+		 mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
+		return 1;
+
+	return 0;
+}
+
+char *hasmntopt(const struct mntent *mnt, const char *opt)
+{
+	return strstr(mnt->mnt_opts, opt);
+}
+
+FILE *setmntent(const char *name, const char *mode)
+{
+	return fopen(name, mode);
+}
+
+int endmntent(FILE * filep)
+{
+	if (filep != NULL)
+		fclose(filep);
+	return 1;
+}

+ 1 - 1
libc/stdio/Makefile

@@ -36,7 +36,7 @@ MOBJ2=printf.o sprintf.o fprintf.o vprintf.o vsprintf.o vfprintf.o snprintf.o vs
 MSRC3=scanf.c
 MOBJ3=scanf.o sscanf.o fscanf.o vscanf.o vsscanf.o vfscanf.o
 
-CSRC=dputs.c popen.c perror.c remove.c
+CSRC=dputs.c popen.c perror.c remove.c getdelim.c getline.c
 COBJS=$(patsubst %.c,%.o, $(CSRC))
 OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS)
 

+ 70 - 0
libc/stdio/getdelim.c

@@ -0,0 +1,70 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * getdelim for uclibc
+ *
+ * Copyright (C) 2000 by Lineo, inc.  Written by Erik Andersen
+ * <andersen@lineo.com>, <andersee@debian.org>
+ *
+ * This program 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.
+ *
+ * This program 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 General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+
+
+/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
+   (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
+   NULL), pointing to *N characters of space.  It is realloc'd as
+   necessary.  Returns the number of characters read (not including the
+   null delimiter), or -1 on error or EOF.  */
+size_t getdelim(char **linebuf, size_t *linebufsz, int delimiter, FILE *file)
+{
+	static const int GROWBY = 80; /* how large we will grow strings by */
+
+	int ch;
+	int idx = 0;
+
+	if (file == NULL || linebuf==NULL || *linebuf == NULL || linebufsz == NULL) {
+	    errno=EINVAL;
+	    return -1;
+	}
+
+	while (1) {
+		ch = fgetc(file);
+		if (ch == EOF)
+			break;
+		/* grow the line buffer as necessary */
+		while (idx > *linebufsz-2) {
+			*linebuf = realloc(*linebuf, *linebufsz += GROWBY);
+			if (!*linebuf) {
+				errno=ENOMEM;
+				return -1;
+			}
+		}
+		*linebuf[idx++] = (char)ch;
+		if ((char)ch == delimiter)
+			break;
+	}
+
+	if (idx != 0)
+	    *linebuf[idx] = 0;
+	return idx;
+}
+

+ 32 - 0
libc/stdio/getline.c

@@ -0,0 +1,32 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * getline for uclibc
+ *
+ * Copyright (C) 2000 by Lineo, inc.  Written by Erik Andersen
+ * <andersen@lineo.com>, <andersee@debian.org>
+ *
+ * This program 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.
+ *
+ * This program 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 General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include <stddef.h>
+#include <stdio.h>
+
+/* Basically getdelim() with the delimiter hard wired to '\n' */
+size_t getline(char **linebuf, size_t *n, FILE *file)
+{
+  return (getdelim (linebuf, n, '\n', file));
+}
+

+ 126 - 58
libc/stdlib/setenv.c

@@ -1,76 +1,144 @@
-/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
- * This file is part of the Linux-8086 C library and is distributed
- * under the GNU Library General Public License.
- */
-#include <string.h>
-#include <stdlib.h>
-#include <malloc.h>
+/* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
 
-extern char **environ;
+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.
 
-#define ADD_NUM 4
+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.
 
-int setenv(var, value, overwrite)
-const char *var;
-const char *value;
-int overwrite;
-{
-	static char **mall_env = 0;
-	static int extras = 0;
-	char **p, **d;
-	char *t;
-	int len;
+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.  */
 
-	len = strlen(var);
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
-	if (!environ) {
-		environ = (char **) malloc(ADD_NUM * sizeof(char *));
-		memset(environ, 0, sizeof(char *) * ADD_NUM);
+#if !defined(HAVE_GNU_LD) && !defined (__ELF__)
+#define	__environ	environ
+#endif
 
-		extras = ADD_NUM;
-	}
+#if defined(_REENTRENT) || defined(_THREAD_SAFE)
+# include <pthread.h>
 
-	for (p = environ; *p; p++) {
-		if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') {
-			if (!overwrite)
-				return -1;
-			/* Overwrite stuff */
-			while ((p[0] = p[1]))
-				p++;
-			extras++;
-			break;
-		}
-	}
+/* We need to initialize the mutex.  For this we use a method provided
+   by pthread function 'pthread_once'.  For this we need a once block.  */
+static pthread_once__t _once_block = pthread_once_init;
+
+/* This is the mutex which protects the global environment of simultaneous
+   modifications.  */
+static pthread_mutex_t _setenv_mutex;
+
+static void
+DEFUN_VOID(_init_setenv_mutex)
+{
+  pthread_mutex_init(&_setenv_mutex, pthread_mutexattr_default);
+}
 
-	if (extras <= 0) {			/* Need more space */
-		d = malloc((p - environ + 1 + ADD_NUM) * sizeof(char *));
+# define LOCK() \
+   do { pthread_once(&_once_block, _init_setenv_mutex);
+        pthread_mutex_lock(&_setenv_mutex); } while (0)
+# define UNLOCK() pthread_mutex_unlock(&_setenv_mutex)
 
-		if (d == 0)
-			return -1;
+#else /* !_REENTRENT && !_THREAD_SAFE */
 
-		memcpy((void *) d, (void *) environ,
+# define LOCK()
+# define UNLOCK()
 
-			   (p - environ + 1) * sizeof(char *));
-		p = d + (p - environ);
-		extras = ADD_NUM;
+#endif /* _REENTRENT || _THREAD_SAFE */
 
-		if (mall_env)
-			free(mall_env);
-		environ = d;
-		mall_env = d;
+int setenv(const char *name, const char *value, int replace)
+{
+  register char **ep;
+  register size_t size;
+  const size_t namelen = strlen (name);
+  const size_t vallen = strlen (value);
+  int result = 0;
+
+  LOCK();
+
+  size = 0;
+  for (ep = __environ; *ep != NULL; ++ep)
+    if (!memcmp (*ep, name, namelen) && (*ep)[namelen] == '=')
+      break;
+    else
+      ++size;
+  
+  if (*ep == NULL)
+    {
+      static char **last_environ = NULL;
+      char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
+      if (new_environ == NULL)
+	{
+	  result = -1;
+	  goto do_return;
+	}
+      (void) memcpy((__ptr_t) new_environ, (__ptr_t) __environ, size * sizeof(char *));
+
+      new_environ[size] = malloc (namelen + 1 + vallen + 1);
+      if (new_environ[size] == NULL)
+	{
+	  free (new_environ);
+	  errno = ENOMEM;
+	  result = -1;
+	  goto do_return;
+	}
+      memcpy (new_environ[size], name, namelen);
+      new_environ[size][namelen] = '=';
+      memcpy (&new_environ[size][namelen + 1], value, vallen + 1);
+
+      new_environ[size + 1] = NULL;
+
+      if (last_environ != NULL)
+	free ((__ptr_t) last_environ);
+      last_environ = new_environ;
+      __environ = new_environ;
+    }
+  else if (replace)
+    {
+      size_t len = strlen (*ep);
+      if (len < namelen + 1 + vallen)
+	{
+	  char *new = malloc (namelen + 1 + vallen + 1);
+	  if (new == NULL)
+	    {
+	      result = -1;
+	      goto do_return;
+	    }
+	  *ep = new;
+	  memcpy (*ep, name, namelen);
+	  (*ep)[namelen] = '=';
 	}
+      memcpy (&(*ep)[namelen + 1], value, vallen + 1);
+    }
 
-	t = malloc(len + 1 + strlen(value) + 1);
-	if (!t)
-		return -1;
+do_return:
+  UNLOCK();
+  return result;
+}
+
+
+void unsetenv(const char *name)
+{
+  register char **ep;
+  register char **dp;
+  const size_t namelen = strlen (name);
 
-	strcpy(t, var);
-	strcat(t, "=");
-	strcat(t, value);
+  LOCK();
 
-	*p++ = (char *) t;
-	*p = '\0';
-	extras--;
+  for (dp = ep = __environ; *ep != NULL; ++ep)
+    if (memcmp (*ep, name, namelen) || (*ep)[namelen] != '=')
+      {
+	*dp = *ep;
+	++dp;
+      }
+  *dp = NULL;
 
-	return 0;
+  UNLOCK();
 }

+ 0 - 3
libc/sysdeps/linux/Makefile

@@ -20,9 +20,6 @@
 # other sundry sources.  Files within this library are copyright by their
 # respective copyright holders.
 
-# Figure out what arch to build...
-ARCH = $(shell uname -m | sed -e 's/i.86/i386/' -e 's/sparc.*/sparc/' -e 's/arm.*/arm/g')
-
 DIRS = $(ARCH) common
 
 all: libc.a