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