From cf4328b16efe844461420da58f4f0b0f75964418 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 26 Oct 2000 07:12:49 +0000 Subject: Add some more stuff -- {get|set}mntent, getline, getdelim, etc. --- libc/misc/Makefile | 2 +- libc/misc/mntent/.indent.pro | 33 ++++++++ libc/misc/mntent/Makefile | 44 +++++++++++ libc/misc/mntent/mntent.c | 79 +++++++++++++++++++ libc/stdio/Makefile | 2 +- libc/stdio/getdelim.c | 70 ++++++++++++++++ libc/stdio/getline.c | 32 ++++++++ libc/stdlib/setenv.c | 184 +++++++++++++++++++++++++++++-------------- libc/sysdeps/linux/Makefile | 3 - 9 files changed, 386 insertions(+), 63 deletions(-) create mode 100644 libc/misc/mntent/.indent.pro create mode 100644 libc/misc/mntent/Makefile create mode 100644 libc/misc/mntent/mntent.c create mode 100644 libc/stdio/getdelim.c create mode 100644 libc/stdio/getline.c (limited to 'libc') diff --git a/libc/misc/Makefile b/libc/misc/Makefile index d839813fc..9907f2d99 100644 --- a/libc/misc/Makefile +++ b/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 diff --git a/libc/misc/mntent/.indent.pro b/libc/misc/mntent/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/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 diff --git a/libc/misc/mntent/Makefile b/libc/misc/mntent/Makefile new file mode 100644 index 000000000..4c60ac1e7 --- /dev/null +++ b/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 + diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c new file mode 100644 index 000000000..2bdd3c7a6 --- /dev/null +++ b/libc/misc/mntent/mntent.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include + + + +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; +} diff --git a/libc/stdio/Makefile b/libc/stdio/Makefile index 2061a745d..5b0b16132 100644 --- a/libc/stdio/Makefile +++ b/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) diff --git a/libc/stdio/getdelim.c b/libc/stdio/getdelim.c new file mode 100644 index 000000000..99d48bb32 --- /dev/null +++ b/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 + * , + * + * 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 +#include +#include +#include +#include +#include + + +/* 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; +} + diff --git a/libc/stdio/getline.c b/libc/stdio/getline.c new file mode 100644 index 000000000..38440e1bf --- /dev/null +++ b/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 + * , + * + * 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 +#include + +/* 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)); +} + diff --git a/libc/stdlib/setenv.c b/libc/stdlib/setenv.c index f7d597139..b17280562 100644 --- a/libc/stdlib/setenv.c +++ b/libc/stdlib/setenv.c @@ -1,76 +1,144 @@ -/* Copyright (C) 1995,1996 Robert de Bath - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - */ -#include -#include -#include +/* 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 +#include +#include - 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 - 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(); } diff --git a/libc/sysdeps/linux/Makefile b/libc/sysdeps/linux/Makefile index 3fac77e5f..a78ecf8f9 100644 --- a/libc/sysdeps/linux/Makefile +++ b/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 -- cgit v1.2.3