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