Annotation of dietlibc/t.c, revision 1.121

1.56      fefe        1: #define _GNU_SOURCE
1.47      fefe        2: #define _FILE_OFFSET_BITS 64
1.1       cvs         3: #include <unistd.h>
                      4: #include <endian.h>
                      5: #include <stdlib.h>
                      6: #include <dirent.h>
                      7: #include <pwd.h>
1.40      fefe        8: #include <shadow.h>
1.1       cvs         9: #include <stdio.h>
                     10: #include <assert.h>
                     11: #include <sys/mount.h>
1.7       fefe       12: #include <time.h>
1.9       fefe       13: #include <sys/stat.h>
1.10      fefe       14: #include <stdio.h>
1.12      fefe       15: #include <arpa/inet.h>
1.13      fefe       16: #include <sys/sem.h>
1.14      fefe       17: #include <sys/shm.h>
1.17      fefe       18: #include <math.h>
1.20      fefe       19: #include <termios.h>
1.22      fefe       20: #include <netdb.h>
1.38      fefe       21: #include <sys/mman.h>
1.45      fefe       22: #include <ctype.h>
1.49      fefe       23: #include <mntent.h>
1.53      fefe       24: #include <regex.h>
1.56      fefe       25: #include <sys/types.h>
                     26: #include <sys/msg.h>
1.63      fefe       27: #include <string.h>
1.76      fefe       28: #include <sys/socket.h>
                     29: #include <netdb.h>
1.86      fefe       30: #include <signal.h>
1.88      fefe       31: #include <sys/io.h>
1.97      fefe       32: #include <getopt.h>
1.101     fefe       33: #include <arpa/nameser.h>
                     34: #include <resolv.h>
1.102     fefe       35: #include <fnmatch.h>
1.109     fefe       36: #include <stdarg.h>
1.118     fefe       37: #include <sys/wait.h>
1.71      fefe       38: 
1.121   ! fefe       39: #include <errno.h>
        !            40: #include <syslog.h>
        !            41: #include <sys/un.h>
        !            42: #include <fcntl.h>
        !            43: 
        !            44: #if 0
        !            45: static const char* Ident;
        !            46: static int Option;
        !            47: static int Facility;
        !            48: static struct sockaddr_un sock;
        !            49: static int fd=-1;
        !            50: 
        !            51: static void syslogconnect(void) {
        !            52:   sock.sun_family=AF_UNIX;
        !            53:   strcpy(sock.sun_path,"/dev/log");
        !            54:   if ((fd=socket(AF_UNIX,SOCK_STREAM,0))==-1) return;
        !            55:   if (connect(fd,(struct sockaddr*)&sock,sizeof(sock))==-1) {
        !            56:     int save=errno;
        !            57:     close(fd);
        !            58:     fd=-1;
1.109     fefe       59:   }
1.121   ! fefe       60:   fcntl(fd,F_SETFL,FD_CLOEXEC);                /* doesn't work?  too bad */
1.109     fefe       61: }
1.107     fefe       62: 
1.121   ! fefe       63: void openlog(const char *ident, int option, int facility) {
        !            64:   Ident=ident;
        !            65:   Option=option;
        !            66:   Facility=facility;
        !            67:   syslogconnect();
1.116     fefe       68: }
                     69: 
1.121   ! fefe       70: void syslog(int priority, const char *format, ...) {
        !            71:   /* write(fd,"<13>Jun 29 19:21:32 leitner: fnord",...) */
        !            72: }
        !            73: 
        !            74: void closelog(void) {
        !            75: }
        !            76: #endif
        !            77: 
