Annotation of dietlibc/t.c, revision 1.122

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

LinuxTV legacy CVS <linuxtv.org/cvs>