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