1.97      fefe       78: int main(int argc,char *argv[]) {
1.120     fefe       79: #if 0
1.121   ! fefe       80:   struct addrinfo *ai;
        !            81:   getaddrinfo("ftp.fu-berlin.de","ftp",0,&ai);
        !            82: #endif
        !            83:   struct hostent host,*res;
        !            84:   char buf[4096];
        !            85:   int fnord;
        !            86: 
        !            87:   gethostbyname2_r("knuth",AF_INET,&host,buf,4096,&res,&fnord);
        !            88: #if 0
1.119     fefe       89:   char buf[128];
1.120     fefe       90:   strcpy(buf,"/tmp/fnord/foo.XXXXXXX");
1.119     fefe       91:   printf("%d\n",mkdtemp(buf));
                     92:   printf("%s\n",buf);
1.120     fefe       93: #endif
1.119     fefe       94: #if 0
1.118     fefe       95:   printf("%d\n",WEXITSTATUS(system("exit 17")));
1.119     fefe       96: #endif
1.113     fefe       97: #if 0
1.109     fefe       98:   fnord("fnord","foo\n","bar\n",0);
1.112     fefe       99:   assert(0);
1.113     fefe      100: #endif
1.105     fefe      101: #if 0
                    102:   printf("%hd %hhd\n",-5,-1234567);
                    103: #endif
1.103     fefe      104: #if 0
1.102     fefe      105:   printf("%d\n",fnmatch("*.o", "x.o", FNM_PATHNAME));
                    106:   printf("%d\n",fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME));
1.103     fefe      107: #endif
1.102     fefe      108: #if 0
1.101     fefe      109:   char buf[1024];
                    110:   int len;
                    111:   len=res_search("fu-berlin.de",ns_c_in,ns_t_ns,buf,sizeof(buf));
1.102     fefe      112: #endif
1.114     fefe      113: #if 0
1.100     fefe      114:   regex_t t;
                    115:   regmatch_t rm;
1.103     fefe      116: //  regcomp(&t,"^ *read",0);
1.113     fefe      117:   regcomp(&t,"\\<foo\\>",0);
                    118:   printf("%d\n",regexec(&t,"  blub foo,",1,&rm,0));
1.100     fefe      119:   printf("ofs %d\n",rm.rm_so);
1.101     fefe      120: #endif
1.100     fefe      121: #if 0
1.98      fefe      122:   char buf[100];
1.99      fefe      123:   printf("%d\n",fread(buf,1,0,stdin));
1.100     fefe      124: #endif
1.99      fefe      125: #if 0
                    126:   char buf[100];
1.98      fefe      127:   memset(buf,17,100);
                    128:   buf[0]=0;
                    129:   strncat(buf,"foobarbaz23",10);
                    130:   puts(buf);
1.99      fefe      131: #endif
1.98      fefe      132: #if 0
1.97      fefe      133:   int aflag = 0;
                    134:   int bflag = 0;
                    135:   char *cvalue = NULL;
                    136:   int index;
                    137:   int c;
                    138: 
                    139:   opterr = 1;
                    140: 
                    141:   while ((c = getopt (argc, argv, "abc:")) != -1)
                    142:     switch (c)
                    143:       {
                    144:       case 'a':
                    145:        aflag = 1;
                    146:        break;
                    147:       case 'b':
                    148:        bflag = 1;
                    149:        break;
                    150:       case 'c':
                    151:        cvalue = optarg;
                    152:        break;
                    153:       case '?':
                    154:        if (isprint (optopt))
                    155:          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                    156:        else
                    157:          fprintf (stderr,
                    158:                  "Unknown option character `\\x%x'.\n",
                    159:                  optopt);
                    160:        return 1;
                    161:       default:
                    162:        abort ();
                    163:       }
1.96      fefe      164: 
1.97      fefe      165:   printf ("aflag = %d, bflag = %d, cvalue = %s\n",
                    166:          aflag, bflag, cvalue);
                    167: 
                    168:   for (index = optind; index < argc; index++)
                    169:     printf ("Non-option argument %s\n", argv[index]);
                    170:   return 0;
1.98      fefe      171: #endif
1.97      fefe      172: #if 0
1.96      fefe      173:   char *t="foobar";
                    174:   char *c;
                    175:   char buf[1000];
                    176:   puts(strcat(strcpy(buf,"HOME="),t));
1.97      fefe      177: #endif
1.89      fefe      178: #if 0
                    179:   struct netent* n=getnetbyname("loopback");
                    180:   printf("%s %s\n",n->n_name,inet_ntoa(*(struct in_addr*)&n->n_net));
                    181: #endif
                    182: #if 0
1.88      fefe      183:   fprintf(stdout,"foo\n");
                    184:   sleep(1);
                    185:   fprintf(stdout,"bar");
                    186:   fprintf(stderr,"blonk");
                    187:   sleep(1);
                    188:   fprintf(stdout,"\rbz");
                    189:   sleep(1);
                    190:   fprintf(stdout,"\n");
                    191:   sleep(1);
