Annotation of dietlibc/t.c, revision 1.120

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

LinuxTV legacy CVS <linuxtv.org/cvs>