From 3e3a822d182adc09a3969f4f37489c07755c52f8 Mon Sep 17 00:00:00 2001 From: Guillaume Cottenceau Date: Mon, 14 May 2001 13:46:55 +0000 Subject: v0_8 --- mdk-stage1/dietlibc/libcruft/___div.c | 11 ++ mdk-stage1/dietlibc/libcruft/dnscruft.c | 115 +++++++++++++++ mdk-stage1/dietlibc/libcruft/dnscruft2.c | 185 ++++++++++++++++++++++++ mdk-stage1/dietlibc/libcruft/entlib.c | 153 ++++++++++++++++++++ mdk-stage1/dietlibc/libcruft/entlib.h | 44 ++++++ mdk-stage1/dietlibc/libcruft/getdtablesize.c | 6 + mdk-stage1/dietlibc/libcruft/getgrgid.c | 14 ++ mdk-stage1/dietlibc/libcruft/getgrnam.c | 14 ++ mdk-stage1/dietlibc/libcruft/gethostbyaddr.c | 38 +++++ mdk-stage1/dietlibc/libcruft/gethostbyaddr_r.c | 51 +++++++ mdk-stage1/dietlibc/libcruft/gethostbyname.c | 39 +++++ mdk-stage1/dietlibc/libcruft/gethostbyname2.c | 40 +++++ mdk-stage1/dietlibc/libcruft/gethostbyname2_r.c | 36 +++++ mdk-stage1/dietlibc/libcruft/gethostbyname_r.c | 30 ++++ mdk-stage1/dietlibc/libcruft/getlogin.c | 5 + mdk-stage1/dietlibc/libcruft/getpagesize.c | 14 ++ mdk-stage1/dietlibc/libcruft/getpass.c | 40 +++++ mdk-stage1/dietlibc/libcruft/getpwnam.c | 14 ++ mdk-stage1/dietlibc/libcruft/getpwuid.c | 14 ++ mdk-stage1/dietlibc/libcruft/getspnam.c | 14 ++ mdk-stage1/dietlibc/libcruft/grent.c | 54 +++++++ mdk-stage1/dietlibc/libcruft/h_errno.c | 1 + mdk-stage1/dietlibc/libcruft/herrno_location.c | 5 + mdk-stage1/dietlibc/libcruft/inet_addr.c | 11 ++ mdk-stage1/dietlibc/libcruft/inet_aton.c | 43 ++++++ mdk-stage1/dietlibc/libcruft/inet_ntoa.c | 10 ++ mdk-stage1/dietlibc/libcruft/inet_ntoa_r.c | 22 +++ mdk-stage1/dietlibc/libcruft/inet_ntop.c | 75 ++++++++++ mdk-stage1/dietlibc/libcruft/inet_pton.c | 97 +++++++++++++ mdk-stage1/dietlibc/libcruft/initgroups.c | 23 +++ mdk-stage1/dietlibc/libcruft/mkstemp.c | 23 +++ mdk-stage1/dietlibc/libcruft/pwent.c | 43 ++++++ mdk-stage1/dietlibc/libcruft/res_init.c | 7 + mdk-stage1/dietlibc/libcruft/spent.c | 45 ++++++ 34 files changed, 1336 insertions(+) create mode 100644 mdk-stage1/dietlibc/libcruft/___div.c create mode 100644 mdk-stage1/dietlibc/libcruft/dnscruft.c create mode 100644 mdk-stage1/dietlibc/libcruft/dnscruft2.c create mode 100644 mdk-stage1/dietlibc/libcruft/entlib.c create mode 100644 mdk-stage1/dietlibc/libcruft/entlib.h create mode 100644 mdk-stage1/dietlibc/libcruft/getdtablesize.c create mode 100644 mdk-stage1/dietlibc/libcruft/getgrgid.c create mode 100644 mdk-stage1/dietlibc/libcruft/getgrnam.c create mode 100644 mdk-stage1/dietlibc/libcruft/gethostbyaddr.c create mode 100644 mdk-stage1/dietlibc/libcruft/gethostbyaddr_r.c create mode 100644 mdk-stage1/dietlibc/libcruft/gethostbyname.c create mode 100644 mdk-stage1/dietlibc/libcruft/gethostbyname2.c create mode 100644 mdk-stage1/dietlibc/libcruft/gethostbyname2_r.c create mode 100644 mdk-stage1/dietlibc/libcruft/gethostbyname_r.c create mode 100644 mdk-stage1/dietlibc/libcruft/getlogin.c create mode 100644 mdk-stage1/dietlibc/libcruft/getpagesize.c create mode 100644 mdk-stage1/dietlibc/libcruft/getpass.c create mode 100644 mdk-stage1/dietlibc/libcruft/getpwnam.c create mode 100644 mdk-stage1/dietlibc/libcruft/getpwuid.c create mode 100644 mdk-stage1/dietlibc/libcruft/getspnam.c create mode 100644 mdk-stage1/dietlibc/libcruft/grent.c create mode 100644 mdk-stage1/dietlibc/libcruft/h_errno.c create mode 100644 mdk-stage1/dietlibc/libcruft/herrno_location.c create mode 100644 mdk-stage1/dietlibc/libcruft/inet_addr.c create mode 100644 mdk-stage1/dietlibc/libcruft/inet_aton.c create mode 100644 mdk-stage1/dietlibc/libcruft/inet_ntoa.c create mode 100644 mdk-stage1/dietlibc/libcruft/inet_ntoa_r.c create mode 100644 mdk-stage1/dietlibc/libcruft/inet_ntop.c create mode 100644 mdk-stage1/dietlibc/libcruft/inet_pton.c create mode 100644 mdk-stage1/dietlibc/libcruft/initgroups.c create mode 100644 mdk-stage1/dietlibc/libcruft/mkstemp.c create mode 100644 mdk-stage1/dietlibc/libcruft/pwent.c create mode 100644 mdk-stage1/dietlibc/libcruft/res_init.c create mode 100644 mdk-stage1/dietlibc/libcruft/spent.c diff --git a/mdk-stage1/dietlibc/libcruft/___div.c b/mdk-stage1/dietlibc/libcruft/___div.c new file mode 100644 index 000000000..d359dc47b --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/___div.c @@ -0,0 +1,11 @@ +#include "dietwarning.h" +#include + +div_t div(int numer, int denom) { + div_t temp; + temp.quot=numer/denom; + temp.rem=numer%denom; + return temp; +} + +link_warning("div","warning: your code uses div(), which is completely superfluous!"); diff --git a/mdk-stage1/dietlibc/libcruft/dnscruft.c b/mdk-stage1/dietlibc/libcruft/dnscruft.c new file mode 100644 index 000000000..d7a0efa50 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/dnscruft.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int __dns_fd=-1; + +void __dns_make_fd() { + int tmp; + struct sockaddr_in si; + if (__dns_fd>0) return; + tmp=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP); + if (tmp<0) { + perror("__dns_make_fd!socket"); + return; + } + si.sin_family=AF_INET; + si.sin_port=0; + si.sin_addr.s_addr=INADDR_ANY; + if (bind(tmp,(struct sockaddr*)&si,sizeof(si))) { + perror("__dns_make_fd!bind"); + return; + } + __dns_fd=tmp; +} + +int __dns_servers=0; +struct sockaddr __dns_server_ips[8]; + +int __dns_search=0; +char *__dns_domains[8]; + +void __dns_readstartfiles() { + int fd; + char *buf=alloca(4096); + int len; + if (__dns_servers>0) return; + { + struct sockaddr_in to; + char *cacheip=getenv("DNSCACHEIP"); + if (cacheip) { + to.sin_port=htons(53); + to.sin_family=AF_INET; + if (inet_aton(cacheip,&to.sin_addr)) { + memmove(__dns_server_ips,&to,sizeof(struct sockaddr)); + ++__dns_servers; + } + } + } + if ((fd=open("/etc/resolv.conf",O_RDONLY))<0) return; + len=read(fd,buf,4096); + close(fd); + { + char *last=buf+len; + for (; buf=last) break; + *buf=0; + if (inet_aton(tmp,&i.sin_addr)) { + i.sin_family=AF_INET; + i.sin_port=htons(53); + memmove(&__dns_server_ips[__dns_servers],&i,sizeof(struct sockaddr)); + if (__dns_servers<8) ++__dns_servers; + } + } + } + } + if (!strncmp(buf,"search",6) || !strncmp(buf,"domain",6)) { + buf+=6; + } + while (buf0&&*tmp; ) { + if ((*tmp>>6)==3) { /* goofy DNS decompression */ + unsigned int ofs=((unsigned int)(*tmp&0x3f)<<8)|*(tmp+1); + if (ofs>=offset) return -1; /* RFC1035: "pointer to a _prior_ occurrance" */ + if (aftermax) return -1; + for (duh=*tmp; duh>0; --duh) + *dest++=*++tmp; + *dest++='.'; ok=1; + ++tmp; + if (tmp>after) { after=tmp; if (!*tmp) ++after; } + } + } + if (ok) --dest; + *dest=0; + return after-packet; +} diff --git a/mdk-stage1/dietlibc/libcruft/dnscruft2.c b/mdk-stage1/dietlibc/libcruft/dnscruft2.c new file mode 100644 index 000000000..b59207072 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/dnscruft2.c @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern int h_errno; + +static char dnspacket[]="\xfe\xfe\001\000\000\001\000\000\000\000\000\000"; + +extern void __dns_make_fd(); +extern int __dns_fd; + +extern int __dns_servers; +extern struct sockaddr __dns_server_ips[]; + +extern void __dns_readstartfiles(); + +extern int __dns_decodename(unsigned char *packet,int offset,unsigned char *dest,int maxlen); + +/* Oh boy, this interface sucks so badly, there are no words for it. + * Not one, not two, but _three_ error signalling methods! (*h_errnop + * nonzero? return value nonzero? *RESULT zero?) The glibc goons + * really outdid themselves with this one. */ +int __dns_gethostbyx_r(const char* name, struct hostent* result, + char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop, int lookfor) { + int names,ips; + unsigned char *cur; + unsigned char *max; + unsigned char packet[512]; + __dns_make_fd(); + + if (lookfor==1) { + result->h_aliases=(char**)(buf+8*4); + result->h_addrtype=AF_INET; + result->h_length=4; + result->h_addr_list=(char**)buf; + } else { + result->h_aliases=(char**)(buf+8*16); + result->h_addrtype=AF_INET6; + result->h_length=16; + result->h_addr_list=(char**)buf; + } + result->h_aliases[0]=0; + + cur=buf+16*sizeof(char*); + max=buf+buflen; + names=ips=0; + + memmove(packet,dnspacket,12); + *(unsigned short*)packet=rand(); + { + unsigned char* x; + const char* y,* tmp; + x=packet+12; y=name; + while (*y) { + while (*y=='.') ++y; + for (tmp=y; *tmp && *tmp!='.'; ++tmp) ; + *x=tmp-y; + if (!(tmp-y)) break; + ++x; + if (x>=packet+510-(tmp-y)) { *h_errnop=ERANGE; return 1; } + memmove(x,y,tmp-y); + x+=tmp-y; + if (!*tmp) { + *x=0; + break; + } + y=tmp; + } + *++x= 0; *++x= lookfor; /* A */ + *++x= 0; *++x= 1; /* IN */ + ++x; + { + int i; /* current server */ + int j; /* timeout count down */ + struct pollfd duh; + i=0; j=30; + __dns_readstartfiles(); + duh.fd=__dns_fd; + duh.events=POLLIN; + for (j=30; j>0; --j) { + sendto(__dns_fd,packet,x-packet,0,(struct sockaddr*)&(__dns_server_ips[i]),sizeof(struct sockaddr)); + if (++i > __dns_servers) i=0; + if (poll(&duh,1,1) == 1) { + /* read and parse answer */ + unsigned char inpkg[1500]; + /*int len=*/ read(__dns_fd,inpkg,1500); +#if 0 + { + int tft=open("duh",0); + read(tft,inpkg,1500); + close(tft); + } +#endif + /* header, question, answer, authority, additional */ + if (inpkg[0]!=packet[0] || inpkg[1]!=packet[1]) continue; /* wrong ID */ + if ((inpkg[2]&0xf9) != 0x81) continue; /* not answer */ + if ((inpkg[3]&0x0f) != 0) break; /* error */ + tmp=inpkg+12; + { + char name[257]; + unsigned short q=((unsigned short)inpkg[4]<<8)+inpkg[5]; + while (q>0) { + while (*tmp) tmp+=*tmp+1; + tmp+=5; + --q; + } + q=((unsigned short)inpkg[6]<<8)+inpkg[7]; + if (q<1) break; + while (q>0) { + int decofs=__dns_decodename(inpkg,tmp-(char*)inpkg,name,256); + if (decofs<0) break; + tmp=inpkg+decofs; + --q; + if (tmp[0]!=0 || tmp[1]!=lookfor || /* TYPE != A */ + tmp[2]!=0 || tmp[3]!=1) { /* CLASS != IN */ + if (tmp[1]==5) { /* CNAME */ + tmp+=10; + decofs=__dns_decodename(inpkg,tmp-(char*)inpkg,name,256); + if (decofs<0) break; + tmp=inpkg+decofs; + } else + break; + continue; + } + tmp+=10; /* skip type, class, TTL and length */ + { + int slen; + if (lookfor==1 || lookfor==28) /* A or AAAA*/ { + slen=strlen(name); + if (cur+slen+8+(lookfor==28?12:0)>=max) { *h_errnop=NO_RECOVERY; return 1; } + } else if (lookfor==12) /* PTR */ { + decofs=__dns_decodename(inpkg,tmp-(char*)inpkg,name,256); + if (decofs<0) break; + tmp=inpkg+decofs; + slen=strlen(name); + } else + slen=strlen(name); + strcpy(cur,name); + if (names==0) + result->h_name=cur; + else + result->h_aliases[names-1]=cur; + result->h_aliases[names]=0; + ++names; +/* cur+=slen+1; */ + cur+=(slen|3)+1; + result->h_addr_list[ips++] = cur; + if (lookfor==1) /* A */ { + *(int*)cur=*(int*)tmp; + cur+=4; + result->h_addr_list[ips]=0; + } else if (lookfor==28) /* AAAA */ { + { + int i; + for (i=0; i<16; ++i) cur[i]=tmp[i]; + } + cur+=16; + result->h_addr_list[ips]=0; + } + } +/* puts(name); */ + } + } +/* printf("%d answers\n",((unsigned short)inpkg[6]<<8)+inpkg[7]); + printf("ok\n");*/ + *h_errnop=0; + *RESULT=result; + return 0; + } +/*kaputt:*/ + } + } + } + return 1; +} diff --git a/mdk-stage1/dietlibc/libcruft/entlib.c b/mdk-stage1/dietlibc/libcruft/entlib.c new file mode 100644 index 000000000..f88b3f057 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/entlib.c @@ -0,0 +1,153 @@ +/* + * dietlibc/lib/entlib.c - Generic delimited-line parsing library + * + * Copyright 2001 Jeff Garzik + * + * This is a brand new implementation, based on the interface + * described in man-pages-1.34-1mdk man pages package. + */ + + +#include "entlib.h" /* this is our only include */ + + +/* + * __ent_start + * + * Allocates and zeroes the module's state structure, + * and open a handle to /etc/passwd. + * + * Returns -1 on failure and sets errno, or zero for success. + */ + +int __ent_start(const char *pathname, struct __ent_state **st_ref) +{ + struct __ent_state *st; + + if (*st_ref) + return 0; + + st = calloc(1, sizeof(*st)); + if (!st) { + errno = ENOMEM; + return -1; + } + + st->fd = open(pathname, O_RDONLY); + if (st->fd == -1) { + /* errno should have been set by open(2) */ + free(st); + st = NULL; + return -1; + } + + *st_ref = st; + return 0; +} + + +/* + * __ent_get_line + * + * Eliminates a previous line from the buffer, if any. + * Then reads in a new line from /etc/passwd, if necessary. + * + * Returns -1 on failure, or zero for success. + */ + +int __ent_get_line(struct __ent_state *st) +{ + int rc; + + /* overwrite previous line, by shifting the rest + * of the rest to the front of the buffer + */ + if (st->bufptr) { + unsigned int slop = st->buflen - st->bufptr; + memmove(st->ent_buf, &st->ent_buf[st->bufptr], slop); + st->bufptr = 0; + st->buflen = slop; + st->ent_buf[st->buflen] = 0; /* null terminate */ + } + + if (st->buflen == __ENT_BUFSIZ || strchr(st->ent_buf, '\n')) + return 0; + + rc = read(st->fd, &st->ent_buf[st->buflen], __ENT_BUFSIZ - st->buflen); + if (rc < 0) + return -1; + + st->buflen += rc; + if (st->buflen == 0) + return -1; + + return 0; +} + + +/* + * __ent_split + * + * Splits a string into parts based on a delimiter. + * Stops processing when \n is reached also. + * + * Returns -1 on failure, or zero on success. + */ + +int __ent_split(struct __ent_state *st, char **parts, + int n_parts, int delimiter, int require_exact) +{ + char *s = &st->ent_buf[st->bufptr]; + int idx = 0; + + /* empty list */ + if (!*s) { + if (!require_exact) + return 0; + return -1; + } + + /* scan through string, sticking string pointers + * into parts[] as delimiters are found + */ + parts[idx++] = s; + while (*s) { + st->bufptr++; + if (*s == '\n') { + *s = 0; /* null terminate */ + break; + } + if (*s == delimiter) { + *s = 0; /* null terminate */ + /* boundary error: too many delimiters */ + if (idx == n_parts) + return -1; + s++; + parts[idx++] = s; + } else { + s++; + } + } + + if (!require_exact) + return 0; + return (n_parts == idx) ? 0 : -1; +} + + +void __ent_set(struct __ent_state *st) +{ + if (!st) + return; + st->buflen = st->bufptr = 0; + lseek(st->fd, 0, SEEK_SET); +} + + +void __ent_end(struct __ent_state *st) +{ + if (!st) + return; + close(st->fd); + free(st); +} diff --git a/mdk-stage1/dietlibc/libcruft/entlib.h b/mdk-stage1/dietlibc/libcruft/entlib.h new file mode 100644 index 000000000..2659d542e --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/entlib.h @@ -0,0 +1,44 @@ +/* + * dietlibc/lib/entlib.h - Generic delimited-line parsing library header + * + * Copyright 2001 Jeff Garzik + * + * This is a brand new implementation, based on the interface + * described in man-pages-1.34-1mdk man pages package. + */ + +#ifndef __ENTLIB_H +#define __ENTLIB_H + +#include /* for open(2), lseek(2) */ +#include /* for open(2) */ +#include /* for open(2) */ +#include /* for close(2), lseek(2) */ +#include /* for calloc(3), free(3) */ +#include /* for errno */ +#include /* for memmove(3) */ +#include /* for atoi(3) */ +#include /* for the file locations */ + +#include /* for struct passwd */ +#include /* for struct group */ +#include /* for struct spwd */ + +/* should be a multiple of 64 to create decent alignment */ +#define __ENT_BUFSIZ (64 * 2) +#define __ENT_RAW_BUFSIZ (__ENT_BUFSIZ + 2) /* some overlap required */ + +struct __ent_state { + char ent_buf[__ENT_RAW_BUFSIZ]; /* raw file data buffer */ + unsigned int buflen; /* amount of valid data in ent_buf */ + unsigned int bufptr; /* next valid position in ent_buf */ + int fd; /* /etc/passwd file descriptor */ +}; + +extern int __ent_start(const char *pathname, struct __ent_state **st_ref); +extern int __ent_get_line(struct __ent_state *st); +extern int __ent_split(struct __ent_state *st, char **parts, int n_parts, int delimiter, int require_exact); +extern void __ent_set(struct __ent_state *st); +extern void __ent_end(struct __ent_state *st); + +#endif /* __ENTLIB_H */ diff --git a/mdk-stage1/dietlibc/libcruft/getdtablesize.c b/mdk-stage1/dietlibc/libcruft/getdtablesize.c new file mode 100644 index 000000000..227354714 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getdtablesize.c @@ -0,0 +1,6 @@ +#include +#include + +int getdtablesize(void) { + return OPEN_MAX; +} diff --git a/mdk-stage1/dietlibc/libcruft/getgrgid.c b/mdk-stage1/dietlibc/libcruft/getgrgid.c new file mode 100644 index 000000000..0c223056c --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getgrgid.c @@ -0,0 +1,14 @@ +#include +#include +#include + +struct group *getgrgid(gid_t gid) { + struct group *tmp; + setgrent(); + for (;;) { + tmp=getgrent(); + if (!tmp) return 0; + if (tmp->gr_gid == gid) + return tmp; + } +} diff --git a/mdk-stage1/dietlibc/libcruft/getgrnam.c b/mdk-stage1/dietlibc/libcruft/getgrnam.c new file mode 100644 index 000000000..1bdbb42a8 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getgrnam.c @@ -0,0 +1,14 @@ +#include +#include +#include + +struct group *getgrnam(const char* name) { + struct group *tmp; + setgrent(); + for (;;) { + tmp=getgrent(); + if (!tmp) return 0; + if (!strcmp(tmp->gr_name,name)) + return tmp; + } +} diff --git a/mdk-stage1/dietlibc/libcruft/gethostbyaddr.c b/mdk-stage1/dietlibc/libcruft/gethostbyaddr.c new file mode 100644 index 000000000..a4207d44d --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/gethostbyaddr.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern int h_errno; + +struct hostent* gethostbyaddr(const void *addr, socklen_t len, int type) { + static struct hostent hostbuf; + struct hostent *hp; + size_t hstbuflen; + char *tmphstbuf; + int res; + int herr; + + hstbuflen = 1024; + /* Allocate buffer, remember to free it to avoid a memory leakage. */ + tmphstbuf = malloc (hstbuflen); + + while ((res = gethostbyaddr_r (addr, len, type, &hostbuf, tmphstbuf, hstbuflen, + &hp, &herr)) == ERANGE) + { + /* Enlarge the buffer. */ + hstbuflen *= 2; + tmphstbuf = realloc (tmphstbuf, hstbuflen); + } + /* Check for errors. */ + if (res || hp == NULL) + return NULL; + return hp; +} + diff --git a/mdk-stage1/dietlibc/libcruft/gethostbyaddr_r.c b/mdk-stage1/dietlibc/libcruft/gethostbyaddr_r.c new file mode 100644 index 000000000..37174ab13 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/gethostbyaddr_r.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern int __dns_gethostbyx_r(const char* name, struct hostent* result, + char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop, int lookfor); + +static int i2a(char* dest,unsigned int x) { + register unsigned int tmp=x; + register int len=0; + if (x>=100) { *dest++=tmp/100+'0'; tmp=tmp%100; ++len; } + if (x>=10) { *dest++=tmp/10+'0'; tmp=tmp%10; ++len; } + *dest++=tmp+'0'; + return len+1; +} + +/* Oh boy, this interface sucks so badly, there are no words for it. + * Not one, not two, but _three_ error signalling methods! (*h_errnop + * nonzero? return value nonzero? *RESULT zero?) The glibc goons + * really outdid themselves with this one. */ +int gethostbyaddr_r(const char* addr, size_t length, int format, + struct hostent* result, char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop) { + if (format==AF_INET) { + char tmpbuf[50]; + char *tmp; + int res; + tmp=tmpbuf+i2a(tmpbuf,(unsigned char)addr[3]); *tmp++='.'; + tmp+=i2a(tmp,(unsigned char)addr[2]); *tmp++='.'; + tmp+=i2a(tmp,(unsigned char)addr[1]); *tmp++='.'; + tmp+=i2a(tmp,(unsigned char)addr[0]); strcpy(tmp,".in-addr.arpa"); + res= __dns_gethostbyx_r(tmpbuf,result,buf+4,buflen-4,RESULT,h_errnop,12); /* 12 == ns_t_ptr */ + if (res==0) { + result->h_addr_list[0]=buf; + result->h_addr_list[1]=buf; + *(int*)buf=*(int*)addr; + } + return res; + } + return 1; +} diff --git a/mdk-stage1/dietlibc/libcruft/gethostbyname.c b/mdk-stage1/dietlibc/libcruft/gethostbyname.c new file mode 100644 index 000000000..5db5bf3c5 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/gethostbyname.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern int h_errno; + +struct hostent* gethostbyname (const char *host) +{ + static struct hostent hostbuf; + struct hostent *hp; + size_t hstbuflen; + char *tmphstbuf; + int res; + int herr; + + hstbuflen = 1024; + /* Allocate buffer, remember to free it to avoid a memory leakage. */ + tmphstbuf = malloc (hstbuflen); + + while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen, + &hp, &herr)) == ERANGE) + { + /* Enlarge the buffer. */ + hstbuflen *= 2; + tmphstbuf = realloc (tmphstbuf, hstbuflen); + } + /* Check for errors. */ + if (res || hp == NULL) + return NULL; + return hp; +} + diff --git a/mdk-stage1/dietlibc/libcruft/gethostbyname2.c b/mdk-stage1/dietlibc/libcruft/gethostbyname2.c new file mode 100644 index 000000000..3d8296616 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/gethostbyname2.c @@ -0,0 +1,40 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern int h_errno; + +struct hostent* gethostbyname2(const char *host,int AF) +{ + static struct hostent hostbuf; + struct hostent *hp; + size_t hstbuflen; + char *tmphstbuf; + int res; + int herr; + + hstbuflen = 1024; + /* Allocate buffer, remember to free it to avoid a memory leakage. */ + tmphstbuf = malloc (hstbuflen); + + while ((res = gethostbyname2_r (host, AF,&hostbuf, tmphstbuf, hstbuflen, + &hp, &herr)) == ERANGE) + { + /* Enlarge the buffer. */ + hstbuflen *= 2; + tmphstbuf = realloc (tmphstbuf, hstbuflen); + } + /* Check for errors. */ + if (res || hp == NULL) + return NULL; + return hp; +} + diff --git a/mdk-stage1/dietlibc/libcruft/gethostbyname2_r.c b/mdk-stage1/dietlibc/libcruft/gethostbyname2_r.c new file mode 100644 index 000000000..b505ccfbc --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/gethostbyname2_r.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern int __dns_gethostbyx_r(const char* name, struct hostent* result, + char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop, int lookfor); + +/* Oh boy, this interface sucks so badly, there are no words for it. + * Not one, not two, but _three_ error signalling methods! (*h_errnop + * nonzero? return value nonzero? *RESULT zero?) The glibc goons + * really outdid themselves with this one. */ +int gethostbyname2_r(const char* name, int AF, struct hostent* result, + char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop) { + int L=strlen(name); + int lookfor=0; + switch (AF) { + case AF_INET: lookfor=1; break; + case AF_INET6: lookfor=28; break; + default: *h_errnop=EINVAL; return 1; + } + result->h_name=buf; + if (buflen +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern int __dns_gethostbyx_r(const char* name, struct hostent* result, + char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop, int lookfor); + +/* Oh boy, this interface sucks so badly, there are no words for it. + * Not one, not two, but _three_ error signalling methods! (*h_errnop + * nonzero? return value nonzero? *RESULT zero?) The glibc goons + * really outdid themselves with this one. */ +int gethostbyname_r(const char* name, struct hostent* result, + char *buf, size_t buflen, + struct hostent **RESULT, int *h_errnop) { + int L=strlen(name); + result->h_name=buf; + if (buflen + +char* getlogin() { + return getenv("LOGNAME"); +} diff --git a/mdk-stage1/dietlibc/libcruft/getpagesize.c b/mdk-stage1/dietlibc/libcruft/getpagesize.c new file mode 100644 index 000000000..dea4a9693 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getpagesize.c @@ -0,0 +1,14 @@ +#include +#include +#include + +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif + +size_t __libc_getpagesize(void) { + return PAGE_SIZE; +} + +size_t getpagesize(void) __attribute__((weak,alias("__libc_getpagesize"))); + diff --git a/mdk-stage1/dietlibc/libcruft/getpass.c b/mdk-stage1/dietlibc/libcruft/getpass.c new file mode 100644 index 000000000..3da7cd2b3 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getpass.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +char *getpass(const char* prompt) { + struct termios old,tmp; + int out,in=open("/dev/tty",O_RDWR); + int doclose=(in>=0); + static char buf[PASS_MAX]; + if (doclose) { in=0; out=2; } else out=in; + if (!tcgetattr(in,&old)) { + tmp=old; + tmp.c_lflag &= ~(ECHO|ISIG); + tcsetattr(in,TCSAFLUSH,&tmp); + } + write(out,prompt,strlen(prompt)); + { + int nread,ofs=0; + for (;;) { + nread=read(in,buf+ofs,PASS_MAX-ofs); + if (nread<0) { + buf[ofs]=0; + break; + } else if (ofs+nread>=PASS_MAX) { + buf[PASS_MAX-1]=0; + break; + } else if (buf[ofs+nread-1]=='\n') { + buf[ofs+nread-1]=0; + break; + } + ofs+=nread; + } + write(out,"\n",1); + } + tcsetattr(in,TCSAFLUSH,&old); + if (doclose) close(in); + return buf; +} diff --git a/mdk-stage1/dietlibc/libcruft/getpwnam.c b/mdk-stage1/dietlibc/libcruft/getpwnam.c new file mode 100644 index 000000000..53aae74d1 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getpwnam.c @@ -0,0 +1,14 @@ +#include +#include +#include + +struct passwd *getpwnam(const char * name) { + struct passwd *tmp; + setpwent(); + for (;;) { + tmp=getpwent(); + if (!tmp) return 0; + if (!strcmp(tmp->pw_name,name)) + return tmp; + } +} diff --git a/mdk-stage1/dietlibc/libcruft/getpwuid.c b/mdk-stage1/dietlibc/libcruft/getpwuid.c new file mode 100644 index 000000000..eef14e6e8 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getpwuid.c @@ -0,0 +1,14 @@ +#include +#include +#include + +struct passwd *getpwuid(uid_t uid) { + struct passwd *tmp; + setpwent(); + for (;;) { + tmp=getpwent(); + if (!tmp) return 0; + if (tmp->pw_uid==uid) + return tmp; + } +} diff --git a/mdk-stage1/dietlibc/libcruft/getspnam.c b/mdk-stage1/dietlibc/libcruft/getspnam.c new file mode 100644 index 000000000..e6573345d --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/getspnam.c @@ -0,0 +1,14 @@ +#include +#include +#include + +struct spwd *getspnam(const char * name) { + struct spwd *tmp; + setspent(); + for (;;) { + tmp=getspent(); + if (!tmp) return 0; + if (!strcmp(tmp->sp_namp,name)) + return tmp; + } +} diff --git a/mdk-stage1/dietlibc/libcruft/grent.c b/mdk-stage1/dietlibc/libcruft/grent.c new file mode 100644 index 000000000..0357657de --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/grent.c @@ -0,0 +1,54 @@ +/* + * dietlibc/lib/grent.c - getgrent(3)/setgrent(3)/endgrent(3) implementation + * + * Copyright 2001 Jeff Garzik + * + * This is a brand new implementation, based on the interface + * described in man-pages-1.34-1mdk man pages package. + */ + + +#include "entlib.h" /* this is our only include */ + +#define MAX_GROUP_MEMBERS 16 /* matches Linux kernel task struct */ + +static struct __ent_state *st = NULL; /* state held across calls */ +static char *gr_mem[MAX_GROUP_MEMBERS+1]; +static struct group gr; + +struct group *getgrent(void) +{ + char *parts[4], *grouplist; + unsigned int bufptr; + + if (__ent_start(_PATH_GROUP, &st)) + return NULL; + if (__ent_get_line(st)) + goto err_out; + if (__ent_split(st, parts, 4, ':', 1) < 0) + goto err_out; + + gr.gr_name = parts[0]; + gr.gr_passwd = parts[1]; + gr.gr_gid = (gid_t) atoi(parts[2]); + grouplist = parts[3]; + + memset(&gr_mem, 0, sizeof(gr_mem)); + gr.gr_mem = gr_mem; + + /* rewind bufptr to beginning of group list */ + bufptr = st->bufptr; + st->bufptr = (int) (grouplist - st->ent_buf); + + __ent_split(st, gr_mem, MAX_GROUP_MEMBERS, ',', 0); + st->bufptr = bufptr; + + return &gr; + +err_out: + st->buflen = st->bufptr = 0; /* invalidate file data buffer */ + return NULL; +} + +void setgrent(void) { __ent_set(st); } +void endgrent(void) { __ent_end(st); st=NULL; } diff --git a/mdk-stage1/dietlibc/libcruft/h_errno.c b/mdk-stage1/dietlibc/libcruft/h_errno.c new file mode 100644 index 000000000..73ead0469 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/h_errno.c @@ -0,0 +1 @@ +int h_errno; diff --git a/mdk-stage1/dietlibc/libcruft/herrno_location.c b/mdk-stage1/dietlibc/libcruft/herrno_location.c new file mode 100644 index 000000000..203f73d4b --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/herrno_location.c @@ -0,0 +1,5 @@ +extern int h_errno; + +int *__h_errno_location() { return &h_errno; } + +int *h_errno_location() __attribute__((weak,alias("__h_errno_location"))); diff --git a/mdk-stage1/dietlibc/libcruft/inet_addr.c b/mdk-stage1/dietlibc/libcruft/inet_addr.c new file mode 100644 index 000000000..0b8531ab1 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/inet_addr.c @@ -0,0 +1,11 @@ +#include +#include +#include + +unsigned long int inet_addr(const char *cp) { + struct in_addr foo; + if (inet_aton(cp,&foo)) + return foo.s_addr; + else + return (unsigned long int)-1; +} diff --git a/mdk-stage1/dietlibc/libcruft/inet_aton.c b/mdk-stage1/dietlibc/libcruft/inet_aton.c new file mode 100644 index 000000000..ac7d9d007 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/inet_aton.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +/* inet_aton() converts the Internet host address cp from the standard + * numbers-and-dots notation into binary data and stores it in the + * structure that inp points to. inet_aton returns nonzero if the + * address is valid, zero if not. */ + +/* problem is, inet_aton is historically quite, uh, lenient. + * the following are all acceptable: + * 0x7f000001 == 127.1 == 127.0.0.1.0 == 127.0.0.1 + * btw: 127.0.0.x.y == 127.0.0.(x|y) + * and: 10.1.1 == 10.1.0.1 (huh?!) + * and: 10 == 0.0.0.10 (?!?!?) + * The Berkeley people must have been so stoned that they are still high. + */ + +/* I hereby disclaim that I wrote this code. */ +int inet_aton(const char *cp, struct in_addr *inp) { + int i; + unsigned int ip=0; + char *tmp=(char*)cp; + for (i=24; ;) { + long j; + j=strtol(tmp,&tmp,0); + if (*tmp==0) { + ip|=j; + break; + } + if (*tmp=='.') { + if (j>255) return 0; + ip|=(j<0) i-=8; + ++tmp; + continue; + } + return 0; + } + inp->s_addr=htonl(ip); + return 1; +} diff --git a/mdk-stage1/dietlibc/libcruft/inet_ntoa.c b/mdk-stage1/dietlibc/libcruft/inet_ntoa.c new file mode 100644 index 000000000..f8c975f7a --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/inet_ntoa.c @@ -0,0 +1,10 @@ +#include +#include +#include + +extern char *inet_ntoa_r(struct in_addr in,char* buf); + +char *inet_ntoa(struct in_addr in) { + static char buf[20]; + return inet_ntoa_r(in,buf); +} diff --git a/mdk-stage1/dietlibc/libcruft/inet_ntoa_r.c b/mdk-stage1/dietlibc/libcruft/inet_ntoa_r.c new file mode 100644 index 000000000..dcf0eab7b --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/inet_ntoa_r.c @@ -0,0 +1,22 @@ +#include +#include +#include + +static unsigned int i2a(char* dest,unsigned int x) { + register unsigned int tmp=x; + register unsigned int len=0; + if (x>=100) { *dest++=tmp/100+'0'; tmp=tmp%100; ++len; } + if (x>=10) { *dest++=tmp/10+'0'; tmp=tmp%10; ++len; } + *dest++=tmp+'0'; + return len+1; +} + +char *inet_ntoa_r(struct in_addr in,char* buf) { + unsigned int len; + unsigned char *ip=(unsigned char*)∈ + len=i2a(buf,ip[0]); buf[len]='.'; ++len; + len+=i2a(buf+ len,ip[1]); buf[len]='.'; ++len; + len+=i2a(buf+ len,ip[2]); buf[len]='.'; ++len; + len+=i2a(buf+ len,ip[3]); buf[len]=0; + return buf; +} diff --git a/mdk-stage1/dietlibc/libcruft/inet_ntop.c b/mdk-stage1/dietlibc/libcruft/inet_ntop.c new file mode 100644 index 000000000..77c47f239 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/inet_ntop.c @@ -0,0 +1,75 @@ +#include + +extern char *inet_ntoa_r(struct in_addr in,char* buf); + +static const unsigned char V4mappedprefix[12]={0,0,0,0,0,0,0,0,0,0,0xff,0xff}; + +static char tohex(char hexdigit) { + return hexdigit>9?hexdigit+'a'-10:hexdigit+'0'; +} + +static int fmt_xlong(char* s,unsigned int i) { + char* bak=s; + *s=tohex((i>>12)&0xf); if (s!=bak || *s!='0') ++s; + *s=tohex((i>>8)&0xf); if (s!=bak || *s!='0') ++s; + *s=tohex((i>>4)&0xf); if (s!=bak || *s!='0') ++s; + *s=tohex(i&0xf); + return s-bak+1; +} + +unsigned int fmt_ip6(char *s,const char ip[16]) +{ + unsigned int len; + unsigned int i; + unsigned int temp; + unsigned int compressing; + int j; + + len = 0; compressing = 0; + for (j=0; j<16; j+=2) { + if (j==12 && !memcmp(ip,V4mappedprefix,12)) { + inet_ntoa_r(*(struct in_addr*)(ip+12),s); + temp=strlen(s); + return len+temp; + } + temp = ((unsigned long) (unsigned char) ip[j] << 8) + + (unsigned long) (unsigned char) ip[j+1]; + if (temp == 0) { + if (!compressing) { + compressing=1; + if (j==0) { + *s++=':'; ++len; + } + } + } else { + if (compressing) { + compressing=0; + *s++=':'; ++len; + } + i = fmt_xlong(s,temp); len += i; s += i; + if (j<14) { + *s++ = ':'; + ++len; + } + } + } + *s=0; + return len; +} + +const char* inet_ntop(int AF, const void *CP, char *BUF, size_t LEN) { + char buf[100]; + int len; + if (AF==AF_INET) { + inet_ntoa_r(*(struct in_addr*)CP,buf); + len=strlen(buf); + } else if (AF==AF_INET6) { + len=fmt_ip6(buf,CP); + } else + return 0; + if (len +#include +#include + +static unsigned int scan_ip6(const char *s,char ip[16]) +{ + unsigned int i; + unsigned int len=0; + unsigned long u; + + char suffix[16]; + int prefixlen=0; + int suffixlen=0; + + for (i=0; i<16; i++) ip[i]=0; + + for (;;) { + if (*s == ':') { + len++; + if (s[1] == ':') { /* Found "::", skip to part 2 */ + s+=2; + len++; + break; + } + s++; + } + { + char *tmp; + u=strtol(s,&tmp,16); + i=tmp-s; + } + + if (!i) return 0; + if (prefixlen==12 && s[i]=='.') { + /* the last 4 bytes may be written as IPv4 address */ + if (inet_aton(s,(struct in_addr*)(ip+12))) + return i+len; + else + return 0; + } + ip[prefixlen++] = (u >> 8); + ip[prefixlen++] = (u & 255); + s += i; len += i; + if (prefixlen==16) + return len; + } + +/* part 2, after "::" */ + for (;;) { + if (*s == ':') { + if (suffixlen==0) + break; + s++; + len++; + } else if (suffixlen!=0) + break; + { + char *tmp; + u=strtol(s,&tmp,16); + i=tmp-s; + } + if (!i) { + len--; + break; + } + if (suffixlen+prefixlen<=12 && s[i]=='.') { + if (inet_aton(s,(struct in_addr*)(suffix+suffixlen))) { + suffixlen+=4; + len+=strlen(s); + break; + } else + prefixlen=12-suffixlen; /* make end-of-loop test true */ + } + suffix[suffixlen++] = (u >> 8); + suffix[suffixlen++] = (u & 255); + s += i; len += i; + if (prefixlen+suffixlen==16) + break; + } + for (i=0; i +#include +#include + +int initgroups(const char *user, gid_t group) { + int n=0; + gid_t grouplist[32]; + struct group *g; + setgrent(); + while ((g=getgrent())) { + char **duh=g->gr_mem; + while (*duh) { + if (!strcmp(*duh,user)) { + grouplist[n]=g->gr_gid; + if (++n>=32) + break; + } + duh++; + } + } + return setgroups(n,grouplist); +} + diff --git a/mdk-stage1/dietlibc/libcruft/mkstemp.c b/mdk-stage1/dietlibc/libcruft/mkstemp.c new file mode 100644 index 000000000..3b24ecb54 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/mkstemp.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +int mkstemp(char* template) { + char *tmp=template+strlen(template)-6; + int randfd; + int i,res; + unsigned int random; + for (i=0; i<6; ++i) if (tmp[i]!='X') { __set_errno(EINVAL); return -1; } + randfd=open("/dev/urandom",O_RDONLY); + for (;;) { + read(randfd,&random,sizeof(random)); + for (i=0; i<6; ++i) { + int hexdigit=(random>>(i*5))&0x1f; + tmp[i]=hexdigit>9?hexdigit+'a'-10:hexdigit+'0'; + } + res=open(template,O_CREAT|O_RDWR|O_EXCL,0600); + if (res>=0) return res; + } + close(randfd); +} diff --git a/mdk-stage1/dietlibc/libcruft/pwent.c b/mdk-stage1/dietlibc/libcruft/pwent.c new file mode 100644 index 000000000..30a8a0957 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/pwent.c @@ -0,0 +1,43 @@ +/* + * dietlibc/lib/pwent.c - getpwent(3)/setpwent(3)/endpwent(3) implementation + * + * Copyright 2001 Jeff Garzik + * + * This is a brand new implementation, based on the interface + * described in man-pages-1.34-1mdk man pages package. + */ + + +#include "entlib.h" /* this is our only include */ + +static struct __ent_state *st = NULL; /* state held across calls */ +static struct passwd pw; + +struct passwd *getpwent(void) +{ + char *parts[7]; + + if (__ent_start(_PATH_PASSWD, &st)) + return NULL; + if (__ent_get_line(st)) + goto err_out; + if (__ent_split(st, parts, 7, ':', 1) < 0) + goto err_out; + + pw.pw_name = parts[0]; + pw.pw_passwd = parts[1]; + pw.pw_uid = (uid_t) atoi(parts[2]); + pw.pw_gid = (gid_t) atoi(parts[3]); + pw.pw_gecos = parts[4]; + pw.pw_dir = parts[5]; + pw.pw_shell = parts[6]; + + return &pw; + +err_out: + st->buflen = st->bufptr = 0; /* invalidate file data buffer */ + return NULL; +} + +void setpwent(void) { __ent_set(st); } +void endpwent(void) { __ent_end(st); st=NULL; } diff --git a/mdk-stage1/dietlibc/libcruft/res_init.c b/mdk-stage1/dietlibc/libcruft/res_init.c new file mode 100644 index 000000000..9322af298 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/res_init.c @@ -0,0 +1,7 @@ +extern int __dns_servers; +extern void __dns_readstartfiles(void); + +int res_init(void) { + __dns_servers=0; + __dns_readstartfiles(); +} diff --git a/mdk-stage1/dietlibc/libcruft/spent.c b/mdk-stage1/dietlibc/libcruft/spent.c new file mode 100644 index 000000000..e4beb81e4 --- /dev/null +++ b/mdk-stage1/dietlibc/libcruft/spent.c @@ -0,0 +1,45 @@ +/* + * dietlibc/lib/spent.c - getspent(3)/setspent(3)/endspent(3) implementation + * + * Copyright 2001 Jeff Garzik + * + * This is a brand new implementation, based on the interface + * described in man-pages-1.34-1mdk man pages package. + */ + + +#include "entlib.h" /* this is our only include */ + +static struct __ent_state *st = NULL; /* state held across calls */ +static struct spwd sp; + +struct spwd *getspent(void) +{ + char *parts[9]; + + if (__ent_start(_PATH_SHADOW, &st)) + return NULL; + if (__ent_get_line(st)) + goto err_out; + if (__ent_split(st, parts, 9, ':', 1) < 0) + goto err_out; + + sp.sp_namp = parts[0]; + sp.sp_pwdp = parts[1]; + sp.sp_lstchg = atoi(parts[2]); /* XXX: atol */ + sp.sp_min = atoi(parts[3]); + sp.sp_max = atoi(parts[4]); + sp.sp_warn = atoi(parts[5]); + sp.sp_inact = atoi(parts[6]); + sp.sp_expire = atoi(parts[7]); + sp.sp_flag = atoi(parts[8]); + + return &sp; + +err_out: + st->buflen = st->bufptr = 0; /* invalidate file data buffer */ + return NULL; +} + +void setspent(void) { __ent_set(st); } +void endspent(void) { __ent_end(st); st=NULL; } -- cgit v1.2.1