diff options
Diffstat (limited to 'libc/stdlib')
-rw-r--r-- | libc/stdlib/.indent.pro | 33 | ||||
-rw-r--r-- | libc/stdlib/Makefile | 11 | ||||
-rw-r--r-- | libc/stdlib/atexit.c | 108 | ||||
-rw-r--r-- | libc/stdlib/bsearch.c | 56 | ||||
-rw-r--r-- | libc/stdlib/getenv.c | 32 | ||||
-rw-r--r-- | libc/stdlib/malloc-simple/.indent.pro | 33 | ||||
-rw-r--r-- | libc/stdlib/malloc-simple/Makefile | 46 | ||||
-rw-r--r-- | libc/stdlib/malloc-simple/alloc.c | 106 | ||||
-rw-r--r-- | libc/stdlib/malloc/Makefile | 17 | ||||
-rw-r--r-- | libc/stdlib/malloc/alloc.c | 48 | ||||
-rw-r--r-- | libc/stdlib/mkstemp.c | 28 | ||||
-rw-r--r-- | libc/stdlib/mktemp.c | 24 | ||||
-rw-r--r-- | libc/stdlib/putenv.c | 103 | ||||
-rw-r--r-- | libc/stdlib/qsort.c | 225 | ||||
-rw-r--r-- | libc/stdlib/rand.c | 24 | ||||
-rw-r--r-- | libc/stdlib/realpath.c | 30 | ||||
-rw-r--r-- | libc/stdlib/setenv.c | 122 | ||||
-rw-r--r-- | libc/stdlib/strtod.c | 113 | ||||
-rw-r--r-- | libc/stdlib/system.c | 80 |
19 files changed, 736 insertions, 503 deletions
diff --git a/libc/stdlib/.indent.pro b/libc/stdlib/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/stdlib/.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/stdlib/Makefile b/libc/stdlib/Makefile index 14ebec492..da402be6c 100644 --- a/libc/stdlib/Makefile +++ b/libc/stdlib/Makefile @@ -35,12 +35,15 @@ CSRC=atoi.c atol.c ltoa.c ltostr.c ctype.c qsort.c bsearch.c rand.c lsearch.c \ mkstemp.c mktemp.c realpath.c getenv.c putenv.c popen.c system.c \ getcwd.c setenv.c execl.c execv.c execlp.c execvp.c execvep.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(MOBJ) $(MOBJ2) $(COBJS) -all: $(MOBJ) $(MOBJ2) $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(MOBJ) $(MOBJ2) $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(MOBJ2) $(COBJS) +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o @@ -48,6 +51,8 @@ $(MOBJ): $(MSRC) $(MOBJ2): $(MSRC2) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o +$(OBJ): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c index 7c6a03af8..1c164ff86 100644 --- a/libc/stdlib/atexit.c +++ b/libc/stdlib/atexit.c @@ -14,20 +14,18 @@ #include <errno.h> /* ATEXIT.H */ -#define MAXONEXIT 20 /* AIUI Posix requires 10 */ +#define MAXONEXIT 20 /* AIUI Posix requires 10 */ typedef void (*vfuncp) (); extern vfuncp __cleanup; extern void __do_exit(); -extern void _exit __P ((int __status)) __attribute__ ((__noreturn__)); +extern void _exit __P((int __status)) __attribute__ ((__noreturn__)); -extern struct exit_table -{ - vfuncp called; - void *argument; -} -__on_exit_table[MAXONEXIT]; +extern struct exit_table { + vfuncp called; + void *argument; +} __on_exit_table[MAXONEXIT]; extern int __on_exit_count; @@ -36,83 +34,75 @@ extern int __on_exit_count; #ifdef L_atexit vfuncp __cleanup; -int -atexit(ptr) +int atexit(ptr) vfuncp ptr; { - if( __on_exit_count < 0 || __on_exit_count >= MAXONEXIT) - { - errno = ENOMEM; - return -1; - } - __cleanup = __do_exit; - if( ptr ) - { - __on_exit_table[__on_exit_count].called = ptr; - __on_exit_table[__on_exit_count].argument = 0; - __on_exit_count++; - } - return 0; + if (__on_exit_count < 0 || __on_exit_count >= MAXONEXIT) { + errno = ENOMEM; + return -1; + } + __cleanup = __do_exit; + if (ptr) { + __on_exit_table[__on_exit_count].called = ptr; + __on_exit_table[__on_exit_count].argument = 0; + __on_exit_count++; + } + return 0; } #endif #ifdef L_on_exit -int -on_exit(ptr, arg) +int on_exit(ptr, arg) vfuncp ptr; void *arg; { - if( __on_exit_count < 0 || __on_exit_count >= MAXONEXIT) - { - errno = ENOMEM; - return -1; - } - __cleanup = __do_exit; - if( ptr ) - { - __on_exit_table[__on_exit_count].called = ptr; - __on_exit_table[__on_exit_count].argument = arg; - __on_exit_count++; - } - return 0; + if (__on_exit_count < 0 || __on_exit_count >= MAXONEXIT) { + errno = ENOMEM; + return -1; + } + __cleanup = __do_exit; + if (ptr) { + __on_exit_table[__on_exit_count].called = ptr; + __on_exit_table[__on_exit_count].argument = arg; + __on_exit_count++; + } + return 0; } #endif #ifdef L___do_exit -int __on_exit_count = 0; +int __on_exit_count = 0; struct exit_table __on_exit_table[MAXONEXIT]; -void -__do_exit(rv) -int rv; +void __do_exit(rv) +int rv; { - register int count = __on_exit_count-1; - register vfuncp ptr; - __on_exit_count = -1; /* ensure no more will be added */ - __cleanup = 0; /* Calling exit won't re-do this */ - - /* In reverse order */ - for (; count >= 0; count--) - { - ptr = __on_exit_table[count].called; - (*ptr) (rv, __on_exit_table[count].argument); - } + register int count = __on_exit_count - 1; + register vfuncp ptr; + + __on_exit_count = -1; /* ensure no more will be added */ + __cleanup = 0; /* Calling exit won't re-do this */ + + /* In reverse order */ + for (; count >= 0; count--) { + ptr = __on_exit_table[count].called; + (*ptr) (rv, __on_exit_table[count].argument); + } } #endif #ifdef L_exit -void -exit(rv) -int rv; +void exit(rv) +int rv; { - if (__cleanup) - __cleanup(); - _exit(rv); + if (__cleanup) + __cleanup(); + _exit(rv); } #endif diff --git a/libc/stdlib/bsearch.c b/libc/stdlib/bsearch.c index 72ba2617a..04d5ab68b 100644 --- a/libc/stdlib/bsearch.c +++ b/libc/stdlib/bsearch.c @@ -10,37 +10,33 @@ */ #include <stdio.h> -static int _bsearch; /* index of element found, or where to - * insert */ +static int _bsearch; /* index of element found, or where to -char * -bsearch(key, base, num, size, cmp) -register char *key; /* item to search for */ -register char *base; /* base address */ -int num; /* number of elements */ -register int size; /* element size in bytes */ -register int (*cmp) (); /* comparison function */ + * insert */ + +char *bsearch(key, base, num, size, cmp) +register char *key; /* item to search for */ +register char *base; /* base address */ +int num; /* number of elements */ +register int size; /* element size in bytes */ +register int (*cmp) (); /* comparison function */ { - register int a, b, c, dir; + register int a, b, c, dir; - a = 0; - b = num - 1; - while (a <= b) - { - c = (a + b) >> 1; /* == ((a + b) / 2) */ - if ((dir = (*cmp) ((base + (c * size)), key))) - { - if (dir > 0) - b = c - 1; - else /* (dir < 0) */ - a = c + 1; - } - else - { - _bsearch = c; - return (base + (c * size)); - } - } - _bsearch = b; - return (NULL); + a = 0; + b = num - 1; + while (a <= b) { + c = (a + b) >> 1; /* == ((a + b) / 2) */ + if ((dir = (*cmp) ((base + (c * size)), key))) { + if (dir > 0) + b = c - 1; + else /* (dir < 0) */ + a = c + 1; + } else { + _bsearch = c; + return (base + (c * size)); + } + } + _bsearch = b; + return (NULL); } diff --git a/libc/stdlib/getenv.c b/libc/stdlib/getenv.c index 1ed83a622..b5d4de9aa 100644 --- a/libc/stdlib/getenv.c +++ b/libc/stdlib/getenv.c @@ -6,26 +6,22 @@ #include <stdlib.h> #include <malloc.h> -extern char ** environ; +extern char **environ; -char * -getenv(var) -const char * var; +char *getenv(var) +const char *var; { - char **p; - int len; + char **p; + int len; - len = strlen(var); - - if (!environ) - return 0; - - for(p=environ; *p; p++) - { - if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' ) - return *p + len + 1; - } - return 0; -} + len = strlen(var); + if (!environ) + return 0; + for (p = environ; *p; p++) { + if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') + return *p + len + 1; + } + return 0; +} diff --git a/libc/stdlib/malloc-simple/.indent.pro b/libc/stdlib/malloc-simple/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/stdlib/malloc-simple/.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/stdlib/malloc-simple/Makefile b/libc/stdlib/malloc-simple/Makefile new file mode 100644 index 000000000..c2f8827b5 --- /dev/null +++ b/libc/stdlib/malloc-simple/Makefile @@ -0,0 +1,46 @@ +# 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.make +LIBC=$(TOPDIR)libc.a + +MSRC=alloc.c +MOBJ=malloc.o realloc.o free.o calloc.o malloc_dbg.o free_dbg.o calloc_dbg.o +OBJS=$(MOBJ) + + +all: $(OBJS) $(LIBC) + +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(MOBJ): $(MSRC) + $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o + +$(OBJ): Makefile + +clean: + rm -f *.[oa] *~ core + diff --git a/libc/stdlib/malloc-simple/alloc.c b/libc/stdlib/malloc-simple/alloc.c new file mode 100644 index 000000000..cf4835c97 --- /dev/null +++ b/libc/stdlib/malloc-simple/alloc.c @@ -0,0 +1,106 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> + + +#ifdef L_calloc_dbg + +void *calloc_dbg(size_t num, size_t size, char *function, char *file, + int line) +{ + void *ptr; + + fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num * size, + function, file, line); + ptr = calloc(num, size); + fprintf(stderr, "%p\n", ptr); + return ptr; +} + +#endif + +#ifdef L_malloc_dbg + +void *malloc_dbg(size_t len, char *function, char *file, int line) +{ + void *result; + + fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, + file, line); + result = malloc(len); + fprintf(stderr, "%p\n", result); + return result; +} + +#endif + +#ifdef L_free_dbg + +void free_dbg(void *ptr, char *function, char *file, int line) +{ + fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, + line); + free(ptr); +} + +#endif + + +#ifdef L_calloc + +void *calloc(size_t num, size_t size) +{ + void *ptr = malloc(num * size); + + if (ptr) + memset(ptr, 0, num * size); + return ptr; +} + +#endif + +#ifdef L_malloc + +void *malloc(size_t len) +{ + void *result = mmap((void *) 0, len, PROT_READ | PROT_WRITE, + //MAP_SHARED | MAP_ANONYMOUS, 0, 0); + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + + if (result == (void *) -1) + return 0; + + return result; +} + +#endif + +#ifdef L_free + +void free(void *ptr) +{ + munmap(ptr, 0); +} + +#endif + +#ifdef L_realloc + +void *realloc(void *ptr, size_t size) +{ + void *newptr = NULL; + + if (size > 0) { + newptr = malloc(size); + if (newptr && ptr) + memcpy(newptr, ptr, size); + } + if (ptr) + free(ptr); + return newptr; +} + +#endif diff --git a/libc/stdlib/malloc/Makefile b/libc/stdlib/malloc/Makefile index ba2567f12..fe3f8b424 100644 --- a/libc/stdlib/malloc/Makefile +++ b/libc/stdlib/malloc/Makefile @@ -27,13 +27,22 @@ LIBC=$(TOPDIR)libc.a CSRC=malloc.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +MSRC=alloc.c +MOBJ=malloc_dbg.o free_dbg.o calloc_dbg.o +OBJS=$(COBJS) $(MOBJ) -all: $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(COBJS) +$(LIBC): ar-target -$(COBJS): Makefile +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(MOBJ): $(MSRC) + $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o + +$(OBJ): Makefile clean: rm -f *.[oa] *~ core + diff --git a/libc/stdlib/malloc/alloc.c b/libc/stdlib/malloc/alloc.c new file mode 100644 index 000000000..b782f6dcf --- /dev/null +++ b/libc/stdlib/malloc/alloc.c @@ -0,0 +1,48 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> + + +#ifdef L_calloc_dbg + +void * +calloc_dbg(size_t num, size_t size, char * function, char * file, int line) +{ + void * ptr; + fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num*size, function, file, line); + ptr = calloc(num,size); + fprintf(stderr, "%p\n", ptr); + return ptr; +} + +#endif + +#ifdef L_malloc_dbg + +void * +malloc_dbg(size_t len, char * function, char * file, int line) +{ + void * result; + fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, file, line); + result = malloc(len); + fprintf(stderr, "%p\n", result); + return result; +} + +#endif + +#ifdef L_free_dbg + +void +free_dbg(void * ptr, char * function, char * file, int line) +{ + fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, line); + free(ptr); +} + +#endif + + diff --git a/libc/stdlib/mkstemp.c b/libc/stdlib/mkstemp.c index de3c682b2..738952815 100644 --- a/libc/stdlib/mkstemp.c +++ b/libc/stdlib/mkstemp.c @@ -4,40 +4,40 @@ #include <fcntl.h> int mkstemp(template) -char * template; +char *template; { int i; - int num __attribute__ ((unused)); /* UNINITIALIZED */ + int num __attribute__ ((unused)); /* UNINITIALIZED */ int n2; int l = strlen(template); - - if (l<6) { + + if (l < 6) { errno = EINVAL; return -1; } - - for(i=l-6;i<l;i++) + + for (i = l - 6; i < l; i++) if (template[i] != 'X') { errno = EINVAL; return -1; } - -again: + + again: n2 = num; - for(i=l-1;i>=l-6;i--) { + for (i = l - 1; i >= l - 6; i--) { template[i] = '0' + n2 % 10; n2 /= 10; } - - i = open(template, O_RDWR|O_EXCL|O_CREAT, 0666); - - if (i==-1) { + + i = open(template, O_RDWR | O_EXCL | O_CREAT, 0666); + + if (i == -1) { if (errno == EEXIST) { num++; goto again; } else return -1; } - + return i; } diff --git a/libc/stdlib/mktemp.c b/libc/stdlib/mktemp.c index bbe589efc..cfdcf0913 100644 --- a/libc/stdlib/mktemp.c +++ b/libc/stdlib/mktemp.c @@ -4,37 +4,37 @@ #include <fcntl.h> #include <sys/stat.h> -char * mktemp(template) -char * template; +char *mktemp(template) +char *template; { int i; - int num __attribute__ ((unused)); /* UNINITIALIZED */ + int num __attribute__ ((unused)); /* UNINITIALIZED */ int n2; int l = strlen(template); struct stat stbuf; - - if (l<6) { + + if (l < 6) { errno = EINVAL; return 0; } - - for(i=l-6;i<l;i++) + + for (i = l - 6; i < l; i++) if (template[i] != 'X') { errno = EINVAL; return 0; } - -again: + + again: n2 = num; - for(i=l-1;i>=l-6;i--) { + for (i = l - 1; i >= l - 6; i--) { template[i] = '0' + n2 % 10; n2 /= 10; } - + if (stat(template, &stbuf) == 0) { num++; goto again; } - + return template; } diff --git a/libc/stdlib/putenv.c b/libc/stdlib/putenv.c index 692aefb5e..5b2ebcb3c 100644 --- a/libc/stdlib/putenv.c +++ b/libc/stdlib/putenv.c @@ -6,57 +6,62 @@ #include <stdlib.h> #include <malloc.h> -extern char ** environ; +extern char **environ; + #define ADD_NUM 4 -int -putenv(var) -const char * var; +int putenv(var) +const char *var; { -static char ** mall_env = 0; -static int extras = 0; - char **p, **d; - char * r; - int len; - - r = strchr(var, '='); - if( r == 0 ) len = strlen(var); - else len = r-var; - - if (!environ) { - environ = (char**)malloc(ADD_NUM * sizeof(char*)); - memset(environ, 0, sizeof(char*)*ADD_NUM); - extras = ADD_NUM; - } - - for(p=environ; *p; p++) - { - if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' ) - { - while( (p[0] = p[1]) ) p++; - extras++; - break; - } - } - if( r == 0 ) return 0; - if( extras <= 0 ) /* Need more space */ - { - d = malloc((p-environ+1+ADD_NUM)*sizeof(char*)); - if( d == 0 ) return -1; - - memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*)); - p = d + (p-environ); - extras=ADD_NUM; - - if( mall_env ) free(mall_env); - environ = d; - mall_env = d; - } - *p++ = strdup((char*)var); - *p = '\0'; - extras--; - - return 0; -} + static char **mall_env = 0; + static int extras = 0; + char **p, **d; + char *r; + int len; + + r = strchr(var, '='); + if (r == 0) + len = strlen(var); + else + len = r - var; + + if (!environ) { + environ = (char **) malloc(ADD_NUM * sizeof(char *)); + memset(environ, 0, sizeof(char *) * ADD_NUM); + + extras = ADD_NUM; + } + for (p = environ; *p; p++) { + if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') { + while ((p[0] = p[1])) + p++; + extras++; + break; + } + } + if (r == 0) + return 0; + if (extras <= 0) { /* Need more space */ + d = malloc((p - environ + 1 + ADD_NUM) * sizeof(char *)); + if (d == 0) + return -1; + + memcpy((void *) d, (void *) environ, + + (p - environ + 1) * sizeof(char *)); + p = d + (p - environ); + extras = ADD_NUM; + + if (mall_env) + free(mall_env); + environ = d; + mall_env = d; + } + *p++ = strdup((char *) var); + *p = '\0'; + extras--; + + return 0; +} diff --git a/libc/stdlib/qsort.c b/libc/stdlib/qsort.c index b45716c83..7cb1d8ab4 100644 --- a/libc/stdlib/qsort.c +++ b/libc/stdlib/qsort.c @@ -9,159 +9,140 @@ */ #include <string.h> -char *_qbuf = 0; /* pointer to storage for qsort() */ +char *_qbuf = 0; /* pointer to storage for qsort() */ #define PIVOT ((i+j)>>1) #define moveitem(dst,src,size) if(dst != src) memcpy(dst, src, size) -static void -_wqsort(base, lo, hi, cmp) +static void _wqsort(base, lo, hi, cmp) register int *base; register int lo; register int hi; register int (*cmp) (); { - int k; - register int i, j, t; - register int *p = &k; + int k; + register int i, j, t; + register int *p = &k; - while (hi > lo) - { - i = lo; - j = hi; - t = PIVOT; - *p = base[t]; - base[t] = base[i]; - base[i] = *p; - while (i < j) - { - while (((*cmp) ((base + j), p)) > 0) - --j; - base[i] = base[j]; - while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) - ++i; - base[j] = base[i]; - } - base[i] = *p; - if ((i - lo) < (hi - i)) - { - _wqsort(base, lo, (i - 1), cmp); - lo = i + 1; - } - else - { - _wqsort(base, (i + 1), hi, cmp); - hi = i - 1; - } - } + while (hi > lo) { + i = lo; + j = hi; + t = PIVOT; + *p = base[t]; + base[t] = base[i]; + base[i] = *p; + while (i < j) { + while (((*cmp) ((base + j), p)) > 0) + --j; + base[i] = base[j]; + while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) + ++i; + base[j] = base[i]; + } + base[i] = *p; + if ((i - lo) < (hi - i)) { + _wqsort(base, lo, (i - 1), cmp); + lo = i + 1; + } else { + _wqsort(base, (i + 1), hi, cmp); + hi = i - 1; + } + } } -static void -_lqsort(base, lo, hi, cmp) +static void _lqsort(base, lo, hi, cmp) register long *base; register int lo; register int hi; register int (*cmp) (); { - long k; - register int i, j, t; - register long *p = &k; + long k; + register int i, j, t; + register long *p = &k; - while (hi > lo) - { - i = lo; - j = hi; - t = PIVOT; - *p = base[t]; - base[t] = base[i]; - base[i] = *p; - while (i < j) - { - while (((*cmp) ((base + j), p)) > 0) - --j; - base[i] = base[j]; - while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) - ++i; - base[j] = base[i]; - } - base[i] = *p; - if ((i - lo) < (hi - i)) - { - _lqsort(base, lo, (i - 1), cmp); - lo = i + 1; - } - else - { - _lqsort(base, (i + 1), hi, cmp); - hi = i - 1; - } - } + while (hi > lo) { + i = lo; + j = hi; + t = PIVOT; + *p = base[t]; + base[t] = base[i]; + base[i] = *p; + while (i < j) { + while (((*cmp) ((base + j), p)) > 0) + --j; + base[i] = base[j]; + while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) + ++i; + base[j] = base[i]; + } + base[i] = *p; + if ((i - lo) < (hi - i)) { + _lqsort(base, lo, (i - 1), cmp); + lo = i + 1; + } else { + _lqsort(base, (i + 1), hi, cmp); + hi = i - 1; + } + } } -static void -_nqsort(base, lo, hi, size, cmp) +static void _nqsort(base, lo, hi, size, cmp) register char *base; register int lo; register int hi; register int size; register int (*cmp) (); { - register int i, j; - register char *p = _qbuf; + register int i, j; + register char *p = _qbuf; - while (hi > lo) - { - i = lo; - j = hi; - p = (base + size * PIVOT); - moveitem(_qbuf, p, size); - moveitem(p, (base + size * i), size); - moveitem((base + size * i), _qbuf, size); - p = _qbuf; - while (i < j) - { - while (((*cmp) ((base + size * j), p)) > 0) - --j; - moveitem((base + size * i), (base + size * j), size); - while ((i < j) && (((*cmp) ((base + size * i), p)) <= 0)) - ++i; - moveitem((base + size * j), (base + size * i), size); - } - moveitem((base + size * i), p, size); - if ((i - lo) < (hi - i)) - { - _nqsort(base, lo, (i - 1), size, cmp); - lo = i + 1; - } - else - { - _nqsort(base, (i + 1), hi, size, cmp); - hi = i - 1; - } - } + while (hi > lo) { + i = lo; + j = hi; + p = (base + size * PIVOT); + moveitem(_qbuf, p, size); + moveitem(p, (base + size * i), size); + moveitem((base + size * i), _qbuf, size); + p = _qbuf; + while (i < j) { + while (((*cmp) ((base + size * j), p)) > 0) + --j; + moveitem((base + size * i), (base + size * j), size); + while ((i < j) && (((*cmp) ((base + size * i), p)) <= 0)) + ++i; + moveitem((base + size * j), (base + size * i), size); + } + moveitem((base + size * i), p, size); + if ((i - lo) < (hi - i)) { + _nqsort(base, lo, (i - 1), size, cmp); + lo = i + 1; + } else { + _nqsort(base, (i + 1), hi, size, cmp); + hi = i - 1; + } + } } extern int qsort(base, num, size, cmp) char *base; -int num; -int size; -int (*cmp) (); +int num; +int size; +int (*cmp) (); { - char _qtemp[128]; + char _qtemp[128]; - if (_qbuf == 0) - { - if (size > sizeof(_qtemp))/* records too large! */ - return 1; - _qbuf = _qtemp; - } - if (size == 2) - _wqsort(base, 0, num - 1, cmp); - else if (size == 4) - _lqsort(base, 0, num - 1, cmp); - else - _nqsort(base, 0, num - 1, size, cmp); - if (_qbuf == _qtemp) - _qbuf = 0; - return 0; + if (_qbuf == 0) { + if (size > sizeof(_qtemp)) /* records too large! */ + return 1; + _qbuf = _qtemp; + } + if (size == 2) + _wqsort(base, 0, num - 1, cmp); + else if (size == 4) + _lqsort(base, 0, num - 1, cmp); + else + _nqsort(base, 0, num - 1, size, cmp); + if (_qbuf == _qtemp) + _qbuf = 0; + return 0; } diff --git a/libc/stdlib/rand.c b/libc/stdlib/rand.c index 4bf98d5bc..b5c5cb764 100644 --- a/libc/stdlib/rand.c +++ b/libc/stdlib/rand.c @@ -12,13 +12,13 @@ static unsigned int sseed = 0; int rand() { - return ( sseed = (((sseed+1L)*75L)%65537L)-1 ) & MAXINT; + return (sseed = (((sseed + 1L) * 75L) % 65537L) - 1) & MAXINT; } void srand(seed) unsigned int seed; { - sseed=seed; + sseed = seed; } #else @@ -32,6 +32,7 @@ unsigned int seed; static int seed1 = 1; static int seed2 = 1; static int seed3 = 1; + #define MAXINT (((unsigned)-1)>>1) #define CRANK(a,b,c,m,s) \ @@ -41,21 +42,22 @@ static int seed3 = 1; int rand() { - register int q; - CRANK(206, 157, 31, 32363, seed1); - CRANK(217, 146, 45, 31727, seed2); - CRANK(222, 142, 133, 31657, seed3); + register int q; + + CRANK(206, 157, 31, 32363, seed1); + CRANK(217, 146, 45, 31727, seed2); + CRANK(222, 142, 133, 31657, seed3); - return seed1^seed2^seed3; + return seed1 ^ seed2 ^ seed3; } void srand(seed) unsigned int seed; { - seed &= MAXINT; - seed1= seed%32362 + 1; - seed2= seed%31726 + 1; - seed3= seed%31656 + 1; + seed &= MAXINT; + seed1 = seed % 32362 + 1; + seed2 = seed % 31726 + 1; + seed3 = seed % 31656 + 1; } #endif diff --git a/libc/stdlib/realpath.c b/libc/stdlib/realpath.c index d053cfcaf..73903371f 100644 --- a/libc/stdlib/realpath.c +++ b/libc/stdlib/realpath.c @@ -22,7 +22,7 @@ #include <stdio.h> #include <string.h> #include <strings.h> -#include <limits.h> /* for PATH_MAX */ +#include <limits.h> /* for PATH_MAX */ #include <sys/param.h> /* for MAXPATHLEN */ #include <errno.h> @@ -43,23 +43,23 @@ #define MAX_READLINKS 32 #ifdef __STDC__ -char *realpath(const char *path, char resolved_path []) +char *realpath(const char *path, char resolved_path[]) #else char *realpath(path, resolved_path) const char *path; -char resolved_path []; +char resolved_path[]; #endif { char copy_path[PATH_MAX]; char link_path[PATH_MAX]; - char got_path [PATH_MAX]; + char got_path[PATH_MAX]; char *new_path = got_path; char *max_path; int readlinks = 0; int n; /* Make a copy of the source path since we may need to modify it. */ - if (strlen(path)>=PATH_MAX-2) { + if (strlen(path) >= PATH_MAX - 2) { errno = ENAMETOOLONG; return NULL; } @@ -78,8 +78,7 @@ char resolved_path []; new_path += strlen(new_path); if (new_path[-1] != '/') *new_path++ = '/'; - } - else { + } else { *new_path++ = '/'; path++; } @@ -103,8 +102,7 @@ char resolved_path []; if (new_path == got_path + 1) continue; /* Handle ".." by backing up. */ - while ((--new_path)[-1] != '/') - ; + while ((--new_path)[-1] != '/'); continue; } } @@ -131,11 +129,10 @@ char resolved_path []; if (errno != EINVAL) { /* Make sure it's null terminated. */ *new_path = '\0'; - strcpy (resolved_path, got_path); + strcpy(resolved_path, got_path); return NULL; } - } - else { + } else { /* Note: readlink doesn't add the null byte. */ link_path[n] = '\0'; if (*link_path == '/') @@ -143,10 +140,9 @@ char resolved_path []; new_path = got_path; else /* Otherwise back up over this component. */ - while (*(--new_path) != '/') - ; + while (*(--new_path) != '/'); /* Safe sex check. */ - if (strlen(path) + n >= PATH_MAX-2) { + if (strlen(path) + n >= PATH_MAX - 2) { errno = ENAMETOOLONG; return NULL; } @@ -155,7 +151,7 @@ char resolved_path []; strcpy(copy_path, link_path); path = copy_path; } -#endif /* S_IFLNK */ +#endif /* S_IFLNK */ *new_path++ = '/'; } /* Delete trailing slash but don't whomp a lone slash. */ @@ -163,6 +159,6 @@ char resolved_path []; new_path--; /* Make sure it's null terminated. */ *new_path = '\0'; - strcpy (resolved_path, got_path); + strcpy(resolved_path, got_path); return resolved_path; } diff --git a/libc/stdlib/setenv.c b/libc/stdlib/setenv.c index afe5676d1..f7d597139 100644 --- a/libc/stdlib/setenv.c +++ b/libc/stdlib/setenv.c @@ -6,69 +6,71 @@ #include <stdlib.h> #include <malloc.h> -extern char ** environ; +extern char **environ; + #define ADD_NUM 4 -int -setenv(var, value, overwrite) -const char * var; -const char * value; +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; - - len = strlen(var); - - if (!environ) { - environ = (char**)malloc(ADD_NUM * sizeof(char*)); - memset(environ, 0, sizeof(char*)*ADD_NUM); - extras = ADD_NUM; - } - - 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; - } - } - - if( extras <= 0 ) /* Need more space */ - { - d = malloc((p-environ+1+ADD_NUM)*sizeof(char*)); - if( d == 0 ) return -1; - - memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*)); - p = d + (p-environ); - extras=ADD_NUM; - - if( mall_env ) free(mall_env); - environ = d; - mall_env = d; - } - - t = malloc(len + 1 + strlen(value) + 1); - if (!t) - return -1; - - strcpy(t, var); - strcat(t, "="); - strcat(t, value); - - *p++ = (char*)t; - *p = '\0'; - extras--; - - return 0; -} + static char **mall_env = 0; + static int extras = 0; + char **p, **d; + char *t; + int len; + + len = strlen(var); + + if (!environ) { + environ = (char **) malloc(ADD_NUM * sizeof(char *)); + memset(environ, 0, sizeof(char *) * ADD_NUM); + + extras = ADD_NUM; + } + + 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; + } + } + + if (extras <= 0) { /* Need more space */ + d = malloc((p - environ + 1 + ADD_NUM) * sizeof(char *)); + if (d == 0) + return -1; + memcpy((void *) d, (void *) environ, + + (p - environ + 1) * sizeof(char *)); + p = d + (p - environ); + extras = ADD_NUM; + + if (mall_env) + free(mall_env); + environ = d; + mall_env = d; + } + + t = malloc(len + 1 + strlen(value) + 1); + if (!t) + return -1; + + strcpy(t, var); + strcat(t, "="); + strcat(t, value); + + *p++ = (char *) t; + *p = '\0'; + extras--; + + return 0; +} diff --git a/libc/stdlib/strtod.c b/libc/stdlib/strtod.c index 0d3bb790a..de3aecbd3 100644 --- a/libc/stdlib/strtod.c +++ b/libc/stdlib/strtod.c @@ -20,77 +20,64 @@ #include <stdlib.h> #include <ctype.h> -float -strtod(const char *nptr, char ** endptr) +float strtod(const char *nptr, char **endptr) { - unsigned short negative; - float number; - float fp_part; - int exponent; - unsigned short exp_negative; + unsigned short negative; + float number; + float fp_part; + int exponent; + unsigned short exp_negative; - /* advance beyond any leading whitespace */ - while (isspace(*nptr)) - nptr++; + /* advance beyond any leading whitespace */ + while (isspace(*nptr)) + nptr++; - /* check for optional '+' or '-' */ - negative=0; - if (*nptr=='-') - { - negative=1; - nptr++; - } - else - if (*nptr=='+') - nptr++; + /* check for optional '+' or '-' */ + negative = 0; + if (*nptr == '-') { + negative = 1; + nptr++; + } else if (*nptr == '+') + nptr++; - number=0; - while (isdigit(*nptr)) - { - number=number*10+(*nptr-'0'); - nptr++; - } - - if (*nptr=='.') - { - nptr++; - fp_part=0; - while (isdigit(*nptr)) - { - fp_part=fp_part/10.0 + (*nptr-'0')/10.0; - nptr++; + number = 0; + while (isdigit(*nptr)) { + number = number * 10 + (*nptr - '0'); + nptr++; } - number+=fp_part; - } - if (*nptr=='e' || *nptr=='E') - { - nptr++; - exp_negative=0; - if (*nptr=='-') - { - exp_negative=1; - nptr++; + if (*nptr == '.') { + nptr++; + fp_part = 0; + while (isdigit(*nptr)) { + fp_part = fp_part / 10.0 + (*nptr - '0') / 10.0; + nptr++; + } + number += fp_part; } - else - if (*nptr=='+') - nptr++; - exponent=0; - while (isdigit(*nptr)) - { - exponent=exponent*10+(*nptr-'0'); - exponent++; + if (*nptr == 'e' || *nptr == 'E') { + nptr++; + exp_negative = 0; + if (*nptr == '-') { + exp_negative = 1; + nptr++; + } else if (*nptr == '+') + nptr++; + + exponent = 0; + while (isdigit(*nptr)) { + exponent = exponent * 10 + (*nptr - '0'); + exponent++; + } } - } - while (exponent) - { - if (exp_negative) - number/=10; - else - number*=10; - exponent--; - } - return (negative ? -number:number); + while (exponent) { + if (exp_negative) + number /= 10; + else + number *= 10; + exponent--; + } + return (negative ? -number : number); } diff --git a/libc/stdlib/system.c b/libc/stdlib/system.c index 6c8a42dee..061dbc914 100644 --- a/libc/stdlib/system.c +++ b/libc/stdlib/system.c @@ -4,46 +4,44 @@ #include <unistd.h> #include <sys/wait.h> -int -system(command) -char * command; +int system(command) +char *command; { - int wait_val, pid; - __sighandler_t save_quit, save_int, save_chld; - - if( command == 0 ) return 1; - - save_quit = signal(SIGQUIT, SIG_IGN); - save_int = signal(SIGINT, SIG_IGN); - save_chld = signal(SIGCHLD, SIG_DFL); - - if( (pid=vfork()) < 0 ) - { - signal(SIGQUIT, save_quit); - signal(SIGINT, save_int); - signal(SIGCHLD, save_chld); - return -1; - } - if( pid == 0 ) - { - signal(SIGQUIT, SIG_DFL); - signal(SIGINT, SIG_DFL); - signal(SIGCHLD, SIG_DFL); - - execl("/bin/sh", "sh", "-c", command, (char*)0); - _exit(127); - } - /* Signals are not absolutly guarenteed with vfork */ - signal(SIGQUIT, SIG_IGN); - signal(SIGINT, SIG_IGN); - - printf("Waiting for child %d\n", pid); - - if (wait4(pid, &wait_val, 0, 0) == -1) - wait_val = -1; - - signal(SIGQUIT, save_quit); - signal(SIGINT, save_int); - signal(SIGCHLD, save_chld); - return wait_val; + int wait_val, pid; + __sighandler_t save_quit, save_int, save_chld; + + if (command == 0) + return 1; + + save_quit = signal(SIGQUIT, SIG_IGN); + save_int = signal(SIGINT, SIG_IGN); + save_chld = signal(SIGCHLD, SIG_DFL); + + if ((pid = vfork()) < 0) { + signal(SIGQUIT, save_quit); + signal(SIGINT, save_int); + signal(SIGCHLD, save_chld); + return -1; + } + if (pid == 0) { + signal(SIGQUIT, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + + execl("/bin/sh", "sh", "-c", command, (char *) 0); + _exit(127); + } + /* Signals are not absolutly guarenteed with vfork */ + signal(SIGQUIT, SIG_IGN); + signal(SIGINT, SIG_IGN); + + printf("Waiting for child %d\n", pid); + + if (wait4(pid, &wait_val, 0, 0) == -1) + wait_val = -1; + + signal(SIGQUIT, save_quit); + signal(SIGINT, save_int); + signal(SIGCHLD, save_chld); + return wait_val; } |