1.89      fefe      192: #endif
1.87      fefe      193: #if 0
1.86      fefe      194:   sigset_t s;  /* sigsetops */
                    195: 
                    196:   sigemptyset(&s);
                    197:   sigaddset(&s,SIGCHLD);
                    198:   sigaddset(&s,SIGHUP);
                    199:   sigsuspend(&s);
1.87      fefe      200: #endif
1.85      fefe      201: #if 0
1.84      fefe      202:   char buf[1024];
                    203:   FILE *f=popen("uname -srm","r");
                    204:   fgets(buf,1023,f);
                    205:   pclose(f);
                    206:   write(1,buf,strlen(buf));
1.85      fefe      207: #endif
1.84      fefe      208: #if 0
1.82      fefe      209:   char type[64];
1.83      fefe      210:   char filename[256];
                    211:   int major,minor;
1.82      fefe      212:   int len;
1.83      fefe      213:   printf("%d\n",sscanf("GET / HTTP/1.0\r\n","%4[A-Z] %255[^ \t\r\n] HTTP/%d.%d",type,filename,&major,&minor));
                    214:   printf("%s %s %d %d\n",type,filename,major,minor);
1.84      fefe      215: #endif
1.82      fefe      216: #if 0
1.81      fefe      217:   char buf[100];
                    218:   char ip[16];
                    219:   memset(ip,0,16);
                    220:   printf("%p %p\n",inet_ntop(AF_INET6,ip,buf,100),buf);
                    221:   puts(buf);
1.82      fefe      222: #endif
1.107     fefe      223: #if 0
1.76      fefe      224:   struct addrinfo *ai;
1.78      fefe      225:   struct addrinfo hints;
1.79      fefe      226:   char buf[16];
1.78      fefe      227:   hints.ai_family = AF_UNSPEC;
1.79      fefe      228:   hints.ai_flags = AI_PASSIVE|AI_CANONNAME;
1.78      fefe      229:   hints.ai_socktype = SOCK_STREAM;
1.106     fefe      230:   printf("%d\n",getaddrinfo("ftp.fu-berlin.de","ftp",0,&ai));
1.76      fefe      231:   while (ai) {
                    232:     printf("found host %s, port %d, family %s, socktype %s\n",ai->ai_canonname,
                    233:           ntohs(ai->ai_family==AF_INET6?((struct sockaddr_in6*)ai->ai_addr)->sin6_port:
                    234:                                   ((struct sockaddr_in*)ai->ai_addr)->sin_port),
                    235:           ai->ai_family==AF_INET6?"PF_INET6":"PF_INET",
                    236:           ai->ai_socktype==SOCK_STREAM?"SOCK_STREAM":"SOCK_DGRAM");
1.80      fefe      237:     {
                    238:       char buf[100];
                    239:       inet_ntop(ai->ai_family,ai->ai_family==AF_INET6?
                    240:                (char*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr):
                    241:                (char*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr),buf,100);
                    242:       printf("  %s\n",buf);
                    243:     }
1.76      fefe      244:     ai=ai->ai_next;
                    245:   }
1.81      fefe      246: #endif
1.76      fefe      247: #if 0
1.71      fefe      248:   int i=foo;
1.75      fefe      249:   printf("load average is %3.2f\n",0.0);
1.76      fefe      250: #endif
1.70      fefe      251: #if 0
1.69      fefe      252:   struct dirent **namelist;
                    253:   int n;
1.16      fefe      254: 
1.69      fefe      255:   n = scandir(".", &namelist, 0, alphasort);
                    256:   if (n < 0)
                    257:     perror("scandir");
                    258:   else {
                    259:     while(n--) {
                    260:       printf("%s\n", namelist[n]->d_name);
                    261:       free(namelist[n]);
                    262:     }
                    263:     free(namelist);
                    264:   }
1.70      fefe      265: #endif
1.68      fefe      266: #if 0
1.67      fefe      267:   char foo[10]="none,zlib";
                    268:   char *tmp,*tmp2=foo;
                    269:   while (tmp=strsep(&tmp2,",")) {
                    270:     puts(tmp);
                    271:   }
