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