Annotation of dietlibc/t.c, revision 1.119

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

LinuxTV legacy CVS <linuxtv.org/cvs>