1.68      fefe      272: #endif
1.65      fefe      273: #if 0
1.61      fefe      274:   char foo[10];
1.64      fefe      275:   printf("%d %d\n",abs(-3),abs(23));
1.63      fefe      276:   strcpy(foo,"foo");
                    277:   strncat(foo,"barbaz",3);
                    278:   foo[6]=0;
                    279:   puts(foo);
1.65      fefe      280: #endif
1.115     fefe      281: #if 0
1.58      fefe      282:   struct hostent * host;
                    283:   struct in_addr i;
1.57      fefe      284: 
1.110     fefe      285:   host = gethostbyname2("knuth",AF_INET);
1.57      fefe      286: 
1.58      fefe      287:   if (!host)
                    288:     printf("host null\n");
1.57      fefe      289: 
1.58      fefe      290:   if (host && host->h_name) {
                    291:     printf("name %s\n", host->h_name);
                    292:   }
                    293:   if (host && (host->h_addr_list)[0]) {
                    294:     struct in_addr address;
                    295:     address = *((struct in_addr *) (host->h_addr_list)[0]);
                    296:     printf("addr %s\n", inet_ntoa(address));
                    297:   }
1.59      fefe      298: #endif
1.57      fefe      299: #if 0
1.56      fefe      300:   struct msgbuf bla;
                    301:   bla.mtype=0;
                    302:   bla.mtext[0]='x';
                    303:   msgsnd(327680,&bla,5,IPC_NOWAIT);
1.57      fefe      304: #endif
1.55      fefe      305: #if 0
1.54      fefe      306:   char buf[PATH_MAX];
                    307:   printf("%s\n",realpath("../../incoming/..///.zshrc",buf));
1.55      fefe      308: #endif
1.73      fefe      309: #if 0
1.53      fefe      310:   regex_t t;
1.68      fefe      311:   regcomp(&t,"^OpenSSH_2\\.5\\.[012]",5);
1.65      fefe      312:   printf("%d\n",regexec(&t,"OpenSSH_2.5.2p2",0,0,0));
1.56      fefe      313: #endif
1.53      fefe      314: #if 0
1.52      fefe      315:   float my_float = 9.2334;
                    316:   char buffer[100];
                    317: 
                    318:   sprintf(buffer, "%.2f", my_float);
                    319:   fprintf(stdout, "%s", buffer);
1.53      fefe      320: #endif
1.52      fefe      321: #if 0
1.51      fefe      322:   printf("%d\n",setenv("foo","bar",0));
                    323:   printf("%d\n",setenv("foo","bar",1));
                    324:   execlp("printenv","printenv","foo",0);
1.52      fefe      325: #endif
1.106     fefe      326: #if 0
1.59      fefe      327:   printf("%d\n",fnmatch("*c*","bin",0));
1.50      fefe      328:   if (!fnmatch("s*", "sub", 0))
                    329:     printf("s* sub\n");
                    330:   if (!fnmatch("s*", "glob", 0))
                    331:     printf("s* glob\n");
                    332:   if (!fnmatch("s*b", "sub", 0))
                    333:     printf("s*b sub\n");
                    334:   if (!fnmatch("s*h", "sub", 0))
                    335:     printf("s*h sub\n");
1.51      fefe      336: #endif
1.49      fefe      337: #if 0
1.48      fefe      338:   char*tmp;
                    339:   int n=asprintf(&tmp,"foo %s %d\n","bar",23);
                    340:   write(1,tmp,n);
                    341:   free(tmp);
1.49      fefe      342: #endif
1.41      fefe      343: #if 0
1.40      fefe      344:   struct passwd *p=getpwnam("leitner");
1.41      fefe      345:   struct spwd *s=getspnam("leitner");
1.39      fefe      346:   printf("%g\n",30.0123);
1.41      fefe      347: #endif
1.43      fefe      348: #if 0
1.42      fefe      349:   initgroups("root",100);
1.43      fefe      350: #endif
1.38      fefe      351: #if 0
1.37      fefe      352:   time_t t=time(0);
                    353:   puts(asctime(localtime(&t)));
