Annotation of dietlibc/t.c, revision 1.117

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

LinuxTV legacy CVS <linuxtv.org/cvs>