summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/lib
diff options
context:
space:
mode:
Diffstat (limited to 'mdk-stage1/dietlibc/lib')
-rw-r--r--mdk-stage1/dietlibc/lib/__dtostr.c190
-rw-r--r--mdk-stage1/dietlibc/lib/__fstat64.c7
-rw-r--r--mdk-stage1/dietlibc/lib/__getcwd.c1
-rw-r--r--mdk-stage1/dietlibc/lib/__isnan.c7
-rw-r--r--mdk-stage1/dietlibc/lib/__lltostr.c4
-rw-r--r--mdk-stage1/dietlibc/lib/__lstat64.c7
-rw-r--r--mdk-stage1/dietlibc/lib/__ltostr.c5
-rw-r--r--mdk-stage1/dietlibc/lib/__stat64.c7
-rw-r--r--mdk-stage1/dietlibc/lib/__stat64_cvt.c2
-rw-r--r--mdk-stage1/dietlibc/lib/__xmknod.c6
-rw-r--r--mdk-stage1/dietlibc/lib/_brk.c13
-rw-r--r--mdk-stage1/dietlibc/lib/abort.c10
-rw-r--r--mdk-stage1/dietlibc/lib/abs.c10
-rw-r--r--mdk-stage1/dietlibc/lib/accept.c8
-rw-r--r--mdk-stage1/dietlibc/lib/alarm.c47
-rw-r--r--mdk-stage1/dietlibc/lib/alloc.c372
-rw-r--r--mdk-stage1/dietlibc/lib/assert_fail.c11
-rw-r--r--mdk-stage1/dietlibc/lib/atexit.c32
-rw-r--r--mdk-stage1/dietlibc/lib/atoi.c21
-rw-r--r--mdk-stage1/dietlibc/lib/atol.c25
-rw-r--r--mdk-stage1/dietlibc/lib/bind.c11
-rw-r--r--mdk-stage1/dietlibc/lib/bsearch.c30
-rw-r--r--mdk-stage1/dietlibc/lib/cfmakeraw.c3
-rw-r--r--mdk-stage1/dietlibc/lib/closedir.c3
-rw-r--r--mdk-stage1/dietlibc/lib/connect.c7
-rw-r--r--mdk-stage1/dietlibc/lib/creat.c4
-rw-r--r--mdk-stage1/dietlibc/lib/errlist.c132
-rw-r--r--mdk-stage1/dietlibc/lib/errno_location.c2
-rw-r--r--mdk-stage1/dietlibc/lib/exec_lib.c7
-rw-r--r--mdk-stage1/dietlibc/lib/execl.c3
-rw-r--r--mdk-stage1/dietlibc/lib/execlp.c6
-rw-r--r--mdk-stage1/dietlibc/lib/execv.c4
-rw-r--r--mdk-stage1/dietlibc/lib/execvp.c23
-rw-r--r--mdk-stage1/dietlibc/lib/ftw.c77
-rw-r--r--mdk-stage1/dietlibc/lib/getdomainname.c3
-rw-r--r--mdk-stage1/dietlibc/lib/getenv.c3
-rw-r--r--mdk-stage1/dietlibc/lib/gethostname.c3
-rw-r--r--mdk-stage1/dietlibc/lib/getopt.c155
-rw-r--r--mdk-stage1/dietlibc/lib/getpeername.c13
-rw-r--r--mdk-stage1/dietlibc/lib/getservent.c137
-rw-r--r--mdk-stage1/dietlibc/lib/getsockname.c11
-rw-r--r--mdk-stage1/dietlibc/lib/getsockopt.c11
-rw-r--r--mdk-stage1/dietlibc/lib/htonl.c5
-rw-r--r--mdk-stage1/dietlibc/lib/htons.c5
-rw-r--r--mdk-stage1/dietlibc/lib/if_indextoname.c3
-rw-r--r--mdk-stage1/dietlibc/lib/if_nametoindex.c5
-rw-r--r--mdk-stage1/dietlibc/lib/isalnum.c8
-rw-r--r--mdk-stage1/dietlibc/lib/isalpha.c7
-rw-r--r--mdk-stage1/dietlibc/lib/isascii.c6
-rw-r--r--mdk-stage1/dietlibc/lib/isatty.c8
-rw-r--r--mdk-stage1/dietlibc/lib/isblank.c4
-rw-r--r--mdk-stage1/dietlibc/lib/iscntrl.c7
-rw-r--r--mdk-stage1/dietlibc/lib/isdigit.c7
-rw-r--r--mdk-stage1/dietlibc/lib/isgraph.c7
-rw-r--r--mdk-stage1/dietlibc/lib/islower.c7
-rw-r--r--mdk-stage1/dietlibc/lib/isprint.c7
-rw-r--r--mdk-stage1/dietlibc/lib/ispunct.c8
-rw-r--r--mdk-stage1/dietlibc/lib/isspace.c7
-rw-r--r--mdk-stage1/dietlibc/lib/isupper.c8
-rw-r--r--mdk-stage1/dietlibc/lib/isxdigit.c9
-rw-r--r--mdk-stage1/dietlibc/lib/listen.c11
-rw-r--r--mdk-stage1/dietlibc/lib/lockf.c1
-rw-r--r--mdk-stage1/dietlibc/lib/longjmp.c57
-rw-r--r--mdk-stage1/dietlibc/lib/lseek64.c10
-rw-r--r--mdk-stage1/dietlibc/lib/memccmp.c17
-rw-r--r--mdk-stage1/dietlibc/lib/memccpy.c1
-rw-r--r--mdk-stage1/dietlibc/lib/memchr.c12
-rw-r--r--mdk-stage1/dietlibc/lib/memcmp.c2
-rw-r--r--mdk-stage1/dietlibc/lib/memcpy.c2
-rw-r--r--mdk-stage1/dietlibc/lib/memmove.c1
-rw-r--r--mdk-stage1/dietlibc/lib/memset.c4
-rw-r--r--mdk-stage1/dietlibc/lib/mkfifo.c4
-rw-r--r--mdk-stage1/dietlibc/lib/msgctl.c3
-rw-r--r--mdk-stage1/dietlibc/lib/msgget.c3
-rw-r--r--mdk-stage1/dietlibc/lib/msgrcv.c8
-rw-r--r--mdk-stage1/dietlibc/lib/msgsnd.c3
-rw-r--r--mdk-stage1/dietlibc/lib/nop.c7
-rw-r--r--mdk-stage1/dietlibc/lib/open64.c9
-rw-r--r--mdk-stage1/dietlibc/lib/opendir.c26
-rw-r--r--mdk-stage1/dietlibc/lib/perror.c32
-rw-r--r--mdk-stage1/dietlibc/lib/pread.c1
-rw-r--r--mdk-stage1/dietlibc/lib/pread64.c3
-rw-r--r--mdk-stage1/dietlibc/lib/putenv.c19
-rw-r--r--mdk-stage1/dietlibc/lib/puts.c12
-rw-r--r--mdk-stage1/dietlibc/lib/qsort.c22
-rw-r--r--mdk-stage1/dietlibc/lib/raise.c3
-rw-r--r--mdk-stage1/dietlibc/lib/rand.c7
-rw-r--r--mdk-stage1/dietlibc/lib/random.c9
-rw-r--r--mdk-stage1/dietlibc/lib/readdir.c2
-rw-r--r--mdk-stage1/dietlibc/lib/reboot.c6
-rw-r--r--mdk-stage1/dietlibc/lib/recv.c8
-rw-r--r--mdk-stage1/dietlibc/lib/recvfrom.c8
-rw-r--r--mdk-stage1/dietlibc/lib/recvmsg.c8
-rw-r--r--mdk-stage1/dietlibc/lib/remove.c2
-rw-r--r--mdk-stage1/dietlibc/lib/sbrk.c11
-rw-r--r--mdk-stage1/dietlibc/lib/semctl.c13
-rw-r--r--mdk-stage1/dietlibc/lib/semget.c3
-rw-r--r--mdk-stage1/dietlibc/lib/semop.c3
-rw-r--r--mdk-stage1/dietlibc/lib/send.c8
-rw-r--r--mdk-stage1/dietlibc/lib/sendmsg.c8
-rw-r--r--mdk-stage1/dietlibc/lib/sendto.c10
-rw-r--r--mdk-stage1/dietlibc/lib/set_errno.c9
-rw-r--r--mdk-stage1/dietlibc/lib/setsockopt.c11
-rw-r--r--mdk-stage1/dietlibc/lib/shmat.c5
-rw-r--r--mdk-stage1/dietlibc/lib/shmctl.c3
-rw-r--r--mdk-stage1/dietlibc/lib/shmdt.c3
-rw-r--r--mdk-stage1/dietlibc/lib/shmget.c3
-rw-r--r--mdk-stage1/dietlibc/lib/shutdown.c8
-rw-r--r--mdk-stage1/dietlibc/lib/sigaddset.c28
-rw-r--r--mdk-stage1/dietlibc/lib/sigdelset.c28
-rw-r--r--mdk-stage1/dietlibc/lib/sigemptyset.c12
-rw-r--r--mdk-stage1/dietlibc/lib/sigfillset.c16
-rw-r--r--mdk-stage1/dietlibc/lib/sigismember.c29
-rw-r--r--mdk-stage1/dietlibc/lib/sigjmp.c35
-rw-r--r--mdk-stage1/dietlibc/lib/sleep.c2
-rw-r--r--mdk-stage1/dietlibc/lib/snprintf.c3
-rw-r--r--mdk-stage1/dietlibc/lib/socket.c11
-rw-r--r--mdk-stage1/dietlibc/lib/speed.c67
-rw-r--r--mdk-stage1/dietlibc/lib/sprintf.c10
-rw-r--r--mdk-stage1/dietlibc/lib/sscanf.c3
-rw-r--r--mdk-stage1/dietlibc/lib/strcasecmp.c29
-rw-r--r--mdk-stage1/dietlibc/lib/strchr.c1
-rw-r--r--mdk-stage1/dietlibc/lib/strcmp.c2
-rw-r--r--mdk-stage1/dietlibc/lib/strcpy.c7
-rw-r--r--mdk-stage1/dietlibc/lib/strerror.c22
-rw-r--r--mdk-stage1/dietlibc/lib/strlen.c2
-rw-r--r--mdk-stage1/dietlibc/lib/strncasecmp.c34
-rw-r--r--mdk-stage1/dietlibc/lib/strncat.c7
-rw-r--r--mdk-stage1/dietlibc/lib/strncmp.c13
-rw-r--r--mdk-stage1/dietlibc/lib/strncpy.c9
-rw-r--r--mdk-stage1/dietlibc/lib/strsep.c4
-rw-r--r--mdk-stage1/dietlibc/lib/strstr.c8
-rw-r--r--mdk-stage1/dietlibc/lib/strtod.c104
-rw-r--r--mdk-stage1/dietlibc/lib/strtok.c2
-rw-r--r--mdk-stage1/dietlibc/lib/strtol.c17
-rw-r--r--mdk-stage1/dietlibc/lib/strtoll.c5
-rw-r--r--mdk-stage1/dietlibc/lib/strtoul.c32
-rw-r--r--mdk-stage1/dietlibc/lib/strtoull.c1
-rw-r--r--mdk-stage1/dietlibc/lib/sys_siglist.c209
-rw-r--r--mdk-stage1/dietlibc/lib/tcdrain.c1
-rw-r--r--mdk-stage1/dietlibc/lib/tcflush.c24
-rw-r--r--mdk-stage1/dietlibc/lib/tcgetpgrp.c7
-rw-r--r--mdk-stage1/dietlibc/lib/tcsetattr.c46
-rw-r--r--mdk-stage1/dietlibc/lib/tcsetpgrp.c1
-rw-r--r--mdk-stage1/dietlibc/lib/tolower.c6
-rw-r--r--mdk-stage1/dietlibc/lib/toupper.c6
-rw-r--r--mdk-stage1/dietlibc/lib/ttyname.c21
-rw-r--r--mdk-stage1/dietlibc/lib/usleep.c6
-rw-r--r--mdk-stage1/dietlibc/lib/vfork.c6
-rw-r--r--mdk-stage1/dietlibc/lib/vfprintf.c19
-rw-r--r--mdk-stage1/dietlibc/lib/vprintf.c14
-rw-r--r--mdk-stage1/dietlibc/lib/vsnprintf.c255
-rw-r--r--mdk-stage1/dietlibc/lib/vsprintf.c10
-rw-r--r--mdk-stage1/dietlibc/lib/vsscanf.c311
154 files changed, 1349 insertions, 2108 deletions
diff --git a/mdk-stage1/dietlibc/lib/__dtostr.c b/mdk-stage1/dietlibc/lib/__dtostr.c
index e9a8a83d3..580258936 100644
--- a/mdk-stage1/dietlibc/lib/__dtostr.c
+++ b/mdk-stage1/dietlibc/lib/__dtostr.c
@@ -1,104 +1,140 @@
-#include <stdio.h>
+#include <stdlib.h>
+#include <endian.h>
+#include <math.h>
/* convert double to string. Helper for sprintf. */
-int __dtostr(double d,char *buf,int maxlen,int prec) {
+static int copystring(char* buf,int maxlen, const char* s) {
+ int i;
+ for (i=0; i<3&&i<maxlen; ++i)
+ buf[i]=s[i];
+ if (i<maxlen) { buf[i]=0; ++i; }
+ return i;
+}
+
+int __dtostr(double d,char *buf,unsigned int maxlen,unsigned int prec,unsigned int prec2) {
+#if 1
unsigned long long *x=(unsigned long long *)&d;
/* step 1: extract sign, mantissa and exponent */
- signed int s=*x>>63;
signed long e=((*x>>52)&((1<<11)-1))-1023;
+#else
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+ signed long e=(((((unsigned long*)&d)[1])>>20)&((1<<11)-1))-1023;
+#else
+ signed long e=(((*((unsigned long*)&d))>>20)&((1<<11)-1))-1023;
+#endif
+#endif
/* unsigned long long m=*x & ((1ull<<52)-1); */
/* step 2: exponent is base 2, compute exponent for base 10 */
- signed long e10=1+(long)(e*0.30102999566398119802); /* log10(2) */
+ signed long e10;
/* step 3: calculate 10^e10 */
- int i;
- double tmp=10.0;
+ unsigned int i;
+ double backup=d;
+ double tmp;
char *oldbuf=buf;
- int initial=1;
+ if (isnan(d)) return copystring(buf,maxlen,"nan");
+ if ((i=isinf(d))) return copystring(buf,maxlen,i>0?"inf":"-inf");
+ e10=1+(long)(e*0.30102999566398119802); /* log10(2) */
+ /* Wir iterieren von Links bis wir bei 0 sind oder maxlen erreicht
+ * ist. Wenn maxlen erreicht ist, machen wir das nochmal in
+ * scientific notation. Wenn dann von prec noch was übrig ist, geben
+ * wir einen Dezimalpunkt aus und geben prec2 Nachkommastellen aus.
+ * Wenn prec2 Null ist, geben wir so viel Stellen aus, wie von prec
+ * noch übrig ist. */
if (d==0.0) {
- *buf='0'; ++buf;
- goto done;
+ prec2=prec2==0?1:prec2+2;
+ prec2=prec2>maxlen?8:prec2;
+ for (i=0; i<prec2; ++i) buf[i]='0';
+ buf[1]='.'; buf[i]=0;
+ return i;
}
- if (s) { d=-d; *buf='-'; --maxlen; buf++; }
+
+ if (d < 0.0) { d=-d; *buf='-'; --maxlen; ++buf; }
+
+ /*
+ Perform rounding. It needs to be done before we generate any
+ digits as the carry could propagate through the whole number.
+ */
+
+ tmp = 0.5;
+ for (i = 0; i < prec2; i++) { tmp *= 0.1; }
+ d += tmp;
+
+ if (d < 1.0) { *buf='0'; --maxlen; ++buf; }
/* printf("e=%d e10=%d prec=%d\n",e,e10,prec); */
- if (e10>=0) {
+ if (e10>0) {
+ int first=1; /* are we about to write the first digit? */
+ tmp = 10.0;
i=e10;
while (i>10) { tmp=tmp*1e10; i-=10; }
while (i>1) { tmp=tmp*10; --i; }
- } else {
- i=(e10=-e10);
- while (i>10) { tmp=tmp*1e-10; i-=10; }
- while (i>1) { tmp=tmp/10; --i; }
- }
- while (d/tmp<1) {
- --e10;
- tmp/=10.0;
- }
- /* step 4: see if precision is sufficient to display all digits */
- if (e10>prec) {
- /* use scientific notation */
- int len=__dtostr(d/tmp,buf,maxlen,prec);
- if (len==0) return 0;
- maxlen-=len; buf+=len;
- if (--maxlen>=0) {
- *buf='e';
- ++buf;
- }
- for (len=1000; len>0; len/=10) {
- if (e10>=len || !initial) {
- if (--maxlen>=0) {
- *buf=(e10/len)+'0';
- ++buf;
+ /* the number is greater than 1. Iterate through digits before the
+ * decimal point until we reach the decimal point or maxlen is
+ * reached (in which case we switch to scientific notation). */
+ while (tmp>0.9) {
+ char digit;
+ double fraction=d/tmp;
+ digit=(int)(fraction); /* floor() */
+ if (!first || digit) {
+ first=0;
+ *buf=digit+'0'; ++buf;
+ if (!maxlen) {
+ /* use scientific notation */
+ int len=__dtostr(backup/tmp,oldbuf,maxlen,prec,prec2);
+ int initial=1;
+ if (len==0) return 0;
+ maxlen-=len; buf+=len;
+ if (maxlen>0) {
+ *buf='e';
+ ++buf;
+ }
+ --maxlen;
+ for (len=1000; len>0; len/=10) {
+ if (e10>=len || !initial) {
+ if (maxlen>0) {
+ *buf=(e10/len)+'0';
+ ++buf;
+ }
+ --maxlen;
+ initial=0;
+ e10=e10%len;
+ }
+ }
+ if (maxlen>0) goto fini;
+ return 0;
}
- initial=0;
- e10=e10%len;
+ d-=digit*tmp;
+ --maxlen;
}
+ tmp/=10.0;
}
- if (maxlen>=0) return buf-oldbuf;
- return 0;
}
- /* step 5: loop through the digits, inserting the decimal point when
- * appropriate */
- if (d<1.0) {
- double x=1.0;
- int first=1;
- do {
- if (--maxlen<0) return buf-oldbuf;
- *buf='0'; ++buf;
- if (first) {
- first=0;
- *buf='.'; ++buf;
- if (--maxlen<0) return buf-oldbuf;
- }
- x/=10.0;
- } while (x>d);
+ else
+ {
+ tmp = 0.1;
}
- for (; prec>0; ) {
- double tmp2=d/tmp;
- char c;
- d-=((int)tmp2*tmp);
- c=((int)tmp2);
- if ((!initial)||c) {
- if (--maxlen>=0) {
- initial=0;
- *buf=c+'0';
- ++buf;
- } else
- return 0;
+
+ if (buf==oldbuf) {
+ if (!maxlen) return 0; --maxlen;
+ *buf='0'; ++buf;
+ }
+ if (prec2 || prec>(unsigned int)(buf-oldbuf)+1) { /* more digits wanted */
+ if (!maxlen) return 0; --maxlen;
+ *buf='.'; ++buf;
+ prec-=buf-oldbuf-1;
+ if (prec2) prec=prec2;
+ if (prec>maxlen) return 0;
+ while (prec>0) {
+ char digit;
+ double fraction=d/tmp;
+ digit=(int)(fraction); /* floor() */
+ *buf=digit+'0'; ++buf;
+ d-=digit*tmp;
+ tmp/=10.0;
--prec;
}
- if (tmp>0.5 && tmp<1.5) {
- tmp=1e-1;
- initial=0;
- if (--maxlen>=0) {
- *buf='.';
- ++buf;
- } else
- return 0;
- } else
- tmp/=10.0;
}
-done:
+fini:
*buf=0;
return buf-oldbuf;
}
diff --git a/mdk-stage1/dietlibc/lib/__fstat64.c b/mdk-stage1/dietlibc/lib/__fstat64.c
index 6f9fcb2c5..f2f9858ea 100644
--- a/mdk-stage1/dietlibc/lib/__fstat64.c
+++ b/mdk-stage1/dietlibc/lib/__fstat64.c
@@ -1,9 +1,8 @@
+#include <errno.h>
#include "dietfeatures.h"
-
#ifdef WANT_LARGEFILE_BACKCOMPAT
#include <sys/stat.h>
#ifndef __NO_STAT64
-#include <errno.h>
extern int __dietlibc_fstat64(int __fd, struct stat64 *__buf);
extern void __stat64_cvt(const struct stat *src,struct stat64 *dest);
@@ -11,11 +10,7 @@ extern void __stat64_cvt(const struct stat *src,struct stat64 *dest);
int fstat64(int __fd, struct stat64 *__buf) {
if (__dietlibc_fstat64(__fd,__buf)) {
struct stat temp;
-#ifdef WANT_THREAD_SAFE
- if (*__errno_location()!=ENOSYS) return -1;
-#else
if (errno!=ENOSYS) return -1;
-#endif
if (fstat(__fd,&temp)) return -1;
__stat64_cvt(&temp,__buf);
}
diff --git a/mdk-stage1/dietlibc/lib/__getcwd.c b/mdk-stage1/dietlibc/lib/__getcwd.c
index e97fc4520..8616704ec 100644
--- a/mdk-stage1/dietlibc/lib/__getcwd.c
+++ b/mdk-stage1/dietlibc/lib/__getcwd.c
@@ -1,3 +1,4 @@
+#include <unistd.h>
#include <stdlib.h>
extern int __syscall_getcwd(char* buf, size_t size);
diff --git a/mdk-stage1/dietlibc/lib/__isnan.c b/mdk-stage1/dietlibc/lib/__isnan.c
index 9d21e9824..de74b8a9f 100644
--- a/mdk-stage1/dietlibc/lib/__isnan.c
+++ b/mdk-stage1/dietlibc/lib/__isnan.c
@@ -1,9 +1,10 @@
-#include <stdio.h>
+#include <math.h>
-int __isnan(double d) {
+int isnan(double d) {
unsigned long long *x=(unsigned long long *)&d;
- return (*x==0x7FF8002000000000ll || *x==0x7FF0002000000000);
+ return (*x==0x7FF8000000000000ll || *x==0x7FF0000000000000 || *x==0xfff8000000000000);
}
+int __isnan(double d) __attribute__((alias("isnan")));
#if 0
TestFromIeeeExtended("7FFF0000000000000000"); /* +infinity */
diff --git a/mdk-stage1/dietlibc/lib/__lltostr.c b/mdk-stage1/dietlibc/lib/__lltostr.c
index eb4a27cef..fe4701f29 100644
--- a/mdk-stage1/dietlibc/lib/__lltostr.c
+++ b/mdk-stage1/dietlibc/lib/__lltostr.c
@@ -1,9 +1,11 @@
#include <string.h>
+int __lltostr(char *s, int size, unsigned long long i, int base, char UpCase);
+
int __lltostr(char *s, int size, unsigned long long i, int base, char UpCase)
{
char *tmp;
- int j=0;
+ unsigned int j=0;
s[--size]=0;
diff --git a/mdk-stage1/dietlibc/lib/__lstat64.c b/mdk-stage1/dietlibc/lib/__lstat64.c
index a871f3917..0eab6a94b 100644
--- a/mdk-stage1/dietlibc/lib/__lstat64.c
+++ b/mdk-stage1/dietlibc/lib/__lstat64.c
@@ -1,9 +1,8 @@
+#include <errno.h>
#include "dietfeatures.h"
-
#ifdef WANT_LARGEFILE_BACKCOMPAT
#include <sys/stat.h>
#ifndef __NO_STAT64
-#include <errno.h>
extern int __dietlibc_lstat64(const char *__file, struct stat64 *__buf);
extern void __stat64_cvt(const struct stat *src,struct stat64 *dest);
@@ -11,11 +10,7 @@ extern void __stat64_cvt(const struct stat *src,struct stat64 *dest);
int lstat64(const char *__file, struct stat64 *__buf) {
if (__dietlibc_lstat64(__file,__buf)) {
struct stat temp;
-#ifdef WANT_THREAD_SAFE
- if (*__errno_location()!=ENOSYS) return -1;
-#else
if (errno!=ENOSYS) return -1;
-#endif
if (lstat(__file,&temp)) return -1;
__stat64_cvt(&temp,__buf);
}
diff --git a/mdk-stage1/dietlibc/lib/__ltostr.c b/mdk-stage1/dietlibc/lib/__ltostr.c
index 57e0690d2..fa71be9ab 100644
--- a/mdk-stage1/dietlibc/lib/__ltostr.c
+++ b/mdk-stage1/dietlibc/lib/__ltostr.c
@@ -1,9 +1,10 @@
#include <string.h>
+#include <stdlib.h>
-int __ltostr(char *s, int size, unsigned long i, int base, char UpCase)
+int __ltostr(char *s, unsigned int size, unsigned long i, unsigned int base, int UpCase)
{
char *tmp;
- int j=0;
+ unsigned int j=0;
s[--size]=0;
diff --git a/mdk-stage1/dietlibc/lib/__stat64.c b/mdk-stage1/dietlibc/lib/__stat64.c
index dfb0d4315..fcbdfef2d 100644
--- a/mdk-stage1/dietlibc/lib/__stat64.c
+++ b/mdk-stage1/dietlibc/lib/__stat64.c
@@ -1,9 +1,8 @@
+#include <errno.h>
#include "dietfeatures.h"
-
#ifdef WANT_LARGEFILE_BACKCOMPAT
#include <sys/stat.h>
#ifndef __NO_STAT64
-#include <errno.h>
extern int __dietlibc_stat64(const char *__file, struct stat64 *__buf);
extern void __stat64_cvt(const struct stat *src,struct stat64 *dest);
@@ -11,11 +10,7 @@ extern void __stat64_cvt(const struct stat *src,struct stat64 *dest);
int stat64(const char *__file, struct stat64 *__buf) {
if (__dietlibc_stat64(__file,__buf)) {
struct stat temp;
-#ifdef WANT_THREAD_SAFE
- if (*__errno_location()!=ENOSYS) return -1;
-#else
if (errno!=ENOSYS) return -1;
-#endif
if (stat(__file,&temp)) return -1;
__stat64_cvt(&temp,__buf);
}
diff --git a/mdk-stage1/dietlibc/lib/__stat64_cvt.c b/mdk-stage1/dietlibc/lib/__stat64_cvt.c
index dced352a4..c4ce8bea4 100644
--- a/mdk-stage1/dietlibc/lib/__stat64_cvt.c
+++ b/mdk-stage1/dietlibc/lib/__stat64_cvt.c
@@ -1,6 +1,8 @@
#include <sys/stat.h>
#ifndef __NO_STAT64
+void __stat64_cvt(const struct stat *src,struct stat64 *dest);
+
void __stat64_cvt(const struct stat *src,struct stat64 *dest) {
dest->st_dev=src->st_dev;
dest->st_ino=src->st_ino;
diff --git a/mdk-stage1/dietlibc/lib/__xmknod.c b/mdk-stage1/dietlibc/lib/__xmknod.c
deleted file mode 100644
index 4739a1f5f..000000000
--- a/mdk-stage1/dietlibc/lib/__xmknod.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <sys/stat.h>
-
-int __xmknod(int ver,char* filename,mode_t mode,dev_t *dev) {
- return mknod(filename,mode,*dev);
-}
-
diff --git a/mdk-stage1/dietlibc/lib/_brk.c b/mdk-stage1/dietlibc/lib/_brk.c
index 6d0b29c03..e73d7b621 100644
--- a/mdk-stage1/dietlibc/lib/_brk.c
+++ b/mdk-stage1/dietlibc/lib/_brk.c
@@ -1,14 +1,13 @@
#include <unistd.h>
-extern int __diet_brk(void *end_data_segment);
+extern void* __diet_brk(void *end_data_segment);
void* __curbrk=0;
-int __brk(void *end_data_segment) {
- int res;
- if ((res=__diet_brk(end_data_segment))==0)
- __curbrk=end_data_segment;
- return res;
+int __libc_brk(void *end_data_segment);
+
+int __libc_brk(void *end_data_segment) {
+ return ((__curbrk=__diet_brk(end_data_segment))==(void*)-1?-1:0);
}
-int brk (void *end_data_segment) __attribute__((weak,alias("__brk")));
+int brk(void *end_data_segment) __attribute__((weak,alias("__libc_brk")));
diff --git a/mdk-stage1/dietlibc/lib/abort.c b/mdk-stage1/dietlibc/lib/abort.c
index 55bfb8f0f..7c49a32ef 100644
--- a/mdk-stage1/dietlibc/lib/abort.c
+++ b/mdk-stage1/dietlibc/lib/abort.c
@@ -1,10 +1,16 @@
-#include <linux/types.h>
-#include <linux/signal.h>
+#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
+#include <stdio.h>
+
+#ifndef __PIC__
+void __stdio_flushall(void) __attribute__((weak));
+void __stdio_flushall(void) { }
+#endif
void abort() {
sigset_t t;
+ __stdio_flushall();
if (!sigemptyset(&t) && !sigaddset(&t, SIGABRT))
sigprocmask(SIG_UNBLOCK, &t, 0);
while (1)
diff --git a/mdk-stage1/dietlibc/lib/abs.c b/mdk-stage1/dietlibc/lib/abs.c
index 9d7055358..d3b75f202 100644
--- a/mdk-stage1/dietlibc/lib/abs.c
+++ b/mdk-stage1/dietlibc/lib/abs.c
@@ -1,6 +1,6 @@
-#include <stdlib.h>
+#include <endian.h>
-int abs(int j)
-{
- return (j < 0) ? -j : j;
-}
+int abs(int i) { return i>=0?i:-i; }
+#if __WORDSIZE == 32
+long labs(long i) __attribute__((alias("abs")));
+#endif
diff --git a/mdk-stage1/dietlibc/lib/accept.c b/mdk-stage1/dietlibc/lib/accept.c
index c7e8254ca..08359b894 100644
--- a/mdk-stage1/dietlibc/lib/accept.c
+++ b/mdk-stage1/dietlibc/lib/accept.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_accept(int a, void * addr, void * addr2);
+
int __libc_accept(int a, void * addr, void * addr2) {
-#ifdef __i386__
- return socketcall(SYS_ACCEPT, (long*)&a);
-#else
unsigned long args[] = { a, (long) addr, (long) addr2 };
return socketcall(SYS_ACCEPT, args);
-#endif
}
int accept(int a, void * addr, void * addr2) __attribute__((weak,alias("__libc_accept")));
diff --git a/mdk-stage1/dietlibc/lib/alarm.c b/mdk-stage1/dietlibc/lib/alarm.c
deleted file mode 100644
index fd35372f2..000000000
--- a/mdk-stage1/dietlibc/lib/alarm.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/* Copyright (C) 1991, 1992, 1994, 1997 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
-
- 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.
-
- 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.
-
- 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., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA. */
-
-#include <unistd.h>
-#include <sys/time.h>
-
-/* Schedule an alarm. In SECONDS seconds, the process will get a SIGALRM.
- If SECONDS is zero, any currently scheduled alarm will be cancelled.
- The function returns the number of seconds remaining until the last
- alarm scheduled would have signaled, or zero if there wasn't one.
- There is no return value to indicate an error, but you can set `errno'
- to 0 and check its value after calling `alarm', and this might tell you.
- The signal may come late due to processor scheduling. */
-unsigned int
-alarm (seconds)
- unsigned int seconds;
-{
- struct itimerval old, new;
- unsigned int retval;
-
- new.it_interval.tv_usec = 0;
- new.it_interval.tv_sec = 0;
- new.it_value.tv_usec = 0;
- new.it_value.tv_sec = (long int) seconds;
- if (setitimer (ITIMER_REAL, &new, &old) < 0)
- return 0;
-
- retval = old.it_value.tv_sec;
- if (old.it_value.tv_usec)
- ++retval;
- return retval;
-}
diff --git a/mdk-stage1/dietlibc/lib/alloc.c b/mdk-stage1/dietlibc/lib/alloc.c
index 8cb87a69b..a2b3b73a1 100644
--- a/mdk-stage1/dietlibc/lib/alloc.c
+++ b/mdk-stage1/dietlibc/lib/alloc.c
@@ -1,23 +1,27 @@
/*
* malloc/free by O.Dreesen
+ *
+ * first TRY:
+ * lists w/magics
+ * and now the second TRY
+ * let the kernel map all the stuff (if there is something to do)
*/
-#include <linux/unistd.h>
-#include <asm/mman.h>
-#include <linux/errno.h>
-#include "dietfeatures.h"
-
-#if 0
+#include <unistd.h>
#include <sys/mman.h>
-#define _LIBC
#include <errno.h>
-#endif
+#include "dietfeatures.h"
-#include <linux/types.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/shm.h> /* for PAGE_SIZE */
-#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
-#define MAP_ANON MAP_ANONYMOUS
-#endif
+
+/* -- HELPER CODE --------------------------------------------------------- */
#ifndef MAP_FAILED
#define MAP_FAILED ((void*)-1)
@@ -27,217 +31,207 @@
#define NULL ((void*)0)
#endif
-extern void * mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
-extern void *memset(void *s, int c, size_t n);
-extern void *memcpy(void *dest, const void *src, size_t n);
+typedef union {
+ void* next;
+ size_t size;
+} __alloc_t;
-typedef struct t_alloc_head {
- unsigned int magic1;
- struct t_alloc_head *ptr;
- unsigned long size;
- unsigned int magic2;
-} alloc_head;
+#define BLOCK_START(b) (((void*)(b))-sizeof(__alloc_t))
+#define BLOCK_RET(b) (((void*)(b))+sizeof(__alloc_t))
-/* guess what ? the virtual block size */
-#define MEM_BLOCK_SIZE 4096
+#define MEM_BLOCK_SIZE PAGE_SIZE
+#define PAGE_ALIGN(s) (((s)+MEM_BLOCK_SIZE-1)&(unsigned long)(~(MEM_BLOCK_SIZE-1)))
-/* minimum allocated bytes */
-#define MEM_ALLOC_MIN 4
+/* a simple mmap :) */
-/* Initial start position in memory */
-#define MEM_ALLOC_START ((char*)0x18000000)
+#ifdef __i386__
+/* regparm exists only on i386 */
+static void *do_mmap(size_t size) __attribute__((regparm(1)));
+static size_t get_index(size_t _size) __attribute__((regparm(1)));
+static void* __small_malloc(size_t _size) __attribute__((regparm(1)));
+#endif
-/* Make every block align */
-#define MEM_ALIGN(s) (((s)+MEM_ALLOC_MIN-1)&(~(MEM_ALLOC_MIN-1)))
-#define PAGE_ALIGN(s) (((s)+MEM_BLOCK_SIZE-1)&(~(MEM_BLOCK_SIZE-1)))
-#define PAGE_ALIGNP(p) ((char*)PAGE_ALIGN((size_t)(p)))
+static void *do_mmap(size_t size) {
+ return mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, (size_t)0);
+}
-#define END_OF_BLOCK(p) ((alloc_head*)(((char*)(p))+((p)->size)))
-#define START_BLOCK(p) ((alloc_head*)(((char*)(p))-sizeof(alloc_head)))
-#define START_DATA(p) (((char*)(p))+sizeof(alloc_head))
-#define MIN_ALLOC(s) (((((s)+sizeof(alloc_head)-1)/MEM_ALLOC_MIN)+1)*MEM_ALLOC_MIN)
+/* -- SMALL MEM ----------------------------------------------------------- */
-#define ALLOC_MAGIC1 0xbad2f7ee
-#define ALLOC_MAGIC2 0xf7ee2bad
+static __alloc_t* __small_mem[8];
-/* freelist handler */
-static alloc_head base = {ALLOC_MAGIC1,&base,0,ALLOC_MAGIC2};
-static char *alloc_get_end = MEM_ALLOC_START;
+#define __SMALL_NR(i) (MEM_BLOCK_SIZE/(i))
-void __libc_free(void *ptr)
-{
- alloc_head *prev,*p,*block;
+#define __MIN_SMALL_SIZE __SMALL_NR(256) /* 16 / 32 */
+#define __MAX_SMALL_SIZE __SMALL_NR(2) /* 2048 / 4096 */
- if (ptr==NULL) return;
+#define GET_SIZE(s) (__MIN_SMALL_SIZE<<get_index((s)))
- block=START_BLOCK(ptr);
- if (block->magic1 != ALLOC_MAGIC1) return;
- if (block->magic2 != ALLOC_MAGIC2) return;
+#define FIRST_SMALL(p) (((unsigned long)(p))&(~(MEM_BLOCK_SIZE-1)))
- prev=&base;
- for (p=prev->ptr ; ; prev=p, p=p->ptr)
- {
- if ((block>prev)&&(block<p)) break; /* found the gap block belongs */
- if ((prev>p)&&(block<p)) break; /* block pre freelist */
- if ((prev>p)&&(block>prev)) break; /* block after freelist */
+static inline int __ind_shift() { return (MEM_BLOCK_SIZE==4096)?4:5; }
- /* emergency escape: freelist has ONLY one entry the freelist base */
- if (p->ptr==p) break;
+static size_t get_index(size_t _size) {
+ register size_t idx=0;
+ if (_size) {
+ register size_t size=((_size-1)&(MEM_BLOCK_SIZE-1))>>__ind_shift();
+ while(size) { size>>=1; ++idx; }
}
- prev->ptr = block;
+ return idx;
+}
- if (END_OF_BLOCK(block)==p)
- { /* join right neighbor */
- block->ptr = p->ptr;
- block->size += p->size;
- }
- else
- block->ptr = p;
+/* small mem */
+static void __small_free(void*_ptr,size_t _size) __attribute__((regparm(2)));
- if (END_OF_BLOCK(prev)==block)
- { /* join left neighbor */
- prev->size += block->size;
- prev->ptr = block->ptr;
- }
+static void __small_free(void*_ptr,size_t _size) {
+ __alloc_t* ptr=BLOCK_START(_ptr);
+ size_t size=_size;
+ size_t idx=get_index(size);
+
+ memset(ptr,0,size); /* allways zero out small mem */
+
+ ptr->next=__small_mem[idx];
+ __small_mem[idx]=ptr;
}
-void free(void *ptr) __attribute__((weak,alias("__libc_free")));
-
-static void *alloc_get_mem(unsigned long size)
-{
- char *tmp;
- alloc_head *ah;
-
- size=PAGE_ALIGN(size);
-
- /* map free pages @ alloc_get_end */
- tmp=mmap(alloc_get_end, size, PROT_READ|PROT_WRITE,
- MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
- if (tmp==MAP_FAILED)
- {
- /* OK we can't map free pages @ alloc_get_end so try free position */
- tmp=mmap(0, size, PROT_READ|PROT_WRITE,
- MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
- if (tmp==MAP_FAILED)
- {
- errno = ENOMEM;
- return NULL; /* PANIC ! */
- }
- alloc_get_end=tmp;
- }
- alloc_get_end+=size;
+static void* __small_malloc(size_t _size) {
+ __alloc_t *ptr;
+ size_t size=_size;
+ size_t idx;
+
+ idx=get_index(size);
+ ptr=__small_mem[idx];
- /* make a header */
- ah=(alloc_head*)tmp;
- ah->magic1=ALLOC_MAGIC1;
- ah->magic2=ALLOC_MAGIC2;
- ah->ptr=ah;
- ah->size=size;
+ if (ptr==0) { /* no free blocks ? */
+ register int i,nr;
+ ptr=do_mmap(MEM_BLOCK_SIZE);
+ if (ptr==MAP_FAILED) return MAP_FAILED;
+
+ __small_mem[idx]=ptr;
+
+ nr=__SMALL_NR(size)-1;
+ for (i=0;i<nr;i++) {
+ ptr->next=(((void*)ptr)+size);
+ ptr=ptr->next;
+ }
+ ptr->next=0;
+
+ ptr=__small_mem[idx];
+ }
- /* link new free maped pages in freelist */
- __libc_free(START_DATA(tmp));
+ /* get a free block */
+ __small_mem[idx]=ptr->next;
+ ptr->next=0;
- return &base;
+ return ptr;
}
-void *__libc_malloc(size_t size)
-{
- alloc_head *p, *prev;
- size_t need;
+/* -- PUBLIC FUNCTIONS ---------------------------------------------------- */
- /* needed MEM_ALLOC_MIN */
- need=MIN_ALLOC(size);
-
- prev=&base;
- for (p=prev->ptr;;prev=p,p=p->ptr)
- {
- if (p->size>=need)
- {
- if (p->size==need)
- { /* fit PERFECT */
- prev->ptr=p->ptr; /* relink freelist */
- }
+static void _alloc_libc_free(void *ptr) {
+ register size_t size;
+ if (ptr) {
+ size=((__alloc_t*)BLOCK_START(ptr))->size;
+ if (size) {
+ if (size<=__MAX_SMALL_SIZE)
+ __small_free(ptr,size);
else
- {
- alloc_head *tmp=(alloc_head*)(((char*)p)+need);
- if ((p->size-need)<sizeof(alloc_head))
- { /* work around: if there is not enough space for freelist head.
- * this waste some bytes ( < sizeof(alloc_head) ) */
- need=p->size;
- prev->ptr=p->ptr; /* relink freelist */
- }
- else
- {
- prev->ptr=tmp;
- tmp->magic1=ALLOC_MAGIC1;
- tmp->magic2=ALLOC_MAGIC2;
- tmp->ptr=p->ptr;
- tmp->size=p->size-need; /* remaining size */
- }
+ munmap(BLOCK_START(ptr),size);
+ }
+ }
+}
+void __libc_free(void *ptr) __attribute__((alias("_alloc_libc_free")));
+void free(void *ptr) __attribute__((weak,alias("_alloc_libc_free")));
+void if_freenameindex(void* ptr) __attribute__((alias("free")));
- p->size=need; /* set size */
- }
- p->ptr=p; /* self-link */
+#ifdef WANT_MALLOC_ZERO
+static __alloc_t zeromem[2]={{0},{0}};
+#endif
- return (void*)START_DATA(p);
- }
- else if (p==&base)
- {
- if ((p=alloc_get_mem(need))==NULL) goto err_out;
- }
+static void* _alloc_libc_malloc(size_t size) {
+ __alloc_t* ptr;
+ size_t need;
+#ifdef WANT_MALLOC_ZERO
+ if (!size) return BLOCK_RET(zeromem);
+#else
+ if (!size) goto retzero;
+#endif
+ size+=sizeof(__alloc_t);
+ if (size<sizeof(__alloc_t)) goto retzero;
+ if (size<=__MAX_SMALL_SIZE) {
+ need=GET_SIZE(size);
+ ptr=__small_malloc(need);
+ }
+ else {
+ need=PAGE_ALIGN(size);
+ if (!need) ptr=MAP_FAILED; else ptr=do_mmap(need);
}
+ if (ptr==MAP_FAILED) goto err_out;
+ ptr->size=need;
+ return BLOCK_RET(ptr);
err_out:
- return NULL;
+ (*__errno_location())=ENOMEM;
+retzero:
+ return 0;
}
-void *malloc(size_t size) __attribute__((weak,alias("__libc_malloc")));
-
-void *calloc(size_t nmemb,size_t size)
-{
- size_t n=nmemb*size;
- void *tmp=malloc(n);
- if (tmp) memset(tmp,0,n);
- return tmp;
+void* __libc_malloc(size_t size) __attribute__((alias("_alloc_libc_malloc")));
+void* malloc(size_t size) __attribute__((weak,alias("_alloc_libc_malloc")));
+
+void *calloc(size_t nmemb, size_t _size) {
+ register size_t size=_size*nmemb;
+ if (nmemb && size/nmemb!=_size) {
+ (*__errno_location())=ENOMEM;
+ return 0;
+ }
+ return malloc(size);
}
-void *realloc(void *ptr,size_t size)
-{
- alloc_head *tmp=0,*tf=0;
- long need=0;
- long diff=0;
-
- if (ptr)
- {
- if (size)
- {
- tmp=START_BLOCK(ptr);
- need=MIN_ALLOC(size); /* only this size will survive */
- diff=tmp->size-need;
- if (diff<0)
- {
- if ((tf=malloc(size)))
- {
- memcpy(tf,ptr,tmp->size-sizeof(alloc_head));
- free(ptr);
- return tf;
+void* __libc_realloc(void* ptr, size_t _size);
+void* __libc_realloc(void* ptr, size_t _size) {
+ register size_t size=_size;
+ if (ptr) {
+ if (size) {
+ __alloc_t* tmp=BLOCK_START(ptr);
+ size+=sizeof(__alloc_t);
+ if (size<sizeof(__alloc_t)) goto retzero;
+ size=(size<=__MAX_SMALL_SIZE)?GET_SIZE(size):PAGE_ALIGN(size);
+ if (tmp->size!=size) {
+ if ((tmp->size<=__MAX_SMALL_SIZE)) {
+ void *new=_alloc_libc_malloc(_size);
+ if (new) {
+ register __alloc_t* foo=BLOCK_START(new);
+ size=foo->size;
+ if (size>tmp->size) size=tmp->size;
+ if (size) memcpy(new,ptr,size-sizeof(__alloc_t));
+ _alloc_libc_free(ptr);
+ }
+ ptr=new;
+ }
+ else {
+ register __alloc_t* foo;
+ size=PAGE_ALIGN(size);
+ foo=mremap(tmp,tmp->size,size,MREMAP_MAYMOVE);
+ if (foo==MAP_FAILED) {
+retzero:
+ (*__errno_location())=ENOMEM;
+ ptr=0;
+ }
+ else {
+ foo->size=size;
+ ptr=BLOCK_RET(foo);
+ }
}
- return NULL;
- }
- if (diff>=sizeof(alloc_head))
- {
- tmp->size=need;
- tf=END_OF_BLOCK(tmp);
- tf->magic1=ALLOC_MAGIC1;
- tf->magic2=ALLOC_MAGIC2;
- tf->ptr=tf;
- tf->size=diff;
- free(START_DATA(tf));
}
- return ptr;
}
- else
- free(ptr);
+ else { /* size==0 */
+ _alloc_libc_free(ptr);
+ }
}
- else if (size>0)
- return malloc(size);
- return NULL;
+ else { /* ptr==0 */
+ if (size) {
+ ptr=_alloc_libc_malloc(size);
+ }
+ }
+ return ptr;
}
+void* realloc(void* ptr, size_t size) __attribute__((weak,alias("__libc_realloc")));
+
diff --git a/mdk-stage1/dietlibc/lib/assert_fail.c b/mdk-stage1/dietlibc/lib/assert_fail.c
index 3d6ba65c1..35b8b186a 100644
--- a/mdk-stage1/dietlibc/lib/assert_fail.c
+++ b/mdk-stage1/dietlibc/lib/assert_fail.c
@@ -2,14 +2,15 @@
#include <string.h>
#include <unistd.h>
#include "dietwarning.h"
+#include <write12.h>
-extern int __ltostr(char *s, int size, unsigned long i, int base, char UpCase);
+void __assert_fail (const char *assertion, const char *file, unsigned int line, const char *function);
void __assert_fail (const char *assertion, const char *file, unsigned int line, const char *function)
{
- int alen=strlen(assertion);
- int flen=strlen(file);
- int fulen=strlen(function);
+ unsigned int alen=strlen(assertion);
+ unsigned int flen=strlen(file);
+ unsigned int fulen=function?strlen(function):0;
char *buf=(char*)alloca(alen+flen+fulen+50);
if (buf) {
char *tmp;
@@ -22,7 +23,7 @@ void __assert_fail (const char *assertion, const char *file, unsigned int line,
strcat(buf,"Assertion `");
strcat(buf,assertion);
strcat(buf,"' failed.\n");
- write(2,buf,strlen(buf));
+ __write2(buf);
}
abort();
}
diff --git a/mdk-stage1/dietlibc/lib/atexit.c b/mdk-stage1/dietlibc/lib/atexit.c
index 8204af06c..e0eef19c6 100644
--- a/mdk-stage1/dietlibc/lib/atexit.c
+++ b/mdk-stage1/dietlibc/lib/atexit.c
@@ -1,23 +1,31 @@
+#include <stdlib.h>
+
typedef void (*function)(void);
-static function __atexitlist[4];
+#define NUM_ATEXIT 32
+
+static function __atexitlist[NUM_ATEXIT];
+static int atexit_counter = 0;
int atexit(function t) {
- int i;
- for (i=0; i<4; i++)
- if (__atexitlist[i]==0) {
- __atexitlist[i]=t;
- return 0;
- }
+ if (atexit_counter<NUM_ATEXIT) {
+ __atexitlist[atexit_counter]=t;
+ ++atexit_counter;
+ return 0;
+ }
return -1;
}
extern void _exit(int code) __attribute__((noreturn));
+extern void __thread_doexit();
-void exit(int code) {
- if (__atexitlist[3]) __atexitlist[3]();
- if (__atexitlist[2]) __atexitlist[2]();
- if (__atexitlist[1]) __atexitlist[1]();
- if (__atexitlist[0]) __atexitlist[0]();
+void __libc_exit(int code);
+void __libc_exit(int code) {
+ register int i=atexit_counter;
+ __thread_doexit();
+ while(i) {
+ __atexitlist[--i]();
+ }
_exit(code);
}
+void exit(int code) __attribute__((alias("__libc_exit")));
diff --git a/mdk-stage1/dietlibc/lib/atoi.c b/mdk-stage1/dietlibc/lib/atoi.c
index fe952b8c4..10b618950 100644
--- a/mdk-stage1/dietlibc/lib/atoi.c
+++ b/mdk-stage1/dietlibc/lib/atoi.c
@@ -1,8 +1,19 @@
+#include <endian.h>
+#include <ctype.h>
+#include <stdlib.h>
+
+#if __WORDSIZE == 64
int atoi(const char* s) {
- int v=0;
+ long int v=0;
int sign=1;
- if (*s=='-') { sign=-1; ++s; } else if (*s=='+') ++s;
- while (*s && (*s>='0') && (*s<='9'))
- v=v*10+*s++-'0';
- return v*sign;
+ while ( *s == ' ' || (unsigned int)(*s - 9) < 5u) s++;
+ switch (*s) {
+ case '-': sign=-1;
+ case '+': ++s;
+ }
+ while ((unsigned int) (*s - '0') < 10u) {
+ v=v*10+*s-'0'; ++s;
+ }
+ return sign==-1?-v:v;
}
+#endif
diff --git a/mdk-stage1/dietlibc/lib/atol.c b/mdk-stage1/dietlibc/lib/atol.c
index 558a004b9..5961a1881 100644
--- a/mdk-stage1/dietlibc/lib/atol.c
+++ b/mdk-stage1/dietlibc/lib/atol.c
@@ -1,8 +1,23 @@
+#include <endian.h>
+#include <ctype.h>
+#include <stdlib.h>
+
long int atol(const char* s) {
long int v=0;
- int sign=1;
- if (*s=='-') { sign=-1; ++s; } else if (*s=='+') ++s;
- while (*s && (*s>='0') && (*s<='9'))
- v=v*10+*s++-'0';
- return v*sign;
+ int sign=0;
+ while ( *s == ' ' || (unsigned int)(*s - 9) < 5u) ++s;
+ switch (*s) {
+ case '-': sign=-1;
+ case '+': ++s;
+ }
+ while ((unsigned int) (*s - '0') < 10u) {
+ v=v*10+*s-'0'; ++s;
+ }
+ return sign?-v:v;
}
+
+#if __WORDSIZE == 64
+long long int atoll(const char* s) __attribute__((alias("atol")));
+#else
+int atoi(const char* s) __attribute__((alias("atol")));
+#endif
diff --git a/mdk-stage1/dietlibc/lib/bind.c b/mdk-stage1/dietlibc/lib/bind.c
index ec1c93e1b..dae80f491 100644
--- a/mdk-stage1/dietlibc/lib/bind.c
+++ b/mdk-stage1/dietlibc/lib/bind.c
@@ -1,12 +1,11 @@
-#include <linux/net.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
-int bind(int a, void * b, int c) {
-#ifdef __i386__
- return socketcall(SYS_BIND, (long*)&a);
-#else
+int __libc_bind(int a, void * b, int c);
+int __libc_bind(int a, void * b, int c) {
unsigned long args[] = { a, (long) b, c };
return socketcall(SYS_BIND, args);
-#endif
}
+
+int bind(int a, void * b, int c) __attribute__((weak,alias("__libc_bind")));
diff --git a/mdk-stage1/dietlibc/lib/bsearch.c b/mdk-stage1/dietlibc/lib/bsearch.c
index d2f5c74b2..14605f1f6 100644
--- a/mdk-stage1/dietlibc/lib/bsearch.c
+++ b/mdk-stage1/dietlibc/lib/bsearch.c
@@ -1,20 +1,20 @@
+#include <assert.h>
#include <stdlib.h>
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
- const char *v, *r;
- int res;
- r=base+(nmemb-1)*size;
- do {
- register int num=nmemb/2;
- v=base+num*size;
- if ((res=compar(key,v))<0) {
- r=v-size;
- nmemb=num;
- } else {
- if (res==0) return (void*)v;
- base=v+size;
- nmemb-=num;
- }
- } while ((char*)base<=r);
+ size_t m;
+ while (nmemb) {
+ int tmp;
+ void *p;
+ m=nmemb/2;
+ p=(void *) (((const char *) base) + (m * size));
+ if ((tmp=(*compar)(key,p))<0) {
+ nmemb=m;
+ } else if (tmp>0) {
+ base=p+size;
+ nmemb-=m+1;
+ } else
+ return p;
+ }
return 0;
}
diff --git a/mdk-stage1/dietlibc/lib/cfmakeraw.c b/mdk-stage1/dietlibc/lib/cfmakeraw.c
index 7f6df9913..e33c783c9 100644
--- a/mdk-stage1/dietlibc/lib/cfmakeraw.c
+++ b/mdk-stage1/dietlibc/lib/cfmakeraw.c
@@ -1,8 +1,7 @@
#include <termios.h>
#include <sys/ioctl.h>
-void cfmakeraw(struct termios *t)
-{
+void cfmakeraw(struct termios *t) {
t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
t->c_oflag &= ~OPOST;
t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
diff --git a/mdk-stage1/dietlibc/lib/closedir.c b/mdk-stage1/dietlibc/lib/closedir.c
index e73025eb1..3aade81b3 100644
--- a/mdk-stage1/dietlibc/lib/closedir.c
+++ b/mdk-stage1/dietlibc/lib/closedir.c
@@ -1,10 +1,11 @@
#include "dietdirent.h"
+#include <sys/mman.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
int closedir (DIR* d) {
int res=close(d->fd);
- free(d);
+ munmap (d, PAGE_SIZE);
return res;
}
diff --git a/mdk-stage1/dietlibc/lib/connect.c b/mdk-stage1/dietlibc/lib/connect.c
index 01fd61a6c..642303ef3 100644
--- a/mdk-stage1/dietlibc/lib/connect.c
+++ b/mdk-stage1/dietlibc/lib/connect.c
@@ -1,14 +1,11 @@
-#include <linux/net.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_connect(int a, void * b, int c);
int __libc_connect(int a, void * b, int c) {
-#ifdef __i386__
- return socketcall(SYS_CONNECT, (long*)&a);
-#else
unsigned long args[] = { a, (long) b, c };
return socketcall(SYS_CONNECT, args);
-#endif
}
int connect(int a, void * b, int c) __attribute__((weak,alias("__libc_connect")));
diff --git a/mdk-stage1/dietlibc/lib/creat.c b/mdk-stage1/dietlibc/lib/creat.c
index 82145b515..41d8236aa 100644
--- a/mdk-stage1/dietlibc/lib/creat.c
+++ b/mdk-stage1/dietlibc/lib/creat.c
@@ -1,5 +1,7 @@
#include <fcntl.h>
-int creat(const char *file,mode_t mode) {
+int __libc_creat(const char *file,mode_t mode);
+int __libc_creat(const char *file,mode_t mode) {
return open(file,O_WRONLY|O_CREAT|O_TRUNC,mode);
}
+int creat(const char *file,mode_t mode) __attribute__((weak,alias("__libc_creat")));
diff --git a/mdk-stage1/dietlibc/lib/errlist.c b/mdk-stage1/dietlibc/lib/errlist.c
deleted file mode 100644
index d0e712190..000000000
--- a/mdk-stage1/dietlibc/lib/errlist.c
+++ /dev/null
@@ -1,132 +0,0 @@
-
-const char *const sys_errlist[] = {
- "Success", /* 0 */
- "Operation not permitted", /* EPERM */
- "No such file or directory", /* ENOENT */
- "No such process", /* ESRCH */
- "Interrupted system call", /* EINTR */
- "I/O error", /* EIO */
- "No such device or address", /* ENXIO */
- "Arg list too long", /* E2BIG */
- "Exec format error", /* ENOEXEC */
- "Bad file number", /* EBADF */
- "No child processes", /* ECHILD */
- "Try again", /* EAGAIN */
- "Out of memory", /* ENOMEM */
- "Permission denied", /* EACCES */
- "Bad address", /* EFAULT */
- "Block device required", /* ENOTBLK */
- "Device or resource busy", /* EBUSY */
- "File exists", /* EEXIST */
- "Cross-device link", /* EXDEV */
- "No such device", /* ENODEV */
- "Not a directory", /* ENOTDIR */
- "Is a directory", /* EISDIR */
- "Invalid argument", /* EINVAL */
- "File table overflow", /* ENFILE */
- "Too many open files", /* EMFILE */
- "Not a typewriter", /* ENOTTY */
- "Text file busy", /* ETXTBSY */
- "File too large", /* EFBIG */
- "No space left on device", /* ENOSPC */
- "Illegal seek", /* ESPIPE */
- "Read-only file system", /* EROFS */
- "Too many links", /* EMLINK */
- "Broken pipe", /* EPIPE */
- "Math argument out of domain of func", /* EDOM */
- "Math result not representable", /* ERANGE */
- "Resource deadlock would occur", /* EDEADLK */
- "File name too long", /* ENAMETOOLONG */
- "No record locks available", /* ENOLCK */
- "Function not implemented", /* ENOSYS */
- "Directory not empty", /* ENOTEMPTY */
- "Too many symbolic links encountered", /* ELOOP */
- "Operation would block", /* EWOULDBLOCK */
- "No message of desired type", /* ENOMSG */
- "Identifier removed", /* EIDRM */
- "Channel number out of range", /* ECHRNG */
- "Level 2 not synchronized", /* EL2NSYNC */
- "Level 3 halted", /* EL3HLT */
- "Level 3 reset", /* EL3RST */
- "Link number out of range", /* ELNRNG */
- "Protocol driver not attached", /* EUNATCH */
- "No CSI structure available", /* ENOCSI */
- "Level 2 halted", /* EL2HLT */
- "Invalid exchange", /* EBADE */
- "Invalid request descriptor", /* EBADR */
- "Exchange full", /* EXFULL */
- "No anode", /* ENOANO */
- "Invalid request code", /* EBADRQC */
- "Invalid slot", /* EBADSLT */
- "File locking deadlock error", /* EDEADLOCK */
- "Bad font file format", /* EBFONT */
- "Device not a stream", /* ENOSTR */
- "No data available", /* ENODATA */
- "Timer expired", /* ETIME */
- "Out of streams resources", /* ENOSR */
- "Machine is not on the network", /* ENONET */
- "Package not installed", /* ENOPKG */
- "Object is remote", /* EREMOTE */
- "Link has been severed", /* ENOLINK */
- "Advertise error", /* EADV */
- "Srmount error", /* ESRMNT */
- "Communication error on send", /* ECOMM */
- "Protocol error", /* EPROTO */
- "Multihop attempted", /* EMULTIHOP */
- "RFS specific error", /* EDOTDOT */
- "Not a data message", /* EBADMSG */
- "Value too large for defined data type", /* EOVERFLOW */
- "Name not unique on network", /* ENOTUNIQ */
- "File descriptor in bad state", /* EBADFD */
- "Remote address changed", /* EREMCHG */
- "Can not access a needed shared library", /* ELIBACC */
- "Accessing a corrupted shared library", /* ELIBBAD */
- ".lib section in a.out corrupted", /* ELIBSCN */
- "Attempting to link in too many shared libraries", /* ELIBMAX */
- "Cannot exec a shared library directly", /* ELIBEXEC */
- "Illegal byte sequence", /* EILSEQ */
- "Interrupted system call should be restarted", /* ERESTART */
- "Streams pipe error", /* ESTRPIPE */
- "Too many users", /* EUSERS */
- "Socket operation on non-socket", /* ENOTSOCK */
- "Destination address required", /* EDESTADDRREQ */
- "Message too long", /* EMSGSIZE */
- "Protocol wrong type for socket", /* EPROTOTYPE */
- "Protocol not available", /* ENOPROTOOPT */
- "Protocol not supported", /* EPROTONOSUPPORT */
- "Socket type not supported", /* ESOCKTNOSUPPORT */
- "Operation not supported on transport endpoint", /* EOPNOTSUPP */
- "Protocol family not supported", /* EPFNOSUPPORT */
- "Address family not supported by protocol", /* EAFNOSUPPORT */
- "Address already in use", /* EADDRINUSE */
- "Cannot assign requested address", /* EADDRNOTAVAIL */
- "Network is down", /* ENETDOWN */
- "Network is unreachable", /* ENETUNREACH */
- "Network dropped connection because of reset", /* ENETRESET */
- "Software caused connection abort", /* ECONNABORTED */
- "Connection reset by peer", /* ECONNRESET */
- "No buffer space available", /* ENOBUFS */
- "Transport endpoint is already connected", /* EISCONN */
- "Transport endpoint is not connected", /* ENOTCONN */
- "Cannot send after transport endpoint shutdown", /* ESHUTDOWN */
- "Too many references: cannot splice", /* ETOOMANYREFS */
- "Connection timed out", /* ETIMEDOUT */
- "Connection refused", /* ECONNREFUSED */
- "Host is down", /* EHOSTDOWN */
- "No route to host", /* EHOSTUNREACH */
- "Operation already in progress", /* EALREADY */
- "Operation now in progress", /* EINPROGRESS */
- "Stale NFS file handle", /* ESTALE */
- "Structure needs cleaning", /* EUCLEAN */
- "Not a XENIX named type file", /* ENOTNAM */
- "No XENIX semaphores available", /* ENAVAIL */
- "Is a named type file", /* EISNAM */
- "Remote I/O error", /* EREMOTEIO */
- "Quota exceeded", /* EDQUOT */
- "No medium found", /* ENOMEDIUM */
- "Wrong medium type", /* EMEDIUMTYPE */
- 0
-};
-
-
-const int sys_nerr = ((sizeof (sys_errlist))/(sizeof(char *))-1);
diff --git a/mdk-stage1/dietlibc/lib/errno_location.c b/mdk-stage1/dietlibc/lib/errno_location.c
index 286f833c9..298c9c3cf 100644
--- a/mdk-stage1/dietlibc/lib/errno_location.c
+++ b/mdk-stage1/dietlibc/lib/errno_location.c
@@ -1,5 +1,5 @@
extern int errno;
+int *__errno_location(void) __attribute__((weak));
int *__errno_location() { return &errno; }
-int *errno_location() __attribute__((weak,alias("__errno_location")));
diff --git a/mdk-stage1/dietlibc/lib/exec_lib.c b/mdk-stage1/dietlibc/lib/exec_lib.c
index 3129224a0..575c03d6d 100644
--- a/mdk-stage1/dietlibc/lib/exec_lib.c
+++ b/mdk-stage1/dietlibc/lib/exec_lib.c
@@ -3,18 +3,19 @@
extern char **environ;
+int __exec_shell(const char *file, char *const argv[]);
int __exec_shell(const char *file, char *const argv[]) {
int i;
for (i = 0; argv[i]; i++);
{
- char *shell_argv[i + 1];
+ const char *shell_argv[i + 1];
shell_argv[0] = _PATH_BSHELL;
- shell_argv[1] = (char *) file;
+ shell_argv[1] = file;
for (; i > 1; i--)
shell_argv[i] = argv[i - 1];
- return execve(_PATH_BSHELL, shell_argv, environ);
+ return execve(_PATH_BSHELL, (char*const*)shell_argv, environ);
}
}
diff --git a/mdk-stage1/dietlibc/lib/execl.c b/mdk-stage1/dietlibc/lib/execl.c
index 71459e71f..360bd3908 100644
--- a/mdk-stage1/dietlibc/lib/execl.c
+++ b/mdk-stage1/dietlibc/lib/execl.c
@@ -2,6 +2,7 @@
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
+#include "dietfeatures.h"
int execl( const char *path,...) {
va_list ap;
@@ -19,6 +20,6 @@ int execl( const char *path,...) {
va_end (ap);
return execve(path,argv,environ);
}
- __set_errno(ENOMEM);
+ errno=ENOMEM;
return -1;
}
diff --git a/mdk-stage1/dietlibc/lib/execlp.c b/mdk-stage1/dietlibc/lib/execlp.c
index 3aea1e960..27dc0bb95 100644
--- a/mdk-stage1/dietlibc/lib/execlp.c
+++ b/mdk-stage1/dietlibc/lib/execlp.c
@@ -2,7 +2,7 @@
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
-#include "dietstdarg.h"
+#include "dietfeatures.h"
int execlp(const char* file, const char *arg,...) {
va_list ap,bak;
@@ -15,12 +15,12 @@ int execlp(const char* file, const char *arg,...) {
++n;
va_end (ap);
if ((argv=(char **)alloca(n*sizeof(char*)))) {
- argv[0]=arg;
+ argv[0]=(char*)arg;
for (i=0; i<n; ++i)
argv[i+1]=va_arg(bak,char *);
va_end (bak);
return execvp(file,argv);
}
- __set_errno(ENOMEM);
+ errno=ENOMEM;
return -1;
}
diff --git a/mdk-stage1/dietlibc/lib/execv.c b/mdk-stage1/dietlibc/lib/execv.c
index efd760253..d7acfa29f 100644
--- a/mdk-stage1/dietlibc/lib/execv.c
+++ b/mdk-stage1/dietlibc/lib/execv.c
@@ -4,10 +4,12 @@
#include <unistd.h>
#include <errno.h>
#include "exec_lib.h"
+#include "dietfeatures.h"
int execv(const char *file, char *const argv[]) {
if (execve(file,argv,environ)==-1) {
- if (errno==ENOEXEC) __exec_shell(file,argv);
+ if (errno==ENOEXEC)
+ __exec_shell(file,argv);
}
return -1;
}
diff --git a/mdk-stage1/dietlibc/lib/execvp.c b/mdk-stage1/dietlibc/lib/execvp.c
index 20521d69f..72447af7a 100644
--- a/mdk-stage1/dietlibc/lib/execvp.c
+++ b/mdk-stage1/dietlibc/lib/execvp.c
@@ -4,31 +4,40 @@
#include <unistd.h>
#include <errno.h>
#include "exec_lib.h"
+#include "dietfeatures.h"
int execvp(const char *file, char *const argv[]) {
- char *path=getenv("PATH");
+ const char *path=getenv("PATH");
char *cur,*next;
char buf[PATH_MAX];
if (strchr((char*)file,'/')) {
if (execve(file,argv,environ)==-1) {
- if (errno==ENOEXEC) __exec_shell(file,argv);
+ if (errno==ENOEXEC)
+ __exec_shell(file,argv);
return -1;
}
}
if (!path) path=_PATH_DEFPATH;
- for (cur=path; cur; cur=next) {
+ for (cur=(char*)path; cur; cur=next) {
next=strchr(cur,':');
if (!next)
next=cur+strlen(cur);
if (next==cur) {
buf[0]='.';
cur--;
- } else
- memmove(buf,cur,next-cur);
+ } else {
+ if (next-cur>=PATH_MAX-3) { error: errno=EINVAL; return -1; }
+ memmove(buf,cur,(size_t)(next-cur));
+ }
buf[next-cur]='/';
- memmove(&buf[next-cur+1],file,strlen(file)+1);
+ {
+ int len=strlen(file);
+ if (len+(next-cur)>=PATH_MAX-2) goto error;
+ memmove(&buf[next-cur+1],file,strlen(file)+1);
+ }
if (execve(buf,argv,environ)==-1) {
- if (errno==ENOEXEC) return __exec_shell(buf,argv);
+ if (errno==ENOEXEC)
+ return __exec_shell(buf,argv);
if ((errno!=EACCES) && (errno!=ENOENT)) return -1;
}
if (*next==0) break;
diff --git a/mdk-stage1/dietlibc/lib/ftw.c b/mdk-stage1/dietlibc/lib/ftw.c
index a3c04af9a..af13a7f86 100644
--- a/mdk-stage1/dietlibc/lib/ftw.c
+++ b/mdk-stage1/dietlibc/lib/ftw.c
@@ -1,52 +1,47 @@
-#include <linux/stat.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <ftw.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
+#include "dietdirent.h"
-int ftw(const char *dir,int (*fn)(const char *file, const struct stat *sb, int flag), int depth) {
- char *cwd;
- int cwdlen;
- DIR *d;
- struct dirent *de;
+int ftw(const char*dir,int(*f)(const char*file,const struct stat*sb,int flag),int dpth){
+ char* cd;
+ size_t cdl;
+ DIR* d;
+ struct dirent* de;
struct stat sb;
- if (chdir(dir)) return -1;
- cwd=alloca(PATH_MAX+1);
- if (!getcwd(cwd,PATH_MAX)) return -1;
- cwd[PATH_MAX]=0;
- cwdlen=strlen(cwd);
-/* write(1,"ftw in ",7); puts(cwd); */
- if (!(d=opendir("."))) return -1;
- while ((de=readdir(d))) {
- int res;
- int flag;
- int nlen;
- char *filename;
- if (de->d_name[0]=='.' &&
- (de->d_name[1]==0 ||
- (de->d_name[1]=='.' && de->d_name[2]==0))) continue;
- nlen=strlen(de->d_name);
- filename=alloca(nlen+cwdlen+3);
- memmove(filename,cwd,cwdlen);
- filename[cwdlen]='/';
- memmove(filename+cwdlen+1,de->d_name,nlen+1);
- if (!lstat(de->d_name,&sb)) {
- if (S_ISLNK(sb.st_mode)) flag=FTW_SL; else
- if (S_ISDIR(sb.st_mode)) flag=FTW_D; else
- flag=FTW_F;
- } else
- flag=FTW_NS;
- res=fn(filename,&sb,flag);
- if (res) return res;
- if (flag==FTW_D && depth>0) {
- res=ftw(filename,fn,depth-1);
- chdir(dir);
- if (res) return res;
+ int r;
+ unsigned int oldlen=0;
+ char* filename; /* the warning gcc issues here is bogus */
+ if(chdir(dir))return-1;
+ cd=alloca(PATH_MAX+1);
+ if(!getcwd(cd,PATH_MAX))return-1;
+ cd[PATH_MAX]='\0';
+ cdl=strlen(cd);
+ if(!(d=opendir(".")))return-1;
+ while((de=readdir(d))){
+ int flg;
+ size_t nl;
+ if(de->d_name[0]=='.'){if(!de->d_name[1])continue;if(de->d_name[1]=='.'&&!de->d_name[2])continue;}
+ nl=strlen(de->d_name);
+ if (nl+cdl+2>oldlen)
+ filename=alloca(oldlen=nl+cdl+2);
+ memmove(filename,cd,cdl);
+ filename[cdl]='/';
+ memmove(filename+cdl+1,de->d_name,nl+1);
+ if(!lstat(de->d_name,&sb)){
+ if(S_ISLNK(sb.st_mode))flg=FTW_SL;else if(S_ISDIR(sb.st_mode))flg=FTW_D;else flg=FTW_F;
+ }else flg=FTW_NS;
+ r=f(filename,&sb,flg);
+ if(r){closedir(d);return r;}
+ if(flg==FTW_D&&dpth){
+ r=ftw(filename,f,dpth-1);
+ fchdir(d->fd);
+ if (r){closedir(d);return r;}
}
-/* puts(de->d_name); */
}
- closedir(d);
- return 0;
+ return closedir(d);
}
diff --git a/mdk-stage1/dietlibc/lib/getdomainname.c b/mdk-stage1/dietlibc/lib/getdomainname.c
index 2554fe4c3..75486b010 100644
--- a/mdk-stage1/dietlibc/lib/getdomainname.c
+++ b/mdk-stage1/dietlibc/lib/getdomainname.c
@@ -2,12 +2,13 @@
#include <sys/types.h>
#include <sys/utsname.h>
+#include <unistd.h>
int getdomainname(char *name,size_t len) {
struct utsname u;
int res=uname(&u);
if (res==0) {
- int i;
+ size_t i;
if (len>=_UTSNAME_DOMAIN_LENGTH)
len=_UTSNAME_DOMAIN_LENGTH;
for (i=0; i<len; i++)
diff --git a/mdk-stage1/dietlibc/lib/getenv.c b/mdk-stage1/dietlibc/lib/getenv.c
index fc37e9927..5b529c17f 100644
--- a/mdk-stage1/dietlibc/lib/getenv.c
+++ b/mdk-stage1/dietlibc/lib/getenv.c
@@ -1,7 +1,6 @@
+#include <stdlib.h>
#include <string.h>
-extern char **environ;
-
extern char *getenv(const char *s)
{
int i;
diff --git a/mdk-stage1/dietlibc/lib/gethostname.c b/mdk-stage1/dietlibc/lib/gethostname.c
index ccc0fbc8e..c3d2f3d5e 100644
--- a/mdk-stage1/dietlibc/lib/gethostname.c
+++ b/mdk-stage1/dietlibc/lib/gethostname.c
@@ -2,12 +2,13 @@
#include <sys/types.h>
#include <sys/utsname.h>
+#include <unistd.h>
int gethostname(char *name,size_t len) {
struct utsname u;
int res=uname(&u);
if (res==0) {
- int i;
+ size_t i;
if (len>=_UTSNAME_NODENAME_LENGTH)
len=_UTSNAME_NODENAME_LENGTH;
for (i=0; i<len; i++)
diff --git a/mdk-stage1/dietlibc/lib/getopt.c b/mdk-stage1/dietlibc/lib/getopt.c
index 93097122f..aa1988f8d 100644
--- a/mdk-stage1/dietlibc/lib/getopt.c
+++ b/mdk-stage1/dietlibc/lib/getopt.c
@@ -1,118 +1,63 @@
-#include "getopt.h"
#include <string.h>
-
-/*
- * by Olaf Dreesen
- */
-
-int opterr;
-
-int optind=1;
-char *optarg;
-
-static int opt_unknown=1,opt_unknown_len;
-
-static int getopt_check(int c,char*o,int ol)
-{
- int i;
- if (c==':') return 2;
- for (i=0;i<ol;i++)
- {
- if (o[i]==c)
- {
- if (o[i+1]==':') return 1;
- return 0;
+#include <getopt.h>
+
+static void getopterror(int which) {
+ static char error1[]="Unknown option `-x'.\n";
+ static char error2[]="Missing argument for `-x'.\n";
+ if (opterr) {
+ if (which) {
+ error2[23]=optopt;
+ write(2,error2,28);
+ } else {
+ error1[17]=optopt;
+ write(2,error1,22);
}
}
- return 2;
}
-static void getopt_sort(char*v[],int oi)
-{
- int i;
- char *tmp, *tmp2=0;
-
- if (opt_unknown_len)
- {
- tmp=v[optind-(1+oi)];
- if (oi) tmp2=v[optind-1];
-
- for (i=opt_unknown+opt_unknown_len;i>opt_unknown;i--) v[i+oi]=v[i-1];
-
- v[opt_unknown++]=tmp;
- if (oi) v[opt_unknown++]=tmp2;
+int getopt(int argc, char * const argv[], const char *optstring) {
+ static int lastidx=0,lastofs=0;
+ char *tmp;
+ if (optind==0) optind=1; /* whoever started setting optind to 0 should be shot */
+again:
+ if (optind>argc || !argv[optind] || *argv[optind]!='-' || argv[optind][1]==0)
+ return -1;
+ if (argv[optind][1]=='-' && argv[optind][2]==0) {
+ ++optind;
+ return -1;
}
-}
-
-static char* nextchar;
-int getopt(int c,char*v[],char*o)
-{
- int ol=strlen(o);
- int ret=0;
- int oi=0;
-
- optarg=0;
-
- while (nextchar || (optind<c))
- {
- if (nextchar)
- {
- if ((ret=(*(++nextchar))))
- {
- switch (getopt_check(ret,o,ol))
- {
- case 1:
- if (*(++nextchar))
- optarg=nextchar;
- else
- {
- if (optind<c)
- {
- oi=1;
- optarg=v[optind++];
- }
- else
- ret='?';
- }
- nextchar=0;
- case 0:
- if (!nextchar)
- getopt_sort(v,oi);
- else
- if (!(*(nextchar+1)))
- getopt_sort(v,oi);
- return ret;
- break;
- default:
- return '?';
- break;
- }
- }
- else
- nextchar=0;
+ if (lastidx!=optind) {
+ lastidx=optind; lastofs=0;
+ }
+ optopt=argv[optind][lastofs+1];
+ if ((tmp=strchr(optstring,optopt))) {
+ if (*tmp==0) { /* apparently, we looked for \0, i.e. end of argument */
+ ++optind;
+ goto again;
}
- else
- {
- if ((v[optind][0]=='-')&&((v[optind][1]!=0)))
- {
- if ((v[optind][1]=='-')&&(v[optind][2]==0))
- {
- getopt_sort(v,oi);
- optind=opt_unknown;
- return -1;
- }
- else
- {
- nextchar=v[optind];
- }
+ if (tmp[1]==':') { /* argument expected */
+ if (tmp[2]==':' || argv[optind][lastofs+2]) { /* "-foo", return "oo" as optarg */
+ if (!*(optarg=argv[optind]+lastofs+2)) optarg=0;
+ goto found;
}
- else
- {
- ++opt_unknown_len;
+ optarg=argv[optind+1];
+ if (!optarg) { /* missing argument */
+ ++optind;
+ if (*optstring==':') return ':';
+ getopterror(1);
+ return ':';
}
++optind;
+ } else {
+ ++lastofs;
+ return optopt;
}
+found:
+ ++optind;
+ return optopt;
+ } else { /* not found */
+ getopterror(0);
+ ++optind;
+ return '?';
}
- optind=opt_unknown;
- return -1;
}
diff --git a/mdk-stage1/dietlibc/lib/getpeername.c b/mdk-stage1/dietlibc/lib/getpeername.c
index 8adcddf55..c806c720d 100644
--- a/mdk-stage1/dietlibc/lib/getpeername.c
+++ b/mdk-stage1/dietlibc/lib/getpeername.c
@@ -1,13 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
+#include <sys/socket.h>
extern int socketcall(int callno,long* args);
-int getpeername(int a, void * b, int c) {
-#ifdef __i386__
- return socketcall(SYS_GETPEERNAME, (long*)&a);
-#else
- unsigned long args[] = { a, (long) b, c };
+int __libc_getpeername(int a, void * b, int *c);
+int __libc_getpeername(int a, void * b, int *c) {
+ unsigned long args[] = { a, (long) b, (long) c };
return socketcall(SYS_GETPEERNAME, args);
-#endif
}
+int getpeername(int a, struct sockaddr* b, socklen_t *c) __attribute__((weak,alias("__libc_getpeername")));
diff --git a/mdk-stage1/dietlibc/lib/getservent.c b/mdk-stage1/dietlibc/lib/getservent.c
deleted file mode 100644
index bf25d046b..000000000
--- a/mdk-stage1/dietlibc/lib/getservent.c
+++ /dev/null
@@ -1,137 +0,0 @@
-#include <unistd.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <sys/mman.h>
-#include <errno.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <ctype.h>
-
-static int servicesfd=-1;
-static char* servicesmap;
-static unsigned int serviceslen;
-
-static char* aliases[10];
-
-static char *cur;
-
-/* nameserver 42/tcp name # IEN 116 */
-struct servent *getservent(void) {
- static struct servent se;
- char *last;
- int aliasidx;
- if (servicesfd<0) {
- servicesfd=open(_PATH_SERVICES,O_RDONLY);
- if (servicesfd<0) return 0;
- serviceslen=lseek(servicesfd,0,SEEK_END);
- servicesmap=mmap(0,serviceslen,PROT_READ|PROT_WRITE,MAP_PRIVATE,servicesfd,0);
- if ((long)servicesmap==(-1)) goto error;
- cur=servicesmap;
- }
- last=servicesmap+serviceslen;
-again:
- se.s_name=0;
- se.s_aliases=aliases; aliases[0]=0;
- se.s_port=0;
- se.s_proto=0;
- if (cur>=last) return 0;
- if (*cur=='#' || *cur=='\n') goto parseerror;
- /* first, the primary name */
- if (!isalpha(*cur)) goto parseerror;
- se.s_name=cur;
- se.s_aliases=aliases;
- while (cur<last && isalnum(*cur)) cur++;
- if (cur>=last) return 0;
- if (*cur=='\n') goto parseerror;
- *cur=0; cur++;
- /* second, the port */
- while (cur<last && isblank(*cur)) cur++;
- while (cur<last && isdigit(*cur)) {
- se.s_port=se.s_port*10+*cur-'0';
- cur++;
- }
- se.s_port=htons(se.s_port);
- if (cur>=last) return 0;
- /* third, "/tcp" or "/udp" */
- if (*cur!='/') goto parseerror;
- cur++;
- se.s_proto=cur;
- while (cur<last && isalpha(*cur)) ++cur;
- if (cur>=last) return 0;
- if (*cur=='\n') { *cur++=0; return &se; }
- *cur=0; cur++;
- /* now the aliases */
- for (aliasidx=0;aliasidx<10;++aliasidx) {
- while (cur<last && isblank(*cur)) ++cur;
- aliases[aliasidx]=cur;
- while (cur<last && isalpha(*cur)) ++cur;
- if (*cur=='\n') { *cur++=0; ++aliasidx; break; }
- if (cur>=last || !isblank(*cur)) break;
- *cur++=0;
- }
- aliases[aliasidx]=0;
- return &se;
-parseerror:
- while (cur<last && *cur!='\n') cur++;
- cur++;
- goto again;
-error:
- if (servicesmap!=(char*)-1) munmap(servicesmap,serviceslen);
- if (servicesfd!=-1) close(servicesfd);
- servicesmap=(char*)-1;
- servicesfd=-1;
- errno=ENOMEM;
- return 0;
-}
-
-void setservent(int stayopen) {
- cur=servicesmap;
-}
-
-struct servent *getservbyname(const char *name, const char *proto) {
- struct servent *s;
- setservent(0);
- for (s=getservent(); s; s=getservent()) {
- char **tmp;
-#if 0
- write(1,"found ",6);
- write(1,s->s_name,strlen(s->s_name));
- write(1,"/",1);
- write(1,s->s_proto,strlen(s->s_proto));
- write(1,"\n",1);
- if (!strcmp(name,"auth")) {
- tmp=s->s_aliases;
- write(1," aka ",5);
- while (*tmp) {
- write(1,*tmp,strlen(*tmp));
- write(1,", ",2);
- ++tmp;
- }
- write(1,"\n",1);
- }
-#endif
- if (!strcmp(name,s->s_name) && !strcmp(proto,s->s_proto))
- return s;
- tmp=s->s_aliases;
- while (*tmp)
- if (!strcmp(name,*tmp++)) return s;
- }
- return 0;
-}
-
-struct servent *getservbyport(int port, const char *proto) {
- struct servent *s;
- for (s=getservent(); s; s=getservent()) {
- if (port==s->s_port && !strcmp(proto,s->s_proto))
- return s;
- }
- return 0;
-}
-
-void endservent(void) {
- if (servicesmap!=(char*)-1) munmap(servicesmap,serviceslen);
- if (servicesfd!=-1) close(servicesfd);
- servicesmap=(char*)-1;
- servicesfd=-1;
-}
-
diff --git a/mdk-stage1/dietlibc/lib/getsockname.c b/mdk-stage1/dietlibc/lib/getsockname.c
index 61a3ea24a..6c81e317d 100644
--- a/mdk-stage1/dietlibc/lib/getsockname.c
+++ b/mdk-stage1/dietlibc/lib/getsockname.c
@@ -1,13 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
+#include <sys/socket.h>
extern int socketcall(int callno,long* args);
-int getsockname(int a, void * b, int c) {
-#ifdef __i386__
- return socketcall(SYS_GETSOCKNAME, (long*)&a);
-#else
+int __libc_getsockname(int a, void * b, int c);
+int __libc_getsockname(int a, void * b, int c) {
unsigned long args[] = { a, (long) b, c };
return socketcall(SYS_GETSOCKNAME, args);
-#endif
}
+int getsockname(int a, struct sockaddr* b, socklen_t* c) __attribute__((weak,alias("__libc_getsockname")));
diff --git a/mdk-stage1/dietlibc/lib/getsockopt.c b/mdk-stage1/dietlibc/lib/getsockopt.c
index 8c0a57557..05b71d77d 100644
--- a/mdk-stage1/dietlibc/lib/getsockopt.c
+++ b/mdk-stage1/dietlibc/lib/getsockopt.c
@@ -1,13 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
+#include <sys/socket.h>
extern int socketcall(int callno,long* args);
-int getsockopt(int a, int b, int c, void *d, int e) {
-#ifdef __i386__
- return socketcall(SYS_GETSOCKOPT, (long*)&a);
-#else
+int __libc_getsockopt(int a, int b, int c, void *d, int e);
+int __libc_getsockopt(int a, int b, int c, void *d, int e) {
unsigned long args[] = { a, b, c, (long)d, e };
return socketcall(SYS_GETSOCKOPT, args);
-#endif
}
+int getsockopt(int s, int level, int optname, void * optval, socklen_t *optlen) __attribute__((weak,alias("__libc_getsockopt")));
diff --git a/mdk-stage1/dietlibc/lib/htonl.c b/mdk-stage1/dietlibc/lib/htonl.c
index 490de42c4..4096d04f5 100644
--- a/mdk-stage1/dietlibc/lib/htonl.c
+++ b/mdk-stage1/dietlibc/lib/htonl.c
@@ -1,6 +1,7 @@
#include <endian.h>
+#include <netinet/in.h>
-unsigned long int htonl(unsigned long int hostlong) {
+uint32_t htonl(uint32_t hostlong) {
#if __BYTE_ORDER==__LITTLE_ENDIAN
return (hostlong>>24) | ((hostlong&0xff0000)>>8) |
((hostlong&0xff00)<<8) | (hostlong<<24);
@@ -9,4 +10,4 @@ unsigned long int htonl(unsigned long int hostlong) {
#endif
}
-unsigned long int ntohl(unsigned long int hostlong) __attribute__((weak,alias("htonl")));
+uint32_t ntohl(uint32_t hostlong) __attribute__((weak,alias("htonl")));
diff --git a/mdk-stage1/dietlibc/lib/htons.c b/mdk-stage1/dietlibc/lib/htons.c
index 765d3bbc5..a64483174 100644
--- a/mdk-stage1/dietlibc/lib/htons.c
+++ b/mdk-stage1/dietlibc/lib/htons.c
@@ -1,6 +1,7 @@
#include <endian.h>
+#include <netinet/in.h>
-unsigned short int htons(unsigned short int hostshort) {
+uint16_t htons(uint16_t hostshort) {
#if __BYTE_ORDER==__LITTLE_ENDIAN
return ((hostshort>>8)&0xff) | (hostshort<<8);
#else
@@ -8,4 +9,4 @@ unsigned short int htons(unsigned short int hostshort) {
#endif
}
-unsigned short int ntohs(unsigned short int hostshort) __attribute__((weak,alias("htons")));
+uint16_t ntohs(uint16_t hostshort) __attribute__((weak,alias("htons")));
diff --git a/mdk-stage1/dietlibc/lib/if_indextoname.c b/mdk-stage1/dietlibc/lib/if_indextoname.c
index e683755f6..cc3111ed8 100644
--- a/mdk-stage1/dietlibc/lib/if_indextoname.c
+++ b/mdk-stage1/dietlibc/lib/if_indextoname.c
@@ -1,5 +1,4 @@
-#include <linux/if.h>
-#include <linux/net.h>
+#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/socket.h>
diff --git a/mdk-stage1/dietlibc/lib/if_nametoindex.c b/mdk-stage1/dietlibc/lib/if_nametoindex.c
index 5923963b1..c9dd1b051 100644
--- a/mdk-stage1/dietlibc/lib/if_nametoindex.c
+++ b/mdk-stage1/dietlibc/lib/if_nametoindex.c
@@ -1,5 +1,4 @@
-#include <linux/if.h>
-#include <linux/net.h>
+#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/socket.h>
@@ -8,7 +7,7 @@
#define SOCK_DGRAM 2
#endif
-int if_nametoindex(char* blub) {
+unsigned int if_nametoindex(const char* blub) {
struct ifreq ifr;
int fd;
char *tmp;
diff --git a/mdk-stage1/dietlibc/lib/isalnum.c b/mdk-stage1/dietlibc/lib/isalnum.c
index 4d363fab9..3192cc20d 100644
--- a/mdk-stage1/dietlibc/lib/isalnum.c
+++ b/mdk-stage1/dietlibc/lib/isalnum.c
@@ -1,7 +1,9 @@
#include <ctype.h>
-int __isalnum_ascii(int c) {
- return isalpha(c) || isdigit(c);
+int __isalnum_ascii ( int ch );
+int __isalnum_ascii ( int ch ) {
+ return (unsigned int)((ch | 0x20) - 'a') < 26u ||
+ (unsigned int)( ch - '0') < 10u;
}
-int isalnum(int c) __attribute__((weak,alias("__isalnum_ascii")));
+int isalnum ( int ch ) __attribute__((weak,alias("__isalnum_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isalpha.c b/mdk-stage1/dietlibc/lib/isalpha.c
index 07ea69696..efd8b1940 100644
--- a/mdk-stage1/dietlibc/lib/isalpha.c
+++ b/mdk-stage1/dietlibc/lib/isalpha.c
@@ -1,7 +1,8 @@
#include <ctype.h>
-int __isalpha_ascii(int c) {
- return (c>='a' && c<='z') || (c>='A' && c<='Z');
+int __isalpha_ascii ( int ch );
+int __isalpha_ascii ( int ch ) {
+ return (unsigned int)((ch | 0x20) - 'a') < 26u;
}
-int isalpha(int c) __attribute__((weak,alias("__isalpha_ascii")));
+int isalpha ( int ch ) __attribute__((weak,alias("__isalpha_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isascii.c b/mdk-stage1/dietlibc/lib/isascii.c
index e06994783..57a23ba44 100644
--- a/mdk-stage1/dietlibc/lib/isascii.c
+++ b/mdk-stage1/dietlibc/lib/isascii.c
@@ -1,6 +1,6 @@
#include <ctype.h>
-int isascii(int c) {
- return (c & 0x80) == 0;
+int isascii ( int ch )
+{
+ return (unsigned int)ch < 128u;
}
-
diff --git a/mdk-stage1/dietlibc/lib/isatty.c b/mdk-stage1/dietlibc/lib/isatty.c
index b31609720..4858a364d 100644
--- a/mdk-stage1/dietlibc/lib/isatty.c
+++ b/mdk-stage1/dietlibc/lib/isatty.c
@@ -2,11 +2,11 @@
#include <termios.h>
#undef ioctl
#include <sys/ioctl.h>
+#include <errno.h>
+#include <unistd.h>
+#include "dietfeatures.h"
-extern int errno;
-
-int isatty(int fd)
-{
+int isatty(int fd) {
int save;
int is_tty;
struct termios term;
diff --git a/mdk-stage1/dietlibc/lib/isblank.c b/mdk-stage1/dietlibc/lib/isblank.c
index 034e543b1..f13ca304a 100644
--- a/mdk-stage1/dietlibc/lib/isblank.c
+++ b/mdk-stage1/dietlibc/lib/isblank.c
@@ -1,6 +1,6 @@
#include <ctype.h>
-int isblank(int ch)
+int isblank ( int ch )
{
- return ((ch==' ')||(ch=='\t'));
+ return ch == ' ' || ch == '\t';
}
diff --git a/mdk-stage1/dietlibc/lib/iscntrl.c b/mdk-stage1/dietlibc/lib/iscntrl.c
index a183a54f1..98dbae6f3 100644
--- a/mdk-stage1/dietlibc/lib/iscntrl.c
+++ b/mdk-stage1/dietlibc/lib/iscntrl.c
@@ -1,7 +1,8 @@
#include <ctype.h>
-int __iscntrl_ascii(int c) {
- return (c<32) || (c==127);
+int __iscntrl_ascii ( int ch );
+int __iscntrl_ascii ( int ch ) {
+ return (unsigned int)ch < 32u || ch == 127;
}
-int iscntrl(int c) __attribute__((weak,alias("__iscntrl_ascii")));
+int iscntrl ( int ch ) __attribute__((weak,alias("__iscntrl_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isdigit.c b/mdk-stage1/dietlibc/lib/isdigit.c
index 8a7c3e801..d04d281a8 100644
--- a/mdk-stage1/dietlibc/lib/isdigit.c
+++ b/mdk-stage1/dietlibc/lib/isdigit.c
@@ -1,7 +1,8 @@
#include <ctype.h>
-int __isdigit_ascii(int c) {
- return (c>='0' && c<='9');
+int __isdigit_ascii ( int ch );
+int __isdigit_ascii ( int ch ) {
+ return (unsigned int)(ch - '0') < 10u;
}
-int isdigit(int c) __attribute__((weak,alias("__isdigit_ascii")));
+int isdigit ( int ch ) __attribute__((weak,alias("__isdigit_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isgraph.c b/mdk-stage1/dietlibc/lib/isgraph.c
index 807f90ff0..83419e788 100644
--- a/mdk-stage1/dietlibc/lib/isgraph.c
+++ b/mdk-stage1/dietlibc/lib/isgraph.c
@@ -1,5 +1,6 @@
-int __isgraph_ascii(int c) {
- return (c>=33 && c<=126);
+int __isgraph_ascii ( int ch );
+int __isgraph_ascii ( int ch ) {
+ return (unsigned int)(ch - '!') < 127u - '!';
}
-int isgraph(int c) __attribute__((weak,alias("__isgraph_ascii")));
+int isgraph ( int ch ) __attribute__((weak,alias("__isgraph_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/islower.c b/mdk-stage1/dietlibc/lib/islower.c
index 6a9afa519..8d219c43a 100644
--- a/mdk-stage1/dietlibc/lib/islower.c
+++ b/mdk-stage1/dietlibc/lib/islower.c
@@ -1,7 +1,8 @@
#include <ctype.h>
-int __islower_ascii(int c) {
- return (c>='a' && c<='z');
+int __islower_ascii ( int ch );
+int __islower_ascii ( int ch ) {
+ return (unsigned int) (ch - 'a') < 26u;
}
-int islower(int c) __attribute__((weak,alias("__islower_ascii")));
+int islower ( int ch ) __attribute__((weak,alias("__islower_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isprint.c b/mdk-stage1/dietlibc/lib/isprint.c
index e980658a5..51ff5b6ee 100644
--- a/mdk-stage1/dietlibc/lib/isprint.c
+++ b/mdk-stage1/dietlibc/lib/isprint.c
@@ -1,6 +1,7 @@
-int __isprint_ascii(int c) {
- return (c>=32 && c<=126);
+int __isprint_ascii ( int ch );
+int __isprint_ascii ( int ch ) {
+ return (unsigned int)(ch - ' ') < 127u - ' ';
}
-int isprint(int c) __attribute__((weak,alias("__isprint_ascii")));
+int isprint ( int ch ) __attribute__((weak,alias("__isprint_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/ispunct.c b/mdk-stage1/dietlibc/lib/ispunct.c
index 2fd1183b1..8bbfcdb5a 100644
--- a/mdk-stage1/dietlibc/lib/ispunct.c
+++ b/mdk-stage1/dietlibc/lib/ispunct.c
@@ -1,7 +1,9 @@
#include <ctype.h>
-int __ispunct_ascii(int c) {
- return isprint(c) && !( isalnum(c) || isspace(c) );
+int __ispunct_ascii ( int ch );
+int __ispunct_ascii ( int ch )
+{
+ return isprint (ch) && !isalnum (ch) && !isspace (ch);
}
-int ispunct(int c) __attribute__((weak,alias("__ispunct_ascii")));
+int ispunct ( int ch ) __attribute__((weak,alias("__ispunct_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isspace.c b/mdk-stage1/dietlibc/lib/isspace.c
index 916f784ab..bfa46648b 100644
--- a/mdk-stage1/dietlibc/lib/isspace.c
+++ b/mdk-stage1/dietlibc/lib/isspace.c
@@ -1,8 +1,9 @@
#include <ctype.h>
-int __isspace_ascii(int ch)
+int __isspace_ascii ( int ch );
+int __isspace_ascii ( int ch )
{
- return ((ch==' ')||(ch=='\f')||(ch=='\t')||(ch=='\v')||(ch=='\r')||(ch=='\n'));
+ return (unsigned int)(ch - 9) < 5u || ch == ' ';
}
-int isspace(int c) __attribute__((weak,alias("__isspace_ascii")));
+int isspace ( int ch ) __attribute__((weak,alias("__isspace_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isupper.c b/mdk-stage1/dietlibc/lib/isupper.c
index 933dfc169..048d4fd6c 100644
--- a/mdk-stage1/dietlibc/lib/isupper.c
+++ b/mdk-stage1/dietlibc/lib/isupper.c
@@ -1,7 +1,9 @@
#include <ctype.h>
-int __isupper_ascii(int c) {
- return (c>='A' && c<='Z');
+int __isupper_ascii ( int ch );
+int __isupper_ascii ( int ch )
+{
+ return (unsigned int)(ch - 'A') < 26u;
}
-int isupper(int c) __attribute__((weak,alias("__isupper_ascii")));
+int isupper ( int ch ) __attribute__((weak,alias("__isupper_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/isxdigit.c b/mdk-stage1/dietlibc/lib/isxdigit.c
index 22a711f2d..bca8e74ac 100644
--- a/mdk-stage1/dietlibc/lib/isxdigit.c
+++ b/mdk-stage1/dietlibc/lib/isxdigit.c
@@ -1,5 +1,8 @@
-int __isxdigit_ascii(int c) {
- return ((c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f'));
+int __isxdigit_ascii ( int ch );
+int __isxdigit_ascii ( int ch )
+{
+ return (unsigned int)( ch - '0') < 10u ||
+ (unsigned int)((ch | 0x20) - 'a') < 6u;
}
-int isxdigit(int c) __attribute__((weak,alias("__isxdigit_ascii")));
+int isxdigit ( int ch ) __attribute__((weak,alias("__isxdigit_ascii")));
diff --git a/mdk-stage1/dietlibc/lib/listen.c b/mdk-stage1/dietlibc/lib/listen.c
index 56a51be06..661b86a63 100644
--- a/mdk-stage1/dietlibc/lib/listen.c
+++ b/mdk-stage1/dietlibc/lib/listen.c
@@ -1,13 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
+#include <sys/socket.h>
extern int socketcall(int callno,long* args);
-int listen(int a, int b) {
-#ifdef __i386__
- return socketcall(SYS_LISTEN, (long*)&a);
-#else
+int __libc_listen(int a, int b);
+int __libc_listen(int a, int b) {
unsigned long args[] = { a, b, 0 };
return socketcall(SYS_LISTEN, args);
-#endif
}
+int listen(int s, int backlog) __attribute__((weak,alias("__libc_listen")));
diff --git a/mdk-stage1/dietlibc/lib/lockf.c b/mdk-stage1/dietlibc/lib/lockf.c
index 2b1d004ef..1b6fcb80c 100644
--- a/mdk-stage1/dietlibc/lib/lockf.c
+++ b/mdk-stage1/dietlibc/lib/lockf.c
@@ -2,6 +2,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
+#include "dietfeatures.h"
int lockf(int fd, int cmd, off_t len) {
struct flock fl;
diff --git a/mdk-stage1/dietlibc/lib/longjmp.c b/mdk-stage1/dietlibc/lib/longjmp.c
index d0a5719c1..6f1afb4b1 100644
--- a/mdk-stage1/dietlibc/lib/longjmp.c
+++ b/mdk-stage1/dietlibc/lib/longjmp.c
@@ -1,51 +1,16 @@
-/* Copyright (C) 1991, 92, 94, 95, 97, 98 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
-
- 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.
-
- 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.
-
- 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., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA. */
-
-/* #include <stddef.h> */
#include <setjmp.h>
#include <signal.h>
-extern int __sigprocmask(int how,void* set,void* oldset);
-extern void __longjmp(void* env, int val);
+void __longjmp(void*env,int val);
-/*
-extern void _longjmp_unwind (jmp_buf env, int val);
-*/
-/* Set the signal mask to the one specified in ENV, and jump
- to the position specified in ENV, causing the setjmp
- call there to return VAL, or 1 if VAL is 0. */
-void __siglongjmp (sigjmp_buf env, int val)
-{
- /* Perform any cleanups needed by the frames being unwound. */
- /*
- _longjmp_unwind (env, val);
- */
-
- if (env[0].__mask_was_saved)
- /* Restore the saved signal mask. */
- (void) __sigprocmask (SIG_SETMASK, &env[0].__saved_mask,
- (sigset_t *) NULL);
-
- /* Call the machine-dependent function to restore machine state. */
- __longjmp (env[0].__jmpbuf, val ?: 1);
+void __libc_longjmp(sigjmp_buf env,int val);
+void __libc_longjmp(sigjmp_buf env,int val) {
+ if (env[0].__mask_was_saved) {
+ sigprocmask(SIG_SETMASK,(sigset_t*)&env[0].__saved_mask,0);
+ }
+ if (val==0) val=1;
+ __longjmp(env[0].__jmpbuf,val);
}
-
-void siglongjmp (sigjmp_buf env, int val) __attribute__((weak,alias("__siglongjmp")));
-void longjmp (sigjmp_buf env, int val) __attribute__((weak,alias("__siglongjmp")));
-void _longjmp (sigjmp_buf env, int val) __attribute__((weak,alias("__siglongjmp")));
-void __libc_longjmp (sigjmp_buf env, int val) __attribute__((weak,alias("__siglongjmp")));
+void __siglongjmp(sigjmp_buf env,int val) __attribute__((alias("__libc_longjmp")));
+void longjmp(sigjmp_buf env,int val) __attribute__((weak,alias("__libc_longjmp")));
+void siglongjmp(sigjmp_buf env,int val) __attribute__((weak,alias("__libc_longjmp")));
diff --git a/mdk-stage1/dietlibc/lib/lseek64.c b/mdk-stage1/dietlibc/lib/lseek64.c
index f39e779f9..62acf4a2b 100644
--- a/mdk-stage1/dietlibc/lib/lseek64.c
+++ b/mdk-stage1/dietlibc/lib/lseek64.c
@@ -1,22 +1,16 @@
+#include <errno.h>
#include "dietfeatures.h"
-#ifdef WANT_LARGEFILE_BACKCOMPAT
#include <sys/stat.h>
#ifndef __NO_STAT64
-#include <errno.h>
#include <unistd.h>
loff_t lseek64(int fildes, loff_t offset, int whence) {
loff_t tmp;
- if (llseek(fildes,offset>>32,offset&0xffffffff,&tmp,whence)) {
-#ifdef WANT_THREAD_SAFE
- if (*__errno_location()!=ENOSYS) return -1;
-#else
+ if (llseek(fildes,(unsigned long)(offset>>32),(unsigned long)offset&0xffffffff,&tmp,whence)) {
if (errno!=ENOSYS) return -1;
-#endif
return (loff_t)lseek(fildes,(off_t)offset,whence);
}
return tmp;
}
#endif
-#endif
diff --git a/mdk-stage1/dietlibc/lib/memccmp.c b/mdk-stage1/dietlibc/lib/memccmp.c
deleted file mode 100644
index 3a8c14f60..000000000
--- a/mdk-stage1/dietlibc/lib/memccmp.c
+++ /dev/null
@@ -1,17 +0,0 @@
-#define _POSIX_SOURCE
-#define _XOPEN_SOURCE
-#include <sys/types.h>
-
-int memccmp(const void *dst, const void *src, int c, size_t count)
-{
- register const char *a = dst;
- register const char *b = src;
- while (count--)
- {
- register int res=(*a - *b);
- if (res) return res;
- if (*a==c) return 0;
- ++a; ++b;
- }
- return 0;
-}
diff --git a/mdk-stage1/dietlibc/lib/memccpy.c b/mdk-stage1/dietlibc/lib/memccpy.c
index 6d6ac98ab..3208cfabb 100644
--- a/mdk-stage1/dietlibc/lib/memccpy.c
+++ b/mdk-stage1/dietlibc/lib/memccpy.c
@@ -1,6 +1,7 @@
#define _POSIX_SOURCE
#define _XOPEN_SOURCE
#include <sys/types.h>
+#include <string.h>
void *memccpy(void *dst, const void *src, int c, size_t count)
{
diff --git a/mdk-stage1/dietlibc/lib/memchr.c b/mdk-stage1/dietlibc/lib/memchr.c
index 93d81328a..ae76bb150 100644
--- a/mdk-stage1/dietlibc/lib/memchr.c
+++ b/mdk-stage1/dietlibc/lib/memchr.c
@@ -1,9 +1,13 @@
#include <sys/types.h>
+#include <string.h>
-char *memchr(const char *s, int c, size_t n) {
+void* memchr(const void *s, int c, size_t n) {
+ register const char* t=s;
int i;
- for (i=n; i; --i)
- if (*s++==c)
- return (char*)s;
+ for (i=n; i; --i) {
+ if (*t==c)
+ return (char*)t;
+ ++t;
+ }
return 0;
}
diff --git a/mdk-stage1/dietlibc/lib/memcmp.c b/mdk-stage1/dietlibc/lib/memcmp.c
index c05a46863..4c8f77142 100644
--- a/mdk-stage1/dietlibc/lib/memcmp.c
+++ b/mdk-stage1/dietlibc/lib/memcmp.c
@@ -1,5 +1,7 @@
#include <sys/types.h>
+/* gcc is broken and has a non-SUSv2 compliant internal prototype.
+ * This causes it to warn about a type mismatch here. Ignore it. */
int memcmp(const void *dst, const void *src, size_t count) {
register int r;
register const char *d=dst;
diff --git a/mdk-stage1/dietlibc/lib/memcpy.c b/mdk-stage1/dietlibc/lib/memcpy.c
index 0c688b509..ebfd1e60a 100644
--- a/mdk-stage1/dietlibc/lib/memcpy.c
+++ b/mdk-stage1/dietlibc/lib/memcpy.c
@@ -1,5 +1,7 @@
#include <sys/types.h>
+/* gcc is broken and has a non-SUSv2 compliant internal prototype.
+ * This causes it to warn about a type mismatch here. Ignore it. */
void* memcpy(void* dst, const void* src, size_t count) {
register char *d=dst;
register const char *s=src;
diff --git a/mdk-stage1/dietlibc/lib/memmove.c b/mdk-stage1/dietlibc/lib/memmove.c
index 7adb2be91..58414d503 100644
--- a/mdk-stage1/dietlibc/lib/memmove.c
+++ b/mdk-stage1/dietlibc/lib/memmove.c
@@ -1,6 +1,7 @@
#define _POSIX_SOURCE
#define _XOPEN_SOURCE
#include <sys/types.h>
+#include <string.h>
void *memmove(void *dst, const void *src, size_t count)
{
diff --git a/mdk-stage1/dietlibc/lib/memset.c b/mdk-stage1/dietlibc/lib/memset.c
index 5f9ae49ee..a019eca44 100644
--- a/mdk-stage1/dietlibc/lib/memset.c
+++ b/mdk-stage1/dietlibc/lib/memset.c
@@ -1,6 +1,8 @@
#include <sys/types.h>
-void * memset(void * dst, int s, size_t count) {
+/* gcc is broken and has a non-SUSv2 compliant internal prototype.
+ * This causes it to warn about a type mismatch here. Ignore it. */
+void* memset(void * dst, int s, size_t count) {
register char * a = dst;
count++; /* this actually creates smaller code than using count-- */
while (--count)
diff --git a/mdk-stage1/dietlibc/lib/mkfifo.c b/mdk-stage1/dietlibc/lib/mkfifo.c
index 596efbe27..1bd132e65 100644
--- a/mdk-stage1/dietlibc/lib/mkfifo.c
+++ b/mdk-stage1/dietlibc/lib/mkfifo.c
@@ -1,6 +1,6 @@
-#include <linux/stat.h>
+#include <sys/stat.h>
#include <unistd.h>
int mkfifo(const char *fn,mode_t mode) {
- return mknod(fn,S_IFIFO|mode,0);
+ return mknod(fn,(mode_t)(mode|S_IFIFO),0);
}
diff --git a/mdk-stage1/dietlibc/lib/msgctl.c b/mdk-stage1/dietlibc/lib/msgctl.c
index 19ea6d8cf..51cffdef0 100644
--- a/mdk-stage1/dietlibc/lib/msgctl.c
+++ b/mdk-stage1/dietlibc/lib/msgctl.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,int,int,void*);
int msgctl(int msqid, int cmd, struct msqid_ds *buf) {
return __ipc(MSGCTL,msqid,cmd,0,buf);
diff --git a/mdk-stage1/dietlibc/lib/msgget.c b/mdk-stage1/dietlibc/lib/msgget.c
index 9bca09ebe..6023c1786 100644
--- a/mdk-stage1/dietlibc/lib/msgget.c
+++ b/mdk-stage1/dietlibc/lib/msgget.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,key_t,int,int,int);
int msgget(key_t key,int flag) {
return __ipc(MSGGET,key,flag,0,0);
diff --git a/mdk-stage1/dietlibc/lib/msgrcv.c b/mdk-stage1/dietlibc/lib/msgrcv.c
index b4ae9cc6c..8168187a5 100644
--- a/mdk-stage1/dietlibc/lib/msgrcv.c
+++ b/mdk-stage1/dietlibc/lib/msgrcv.c
@@ -1,9 +1,13 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,size_t,int,void*);
+
+struct ipc_kludge {
+ struct msgbuf *msgp;
+ long msgtyp;
+};
int msgrcv(int msqid, void *msgp, size_t msgsz, long int msgtyp, int msgflg) {
struct ipc_kludge tmp;
diff --git a/mdk-stage1/dietlibc/lib/msgsnd.c b/mdk-stage1/dietlibc/lib/msgsnd.c
index 6203053f0..6e9ef445e 100644
--- a/mdk-stage1/dietlibc/lib/msgsnd.c
+++ b/mdk-stage1/dietlibc/lib/msgsnd.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,size_t,int,const void*);
int msgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg) {
return __ipc(MSGSND,msqid, msgsz, msgflg, msgp);
diff --git a/mdk-stage1/dietlibc/lib/nop.c b/mdk-stage1/dietlibc/lib/nop.c
deleted file mode 100644
index c05bea813..000000000
--- a/mdk-stage1/dietlibc/lib/nop.c
+++ /dev/null
@@ -1,7 +0,0 @@
-
-int __fflush_stdin() __attribute__((weak,alias("__return0")));
-int __fflush_stdout() __attribute__((weak,alias("__return0")));
-int __fflush_stderr() __attribute__((weak,alias("__return0")));
-
-/* used for weak aliases */
-int __return0() { return 0; }
diff --git a/mdk-stage1/dietlibc/lib/open64.c b/mdk-stage1/dietlibc/lib/open64.c
index f1499d017..2599e2259 100644
--- a/mdk-stage1/dietlibc/lib/open64.c
+++ b/mdk-stage1/dietlibc/lib/open64.c
@@ -1,14 +1,13 @@
-#include <linux/types.h>
-#include <linux/fcntl.h>
+#include <sys/types.h>
+#include <fcntl.h>
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
-extern int open(const char* file,int oflag,int mode);
-
+int __libc_open64(const char* file,int oflag,int mode);
int __libc_open64(const char* file,int oflag,int mode) {
return open(file,oflag|O_LARGEFILE,mode);
}
-int open64(const char* file,int oflag,int mode) __attribute__((weak,alias("__libc_open64")));
+int open64(const char* file,int oflag,...) __attribute__((weak,alias("__libc_open64")));
diff --git a/mdk-stage1/dietlibc/lib/opendir.c b/mdk-stage1/dietlibc/lib/opendir.c
index 5c0c4a305..2530d1ac9 100644
--- a/mdk-stage1/dietlibc/lib/opendir.c
+++ b/mdk-stage1/dietlibc/lib/opendir.c
@@ -1,18 +1,26 @@
#include "dietdirent.h"
+#include <sys/mman.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <fcntl.h>
-DIR *opendir (const char *name) {
- DIR *t=(DIR*)malloc(sizeof(DIR));
- if (t) {
- if ((t->fd=open(name,O_RDONLY|O_DIRECTORY))>=0) {
- t->num=t->cur=0;
- } else {
- free(t);
- t=0;
- }
+DIR* opendir ( const char* name ) {
+ int fd = open (name, O_RDONLY | O_DIRECTORY);
+ DIR* t = NULL;
+
+ if ( fd >= 0 ) {
+ if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
+ goto lose;
+ t = (DIR *) mmap (NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (t == MAP_FAILED)
+lose:
+ close (fd);
+ else
+ t->fd = fd;
}
+
+
return t;
}
diff --git a/mdk-stage1/dietlibc/lib/perror.c b/mdk-stage1/dietlibc/lib/perror.c
index ecab7250b..0a45fb9d6 100644
--- a/mdk-stage1/dietlibc/lib/perror.c
+++ b/mdk-stage1/dietlibc/lib/perror.c
@@ -1,22 +1,24 @@
#include "dietfeatures.h"
#include <unistd.h>
#include <string.h>
+#include <stdio.h>
+
+#define _BSD_SOURCE
#include <errno.h>
-extern char *sys_errlist[];
-extern int sys_nerr;
-extern int errno;
+extern const char __sys_err_unknown [];
+
+void perror ( const char* prepend )
+{
+ register const char* message = __sys_err_unknown;
+
+ if ( (unsigned int) errno < (unsigned int) __SYS_NERR )
+ message = sys_errlist [errno];
-void perror(const char *s) {
- register char *message="[unknown error]";
- write(2,s,strlen(s));
- write(2,": ",2);
- if (errno>=0 && errno<sys_nerr)
-#ifdef WANT_THREAD_SAFE
- message=sys_errlist[*__errno_location()];
-#else
- message=sys_errlist[errno];
-#endif
- write(2,message,strlen(message));
- write(2,"\n",1);
+ if (prepend) {
+ write ( 2, prepend, strlen(prepend) );
+ write ( 2, ": ", 2 );
+ }
+ write ( 2, message, strlen(message) );
+ write ( 2, "\n", 1 );
}
diff --git a/mdk-stage1/dietlibc/lib/pread.c b/mdk-stage1/dietlibc/lib/pread.c
index b922aa93e..fd6c49149 100644
--- a/mdk-stage1/dietlibc/lib/pread.c
+++ b/mdk-stage1/dietlibc/lib/pread.c
@@ -3,6 +3,7 @@
extern size_t __pread(int fd, void *buf, size_t count, off_t a,off_t b);
+size_t __libc_pread(int fd, void *buf, size_t count, off_t offset);
size_t __libc_pread(int fd, void *buf, size_t count, off_t offset) {
return __pread(fd,buf,count,offset,0);
}
diff --git a/mdk-stage1/dietlibc/lib/pread64.c b/mdk-stage1/dietlibc/lib/pread64.c
index e6ba4d103..6dc93cefa 100644
--- a/mdk-stage1/dietlibc/lib/pread64.c
+++ b/mdk-stage1/dietlibc/lib/pread64.c
@@ -5,8 +5,9 @@
#ifndef __NO_STAT64
extern size_t __pread(int fd, void *buf, size_t count, off_t a,off_t b);
+size_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset);
size_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset) {
- return __pread(fd,buf,count,__LONG_LONG_PAIR (offset&0xffffffff,offset>>32));
+ return __pread(fd,buf,count,__LONG_LONG_PAIR ((off_t)(offset&0xffffffff),(off_t)(offset>>32)));
}
int pread64(int fd, void *buf, size_t count, off_t offset) __attribute__((weak,alias("__libc_pread64")));
diff --git a/mdk-stage1/dietlibc/lib/putenv.c b/mdk-stage1/dietlibc/lib/putenv.c
index 274b16b0e..448d59840 100644
--- a/mdk-stage1/dietlibc/lib/putenv.c
+++ b/mdk-stage1/dietlibc/lib/putenv.c
@@ -1,21 +1,24 @@
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
int putenv(const char *string) {
- int len;
+ size_t len;
int envc;
+ int remove=0;
char *tmp;
const char **ep;
char **newenv;
- static char **origenv=0;
+ static char **origenv;
if (!origenv) origenv=environ;
- if (!(tmp=strchr(string,'=')))
+ if (!(tmp=strchr(string,'='))) {
len=strlen(string);
- else
+ remove=1;
+ } else
len=tmp-string+1;
for (envc=0, ep=(const char**)environ; *ep; ++ep) {
- if (!memcmp(string,*ep,len)) { /* found */
- if (!tmp) {
+ if (*string == **ep && !memcmp(string,*ep,len)) {
+ if (remove) {
for (; ep[1]; ++ep) ep[0]=ep[1];
ep[0]=0;
return 0;
@@ -26,11 +29,11 @@ int putenv(const char *string) {
++envc;
}
if (tmp) {
- newenv=(char**)malloc((envc+2)*sizeof(char*));
+ newenv = (char**) realloc(environ==origenv?0:origenv,
+ (envc+2)*sizeof(char*));
if (!newenv) return -1;
newenv[0]=(char*)string;
memcpy(newenv+1,environ,(envc+1)*sizeof(char*));
- if (environ!=origenv) free(environ);
environ=newenv;
}
return 0;
diff --git a/mdk-stage1/dietlibc/lib/puts.c b/mdk-stage1/dietlibc/lib/puts.c
deleted file mode 100644
index 54415151d..000000000
--- a/mdk-stage1/dietlibc/lib/puts.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <unistd.h>
-#include <string.h>
-#include "dietstdio.h"
-#include "dietfeatures.h"
-
-int puts(const char *s) {
-#ifdef WANT_BUFFERED_STDIO
- return fwrite(s,1,strlen(s),stdout) && fputc('\n',stdout);
-#else
- return write(1,s,strlen(s)) && write(1,"\n",1);
-#endif
-}
diff --git a/mdk-stage1/dietlibc/lib/qsort.c b/mdk-stage1/dietlibc/lib/qsort.c
index 2a8824bf3..62217e142 100644
--- a/mdk-stage1/dietlibc/lib/qsort.c
+++ b/mdk-stage1/dietlibc/lib/qsort.c
@@ -1,3 +1,4 @@
+#include <sys/cdefs.h>
#include <stdlib.h>
#include <assert.h>
@@ -19,7 +20,7 @@ static inline void iswap(void *a,void *b,size_t size) {
}
static inline void swap(void *base,size_t size,size_t a,size_t b) {
- iswap(base+a*size,base+b*size,size);
+ iswap((char*)base+a*size,(char*)base+b*size,size);
}
#if 0
@@ -30,18 +31,19 @@ void dumparray() {
}
#endif
+void isort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
void isort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
- int i;
- while (nmemb>1) {
+ size_t i;
+ while (expect(nmemb>1,1)) {
char *min=base;
char *tmp=min+size;
for (i=1; i<nmemb; ++i) {
- if (compar(tmp,min)<0)
+ if (expect(compar(tmp,min)<0,0))
min=tmp;
tmp+=size;
}
iswap(min,base,size);
- base+=size;
+ (char*)base+=size;
nmemb-=1;
}
}
@@ -55,7 +57,7 @@ void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, co
static int level=0;
char* v; /* pivot */
char* mid, *max, *min;
- int lmemb;
+ size_t lmemb;
#if 0
int left,right;
@@ -71,8 +73,8 @@ void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, co
return isort(base,nmemb,size,compar);
}
{
- mid=base+(nmemb/2)*size;
- max=base+(nmemb-1)*size;
+ mid=(char*)base+(nmemb/2)*size;
+ max=(char*)base+(nmemb-1)*size;
if (compar(base,max)<0) /* a[left] < a[right] */
if (compar(base,mid)<0) /* a[left] < a[med] */
@@ -97,8 +99,8 @@ void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, co
v=max;
min=base; lmemb=0;
for (;;) {
- while (compar(min,v)<0) { min+=size; ++lmemb; }
- while (compar(max-=size,v)>0) ;
+ while (expect(compar(min,v)<0,1)) { min+=size; ++lmemb; }
+ while (expect(compar(max-=size,v)>0,1)) ;
if (min>=max) break;
iswap(min,max,size);
}
diff --git a/mdk-stage1/dietlibc/lib/raise.c b/mdk-stage1/dietlibc/lib/raise.c
index 606546034..0f548fc3b 100644
--- a/mdk-stage1/dietlibc/lib/raise.c
+++ b/mdk-stage1/dietlibc/lib/raise.c
@@ -1,7 +1,6 @@
-#include <linux/types.h>
+#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
-#include "syscalls.h"
int raise(int sig) {
return kill(getpid(),sig);
diff --git a/mdk-stage1/dietlibc/lib/rand.c b/mdk-stage1/dietlibc/lib/rand.c
index 10bfaf94d..beb2d2717 100644
--- a/mdk-stage1/dietlibc/lib/rand.c
+++ b/mdk-stage1/dietlibc/lib/rand.c
@@ -2,8 +2,11 @@
static unsigned int seed=1;
-int rand() {
- return ((seed = seed * 1103515245 + 12345) % ((unsigned int)RAND_MAX + 1));
+int rand(void) {
+ return rand_r(&seed);
}
void srand(unsigned int i) { seed=i; }
+
+int random(void) __attribute__((alias("rand")));
+void srandom(unsigned int i) __attribute__((alias("srand")));
diff --git a/mdk-stage1/dietlibc/lib/random.c b/mdk-stage1/dietlibc/lib/random.c
deleted file mode 100644
index e7785c455..000000000
--- a/mdk-stage1/dietlibc/lib/random.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <stdlib.h>
-
-static unsigned int seed=1;
-
-long int random() {
- return ((seed = seed * 1103515245 + 12345) % ((unsigned int)RAND_MAX + 1));
-}
-
-void srandom(unsigned int i) { seed=i; }
diff --git a/mdk-stage1/dietlibc/lib/readdir.c b/mdk-stage1/dietlibc/lib/readdir.c
index 689f5d08b..ed885a5f0 100644
--- a/mdk-stage1/dietlibc/lib/readdir.c
+++ b/mdk-stage1/dietlibc/lib/readdir.c
@@ -5,7 +5,7 @@
struct dirent* readdir(DIR *d) {
if (!d->num || (d->cur += ((struct dirent*)(d->buf+d->cur))->d_reclen)>=d->num) {
- int res=getdents(d->fd,(struct dirent*)d->buf,1023);
+ int res=getdents(d->fd,(struct dirent*)d->buf,sizeof (d->buf)-1);
if (res<=0) return 0;
d->num=res; d->cur=0;
}
diff --git a/mdk-stage1/dietlibc/lib/reboot.c b/mdk-stage1/dietlibc/lib/reboot.c
index 7f47a95dd..421068504 100644
--- a/mdk-stage1/dietlibc/lib/reboot.c
+++ b/mdk-stage1/dietlibc/lib/reboot.c
@@ -1,8 +1,8 @@
#include <sys/reboot.h>
-int __reboot(int magic1, int magic2, int cmd, void * arg);
+int __reboot(unsigned int magic1, unsigned int magic2, int cmd);
-int reboot(int cmd, void *arg)
+int reboot(int cmd)
{
- return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, arg);
+ return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd);
}
diff --git a/mdk-stage1/dietlibc/lib/recv.c b/mdk-stage1/dietlibc/lib/recv.c
index bc6b1e5d7..c6bab4fb1 100644
--- a/mdk-stage1/dietlibc/lib/recv.c
+++ b/mdk-stage1/dietlibc/lib/recv.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <sys/types.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_recv(int a, const void * b, size_t c, int flags);
int __libc_recv(int a, const void * b, size_t c, int flags) {
-#ifdef __i386__
- return socketcall(SYS_RECV,(long*)&a);
-#else
unsigned long args[] = { a, (long) b, c, flags };
return socketcall(SYS_RECV, args);
-#endif
}
int recv(int a, const void * b, size_t c, int flags)
diff --git a/mdk-stage1/dietlibc/lib/recvfrom.c b/mdk-stage1/dietlibc/lib/recvfrom.c
index 802ad665f..b66ce3813 100644
--- a/mdk-stage1/dietlibc/lib/recvfrom.c
+++ b/mdk-stage1/dietlibc/lib/recvfrom.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <sys/types.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_recvfrom(int a, const void * b, size_t c, int flags, void *to, void *tolen);
int __libc_recvfrom(int a, const void * b, size_t c, int flags, void *to, void *tolen) {
-#ifdef __i386__
- return socketcall(SYS_RECVFROM, (long*)&a);
-#else
unsigned long args[] = { a, (long) b, c, flags, (long) to, (long) tolen };
return socketcall(SYS_RECVFROM, args);
-#endif
}
int recvfrom(int a, const void * b, size_t c, int flags, void *to, void *tolen)
diff --git a/mdk-stage1/dietlibc/lib/recvmsg.c b/mdk-stage1/dietlibc/lib/recvmsg.c
index c14c58c78..d843ce9c0 100644
--- a/mdk-stage1/dietlibc/lib/recvmsg.c
+++ b/mdk-stage1/dietlibc/lib/recvmsg.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <sys/socket.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_recvmsg(int a, struct msghdr* msg, int flags);
int __libc_recvmsg(int a, struct msghdr* msg, int flags) {
-#ifdef __i386__
- return socketcall(SYS_RECVMSG, (long*)&a);
-#else
unsigned long args[] = { a, (long) msg, flags };
return socketcall(SYS_RECVMSG, args);
-#endif
}
int recvmsg(int a, struct msghdr *msg, int flags)
diff --git a/mdk-stage1/dietlibc/lib/remove.c b/mdk-stage1/dietlibc/lib/remove.c
index d5125bfed..06f686356 100644
--- a/mdk-stage1/dietlibc/lib/remove.c
+++ b/mdk-stage1/dietlibc/lib/remove.c
@@ -1,5 +1,7 @@
#include <unistd.h>
#include <errno.h>
+#include "dietfeatures.h"
+#include <stdio.h>
int remove(const char* filename) {
if (unlink(filename)) {
diff --git a/mdk-stage1/dietlibc/lib/sbrk.c b/mdk-stage1/dietlibc/lib/sbrk.c
index 945ad5a16..799ce7428 100644
--- a/mdk-stage1/dietlibc/lib/sbrk.c
+++ b/mdk-stage1/dietlibc/lib/sbrk.c
@@ -1,20 +1,21 @@
#include <unistd.h>
-extern int __brk(void *end_data_segment);
+extern int __libc_brk(void *end_data_segment);
extern void* __curbrk;
-void* __sbrk(ptrdiff_t increment) {
+void* __libc_sbrk(ptrdiff_t increment);
+void* __libc_sbrk(ptrdiff_t increment) {
void* oldbrk;
if (__curbrk==0)
- if (__brk(0) < 0)
+ if (__libc_brk(0) < 0)
return (void*)-1;
if (increment==0)
return __curbrk;
oldbrk=__curbrk;
- if (__brk(oldbrk+increment)<0)
+ if (__libc_brk((char*)oldbrk+increment)<0)
return (void*)-1;
return oldbrk;
}
-void* sbrk (ptrdiff_t increment) __attribute__((weak,alias("__sbrk")));
+void* sbrk (ptrdiff_t increment) __attribute__((weak,alias("__libc_sbrk")));
diff --git a/mdk-stage1/dietlibc/lib/semctl.c b/mdk-stage1/dietlibc/lib/semctl.c
index ddc0aae95..cec0b7535 100644
--- a/mdk-stage1/dietlibc/lib/semctl.c
+++ b/mdk-stage1/dietlibc/lib/semctl.c
@@ -1,10 +1,17 @@
#include <sys/types.h>
#include <sys/ipc.h>
-#include <sys/sem.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,int,int,void*);
+union semun {
+ int val; /* value for SETVAL */
+ struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
+ unsigned short *array; /* array for GETALL & SETALL */
+ struct seminfo *__buf; /* buffer for IPC_INFO */
+ void *__pad;
+};
+
+int semctl(int semid, int semnum, int cmd, union semun arg);
int semctl(int semid, int semnum, int cmd, union semun arg) {
return __ipc(SEMCTL,semid,semnum,cmd,&arg);
}
diff --git a/mdk-stage1/dietlibc/lib/semget.c b/mdk-stage1/dietlibc/lib/semget.c
index 056aeae04..c3649eea1 100644
--- a/mdk-stage1/dietlibc/lib/semget.c
+++ b/mdk-stage1/dietlibc/lib/semget.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,key_t,int,int,int);
int semget(key_t key, int nsems, int semflg) {
return __ipc(SEMGET,key,nsems,semflg,0);
diff --git a/mdk-stage1/dietlibc/lib/semop.c b/mdk-stage1/dietlibc/lib/semop.c
index abfde73a7..4d2e748cc 100644
--- a/mdk-stage1/dietlibc/lib/semop.c
+++ b/mdk-stage1/dietlibc/lib/semop.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,unsigned,int,void*);
int semop(int semid,struct sembuf *sops,unsigned nsops) {
return __ipc(SEMOP,semid,nsops,0,sops);
diff --git a/mdk-stage1/dietlibc/lib/send.c b/mdk-stage1/dietlibc/lib/send.c
index c1adf1b5d..783ecaf89 100644
--- a/mdk-stage1/dietlibc/lib/send.c
+++ b/mdk-stage1/dietlibc/lib/send.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <sys/types.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_send(int a, const void * b, size_t c, int flags);
int __libc_send(int a, const void * b, size_t c, int flags) {
-#ifdef __i386__
- return socketcall(SYS_SEND, (long*)&a);
-#else
unsigned long args[] = { a, (long) b, c, flags };
return socketcall(SYS_SEND, args);
-#endif
}
int send(int a, const void * b, size_t c, int flags)
diff --git a/mdk-stage1/dietlibc/lib/sendmsg.c b/mdk-stage1/dietlibc/lib/sendmsg.c
index 277265985..bb5d1711a 100644
--- a/mdk-stage1/dietlibc/lib/sendmsg.c
+++ b/mdk-stage1/dietlibc/lib/sendmsg.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <sys/socket.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_sendmsg(int a, const struct msghdr* msg, int flags);
int __libc_sendmsg(int a, const struct msghdr* msg, int flags) {
-#ifdef __i386__
- return socketcall(SYS_SENDMSG, (long*)&a);
-#else
unsigned long args[] = { a, (long) msg, flags };
return socketcall(SYS_SENDMSG, args);
-#endif
}
int sendmsg(int a, const struct msghdr *msg, int flags)
diff --git a/mdk-stage1/dietlibc/lib/sendto.c b/mdk-stage1/dietlibc/lib/sendto.c
index 078c010ea..d3239932b 100644
--- a/mdk-stage1/dietlibc/lib/sendto.c
+++ b/mdk-stage1/dietlibc/lib/sendto.c
@@ -1,15 +1,13 @@
-#include <linux/net.h>
+#include <sys/socket.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
+int __libc_sendto(int a, const void * b, size_t c, int flags, void *to, int tolen);
int __libc_sendto(int a, const void * b, size_t c, int flags, void *to, int tolen) {
-#ifdef __i386__
- return socketcall(SYS_SENDTO, (long*)&a);
-#else
unsigned long args[] = { a, (long) b, c, flags, (long) to, tolen };
return socketcall(SYS_SENDTO, args);
-#endif
}
-int sendto(int a, const void * b, size_t c, int flags, void *to, int tolen)
+int sendto(int a, const void * b, size_t c, int flags, const struct sockaddr* to, socklen_t tolen)
__attribute__ ((weak, alias("__libc_sendto")));
diff --git a/mdk-stage1/dietlibc/lib/set_errno.c b/mdk-stage1/dietlibc/lib/set_errno.c
deleted file mode 100644
index 6553bc496..000000000
--- a/mdk-stage1/dietlibc/lib/set_errno.c
+++ /dev/null
@@ -1,9 +0,0 @@
-extern int errno;
-
-void __set_errno(int error) __attribute__ ((weak));
-
-void __set_errno(int error)
-{
- errno=error;
-}
-
diff --git a/mdk-stage1/dietlibc/lib/setsockopt.c b/mdk-stage1/dietlibc/lib/setsockopt.c
index 781e9c31c..e4bab349f 100644
--- a/mdk-stage1/dietlibc/lib/setsockopt.c
+++ b/mdk-stage1/dietlibc/lib/setsockopt.c
@@ -1,13 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
+#include <sys/socket.h>
extern int socketcall(int callno,long* args);
-int setsockopt(int a, int b, int c, void *d, void *e) {
-#ifdef __i386__
- return socketcall(SYS_SETSOCKOPT, (long*)&a);
-#else
+int __libc_setsockopt(int a, int b, int c, void *d, void *e);
+int __libc_setsockopt(int a, int b, int c, void *d, void *e) {
unsigned long args[] = { a, b, c, (long)d, (long) e };
return socketcall(SYS_SETSOCKOPT, args);
-#endif
}
+int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen) __attribute__((weak,alias("__libc_setsockopt")));
diff --git a/mdk-stage1/dietlibc/lib/shmat.c b/mdk-stage1/dietlibc/lib/shmat.c
index 63284b1ea..d4b34c120 100644
--- a/mdk-stage1/dietlibc/lib/shmat.c
+++ b/mdk-stage1/dietlibc/lib/shmat.c
@@ -1,15 +1,14 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <asm/ipc.h>
-extern void* __ipc();
+extern void* __ipc(int,int,int,void*,const void*);
void* shmat(int shmid,const void* shmaddr,int shmflg) {
void* raddr;
register void* result;
result=__ipc(SHMAT,shmid,shmflg,&raddr,shmaddr);
- if ((unsigned long)result <= -(unsigned long)SHMLBA)
+ if ((unsigned long)result <= -(unsigned long)8196)
result=raddr;
return result;
}
diff --git a/mdk-stage1/dietlibc/lib/shmctl.c b/mdk-stage1/dietlibc/lib/shmctl.c
index 8f942f2a4..92b9d1fbb 100644
--- a/mdk-stage1/dietlibc/lib/shmctl.c
+++ b/mdk-stage1/dietlibc/lib/shmctl.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,int,int,void*);
int shmctl(int shmid, int cmd, struct shmid_ds *buf) {
return __ipc(SHMCTL,shmid,cmd,0,buf);
diff --git a/mdk-stage1/dietlibc/lib/shmdt.c b/mdk-stage1/dietlibc/lib/shmdt.c
index 36db10f08..54dc6f41b 100644
--- a/mdk-stage1/dietlibc/lib/shmdt.c
+++ b/mdk-stage1/dietlibc/lib/shmdt.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,int,int,int,const void*);
int shmdt(const void* shmaddr) {
return __ipc(SHMDT,0,0,0,shmaddr);
diff --git a/mdk-stage1/dietlibc/lib/shmget.c b/mdk-stage1/dietlibc/lib/shmget.c
index 9b03f5deb..bf1dc2030 100644
--- a/mdk-stage1/dietlibc/lib/shmget.c
+++ b/mdk-stage1/dietlibc/lib/shmget.c
@@ -1,9 +1,8 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <asm/ipc.h>
-extern int __ipc();
+extern int __ipc(int,key_t,int,int,int);
int shmget(key_t key, int size, int shmflg) {
return __ipc(SHMGET,key,size,shmflg,0);
diff --git a/mdk-stage1/dietlibc/lib/shutdown.c b/mdk-stage1/dietlibc/lib/shutdown.c
index 0a6be6c37..0e7b8755c 100644
--- a/mdk-stage1/dietlibc/lib/shutdown.c
+++ b/mdk-stage1/dietlibc/lib/shutdown.c
@@ -1,14 +1,12 @@
-#include <linux/net.h>
+#include <linuxnet.h>
+#include <sys/socket.h>
extern int socketcall(int callno,long* args);
+int __libc_shutdown(int s, int how);
int __libc_shutdown(int s, int how) {
-#ifdef __i386__
- return socketcall(SYS_SHUTDOWN, (long*)&s);
-#else
unsigned long args[] = { s, (long) how, 0 };
return socketcall(SYS_SHUTDOWN, args);
-#endif
}
int shutdown(int s, int how) __attribute__((weak,alias("__libc_shutdown")));
diff --git a/mdk-stage1/dietlibc/lib/sigaddset.c b/mdk-stage1/dietlibc/lib/sigaddset.c
index 8ce69ea77..1c2632e67 100644
--- a/mdk-stage1/dietlibc/lib/sigaddset.c
+++ b/mdk-stage1/dietlibc/lib/sigaddset.c
@@ -1,15 +1,17 @@
-#define __KERNEL__
-#define sigaddset foobar
-#include <asm/signal.h>
-#undef sigaddset
+#include <signal.h>
+#include <errno.h>
-int __sigaddset(sigset_t *set, int signo) {
- unsigned long sig = signo - 1;
- if (_NSIG_WORDS == 1)
- set->sig[0] |= 1UL << sig;
- else
- set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
- return 0;
-}
+#define __sigmask(sig) ( ((unsigned long)1) << (((sig)-1) % (8*sizeof(unsigned long))) )
+#define __sigword(sig) ( ((sig)-1) / (8*sizeof(unsigned long)) )
-int sigaddset (sigset_t *env, int signo) __attribute__((weak,alias("__sigaddset")));
+int sigaddset(sigset_t *set, int signo) {
+ if ((signo<1)||(signo>SIGRTMAX)) {
+ (*__errno_location())=EINVAL;
+ return -1;
+ } else {
+ unsigned long __mask = __sigmask (signo);
+ unsigned long __word = __sigword (signo);
+ set->sig[__word]|=__mask;
+ return 0;
+ }
+}
diff --git a/mdk-stage1/dietlibc/lib/sigdelset.c b/mdk-stage1/dietlibc/lib/sigdelset.c
index 2c3385ea8..0c47ce348 100644
--- a/mdk-stage1/dietlibc/lib/sigdelset.c
+++ b/mdk-stage1/dietlibc/lib/sigdelset.c
@@ -1,15 +1,17 @@
-#define __KERNEL__
-#define sigdelset foobar
-#include <asm/signal.h>
-#undef sigdelset
+#include <signal.h>
+#include <errno.h>
-int __sigdelset(sigset_t *set, int signo) {
- unsigned long sig = signo - 1;
- if (_NSIG_WORDS == 1)
- set->sig[0] &= ~(1UL << sig);
- else
- set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
- return 0;
-}
+#define __sigmask(sig) ( ((unsigned long)1) << (((sig)-1) % (8*sizeof(unsigned long))) )
+#define __sigword(sig) ( ((sig)-1) / (8*sizeof(unsigned long)) )
-int sigdelset (sigset_t *env, int signo) __attribute__((weak,alias("__sigdelset")));
+int sigdelset(sigset_t *set, int signo) {
+ if ((signo<1)||(signo>SIGRTMAX)) {
+ (*__errno_location())=EINVAL;
+ return -1;
+ } else {
+ unsigned long __mask = __sigmask (signo);
+ unsigned long __word = __sigword (signo);
+ set->sig[__word]&=~__mask;
+ return 0;
+ }
+}
diff --git a/mdk-stage1/dietlibc/lib/sigemptyset.c b/mdk-stage1/dietlibc/lib/sigemptyset.c
index f4532f96e..4efee2550 100644
--- a/mdk-stage1/dietlibc/lib/sigemptyset.c
+++ b/mdk-stage1/dietlibc/lib/sigemptyset.c
@@ -1,14 +1,12 @@
-#define __USE_EXTERN_INLINES 1
#include <signal.h>
-#include <errno.h>
-#include <string.h>
int sigemptyset(sigset_t *set) {
- if (set==NULL) {
- __set_errno(EINVAL);
- return -1;
+ set->sig[0]=0;
+ if (_NSIG_WORDS>1) set->sig[1]=0;
+ if (_NSIG_WORDS>2) {
+ set->sig[2]=0;
+ set->sig[3]=0;
}
- memset(set,0,sizeof(*set));
return 0;
}
diff --git a/mdk-stage1/dietlibc/lib/sigfillset.c b/mdk-stage1/dietlibc/lib/sigfillset.c
index ddac6e391..b4e369fe4 100644
--- a/mdk-stage1/dietlibc/lib/sigfillset.c
+++ b/mdk-stage1/dietlibc/lib/sigfillset.c
@@ -1,11 +1,11 @@
-#define __KERNEL__
-#include <asm/signal.h>
+#include <signal.h>
-int __sigfillset (sigset_t *set) {
- int i;
- for (i=0; i<_NSIG_WORDS; i++)
- set->sig[i]=~0;
+int sigfillset(sigset_t *set) {
+ set->sig[0]=(unsigned long)-1;
+ if (_NSIG_WORDS>1) set->sig[1]=(unsigned long)-1;
+ if (_NSIG_WORDS>2) {
+ set->sig[2]=(unsigned long)-1;
+ set->sig[3]=(unsigned long)-1;
+ }
return 0;
}
-
-int sigfillset(sigset_t *env) __attribute__((weak,alias("__sigfillset")));
diff --git a/mdk-stage1/dietlibc/lib/sigismember.c b/mdk-stage1/dietlibc/lib/sigismember.c
index 67a258884..8ffe9762a 100644
--- a/mdk-stage1/dietlibc/lib/sigismember.c
+++ b/mdk-stage1/dietlibc/lib/sigismember.c
@@ -1,21 +1,16 @@
-#define __USE_EXTERN_INLINES 1
#include <signal.h>
+#include <errno.h>
-# define __sigmask(sig) \
- (((unsigned long int) 1) << (((sig) - 1) % (8 * sizeof (unsigned long int))))
+#define __sigmask(sig) ( ((unsigned long)1) << (((sig)-1) % (8*sizeof(unsigned long))) )
+#define __sigword(sig) ( ((sig)-1) / (8*sizeof(unsigned long)) )
-# define __sigword(sig) (((sig) - 1) / (8 * sizeof (unsigned long int)))
-
-# define _SIGSET_NWORDS (1024 / (8 * sizeof (unsigned long int)))
-typedef struct
- {
- unsigned long int __val[_SIGSET_NWORDS];
- } __sigset_t;
-
-int __sigismember(const __sigset_t *set, int signo) {
- unsigned long int __mask = __sigmask (signo);
- unsigned long int __word = __sigword (signo);
- return (set->__val[__word] & __mask);
+int sigismember(const sigset_t *set, int signo) {
+ if ((signo<1)||(signo>SIGRTMAX)) {
+ (*__errno_location())=EINVAL;
+ return -1;
+ } else {
+ unsigned long __mask = __sigmask (signo);
+ unsigned long __word = __sigword (signo);
+ return (set->sig[__word] & __mask)?1:0;
+ }
}
-
-int sigismember(const sigset_t *env, int signo) __attribute__((weak,alias("__sigismember")));
diff --git a/mdk-stage1/dietlibc/lib/sigjmp.c b/mdk-stage1/dietlibc/lib/sigjmp.c
index be97c62cc..73c8baa8e 100644
--- a/mdk-stage1/dietlibc/lib/sigjmp.c
+++ b/mdk-stage1/dietlibc/lib/sigjmp.c
@@ -1,35 +1,10 @@
-/* Copyright (C) 1992, 1994, 1997 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
-
- 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.
-
- 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.
-
- 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., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA. */
-
#include <setjmp.h>
#include <signal.h>
-#include <string.h>
-
-/* This function is called by the `sigsetjmp' macro
- before doing a `__setjmp' on ENV[0].__jmpbuf.
- Always return zero. */
-
-int
-__sigjmp_save (sigjmp_buf env, int savemask)
-{
- env[0].__mask_was_saved = (savemask &&
- sigprocmask (SIG_BLOCK, (sigset_t *) NULL,
- (sigset_t*)&env[0].__saved_mask) == 0);
+int __sigjmp_save(sigjmp_buf env,int savemask);
+int __sigjmp_save(sigjmp_buf env,int savemask) {
+ if (savemask) {
+ env[0].__mask_was_saved=(sigprocmask(SIG_BLOCK,(sigset_t*)0,(sigset_t*)&env[0].__saved_mask)==0);
+ }
return 0;
}
diff --git a/mdk-stage1/dietlibc/lib/sleep.c b/mdk-stage1/dietlibc/lib/sleep.c
index ec5b99533..45c51f1b6 100644
--- a/mdk-stage1/dietlibc/lib/sleep.c
+++ b/mdk-stage1/dietlibc/lib/sleep.c
@@ -1,4 +1,4 @@
-#include <linux/time.h>
+#include <unistd.h>
#include <time.h>
unsigned int sleep(unsigned int secs) {
diff --git a/mdk-stage1/dietlibc/lib/snprintf.c b/mdk-stage1/dietlibc/lib/snprintf.c
index 096c06115..141b4ec13 100644
--- a/mdk-stage1/dietlibc/lib/snprintf.c
+++ b/mdk-stage1/dietlibc/lib/snprintf.c
@@ -1,7 +1,6 @@
#include <stdarg.h>
#include <sys/types.h>
-
-int vsnprintf (char *str,size_t size,const char *format, va_list arg_ptr);
+#include <stdio.h>
int snprintf(char *str,size_t size,const char *format,...)
{
diff --git a/mdk-stage1/dietlibc/lib/socket.c b/mdk-stage1/dietlibc/lib/socket.c
index 1684a6a2b..c18df5123 100644
--- a/mdk-stage1/dietlibc/lib/socket.c
+++ b/mdk-stage1/dietlibc/lib/socket.c
@@ -1,12 +1,11 @@
-#include <linux/net.h>
+#include <linuxnet.h>
extern int socketcall(int callno,long* args);
-int socket(int a, int b, int c) {
-#ifdef __i386__
- return socketcall(SYS_SOCKET, (long*)&a);
-#else
+int __libc_socket(int a, int b, int c);
+int __libc_socket(int a, int b, int c) {
unsigned long args[] = { a, b, c };
return socketcall(SYS_SOCKET, args);
-#endif
}
+
+int socket(int a,int b,int c) __attribute__((weak,alias("__libc_socket")));
diff --git a/mdk-stage1/dietlibc/lib/speed.c b/mdk-stage1/dietlibc/lib/speed.c
deleted file mode 100644
index a7fcca7c9..000000000
--- a/mdk-stage1/dietlibc/lib/speed.c
+++ /dev/null
@@ -1,67 +0,0 @@
-#include <unistd.h>
-#include <termios.h>
-#include <sys/types.h>
-
-#include <asm/errno.h>
-
-extern int errno;
-
-/* Hack around a kernel bug; value must correspond to the one used in tcsetattr.c */
-#define IBAUD0 020000000000
-
-
-/* Return the output baud rate stored in *TERMIOS_P. */
-speed_t cfgetospeed (struct termios *termios_p)
-{
- return termios_p->c_cflag & (CBAUD | CBAUDEX);
-}
-
-
-/* Return the input baud rate stored in *TERMIOS_P.
- Although for Linux there is no difference between input and output
- speed, the numerical 0 is a special case for the input baud rate. It
- should set the input baud rate to the output baud rate. */
-speed_t cfgetispeed (struct termios *termios_p)
-{
- return ((termios_p->c_iflag & IBAUD0)
- ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
-}
-
-
-/* Set the output baud rate stored in *TERMIOS_P to SPEED. */
-int cfsetospeed (struct termios *termios_p, speed_t speed)
-{
- if ((speed & ~CBAUD) != 0 && (speed < B57600 || speed > B460800)) {
- errno = EINVAL;
- return -1;
- }
-
- termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
- termios_p->c_cflag |= speed;
-
- return 0;
-}
-
-
-/* Set the input baud rate stored in *TERMIOS_P to SPEED.
- Although for Linux there is no difference between input and output
- speed, the numerical 0 is a special case for the input baud rate. It
- should set the input baud rate to the output baud rate. */
-int cfsetispeed (struct termios *termios_p, speed_t speed)
-{
- if ((speed & ~CBAUD) != 0 && (speed < B57600 || speed > B460800)) {
- errno = EINVAL;
- return -1;
- }
-
- if (speed == 0)
- termios_p->c_iflag |= IBAUD0;
- else
- {
- termios_p->c_iflag &= ~IBAUD0;
- termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
- termios_p->c_cflag |= speed;
- }
-
- return 0;
-}
diff --git a/mdk-stage1/dietlibc/lib/sprintf.c b/mdk-stage1/dietlibc/lib/sprintf.c
index b355d01f9..37fd831d9 100644
--- a/mdk-stage1/dietlibc/lib/sprintf.c
+++ b/mdk-stage1/dietlibc/lib/sprintf.c
@@ -1,18 +1,14 @@
#include <stdarg.h>
-#include <linux/types.h>
+#include <sys/types.h>
#include <stdlib.h>
-#include "dietwarning.h"
-
-int vsnprintf (char *str,size_t size,const char *format, va_list arg_ptr);
+#include <stdio.h>
int sprintf(char *dest,const char *format,...)
{
int n;
va_list arg_ptr;
va_start(arg_ptr, format);
- n=vsnprintf(dest,1000000,format,arg_ptr);
+ n=vsprintf(dest,format,arg_ptr);
va_end (arg_ptr);
return n;
}
-
-link_warning("sprintf","warning: Avoid sprintf; use snprintf. It is more secure and faster.")
diff --git a/mdk-stage1/dietlibc/lib/sscanf.c b/mdk-stage1/dietlibc/lib/sscanf.c
index 341e74bba..e608878ef 100644
--- a/mdk-stage1/dietlibc/lib/sscanf.c
+++ b/mdk-stage1/dietlibc/lib/sscanf.c
@@ -1,6 +1,5 @@
#include <stdarg.h>
-
-int vsscanf(const char *str, const char *format, va_list arg_ptr);
+#include <stdio.h>
int sscanf(const char *str, const char *format, ...)
{
diff --git a/mdk-stage1/dietlibc/lib/strcasecmp.c b/mdk-stage1/dietlibc/lib/strcasecmp.c
index d978488a4..cf1592567 100644
--- a/mdk-stage1/dietlibc/lib/strcasecmp.c
+++ b/mdk-stage1/dietlibc/lib/strcasecmp.c
@@ -1,17 +1,18 @@
-#include <ctype.h>
-#include "dietfeatures.h"
+#include <strings.h>
-int strcasecmp(register const char *s,register const char *t) {
- register char x;
+int strcasecmp ( const char* s1, const char* s2 )
+{
+ register unsigned int x2;
+ register unsigned int x1;
- for (;;) {
- x = tolower(*s); if (x != tolower(*t)) break; if (!x) break; ++s; ++t;
-#ifndef WANT_SMALL_STRING_ROUTINES
- x = tolower(*s); if (x != tolower(*t)) break; if (!x) break; ++s; ++t;
- x = tolower(*s); if (x != tolower(*t)) break; if (!x) break; ++s; ++t;
- x = tolower(*s); if (x != tolower(*t)) break; if (!x) break; ++s; ++t;
-#endif
- }
- return ((int)(unsigned int)(unsigned char) x)
- - ((int)(unsigned int)(unsigned char) *t);
+ while (1) {
+ x2 = *s2++ - 'A'; if (x2 < 26u) x2 += 32;
+ x1 = *s1++ - 'A'; if (x1 < 26u) x1 += 32;
+ if ( x2 != x1 )
+ break;
+ if ( x1 == (unsigned int)-'A' )
+ break;
+ }
+
+ return x1 - x2;
}
diff --git a/mdk-stage1/dietlibc/lib/strchr.c b/mdk-stage1/dietlibc/lib/strchr.c
index 0c2050da6..3a4c9b5b4 100644
--- a/mdk-stage1/dietlibc/lib/strchr.c
+++ b/mdk-stage1/dietlibc/lib/strchr.c
@@ -1,4 +1,5 @@
#include "dietfeatures.h"
+#include <string.h>
char *strchr(register const char *t, int c) {
register char ch;
diff --git a/mdk-stage1/dietlibc/lib/strcmp.c b/mdk-stage1/dietlibc/lib/strcmp.c
index 0db324e66..3892379e5 100644
--- a/mdk-stage1/dietlibc/lib/strcmp.c
+++ b/mdk-stage1/dietlibc/lib/strcmp.c
@@ -14,3 +14,5 @@ int strcmp(register const char *s,register const char *t) {
return ((int)(unsigned int)(unsigned char) x)
- ((int)(unsigned int)(unsigned char) *t);
}
+
+int strcoll(const char *s,const char* t) __attribute__((weak,alias("strcmp")));
diff --git a/mdk-stage1/dietlibc/lib/strcpy.c b/mdk-stage1/dietlibc/lib/strcpy.c
index 49693c30d..444054e71 100644
--- a/mdk-stage1/dietlibc/lib/strcpy.c
+++ b/mdk-stage1/dietlibc/lib/strcpy.c
@@ -3,13 +3,16 @@
char* strcpy(register char* s,register const char* t)
{
char *dest=s;
+#ifndef WANT_SMALL_STRING_ROUTINES
for (;;) {
if (!(*s = *t)) return dest; ++s; ++t;
-#ifndef WANT_SMALL_STRING_ROUTINES
if (!(*s = *t)) return dest; ++s; ++t;
if (!(*s = *t)) return dest; ++s; ++t;
if (!(*s = *t)) return dest; ++s; ++t;
-#endif
}
+#else
+ while ((*dest++=*t++));
+ return s;
+#endif
}
diff --git a/mdk-stage1/dietlibc/lib/strerror.c b/mdk-stage1/dietlibc/lib/strerror.c
index f15b86aae..7cb79d988 100644
--- a/mdk-stage1/dietlibc/lib/strerror.c
+++ b/mdk-stage1/dietlibc/lib/strerror.c
@@ -1,10 +1,20 @@
+#include "dietfeatures.h"
+#include <unistd.h>
#include <string.h>
-extern char *sys_errlist[];
-extern int sys_nerr;
+#define _BSD_SOURCE
+#include <errno.h>
-char *strerror(int errnum) {
- if (errnum>=0 && errnum<sys_nerr)
- return sys_errlist[errnum];
- return "[unknown error]";
+extern const char __sys_err_unknown[];
+
+char*strerror(int errnum) {
+ register const char*message=__sys_err_unknown;
+
+ if ( (unsigned int)errnum < (unsigned int)__SYS_NERR )
+ message=sys_errlist[errnum];
+#if defined(__mips__)
+ if ( errnum == 1133 )
+ message="Quota exceeded";
+#endif
+ return (char*)message;
}
diff --git a/mdk-stage1/dietlibc/lib/strlen.c b/mdk-stage1/dietlibc/lib/strlen.c
index 56c085dac..96da5a540 100644
--- a/mdk-stage1/dietlibc/lib/strlen.c
+++ b/mdk-stage1/dietlibc/lib/strlen.c
@@ -4,7 +4,7 @@
#ifdef WANT_SMALL_STRING_ROUTINES
size_t strlen(const char *s) {
- register int i;
+ register size_t i;
if (!s) return 0;
for (i=0; *s; ++s) ++i;
return i;
diff --git a/mdk-stage1/dietlibc/lib/strncasecmp.c b/mdk-stage1/dietlibc/lib/strncasecmp.c
index 8a5445e53..543fe56ba 100644
--- a/mdk-stage1/dietlibc/lib/strncasecmp.c
+++ b/mdk-stage1/dietlibc/lib/strncasecmp.c
@@ -1,18 +1,22 @@
-#include <ctype.h>
#include <sys/types.h>
-#include <string.h>
+#include <strings.h>
-int strncasecmp(const char *s, const char *t, size_t n) {
- register char x;
- register const char* u=s+n;
- for (;;) {
- x = tolower(*s); if (x!=tolower(*t)) break; if (!x) break; if (++s>=u) return 0; ++t;
-#ifndef WANT_SMALL_STRING_ROUTINES
- x = tolower(*s); if (x!=tolower(*t)) break; if (!x) break; if (++s>=u) return 0; ++t;
- x = tolower(*s); if (x!=tolower(*t)) break; if (!x) break; if (++s>=u) return 0; ++t;
- x = tolower(*s); if (x!=tolower(*t)) break; if (!x) break; if (++s>=u) return 0; ++t;
-#endif
- }
- return ((int)(unsigned int)(unsigned char) x)
- - ((int)(unsigned int)(unsigned char) *t);
+int strncasecmp ( const char* s1, const char* s2, size_t len )
+{
+ register unsigned int x2;
+ register unsigned int x1;
+ register const char* end = s1 + len;
+
+ while (1) {
+ if ( s1 >= end )
+ return 0;
+ x2 = *s2++ - 'A'; if (x2 < 26u) x2 += 32;
+ x1 = *s1++ - 'A'; if (x1 < 26u) x1 += 32;
+ if ( x2 != x1 )
+ break;
+ if ( x1 == (unsigned int)-'A' )
+ break;
+ }
+
+ return x1 - x2;
}
diff --git a/mdk-stage1/dietlibc/lib/strncat.c b/mdk-stage1/dietlibc/lib/strncat.c
index a08e97916..9802d5678 100644
--- a/mdk-stage1/dietlibc/lib/strncat.c
+++ b/mdk-stage1/dietlibc/lib/strncat.c
@@ -9,14 +9,16 @@
* the last written bytes is always '\0'. */
#endif
+/* gcc is broken and has a non-SUSv2 compliant internal prototype.
+ * This causes it to warn about a type mismatch here. Ignore it. */
char *strncat(char *s, const char *t, size_t n) {
char *dest=s;
register char *max;
s+=strlen(s);
#ifdef WANT_NON_COMPLIANT_STRNCAT
- max=s+n-1;
+ if ((max=s+n-1)<=s) goto fini;
#else
- max=s+n;
+ if ((max=s+n)==s) goto fini;
#endif
for (;;) {
if (!(*s = *t)) break; if (++s==max) break; ++t;
@@ -27,5 +29,6 @@ char *strncat(char *s, const char *t, size_t n) {
#endif
}
*s=0;
+fini:
return dest;
}
diff --git a/mdk-stage1/dietlibc/lib/strncmp.c b/mdk-stage1/dietlibc/lib/strncmp.c
index 7c08c0fa7..b3b56cfbb 100644
--- a/mdk-stage1/dietlibc/lib/strncmp.c
+++ b/mdk-stage1/dietlibc/lib/strncmp.c
@@ -1,6 +1,17 @@
#include <sys/types.h>
#include <string.h>
+/* gcc is broken and has a non-SUSv2 compliant internal prototype.
+ * This causes it to warn about a type mismatch here. Ignore it. */
int strncmp(const char *s1, const char *s2, size_t n) {
- return memccmp(s1,s2,0,n);
+ register const char* a=s1;
+ register const char* b=s2;
+ register const char* fini=a+n;
+ while (a<fini) {
+ register int res=*a-*b;
+ if (res) return res;
+ if (!*a) return 0;
+ ++a; ++b;
+ }
+ return 0;
}
diff --git a/mdk-stage1/dietlibc/lib/strncpy.c b/mdk-stage1/dietlibc/lib/strncpy.c
index 531387b7f..31b682a1e 100644
--- a/mdk-stage1/dietlibc/lib/strncpy.c
+++ b/mdk-stage1/dietlibc/lib/strncpy.c
@@ -2,9 +2,14 @@
#define _XOPEN_SOURCE
#include <sys/types.h>
#include <string.h>
+#include "dietfeatures.h"
-char *strncpy(char *dest, const char *src, size_t n)
-{
+/* gcc is broken and has a non-SUSv2 compliant internal prototype.
+ * This causes it to warn about a type mismatch here. Ignore it. */
+char *strncpy(char *dest, const char *src, size_t n) {
+#ifdef WANT_FULL_POSIX_COMPAT
+ memset(dest,0,n);
+#endif
memccpy(dest,src,0,n);
return dest;
}
diff --git a/mdk-stage1/dietlibc/lib/strsep.c b/mdk-stage1/dietlibc/lib/strsep.c
index a1bf1872a..db76f89b5 100644
--- a/mdk-stage1/dietlibc/lib/strsep.c
+++ b/mdk-stage1/dietlibc/lib/strsep.c
@@ -4,6 +4,7 @@ char *strsep(char **stringp, const char *delim) {
register char *tmp=*stringp;
register char *tmp2=tmp;
register const char *tmp3;
+ if (!*stringp) return 0;
for (tmp2=tmp; *tmp2; ++tmp2) {
for (tmp3=delim; *tmp3; ++tmp3)
if (*tmp2==*tmp3) { /* delimiter found */
@@ -12,5 +13,6 @@ char *strsep(char **stringp, const char *delim) {
return tmp;
}
}
- return 0;
+ *stringp=0;
+ return tmp;
}
diff --git a/mdk-stage1/dietlibc/lib/strstr.c b/mdk-stage1/dietlibc/lib/strstr.c
index 641d9d7ac..0632cc28a 100644
--- a/mdk-stage1/dietlibc/lib/strstr.c
+++ b/mdk-stage1/dietlibc/lib/strstr.c
@@ -2,12 +2,14 @@
#include <string.h>
char *strstr(const char *haystack, const char *needle) {
- int nl=strlen(needle);
- int hl=strlen(haystack);
+ size_t nl=strlen(needle);
+ size_t hl=strlen(haystack);
int i;
+ if (!nl) goto found;
if (nl>hl) return 0;
for (i=hl-nl+1; i; --i) {
- if (!memcmp(haystack,needle,nl))
+ if (*haystack==*needle && !memcmp(haystack,needle,nl))
+found:
return (char*)haystack;
++haystack;
}
diff --git a/mdk-stage1/dietlibc/lib/strtod.c b/mdk-stage1/dietlibc/lib/strtod.c
index cbe983d84..5d50cc535 100644
--- a/mdk-stage1/dietlibc/lib/strtod.c
+++ b/mdk-stage1/dietlibc/lib/strtod.c
@@ -1,52 +1,66 @@
#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
-extern char isspace(char c);
-
-#ifdef __GNUC__
-static inline char isdigit(char c) { return (c>='0' && c<='9'); }
-#endif
-
-double strtod(const char *nptr, char **endptr) {
- double d=0;
- register const char *c=nptr;
- char neg=0;
- while (*c && isspace(*c)) ++c;
- switch (*c) {
- case '-': neg=1;
- case '+': c++; break;
- default: break;
- }
- while (isdigit(*c)) {
- d=d*10+(*c-'0');
- ++c;
- }
- if (*c=='.') {
- double factor=.1;
- while (isdigit(*++c)) {
- d=d+(factor*(*c-'0'));
- factor/=10;
+double strtod(const char* s, char** endptr) {
+ register const char* p = s;
+ register long double value = 0.L;
+ int sign = +1;
+ long double factor;
+ unsigned int expo;
+
+ while ( isspace(*p) )
+ p++;
+
+ switch (*p) {
+ case '-': sign = -1;
+ case '+': p++;
+ default : break;
}
- }
- if ((*c|32)=='e') {
- int exp=0;
- double factor=10;
- if (c[1]<'0') {
- switch (c[1]) {
- case '-': factor=0.1;
- case '+': c++; break;
- default:
- d=0;
- c=nptr;
- goto done;
- }
+
+ while ( (unsigned int)(*p - '0') < 10u )
+ value = value*10 + (*p++ - '0');
+
+ if ( *p == '.' ) {
+ factor = 1.;
+
+ p++;
+ while ( (unsigned int)(*p - '0') < 10u ) {
+ factor *= 0.1;
+ value += (*p++ - '0') * factor;
+ }
}
- while (isdigit(*++c))
- exp=exp*10+(*c-'0');
- while (exp) { /* XXX: this introduces rounding errors */
- d*=factor; --exp;
+
+ if ( (*p | 32) == 'e' ) {
+ expo = 0;
+ factor = 10.L;
+
+ switch (*++p) { // ja hier weiß ich nicht, was mindestens nach einem 'E' folgenden MUSS.
+ case '-': factor = 0.1;
+ case '+': p++;
+ break;
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
+ break;
+ default : value = 0.L;
+ p = s;
+ goto done;
+ }
+
+ while ( (unsigned int)(*p - '0') < 10u )
+ expo = 10 * expo + (*p++ - '0');
+
+ while ( 1 ) {
+ if ( expo & 1 )
+ value *= factor;
+ if ( (expo >>= 1) == 0 )
+ break;
+ factor *= factor;
+ }
}
- }
+
done:
- if (endptr) *endptr=(char*)c;
- return d;
+ if ( endptr != NULL )
+ *endptr = (char*)p;
+
+ return value * sign;
}
diff --git a/mdk-stage1/dietlibc/lib/strtok.c b/mdk-stage1/dietlibc/lib/strtok.c
index 86337da7b..e041703e3 100644
--- a/mdk-stage1/dietlibc/lib/strtok.c
+++ b/mdk-stage1/dietlibc/lib/strtok.c
@@ -1,4 +1,4 @@
-char *strtok_r(char *s, const char *delim, char **ptrptr);
+#include <string.h>
static char *strtok_pos;
diff --git a/mdk-stage1/dietlibc/lib/strtol.c b/mdk-stage1/dietlibc/lib/strtol.c
index ead89f408..2624aca0e 100644
--- a/mdk-stage1/dietlibc/lib/strtol.c
+++ b/mdk-stage1/dietlibc/lib/strtol.c
@@ -1,7 +1,10 @@
#include <ctype.h>
+#include "dietfeatures.h"
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
-extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
-
+#define ABS_LONG_MIN 2147483648UL
long int strtol(const char *nptr, char **endptr, int base)
{
int neg=0;
@@ -9,7 +12,15 @@ long int strtol(const char *nptr, char **endptr, int base)
while(isspace(*nptr)) nptr++;
- if (*nptr == '-' && isdigit(nptr[1])) { neg=-1; nptr++; }
+ if (*nptr == '-') { neg=-1; ++nptr; }
v=strtoul(nptr,endptr,base);
+ if (v>=ABS_LONG_MIN) {
+ if (v==ABS_LONG_MIN && neg) {
+ errno=0;
+ return v;
+ }
+ errno=ERANGE;
+ return (neg?LONG_MIN:LONG_MAX);
+ }
return (neg?-v:v);
}
diff --git a/mdk-stage1/dietlibc/lib/strtoll.c b/mdk-stage1/dietlibc/lib/strtoll.c
index 7b8e28a1d..9e05e740e 100644
--- a/mdk-stage1/dietlibc/lib/strtoll.c
+++ b/mdk-stage1/dietlibc/lib/strtoll.c
@@ -1,8 +1,7 @@
#include <ctype.h>
+#include <stdlib.h>
-extern unsigned long int strtoull(const char *nptr, char **endptr, int base);
-
-long int strtoll(const char *nptr, char **endptr, int base)
+long long int strtoll(const char *nptr, char **endptr, int base)
{
int neg=0;
unsigned long long int v;
diff --git a/mdk-stage1/dietlibc/lib/strtoul.c b/mdk-stage1/dietlibc/lib/strtoul.c
index 3f93962a9..e9c09fb59 100644
--- a/mdk-stage1/dietlibc/lib/strtoul.c
+++ b/mdk-stage1/dietlibc/lib/strtoul.c
@@ -1,32 +1,44 @@
#include <ctype.h>
-
-/* static char *num="0123456789abcdefghijklmnopqrstuvwxyz"; */
+#include "dietfeatures.h"
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
unsigned long int strtoul(const char *nptr, char **endptr, int base)
{
- long int v=0;
+ int neg = 0;
+ unsigned long int v=0;
while(isspace(*nptr)) ++nptr;
-
+ if (*nptr == '-') { neg=1; nptr++; }
if (*nptr == '+') ++nptr;
+ if (base==16 && nptr[0]=='0') goto skip0x;
if (!base) {
if (*nptr=='0') {
base=8;
- if ((*(nptr+1)=='x')||(*(nptr+1)=='X')) {
+skip0x:
+ if (nptr[1]=='x'||nptr[1]=='X') {
nptr+=2;
base=16;
}
- }
- else
+ } else
base=10;
}
while(*nptr) {
register unsigned char c=*nptr;
- c=(c>='a'?c-'a'+10:c>='A'?c-'A'+10:c-'0');
+ c=(c>='a'?c-'a'+10:c>='A'?c-'A'+10:c<='9'?c-'0':0xff);
if (c>=base) break;
- v=v*base+c;
+ {
+ register unsigned long int w=v*base;
+ if (w<v) {
+ errno=ERANGE;
+ return ULONG_MAX;
+ }
+ v=w+c;
+ }
++nptr;
}
if (endptr) *endptr=(char *)nptr;
- return v;
+ errno=0; /* in case v==ULONG_MAX, ugh! */
+ return (neg?-v:v);
}
diff --git a/mdk-stage1/dietlibc/lib/strtoull.c b/mdk-stage1/dietlibc/lib/strtoull.c
index 40818958d..d6073e32a 100644
--- a/mdk-stage1/dietlibc/lib/strtoull.c
+++ b/mdk-stage1/dietlibc/lib/strtoull.c
@@ -1,4 +1,5 @@
#include <ctype.h>
+#include <stdlib.h>
unsigned long long int strtoull(const char *nptr, char **endptr, int base)
{
diff --git a/mdk-stage1/dietlibc/lib/sys_siglist.c b/mdk-stage1/dietlibc/lib/sys_siglist.c
index 80d7b3fb0..a753848d9 100644
--- a/mdk-stage1/dietlibc/lib/sys_siglist.c
+++ b/mdk-stage1/dietlibc/lib/sys_siglist.c
@@ -1,37 +1,174 @@
-const char *const sys_siglist[] =
- {
- "Signal 0",
- "Hangup",
- "Interrupt",
- "Quit",
- "Illegal instruction",
- "Trace/breakpoint trap",
- "IOT trap",
- "EMT trap",
- "Floating point exception",
- "Killed",
- "Bus error",
- "Segmentation fault",
- "Bad system call",
- "Broken pipe",
- "Alarm clock",
- "Terminated",
- "Urgent I/O condition",
- "Stopped (signal)",
- "Stopped",
- "Continued",
- "Child exited",
- "Stopped (tty input)",
- "Stopped (tty output)",
- "I/O possible",
- "CPU time limit exceeded",
- "File size limit exceeded",
- "Virtual timer expired",
- "Profiling timer expired",
- "Window changed",
- "Resource lost",
- "User defined signal 1",
- "User defined signal 2",
- 0
- };
+const char *const sys_siglist[] = {
+ "Signal 0",
+ "Hangup",
+ "Interrupt",
+ "Quit",
+ "Illegal instruction",
+ "Trace/breakpoint trap",
+ "ABRT/IOT trap",
+#if defined(__alpha__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__)
+ "EMT trap",
+#else
+ "Bus error",
+#endif
+ "Floating point exception",
+ "Killed",
+#if defined(__alpha__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__)
+ "Bus error",
+#else
+ "User defined signal 1",
+#endif
+ "Segmentation fault",
+#if defined(__alpha__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__)
+ "Bad system call",
+#else
+ "User defined signal 2",
+#endif
+ "Broken pipe",
+ "Alarm clock",
+ "Terminated",
+#if defined(__hppa__)
+ "User defined signal 1",
+ "User defined signal 2",
+ "Child exited",
+ "Power lost",
+ "Virtual timer expired",
+ "Profiling timer expired",
+ "I/O possible",
+ "Window changed",
+ "Stopped (signal)",
+ "Stopped",
+ "Continued",
+ "Stopped (tty input)",
+ "Stopped (tty output)",
+ "Urgent I/O condition",
+ "Power lost",
+ "Unknown",
+ "Unknown",
+ "CPU time limit exceeded",
+ "File size limit exceeded",
+ "Unknown",
+ "Stack fault",
+#elif defined(__mips__)
+ "User defined signal 1",
+ "User defined signal 2",
+ "Child exited",
+ "Power lost",
+ "Window changed",
+ "Urgent I/O condition",
+ "I/O possible",
+ "Stopped (signal)",
+ "Stopped",
+ "Continued",
+ "Stopped (tty input)",
+ "Stopped (tty output)",
+ "Virtual timer expired",
+ "Profiling timer expired",
+ "CPU time limit exceeded",
+ "File size limit exceeded",
+#elif defined(__alpha__) || defined(__sparc__)
+ "Urgent I/O condition",
+ "Stopped (signal)",
+ "Stopped",
+ "Continued",
+ "Child exited",
+ "Stopped (tty input)",
+ "Stopped (tty output)",
+ "I/O possible",
+ "CPU time limit exceeded",
+ "File size limit exceeded",
+ "Virtual timer expired",
+ "Profiling timer expired",
+ "Window changed",
+ "Power/Resource lost",
+ "User defined signal 1",
+ "User defined signal 2",
+#else
+ "Stack fault",
+ "Child exited",
+ "Continued",
+ "Stopped (signal)",
+ "Stopped",
+ "Stopped (tty input)",
+ "Stopped (tty output)",
+ "Urgent I/O condition",
+ "CPU time limit exceeded",
+ "File size limit exceeded",
+ "Virtual timer expired",
+ "Profiling timer expired",
+ "Window changed",
+ "I/O possible",
+ "Power lost",
+ "Bad system call",
+#endif
+#ifndef __hppa__
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+#endif
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+#ifdef __mips__
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+ "Real time signal",
+#endif
+ 0
+};
diff --git a/mdk-stage1/dietlibc/lib/tcdrain.c b/mdk-stage1/dietlibc/lib/tcdrain.c
index 7a760f643..bb2018fb4 100644
--- a/mdk-stage1/dietlibc/lib/tcdrain.c
+++ b/mdk-stage1/dietlibc/lib/tcdrain.c
@@ -1,5 +1,6 @@
#include <sys/ioctl.h>
+int __libc_tcdrain(int fd);
int __libc_tcdrain(int fd)
{
return ioctl(fd, TCSBRK, 1);
diff --git a/mdk-stage1/dietlibc/lib/tcflush.c b/mdk-stage1/dietlibc/lib/tcflush.c
index a19fe11d1..2e5447728 100644
--- a/mdk-stage1/dietlibc/lib/tcflush.c
+++ b/mdk-stage1/dietlibc/lib/tcflush.c
@@ -1,23 +1,9 @@
-#include <unistd.h>
-#include <termios.h>
#include <sys/ioctl.h>
-#include <asm/errno.h>
-
-extern int errno;
-
-/* Flush pending data on FD. */
-int tcflush(int fd, int queue_selector)
+int __libc_tcflush(int fd, int queue_selector);
+int __libc_tcflush(int fd, int queue_selector)
{
- switch (queue_selector) {
- case TCIFLUSH:
- return ioctl(fd, TCFLSH, 0);
- case TCOFLUSH:
- return ioctl(fd, TCFLSH, 1);
- case TCIOFLUSH:
- return ioctl(fd, TCFLSH, 2);
- default:
- errno = EINVAL;
- return -1;
- }
+ return ioctl(fd, TCFLSH, queue_selector);
}
+
+int tcflush(int fd) __attribute__((weak,alias("__libc_tcflush")));
diff --git a/mdk-stage1/dietlibc/lib/tcgetpgrp.c b/mdk-stage1/dietlibc/lib/tcgetpgrp.c
index f2a290651..8d5d4614a 100644
--- a/mdk-stage1/dietlibc/lib/tcgetpgrp.c
+++ b/mdk-stage1/dietlibc/lib/tcgetpgrp.c
@@ -1,7 +1,12 @@
#include <termios.h>
#include <sys/ioctl.h>
+#include <unistd.h>
int tcgetpgrp(int fildes)
{
- return ioctl(fildes, TIOCGPGRP);
+ int foo;
+ if (ioctl(fildes, TIOCGPGRP, &foo)==-1)
+ return -1;
+ else
+ return foo;
}
diff --git a/mdk-stage1/dietlibc/lib/tcsetattr.c b/mdk-stage1/dietlibc/lib/tcsetattr.c
index cf70354d2..03fe8adee 100644
--- a/mdk-stage1/dietlibc/lib/tcsetattr.c
+++ b/mdk-stage1/dietlibc/lib/tcsetattr.c
@@ -1,27 +1,37 @@
-#define tcsetattr libc_tcsetattr
#include <termios.h>
#include <sys/ioctl.h>
-#undef tcsetattr
+#include <errno.h>
+#include "dietfeatures.h"
-#include <asm/errno.h>
+#if !defined(__powerpc__) && !defined(__sparc__) && !defined(__alpha__) && !defined(__hppa__)
+#if TCSANOW==0 && TCSADRAIN==1 && TCSAFLUSH==2 && TCSETSW-TCSETS==1 && TCSETSF-TCSETS==2
+#define shortcut
+#endif
+#endif
-extern int errno;
+int tcsetattr ( int fildes, int optional_actions, struct termios* termios_p )
+{
+#ifdef shortcut
-/* Hack around a kernel bug; value must correspond to the one used in speed.c */
-#define IBAUD0 020000000000
+ if ( (unsigned int)optional_actions < 3u )
+ return ioctl ( fildes, TCSETS+optional_actions, termios_p );
-int tcsetattr(int fildes, int optional_actions, struct termios *termios_p)
-{
- termios_p->c_iflag &= ~IBAUD0;
- switch (optional_actions) {
- case TCSANOW:
- return ioctl(fildes, TCSETS, termios_p);
- case TCSADRAIN:
- return ioctl(fildes, TCSETSW, termios_p);
- case TCSAFLUSH:
- return ioctl(fildes, TCSETSF, termios_p);
- default:
errno = EINVAL;
return -1;
- }
+
+#else
+
+ switch ( optional_actions ) {
+ case TCSANOW:
+ return ioctl ( fildes, TCSETS , termios_p );
+ case TCSADRAIN:
+ return ioctl ( fildes, TCSETSW, termios_p );
+ case TCSAFLUSH:
+ return ioctl ( fildes, TCSETSF, termios_p );
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+
+#endif
}
diff --git a/mdk-stage1/dietlibc/lib/tcsetpgrp.c b/mdk-stage1/dietlibc/lib/tcsetpgrp.c
index 6c40b5c79..8cf3165c2 100644
--- a/mdk-stage1/dietlibc/lib/tcsetpgrp.c
+++ b/mdk-stage1/dietlibc/lib/tcsetpgrp.c
@@ -1,5 +1,6 @@
#include <termios.h>
#include <sys/ioctl.h>
+#include <unistd.h>
int tcsetpgrp(int fildes, pid_t pgrpid)
{
diff --git a/mdk-stage1/dietlibc/lib/tolower.c b/mdk-stage1/dietlibc/lib/tolower.c
index 4b7c7cf2d..c24932ef4 100644
--- a/mdk-stage1/dietlibc/lib/tolower.c
+++ b/mdk-stage1/dietlibc/lib/tolower.c
@@ -1,6 +1,8 @@
#include <ctype.h>
-inline int tolower(int c) {
- return (c>='A' && c<='Z')?c-'A'+'a':c;
+int tolower(int ch) {
+ if ( (unsigned int)(ch - 'A') < 26u )
+ ch += 'a' - 'A';
+ return ch;
}
diff --git a/mdk-stage1/dietlibc/lib/toupper.c b/mdk-stage1/dietlibc/lib/toupper.c
index c048e60bb..f7d415e95 100644
--- a/mdk-stage1/dietlibc/lib/toupper.c
+++ b/mdk-stage1/dietlibc/lib/toupper.c
@@ -1,6 +1,8 @@
#include <ctype.h>
-inline int toupper(int c) {
- return (c>='a' && c<='z')?c-'a'+'A':c;
+inline int toupper(int ch) {
+ if ( (unsigned int)(ch - 'a') < 26u )
+ ch += 'A' - 'a';
+ return ch;
}
diff --git a/mdk-stage1/dietlibc/lib/ttyname.c b/mdk-stage1/dietlibc/lib/ttyname.c
index a6b479088..2bbc7f2a9 100644
--- a/mdk-stage1/dietlibc/lib/ttyname.c
+++ b/mdk-stage1/dietlibc/lib/ttyname.c
@@ -1,30 +1,35 @@
#include "dietfeatures.h"
#include <unistd.h>
#include <sys/stat.h>
+#include <string.h>
#ifdef __linux__
-extern int __ltostr(char *s, int size, unsigned long i, int base, char UpCase);
+#include <stdlib.h>
char *ttyname(int fd) {
#ifdef SLASH_PROC_OK
char ibuf[20];
static char obuf[20];
+ int len;
+ if (!isatty(fd)) return 0;
strcpy(ibuf,"/proc/self/fd/");
- ibuf[__ltostr(ibuf+14,6,fd,10,0)+14]=0;
- if (readlink(ibuf,obuf,sizeof(obuf)-1)<0) return 0;
+ ibuf[__ltostr(ibuf+14,6,(unsigned long)fd,10,0)+14]=0;
+ if ((len=readlink(ibuf,obuf,sizeof(obuf)-1))<0) return 0;
+ obuf[len]=0;
return obuf;
#else
- static char buf[20]="/dev/tty";
+ static char buf[20];
struct stat s;
char *c=buf+8;
int n;
+ if (!isatty(fd)) return 0;
if (fstat(fd,&s)) return 0;
+ strcpy(buf,"/dev/tty");
if (S_ISCHR(s.st_mode)) {
n=minor(s.st_rdev);
switch (major(s.st_rdev)) {
case 4:
- buf[5]='t'; buf[7]='y';
if (n>63) {
n-=64;
*c='S';
@@ -34,16 +39,18 @@ num:
c[__ltostr(c,6,n,10,0)]=0;
break;
case 2:
- buf[5]='p'; buf[7]='y';
buf[8]='p'-(n>>4);
buf[9]=n%4+'0';
if (buf[9]>'9') *c+='a'-'0';
buf[10]=0;
+ goto duh;
case 136:
case 137:
case 138:
case 139:
- buf[5]='p'; buf[7]='s';
+ buf[7]='s';
+duh:
+ buf[5]='p';
n+=(major(s.st_rdev)-136)<<8;
*c='/'; ++c;
goto num;
diff --git a/mdk-stage1/dietlibc/lib/usleep.c b/mdk-stage1/dietlibc/lib/usleep.c
index 494f99233..8f07416a8 100644
--- a/mdk-stage1/dietlibc/lib/usleep.c
+++ b/mdk-stage1/dietlibc/lib/usleep.c
@@ -1,12 +1,12 @@
-#include <linux/time.h>
#include <time.h>
+#include <unistd.h>
/* nano * 1000 == usecs
* usecs * 1000 == msecs
* msecs * 1000 = secs */
-void usleep(unsigned int usecs) {
+int usleep(unsigned long usecs) {
struct timespec t;
t.tv_sec=usecs/1000000;
t.tv_nsec=(usecs%1000000)*1000;
- nanosleep(&t,&t);
+ return nanosleep(&t,&t);
}
diff --git a/mdk-stage1/dietlibc/lib/vfork.c b/mdk-stage1/dietlibc/lib/vfork.c
index 795cddb05..d54416d5d 100644
--- a/mdk-stage1/dietlibc/lib/vfork.c
+++ b/mdk-stage1/dietlibc/lib/vfork.c
@@ -1,6 +1,4 @@
-#include <linux/types.h>
#include <unistd.h>
-pid_t vfork(void) {
- return fork();
-}
+pid_t vfork() { return fork(); }
+
diff --git a/mdk-stage1/dietlibc/lib/vfprintf.c b/mdk-stage1/dietlibc/lib/vfprintf.c
deleted file mode 100644
index c33a2404b..000000000
--- a/mdk-stage1/dietlibc/lib/vfprintf.c
+++ /dev/null
@@ -1,19 +0,0 @@
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "dietstdarg.h"
-
-int vfprintf(FILE *fstream, const char *format, va_list ap)
-{
- char *tmp;
- va_list cp_ap;
- size_t n = 0;
-
- va_copy(cp_ap, ap);
- n=vsnprintf(0, 1000000, format, cp_ap);
- tmp=alloca(n+2);
- vsnprintf(tmp, n+1, format, ap);
- fwrite(tmp, n,1, fstream);
- return n;
-}
-
diff --git a/mdk-stage1/dietlibc/lib/vprintf.c b/mdk-stage1/dietlibc/lib/vprintf.c
deleted file mode 100644
index 46ecc47a4..000000000
--- a/mdk-stage1/dietlibc/lib/vprintf.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <stdarg.h>
-#include <linux/types.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-int vsnprintf (char *str,size_t size,const char *format, va_list arg_ptr);
-
-int vprintf(const char *format, va_list ap)
-{
- char tmp[1000000];
- size_t n = vsnprintf(tmp, sizeof(tmp), format, ap);
- write(1, tmp, n);
- return n;
-}
diff --git a/mdk-stage1/dietlibc/lib/vsnprintf.c b/mdk-stage1/dietlibc/lib/vsnprintf.c
index f425066e3..dd06d6483 100644
--- a/mdk-stage1/dietlibc/lib/vsnprintf.c
+++ b/mdk-stage1/dietlibc/lib/vsnprintf.c
@@ -1,238 +1,33 @@
-#include "dietfeatures.h"
#include <stdarg.h>
#include <sys/types.h>
+#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include "dietwarning.h"
-
-extern int __ltostr(char *s, int size, unsigned long i, int base, char UpCase);
-extern int __dtostr(double d,char *buf,int maxlen,int prec);
-
-int vsnprintf (char *str, size_t size, const char *format, va_list arg_ptr)
-{
- size_t apos,i;
- char ch,buf[1024];
- char *pb;
- char flag_in_sign;
- char flag_hash,flag_zero,flag_left,flag_space,flag_sign,flag_dot,flag_long;
- long number,width,preci,buf_len,pad;
- char padwith;
-
- size--;
-
- apos=0;
- while (apos<size)
- {
- ch=*format++;
- switch (ch)
- {
- case '%':
- flag_hash=0;
- flag_zero=0;
- flag_left=0;
- flag_space=0;
- flag_sign=0;
- flag_dot=0;
- flag_in_sign=0;
- flag_long=0;
-
- width=0;
- padwith=' ';
-
-inn_vsnprintf:
- if (apos>=size) continue; /* ARGL !!! */
-
- ch=*format++;
- switch (ch)
- {
-/* Format end ?!? */
- case 0:
- return -1;
- break;
-
-/* Format flag chars */
- case '#':
- flag_hash=1;
- goto inn_vsnprintf;
-
- case 'l':
- ++flag_long;
- goto inn_vsnprintf;
-
- case '0':
- padwith='0';
- goto inn_vsnprintf;
-
- case '-':
- flag_left=1;
- goto inn_vsnprintf;
-
- case ' ':
- flag_space=1;
- goto inn_vsnprintf;
-
- case '+':
- flag_sign=1;
- goto inn_vsnprintf;
-
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if(flag_dot) return -1;
- width=strtol(--format,&pb,10);
- format=pb;
- goto inn_vsnprintf;
-
- case '*':
- width=va_arg(arg_ptr,int);
- goto inn_vsnprintf;
-
- case '.':
- flag_dot=1;
- if (*format=='*') {
- preci=va_arg(arg_ptr,int);
- ++format;
- } else {
- preci=strtol(format,&pb,10);
- format=pb;
- }
- goto inn_vsnprintf;
-
-/* Format conversion chars */
- case 'c':
- ch=(char)va_arg(arg_ptr,int);
- case '%':
- if (str) str[apos]=ch; ++apos;
- break;
-
- case 's':
- pb=va_arg(arg_ptr,char *);
-#ifdef WANT_NULL_PRINTF
- if (!pb) pb="(null)";
-#endif
- buf_len=strlen(pb);
- if (flag_dot && buf_len>preci) buf_len=preci;
- if (buf_len>size-apos) buf_len=size-apos;
-
-print_out:
- if (str) {
- if (width && (!flag_left))
- {
- for (pad=width-buf_len; pad>0; --pad) str[apos++]=padwith;
- }
- for(i=0;i<buf_len;++i) { str[apos++]=pb[i]; } /* strncpy */
- if (width && (flag_left))
- {
- for (pad=width-buf_len; pad>0; --pad) str[apos++]=padwith;
- }
- } else {
- if (width) {
- apos+=width>buf_len?width:buf_len;
- } else {
- apos+=size>buf_len?buf_len:size;
- }
- }
-
- break;
-
- /* Numbers */
- case 'b':
- i=2;
- goto num_vsnprintf;
- case 'p':
- flag_hash=1;
- width=sizeof(void *)<<1;
- padwith='0';
- ch='x';
- case 'X':
- case 'x':
- i=16;
- if (flag_hash)
- {
- if (str) {
- str[apos++]='0';
- str[apos++]=ch;
- } else
- apos+=2;
- }
- goto num_vsnprintf;
- case 'd':
- case 'i':
- flag_in_sign=1;
- case 'u':
- i=10;
- goto num_vsnprintf;
- case 'o':
- i=8;
- if (flag_hash) { if (str) str[apos]='0'; ++apos; }
-
-num_vsnprintf:
- if (apos>=size) continue; /* ARGL !!! */
-
- if (flag_long)
- number=va_arg(arg_ptr,long);
- else
- number=va_arg(arg_ptr,int);
-
- if (flag_in_sign && (number<0))
- {
- number*=-1;
- flag_in_sign=2;
- }
-
- buf_len=__ltostr(buf+1,sizeof(buf)-1,(unsigned long) number,i,0);
- pb=buf+1;
-
- if (flag_in_sign==2)
- {
- *(--pb)='-';
- buf_len++;
- }
- else if ((flag_in_sign)&&(flag_sign || flag_space))
- {
- *(--pb)=(flag_sign)?'+':' ';
- buf_len++;
- }
- goto print_out;
-
-#ifdef WANT_FLOATING_POINT_IN_PRINTF
- case 'f':
- case 'g':
- {
- double d=va_arg(arg_ptr,double);
- buf_len=__dtostr(d,buf,sizeof(buf),width?width:6);
- if (flag_dot) {
- char *tmp;
- if ((tmp=strchr(buf,'.'))) {
- while (preci>-1 && *++tmp) --preci;
- *tmp=0;
- }
- }
- pb=buf;
- goto print_out;
- }
-#endif
- default:
- break;
- }
- break;
- case 0:
- if (str) str[apos]=0;
- return apos;
- default:
- if (str) str[apos]=ch; apos++;
- break;
+#include "dietstdio.h"
+
+struct str_data {
+ unsigned char* str;
+ size_t len;
+ size_t size;
+};
+
+static int swrite(void*ptr, size_t nmemb, struct str_data* sd) {
+ size_t tmp=sd->size-sd->len;
+ if (tmp>0) {
+ size_t len=nmemb;
+ if (len>tmp) len=tmp;
+ if (sd->str) {
+ memcpy(sd->str+sd->len,ptr,len);
+ sd->str[sd->len+len]=0;
}
+ sd->len+=len;
}
- if (str) str[apos]=0;
- return apos;
+ return nmemb;
}
-link_warning("vsnprintf","warning: the printf functions add several kilobytes of bloat.")
-
+int vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr) {
+ struct str_data sd = { str, 0, size };
+ struct arg_printf ap = { &sd, (int(*)(void*,size_t,void*)) swrite };
+ if (size) --sd.size;
+ return __v_printf(&ap,format,arg_ptr);
+}
diff --git a/mdk-stage1/dietlibc/lib/vsprintf.c b/mdk-stage1/dietlibc/lib/vsprintf.c
index 10ff04bd7..6cd5a40b1 100644
--- a/mdk-stage1/dietlibc/lib/vsprintf.c
+++ b/mdk-stage1/dietlibc/lib/vsprintf.c
@@ -1,10 +1,12 @@
#include <stdarg.h>
-#include <linux/types.h>
+#include <sys/types.h>
#include <stdlib.h>
-
-int vsnprintf (char *str,size_t size,const char *format, va_list arg_ptr);
+#include <stdio.h>
+#include "dietwarning.h"
int vsprintf(char *dest,const char *format, va_list arg_ptr)
{
- return vsnprintf(dest,1000000,format,arg_ptr);
+ return vsnprintf(dest,(size_t)-1,format,arg_ptr);
}
+
+link_warning("vsprintf","warning: Avoid *sprintf; use *snprintf. It is more secure.")
diff --git a/mdk-stage1/dietlibc/lib/vsscanf.c b/mdk-stage1/dietlibc/lib/vsscanf.c
index 1c4c9b787..329cfd02a 100644
--- a/mdk-stage1/dietlibc/lib/vsscanf.c
+++ b/mdk-stage1/dietlibc/lib/vsscanf.c
@@ -1,304 +1,25 @@
-#include "dietfeatures.h"
#include <stdarg.h>
#include <sys/types.h>
-#include <ctype.h>
#include <stdlib.h>
-#include <string.h>
+#include "dietstdio.h"
+#include <unistd.h>
-const char *skip_ws(const char *str)
-{
- while ((*str)&&(isspace(*str))) str++;
- return str;
+struct str_data {
+ unsigned char* str;
+};
+
+static int sgetc(struct str_data* sd) {
+ register unsigned int ret = *(sd->str++);
+ return (ret)?(int)ret:-1;
}
-extern double strtod(const char *s,char **f);
+static int sputc(int c, struct str_data* sd) {
+ return (*(--sd->str)==c)?c:-1;
+}
-int vsscanf(const char *str, const char *format, va_list arg_ptr)
+int vsscanf(const char* str, const char* format, va_list arg_ptr)
{
- int n=0,div;
- unsigned char ch;
-
- char flag_discard, flag_malloc, flag_half, flag_long, flag_longlong;
- char flag_width;
-
- unsigned long width;
-
- /* arg_ptr tmps */
-#ifdef WANT_FLOATING_POINT_IN_SCANF
- double d,*pd;
- float *pf;
-#endif
-
- long l=0,*pl;
- short *ph;
- int *pi;
- char *s;
-
- while ((*str)&&(*format))
- {
- const char *prevfmt=format;
- format=skip_ws(format);
- ch=*format++;
- if (!ch) continue;
-
- switch (ch)
- {
- case '%':
- div=0;
- flag_discard=0;
- flag_malloc=0;
- flag_half=0;
- flag_long=0;
- flag_longlong=0;
-
- flag_width=0;
- width=-1;
-
-inn_vsscanf:
- ch=*format++;
-
- switch (ch)
- {
- case 0:
- return 0;
-
- case '%':
- if (*(str++)!=ch) return n;
- break;
-
- /* flags */
- case '*':
- flag_discard=1;
- goto inn_vsscanf;
-
- case 'a':
- flag_malloc=1;
- goto inn_vsscanf;
-
- case 'h':
- flag_half=1;
- goto inn_vsscanf;
-
- case 'l':
- if (flag_long) flag_longlong=1;
- flag_long=1;
- goto inn_vsscanf;
-
- /* longlong ? NOT YET ! */
- case 'q':
- case 'L':
- flag_longlong=1;
- goto inn_vsscanf;
-
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- width=strtol(format-1,&s,10);
- format=s;
- flag_width=1;
- goto inn_vsscanf;
-
- /* conversion */
-
- case 'n':
- while (width && *str)
- {
- *(s++)=*(str++);
- --width;
- l++;
- }
- if (!flag_discard)
- {
- pl=(long *)va_arg(arg_ptr,long*);
- *pl=l;
- ++n;
- }
- break;
-
- case 'p':
- case 'X':
- case 'x':
- div+=6;
- case 'd':
- div+=2;
- case 'o':
- div+=8;
- case 'u':
- case 'i':
- if (*(str=skip_ws(str)))
- {
- l=strtol(str,&s,div);
- if (str!=s)
- {
- if (!flag_discard)
- {
- if (flag_long)
- {
- pl=(long *)va_arg(arg_ptr,long*);
- *pl=l;
- }
- else if (flag_half)
- {
- ph=(short *)va_arg(arg_ptr,short*);
- *ph=l;
- }
- else
- {
- pi=(int *)va_arg(arg_ptr,int*);
- *pi=l;
- }
- ++n;
- }
- str=s;
- }
- else
- return n;
- }
- break;
-
-#ifdef WANT_FLOATING_POINT_IN_SCANF
- case 'e':
- case 'E':
- case 'f':
- case 'g':
- if (*(str=skip_ws(str)))
- {
- d=strtod(str,&s);
- if (str!=s)
- {
- if (!flag_discard)
- {
- if (flag_long)
- {
- pd=(double *)va_arg(arg_ptr,double*);
- *pd=d;
- }
- else
- {
- pf=(float *)va_arg(arg_ptr,float*);
- *pf=d;
- }
- ++n;
- }
- str=s;
- }
- else
- return n;
- }
- break;
-#endif
-
- case 'c':
- if (!flag_discard)
- {
- s=(char *)va_arg(arg_ptr,char*);
- ++n;
- }
- if (!flag_width) width=1;
- while (width && *str)
- {
- if (!flag_discard) *(s++)=*(str);
- ++str;
- --width;
- }
- break;
-
- case 's':
- if (!flag_discard)
- {
- s=(char *)va_arg(arg_ptr,char*);
- ++n;
- }
- if (*(str=skip_ws(str)))
- {
- while (width && (!isspace(*str)))
- {
- if (!flag_discard) *(s++)=*(str);
- if (!*str) break;
- ++str;
- --width;
- }
- *s=0;
- }
- break;
-
-#ifdef WANT_CHARACTER_CLASSES_IN_SCANF
- case '[':
- {
- char cset[256];
- int flag_not=0;
- int flag_dash=0;
- memset(cset,0,sizeof(cset));
- ch=*format++;
-
- /* first char specials */
- if (ch=='^')
- {
- flag_not=1;
- ch=*format++;
- }
- if ((ch=='-')||(ch==']'))
- {
- cset[ch]=1;
- ch=*format++;
- }
-
- /* almost all non special chars */
- for (;(*format) && (*format!=']');++format) {
- if (flag_dash)
- {
- register unsigned char tmp=*format;
- for (;ch<=tmp;++ch) cset[ch]=1;
- flag_dash=0;
- ch=*(++format);
- }
- else if (*format=='-') flag_dash=1;
- else
- {
- cset[ch]=1;
- ch=*format;
- }
- }
-
- /* last char specials */
- if (flag_dash) cset['-']=1;
- else cset[ch]=1;
-
- /* like %c or %s */
- if (!flag_discard)
- {
- s=(char *)va_arg(arg_ptr,char*);
- ++n;
- }
- while (width && (cset[(unsigned char)(*str)]-flag_not))
- {
- if (!flag_discard) *(s++)=*(str);
- if (!*str) break;
- ++str;
- --width;
- }
- }
- break;
-#endif
- }
- break;
-
- default:
- if (prevfmt<format) {
- while (prevfmt<format) {
- if (*str!=*prevfmt) return n;
- ++str; ++prevfmt;
- }
- } else
- if (*(str++)!=ch) return n;
- break;
- }
- }
- return n;
+ struct str_data fdat = { (unsigned char*)str };
+ struct arg_scanf farg = { (void*)&fdat, (int(*)(void*))sgetc, (int(*)(int,void*))sputc };
+ return __v_scanf(&farg,format,arg_ptr);
}