1.38      fefe      354: #endif
1.37      fefe      355: #if 0
1.36      fefe      356:   struct servent *foo=getservbyname("ident","tcp");
                    357:   if (foo)
                    358:     printf("found service %s on port %d\n",foo->s_name,foo->s_port);
1.37      fefe      359: #endif
1.121   ! fefe      360: #if 0
1.33      fefe      361:   char buf[128];
1.120     fefe      362:   strcpy(buf,"/tmp/blub/foo.XXXXXXX");
1.33      fefe      363:   printf("%d\n",mkstemp(buf));
1.34      fefe      364:   printf("%s\n",buf);
                    365:   unlink(buf);
1.35      fefe      366: #endif
1.33      fefe      367: #if 0
1.32      fefe      368:   char buf[512]="foo";
                    369:   strncat(buf,"barbaz",3);
                    370:   puts(buf);
1.33      fefe      371: #endif
1.32      fefe      372: #if 0
1.31      fefe      373:   time_t oink=time(0);
                    374:   struct tm *duh=localtime(&oink);
                    375:   strftime(buf,512,"%A %B %Y\n",duh);
                    376:   puts(buf);
1.32      fefe      377: #endif
1.116     fefe      378: #if 0
1.28      fefe      379:   struct in_addr bar;
                    380:   struct hostent *foo;
1.29      fefe      381: /*  inet_aton("160.45.10.8",&bar); */
                    382:   foo=gethostbyname("zeit.fu-berlin.de");
                    383: /*  foo=gethostbyaddr(&bar,4,AF_INET); */
1.30      fefe      384:   if (foo)
                    385:     printf("%s -> %s\n",foo->h_name,inet_ntoa(*(struct in_addr*)foo->h_addr));
1.22      fefe      386: /*  printf("%g %g\n",1e-10,1e10); */
1.31      fefe      387: #endif
1.18      fefe      388: #if 0
1.17      fefe      389:   double d=0.0;
                    390:   long long t=0x12345678ABCDEF01;
                    391:   d/=0.0;
                    392:   printf("%d %llx\n",__isnan(d),t,*(long long*)&d);
1.18      fefe      393: #endif
1.17      fefe      394: #if 0
1.16      fefe      395:   int i,j;
                    396:   long a,b,c;
                    397:   int *res;
                    398:   printf("%p\n",malloc(0));
                    399:   qsort(array,2,sizeof(int),compint);
                    400:   for (i=0; i<SIZE; ++i)
                    401:     array[i]=rand();
                    402:   rdtscl(a);
                    403:   qsort(array,SIZE,sizeof(int),compint);
                    404:   rdtscl(b);
                    405:   j=array[LOOKFOR];
                    406:   res=bsearch(&j,array,SIZE,sizeof(int),compint);
                    407:   rdtscl(c);
                    408:   printf("%lu cycles sort, %lu cycles bsearch\n",b-a,c-b);
                    409:   for (i=0; i<SIZE-1; ++i)
                    410:     if (array[i]>array[i+1]) {
                    411:       printf("qsort does not work, index %d: %d > %d\n",i,array[i],array[i+1]);
                    412:       return 1;
                    413:     }
                    414:   if (*res!=j)
                    415:     printf("besearch does not work, returned %p (%d) instead of %p (%d)\n",res,res?*res:-1,array+LOOKFOR,j);
                    416: /*  printf("array={%d,%d,%d,%d,%d}\n",array[0],array[1],array[2],array[3],array[4]); */
1.17      fefe      417: #endif
1.13      fefe      418: #if 0
1.12      fefe      419:   struct in_addr duh;
                    420:   printf("%d\n",inet_aton(argv[1]?argv[1]:"10.0.0.1",&duh));
                    421:   printf("%x\n",duh.s_addr);
1.13      fefe      422: #endif
1.12      fefe      423: /*  printf("%-19s %10lu %9lu %9lu %3d%% %s\n","/dev/ide/host0/bus0/target0/lun0/part2",8393688,705683,1337084,85,"/"); */
1.11      fefe      424: #if 0
1.10      fefe      425:   char buf[100];
                    426:   fgets(buf,100,stdin); printf("got %d bytes\n",strlen(buf));
                    427:   fgets(buf,100,stdin); printf("got %d bytes\n",strlen(buf));
