Annotation of dietlibc/t.c, revision 1.116

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

LinuxTV legacy CVS <linuxtv.org/cvs>