1.11      fefe      428: #endif
1.9       fefe      429: #if 0
1.7       fefe      430:   struct tm duh;
                    431:   time_t t;
1.8       fefe      432:   time(&t);
                    433:   gmtime_r(&t,&duh);
                    434:   printf("%s\n",asctime(&duh));
1.9       fefe      435: #endif
1.8       fefe      436: #if 0
1.7       fefe      437:   char buf[30];
                    438:   duh.tm_sec=42;
                    439:   duh.tm_min=23;
                    440:   duh.tm_hour=17;
                    441:   duh.tm_mday=2;
                    442:   duh.tm_mon=7;
                    443:   duh.tm_year=100;
                    444:   t=mktime(&duh);
                    445:   printf("%s\n",asctime_r(&duh,buf));
1.8       fefe      446: #endif
1.2       fefe      447: #if 0
1.1       cvs       448:   int i;
                    449:   for (i=0; i<5; i++) {
                    450:     fprintf(stdout,"first message\n");
                    451:     fprintf(stdout,"second message\n");
                    452:     fprintf(stdout,"third message\n");
                    453:     printf("foo %d\n",i);
                    454:   }
1.2       fefe      455: #endif
1.1       cvs       456: #if 0
                    457:   char buf[1024];
                    458:   sscanf("foo bar","%s",buf);
                    459:   printf("%s\n",buf);
                    460: #endif
                    461: #if 0
                    462:   mount("/dev/scsi/host0/bus0/target2/lun0/cd", "/cd", "iso9660", MS_MGC_VAL|MS_RDONLY, NULL);
                    463:   perror("mount");
                    464: #endif
                    465: #if 0
                    466:   char *t="<4>Linux version 2.4.0-test10 (leitner@hellhound) (gcc version 2.95.2 19991024 (release))";
                    467:   int i=strtol(t+1,&t,10);
                    468:   printf("%d %s\n",i,t);
                    469: #endif
                    470: #if 0
                    471:   char **tmp;
1.115     fefe      472:   putenv("A=foo");
1.1       cvs       473:   for (tmp=environ; *tmp; tmp++)
                    474:     puts(*tmp);
                    475: #endif
                    476: #if 0
                    477:   char buf[1024];
                    478:   printf("%d\n",fprintf(stderr,"duh\n"));
                    479: #endif
                    480: #if 0
                    481:   struct passwd *p=getpwuid(100);
                    482:   puts(p->pw_name);
                    483: #endif
                    484: #if 0
                    485:   int pid;
                    486:   char name[32];
                    487:   sscanf("1 (init","%d (%15c",&pid,name);
                    488:   printf("pid %d name %s\n",pid,name);
                    489: #endif
1.109     fefe      490: #if 0
1.1       cvs       491:   DIR *d=opendir("/proc");
                    492:   if (d) {
                    493:     struct dirent *D;
                    494:     while (D=readdir(d))
                    495:       puts(D->d_name);
                    496:     closedir(d);
                    497:   }
                    498: #endif
                    499: #if 0
                    500:   char buf[1024];
                    501:   int fd=open("/etc/passwd",0);
                    502:   pread(fd,buf,30,32);
                    503:   close(fd);
                    504:   write(1,buf,32);
                    505: #endif
                    506: #if 0
                    507:   char *argv[] = {"echo","foo",0};
                    508:   char buf[100];
                    509:   buf[5]='x';
                    510:   sprintf(buf,"foo\n");
                    511:   if (buf[5] == 'x')
                    512:     exit(0);
                    513:   else
                    514:     exit(1);
                    515:   execvp(argv[0],argv);
                    516: #endif
1.10      fefe      517: #if 0
1.1       cvs       518:   struct stat64 f;
                    519:   char buf[128];
                    520:   fstat64(0,&f);
                    521:   fprintf(stderr,"%d %d\n",f.st_size,sizeof(f));
                    522:   return 0;
                    523: #endif
                    524: #if 0
                    525:   FILE *f=fopen("foo","w");
                    526:   fputc('a',f);
                    527:   fputc('b',f);
                    528:   fputc('c',f);
                    529: #endif
                    530: /*  fprintf(stdout,"foo\n"); */
                    531: }

LinuxTV legacy CVS <linuxtv.org/cvs>