1 /*************************************************************************************************
2  * Popular encoders and decoders of the utility API
3  *                                                               Copyright (C) 2006-2012 FAL Labs
4  * This file is part of Tokyo Cabinet.
5  * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
6  * the GNU Lesser General Public License as published by the Free Software Foundation; either
7  * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope
8  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
10  * License for more details.
11  * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12  * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13  * Boston, MA 02111-1307 USA.
14  *************************************************************************************************/
15 
16 
17 #include <tcutil.h>
18 #include "myconf.h"
19 
20 
21 /* global variables */
22 const char *g_progname;                  // program name
23 
24 
25 /* function prototypes */
26 int main(int argc, char **argv);
27 static void usage(void);
28 static void eprintf(const char *format, ...);
29 static int runurl(int argc, char **argv);
30 static int runbase(int argc, char **argv);
31 static int runquote(int argc, char **argv);
32 static int runmime(int argc, char **argv);
33 static int runhex(int argc, char **argv);
34 static int runpack(int argc, char **argv);
35 static int runtcbs(int argc, char **argv);
36 static int runzlib(int argc, char **argv);
37 static int runbzip(int argc, char **argv);
38 static int runxml(int argc, char **argv);
39 static int runcstr(int argc, char **argv);
40 static int runucs(int argc, char **argv);
41 static int runhash(int argc, char **argv);
42 static int runcipher(int argc, char **argv);
43 static int rundate(int argc, char **argv);
44 static int runtmpl(int argc, char **argv);
45 static int runconf(int argc, char **argv);
46 static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base);
47 static int procbase(const char *ibuf, int isiz, bool dec);
48 static int procquote(const char *ibuf, int isiz, bool dec);
49 static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on,
50                     bool hd, bool bd, int part);
51 static int prochex(const char *ibuf, int isiz, bool dec);
52 static int procpack(const char *ibuf, int isiz, bool dec, bool bwt);
53 static int proctcbs(const char *ibuf, int isiz, bool dec);
54 static int proczlib(const char *ibuf, int isiz, bool dec, bool gz);
55 static int procbzip(const char *ibuf, int isiz, bool dec);
56 static int procxml(const char *ibuf, int isiz, bool dec, bool br);
57 static int proccstr(const char *ibuf, int isiz, bool dec, bool js);
58 static int procucs(const char *ibuf, int isiz, bool dec, bool un, const char *kw);
59 static int prochash(const char *ibuf, int isiz, bool crc, int ch);
60 static int proccipher(const char *ibuf, int isiz, const char *key);
61 static int procdate(const char *str, int jl, bool wf, bool rf);
62 static int proctmpl(const char *ibuf, int isiz, TCMAP *vars);
63 static int procconf(int mode);
64 
65 
66 /* main routine */
main(int argc,char ** argv)67 int main(int argc, char **argv){
68   g_progname = argv[0];
69   if(argc < 2) usage();
70   int rv = 0;
71   if(!strcmp(argv[1], "url")){
72     rv = runurl(argc, argv);
73   } else if(!strcmp(argv[1], "base")){
74     rv = runbase(argc, argv);
75   } else if(!strcmp(argv[1], "quote")){
76     rv = runquote(argc, argv);
77   } else if(!strcmp(argv[1], "mime")){
78     rv = runmime(argc, argv);
79   } else if(!strcmp(argv[1], "hex")){
80     rv = runhex(argc, argv);
81   } else if(!strcmp(argv[1], "pack")){
82     rv = runpack(argc, argv);
83   } else if(!strcmp(argv[1], "tcbs")){
84     rv = runtcbs(argc, argv);
85   } else if(!strcmp(argv[1], "zlib")){
86     rv = runzlib(argc, argv);
87   } else if(!strcmp(argv[1], "bzip")){
88     rv = runbzip(argc, argv);
89   } else if(!strcmp(argv[1], "xml")){
90     rv = runxml(argc, argv);
91   } else if(!strcmp(argv[1], "cstr")){
92     rv = runcstr(argc, argv);
93   } else if(!strcmp(argv[1], "ucs")){
94     rv = runucs(argc, argv);
95   } else if(!strcmp(argv[1], "hash")){
96     rv = runhash(argc, argv);
97   } else if(!strcmp(argv[1], "cipher")){
98     rv = runcipher(argc, argv);
99   } else if(!strcmp(argv[1], "date")){
100     rv = rundate(argc, argv);
101   } else if(!strcmp(argv[1], "tmpl")){
102     rv = runtmpl(argc, argv);
103   } else if(!strcmp(argv[1], "conf")){
104     rv = runconf(argc, argv);
105   } else {
106     usage();
107   }
108   return rv;
109 }
110 
111 
112 /* print the usage and exit */
usage(void)113 static void usage(void){
114   fprintf(stderr, "%s: popular encoders and decoders of Tokyo Cabinet\n", g_progname);
115   fprintf(stderr, "\n");
116   fprintf(stderr, "usage:\n");
117   fprintf(stderr, "  %s url [-d] [-br] [-rs base] [file]\n", g_progname);
118   fprintf(stderr, "  %s base [-d] [file]\n", g_progname);
119   fprintf(stderr, "  %s quote [-d] [file]\n", g_progname);
120   fprintf(stderr, "  %s mime [-d] [-en name] [-q] [-on] [-hd] [-bd] [-part num] [file]\n",
121           g_progname);
122   fprintf(stderr, "  %s hex [-d] [file]\n", g_progname);
123   fprintf(stderr, "  %s pack [-d] [-bwt] [file]\n", g_progname);
124   fprintf(stderr, "  %s tcbs [-d] [file]\n", g_progname);
125   fprintf(stderr, "  %s zlib [-d] [-gz] [file]\n", g_progname);
126   fprintf(stderr, "  %s bzip [-d] [file]\n", g_progname);
127   fprintf(stderr, "  %s xml [-d] [-br] [file]\n", g_progname);
128   fprintf(stderr, "  %s cstr [-d] [-js] [file]\n", g_progname);
129   fprintf(stderr, "  %s ucs [-d] [-un] [file]\n", g_progname);
130   fprintf(stderr, "  %s hash [-crc] [-ch num] [file]\n", g_progname);
131   fprintf(stderr, "  %s cipher [-key str] [file]\n", g_progname);
132   fprintf(stderr, "  %s date [-ds str] [-jl num] [-wf] [-rf]\n", g_progname);
133   fprintf(stderr, "  %s tmpl [-var name val] [file]\n", g_progname);
134   fprintf(stderr, "  %s conf [-v|-i|-l|-p]\n", g_progname);
135   fprintf(stderr, "\n");
136   exit(1);
137 }
138 
139 
140 /* print formatted error string */
eprintf(const char * format,...)141 static void eprintf(const char *format, ...){
142   va_list ap;
143   va_start(ap, format);
144   fprintf(stderr, "%s: ", g_progname);
145   vfprintf(stderr, format, ap);
146   fprintf(stderr, "\n");
147   va_end(ap);
148 }
149 
150 
151 /* parse arguments of url command */
runurl(int argc,char ** argv)152 static int runurl(int argc, char **argv){
153   char *path = NULL;
154   bool dec = false;
155   bool br = false;
156   char *base = NULL;
157   for(int i = 2; i < argc; i++){
158     if(!path && argv[i][0] == '-'){
159       if(!strcmp(argv[i], "-d")){
160         dec = true;
161       } else if(!strcmp(argv[i], "-br")){
162         br = true;
163       } else if(!strcmp(argv[i], "-rs")){
164         if(++i >= argc) usage();
165         base = argv[i];
166       } else {
167         usage();
168       }
169     } else if(!path){
170       path = argv[i];
171     } else {
172       usage();
173     }
174   }
175   char *ibuf;
176   int isiz;
177   if(path && path[0] == '@'){
178     isiz = strlen(path) - 1;
179     ibuf = tcmemdup(path + 1, isiz);
180   } else {
181     ibuf = tcreadfile(path, -1, &isiz);
182   }
183   if(!ibuf){
184     eprintf("%s: cannot open", path ? path : "(stdin)");
185     return 1;
186   }
187   int rv = procurl(ibuf, isiz, dec, br, base);
188   if(path && path[0] == '@' && !br) printf("\n");
189   tcfree(ibuf);
190   return rv;
191 }
192 
193 
194 /* parse arguments of base command */
runbase(int argc,char ** argv)195 static int runbase(int argc, char **argv){
196   char *path = NULL;
197   bool dec = false;
198   for(int i = 2; i < argc; i++){
199     if(!path && argv[i][0] == '-'){
200       if(!strcmp(argv[i], "-d")){
201         dec = true;
202       } else {
203         usage();
204       }
205     } else if(!path){
206       path = argv[i];
207     } else {
208       usage();
209     }
210   }
211   char *ibuf;
212   int isiz;
213   if(path && path[0] == '@'){
214     isiz = strlen(path) - 1;
215     ibuf = tcmemdup(path + 1, isiz);
216   } else {
217     ibuf = tcreadfile(path, -1, &isiz);
218   }
219   if(!ibuf){
220     eprintf("%s: cannot open", path ? path : "(stdin)");
221     return 1;
222   }
223   int rv = procbase(ibuf, isiz, dec);
224   if(path && path[0] == '@') printf("\n");
225   tcfree(ibuf);
226   return rv;
227 }
228 
229 
230 /* parse arguments of quote command */
runquote(int argc,char ** argv)231 static int runquote(int argc, char **argv){
232   char *path = NULL;
233   bool dec = false;
234   for(int i = 2; i < argc; i++){
235     if(!path && argv[i][0] == '-'){
236       if(!strcmp(argv[i], "-d")){
237         dec = true;
238       } else {
239         usage();
240       }
241     } else if(!path){
242       path = argv[i];
243     } else {
244       usage();
245     }
246   }
247   char *ibuf;
248   int isiz;
249   if(path && path[0] == '@'){
250     isiz = strlen(path) - 1;
251     ibuf = tcmemdup(path + 1, isiz);
252   } else {
253     ibuf = tcreadfile(path, -1, &isiz);
254   }
255   if(!ibuf){
256     eprintf("%s: cannot open", path ? path : "(stdin)");
257     return 1;
258   }
259   int rv = procquote(ibuf, isiz, dec);
260   if(path && path[0] == '@') printf("\n");
261   tcfree(ibuf);
262   return rv;
263 }
264 
265 
266 /* parse arguments of mime command */
runmime(int argc,char ** argv)267 static int runmime(int argc, char **argv){
268   char *path = NULL;
269   bool dec = false;
270   char *ename = NULL;
271   bool qb = false;
272   bool on = false;
273   bool hd = false;
274   bool bd = false;
275   int part = -1;
276   for(int i = 2; i < argc; i++){
277     if(!path && argv[i][0] == '-'){
278       if(!strcmp(argv[i], "-d")){
279         dec = true;
280       } else if(!strcmp(argv[i], "-en")){
281         if(++i >= argc) usage();
282         ename = argv[i];
283       } else if(!strcmp(argv[i], "-q")){
284         qb = true;
285       } else if(!strcmp(argv[i], "-on")){
286         on = true;
287       } else if(!strcmp(argv[i], "-hd")){
288         hd = true;
289       } else if(!strcmp(argv[i], "-bd")){
290         bd = true;
291       } else if(!strcmp(argv[i], "-part")){
292         if(++i >= argc) usage();
293         part = tcatoix(argv[i]);
294       } else {
295         usage();
296       }
297     } else if(!path){
298       path = argv[i];
299     } else {
300       usage();
301     }
302   }
303   char *ibuf;
304   int isiz;
305   if(path && path[0] == '@'){
306     isiz = strlen(path) - 1;
307     ibuf = tcmemdup(path + 1, isiz);
308   } else {
309     ibuf = tcreadfile(path, -1, &isiz);
310   }
311   if(!ibuf){
312     eprintf("%s: cannot open", path ? path : "(stdin)");
313     return 1;
314   }
315   if(!ename) ename = "UTF-8";
316   int rv = procmime(ibuf, isiz, dec, ename, qb, on, hd, bd, part);
317   if(path && path[0] == '@') printf("\n");
318   tcfree(ibuf);
319   return rv;
320 }
321 
322 
323 /* parse arguments of hex command */
runhex(int argc,char ** argv)324 static int runhex(int argc, char **argv){
325   char *path = NULL;
326   bool dec = false;
327   for(int i = 2; i < argc; i++){
328     if(!path && argv[i][0] == '-'){
329       if(!strcmp(argv[i], "-d")){
330         dec = true;
331       } else {
332         usage();
333       }
334     } else if(!path){
335       path = argv[i];
336     } else {
337       usage();
338     }
339   }
340   char *ibuf;
341   int isiz;
342   if(path && path[0] == '@'){
343     isiz = strlen(path) - 1;
344     ibuf = tcmemdup(path + 1, isiz);
345   } else {
346     ibuf = tcreadfile(path, -1, &isiz);
347   }
348   if(!ibuf){
349     eprintf("%s: cannot open", path ? path : "(stdin)");
350     return 1;
351   }
352   int rv = prochex(ibuf, isiz, dec);
353   if(path && path[0] == '@') printf("\n");
354   tcfree(ibuf);
355   return rv;
356 }
357 
358 
359 /* parse arguments of pack command */
runpack(int argc,char ** argv)360 static int runpack(int argc, char **argv){
361   char *path = NULL;
362   bool dec = false;
363   bool bwt = false;
364   for(int i = 2; i < argc; i++){
365     if(!path && argv[i][0] == '-'){
366       if(!strcmp(argv[i], "-d")){
367         dec = true;
368       } else if(!strcmp(argv[i], "-bwt")){
369         bwt = true;
370       } else {
371         usage();
372       }
373     } else if(!path){
374       path = argv[i];
375     } else {
376       usage();
377     }
378   }
379   char *ibuf;
380   int isiz;
381   if(path && path[0] == '@'){
382     isiz = strlen(path) - 1;
383     ibuf = tcmemdup(path + 1, isiz);
384   } else {
385     ibuf = tcreadfile(path, -1, &isiz);
386   }
387   if(!ibuf){
388     eprintf("%s: cannot open", path ? path : "(stdin)");
389     return 1;
390   }
391   int rv = procpack(ibuf, isiz, dec, bwt);
392   if(path && path[0] == '@') printf("\n");
393   tcfree(ibuf);
394   return rv;
395 }
396 
397 
398 /* parse arguments of tcbs command */
runtcbs(int argc,char ** argv)399 static int runtcbs(int argc, char **argv){
400   char *path = NULL;
401   bool dec = false;
402   for(int i = 2; i < argc; i++){
403     if(!path && argv[i][0] == '-'){
404       if(!strcmp(argv[i], "-d")){
405         dec = true;
406       } else {
407         usage();
408       }
409     } else if(!path){
410       path = argv[i];
411     } else {
412       usage();
413     }
414   }
415   char *ibuf;
416   int isiz;
417   if(path && path[0] == '@'){
418     isiz = strlen(path) - 1;
419     ibuf = tcmemdup(path + 1, isiz);
420   } else {
421     ibuf = tcreadfile(path, -1, &isiz);
422   }
423   if(!ibuf){
424     eprintf("%s: cannot open", path ? path : "(stdin)");
425     return 1;
426   }
427   int rv = proctcbs(ibuf, isiz, dec);
428   if(path && path[0] == '@') printf("\n");
429   tcfree(ibuf);
430   return rv;
431 }
432 
433 
434 /* parse arguments of zlib command */
runzlib(int argc,char ** argv)435 static int runzlib(int argc, char **argv){
436   char *path = NULL;
437   bool dec = false;
438   bool gz = false;
439   for(int i = 2; i < argc; i++){
440     if(!path && argv[i][0] == '-'){
441       if(!strcmp(argv[i], "-d")){
442         dec = true;
443       } else if(!strcmp(argv[i], "-gz")){
444         gz = true;
445       } else {
446         usage();
447       }
448     } else if(!path){
449       path = argv[i];
450     } else {
451       usage();
452     }
453   }
454   char *ibuf;
455   int isiz;
456   if(path && path[0] == '@'){
457     isiz = strlen(path) - 1;
458     ibuf = tcmemdup(path + 1, isiz);
459   } else {
460     ibuf = tcreadfile(path, -1, &isiz);
461   }
462   if(!ibuf){
463     eprintf("%s: cannot open", path ? path : "(stdin)");
464     return 1;
465   }
466   int rv = proczlib(ibuf, isiz, dec, gz);
467   if(path && path[0] == '@') printf("\n");
468   tcfree(ibuf);
469   return rv;
470 }
471 
472 
473 /* parse arguments of bzip command */
runbzip(int argc,char ** argv)474 static int runbzip(int argc, char **argv){
475   char *path = NULL;
476   bool dec = false;
477   for(int i = 2; i < argc; i++){
478     if(!path && argv[i][0] == '-'){
479       if(!strcmp(argv[i], "-d")){
480         dec = true;
481       } else {
482         usage();
483       }
484     } else if(!path){
485       path = argv[i];
486     } else {
487       usage();
488     }
489   }
490   char *ibuf;
491   int isiz;
492   if(path && path[0] == '@'){
493     isiz = strlen(path) - 1;
494     ibuf = tcmemdup(path + 1, isiz);
495   } else {
496     ibuf = tcreadfile(path, -1, &isiz);
497   }
498   if(!ibuf){
499     eprintf("%s: cannot open", path ? path : "(stdin)");
500     return 1;
501   }
502   int rv = procbzip(ibuf, isiz, dec);
503   if(path && path[0] == '@') printf("\n");
504   tcfree(ibuf);
505   return rv;
506 }
507 
508 
509 /* parse arguments of xml command */
runxml(int argc,char ** argv)510 static int runxml(int argc, char **argv){
511   char *path = NULL;
512   bool dec = false;
513   bool br = false;
514   for(int i = 2; i < argc; i++){
515     if(!path && argv[i][0] == '-'){
516       if(!strcmp(argv[i], "-d")){
517         dec = true;
518       } else if(!strcmp(argv[i], "-br")){
519         br = true;
520       } else {
521         usage();
522       }
523     } else if(!path){
524       path = argv[i];
525     } else {
526       usage();
527     }
528   }
529   char *ibuf;
530   int isiz;
531   if(path && path[0] == '@'){
532     isiz = strlen(path) - 1;
533     ibuf = tcmemdup(path + 1, isiz);
534   } else {
535     ibuf = tcreadfile(path, -1, &isiz);
536   }
537   if(!ibuf){
538     eprintf("%s: cannot open", path ? path : "(stdin)");
539     return 1;
540   }
541   int rv = procxml(ibuf, isiz, dec, br);
542   if(path && path[0] == '@') printf("\n");
543   tcfree(ibuf);
544   return rv;
545 }
546 
547 
548 /* parse arguments of cstr command */
runcstr(int argc,char ** argv)549 static int runcstr(int argc, char **argv){
550   char *path = NULL;
551   bool dec = false;
552   bool js = false;
553   for(int i = 2; i < argc; i++){
554     if(!path && argv[i][0] == '-'){
555       if(!strcmp(argv[i], "-d")){
556         dec = true;
557       } else if(!strcmp(argv[i], "-js")){
558         js = true;
559       } else {
560         usage();
561       }
562     } else if(!path){
563       path = argv[i];
564     } else {
565       usage();
566     }
567   }
568   char *ibuf;
569   int isiz;
570   if(path && path[0] == '@'){
571     isiz = strlen(path) - 1;
572     ibuf = tcmemdup(path + 1, isiz);
573   } else {
574     ibuf = tcreadfile(path, -1, &isiz);
575   }
576   if(!ibuf){
577     eprintf("%s: cannot open", path ? path : "(stdin)");
578     return 1;
579   }
580   int rv = proccstr(ibuf, isiz, dec, js);
581   if(path && path[0] == '@') printf("\n");
582   tcfree(ibuf);
583   return rv;
584 }
585 
586 
587 /* parse arguments of ucs command */
runucs(int argc,char ** argv)588 static int runucs(int argc, char **argv){
589   char *path = NULL;
590   bool dec = false;
591   bool un = false;
592   char *kw = NULL;
593   for(int i = 2; i < argc; i++){
594     if(!path && argv[i][0] == '-'){
595       if(!strcmp(argv[i], "-d")){
596         dec = true;
597       } else if(!strcmp(argv[i], "-un")){
598         un = true;
599       } else if(!strcmp(argv[i], "-kw")){
600         if(++i >= argc) usage();
601         kw = argv[i];
602       } else {
603         usage();
604       }
605     } else if(!path){
606       path = argv[i];
607     } else {
608       usage();
609     }
610   }
611   char *ibuf;
612   int isiz;
613   if(path && path[0] == '@'){
614     isiz = strlen(path) - 1;
615     ibuf = tcmemdup(path + 1, isiz);
616   } else {
617     ibuf = tcreadfile(path, -1, &isiz);
618   }
619   if(!ibuf){
620     eprintf("%s: cannot open", path ? path : "(stdin)");
621     return 1;
622   }
623   int rv = procucs(ibuf, isiz, dec, un, kw);
624   if(path && path[0] == '@') printf("\n");
625   tcfree(ibuf);
626   return rv;
627 }
628 
629 
630 /* parse arguments of hash command */
runhash(int argc,char ** argv)631 static int runhash(int argc, char **argv){
632   char *path = NULL;
633   bool crc = false;
634   int ch = 0;
635   for(int i = 2; i < argc; i++){
636     if(!path && argv[i][0] == '-'){
637       if(!strcmp(argv[i], "-crc")){
638         crc = true;
639       } else if(!strcmp(argv[i], "-ch")){
640         if(++i >= argc) usage();
641         ch = tcatoix(argv[i]);
642       } else {
643         usage();
644       }
645     } else if(!path){
646       path = argv[i];
647     } else {
648       usage();
649     }
650   }
651   char *ibuf;
652   int isiz;
653   if(path && path[0] == '@'){
654     isiz = strlen(path) - 1;
655     ibuf = tcmemdup(path + 1, isiz);
656   } else {
657     ibuf = tcreadfile(path, -1, &isiz);
658   }
659   if(!ibuf){
660     eprintf("%s: cannot open", path ? path : "(stdin)");
661     return 1;
662   }
663   int rv = prochash(ibuf, isiz, crc, ch);
664   tcfree(ibuf);
665   return rv;
666 }
667 
668 
669 /* parse arguments of cipher command */
runcipher(int argc,char ** argv)670 static int runcipher(int argc, char **argv){
671   char *path = NULL;
672   char *key = NULL;
673   for(int i = 2; i < argc; i++){
674     if(!path && argv[i][0] == '-'){
675       if(!strcmp(argv[i], "-key")){
676         if(++i >= argc) usage();
677         key = argv[i];
678       } else {
679         usage();
680       }
681     } else if(!path){
682       path = argv[i];
683     } else {
684       usage();
685     }
686   }
687   char *ibuf;
688   int isiz;
689   if(path && path[0] == '@'){
690     isiz = strlen(path) - 1;
691     ibuf = tcmemdup(path + 1, isiz);
692   } else {
693     ibuf = tcreadfile(path, -1, &isiz);
694   }
695   if(!ibuf){
696     eprintf("%s: cannot open", path ? path : "(stdin)");
697     return 1;
698   }
699   int rv = proccipher(ibuf, isiz, key);
700   if(path && path[0] == '@') printf("\n");
701   tcfree(ibuf);
702   return rv;
703 }
704 
705 
706 /* parse arguments of date command */
rundate(int argc,char ** argv)707 static int rundate(int argc, char **argv){
708   char *str = NULL;
709   int jl = INT_MAX;
710   bool wf = false;
711   bool rf = false;
712   for(int i = 2; i < argc; i++){
713     if(argv[i][0] == '-'){
714       if(!strcmp(argv[i], "-ds")){
715         if(++i >= argc) usage();
716         str = argv[i];
717       } else if(!strcmp(argv[i], "-jl")){
718         if(++i >= argc) usage();
719         jl = tcatoix(argv[i]);
720       } else if(!strcmp(argv[i], "-wf")){
721         wf = true;
722       } else if(!strcmp(argv[i], "-rf")){
723         rf = true;
724       } else {
725         usage();
726       }
727     } else {
728       usage();
729     }
730   }
731   int rv = procdate(str, jl, wf, rf);
732   return rv;
733 }
734 
735 
736 /* parse arguments of tmpl command */
runtmpl(int argc,char ** argv)737 static int runtmpl(int argc, char **argv){
738   char *path = NULL;
739   TCMAP *vars = tcmpoolmapnew(tcmpoolglobal());
740   for(int i = 2; i < argc; i++){
741     if(!path && argv[i][0] == '-'){
742       if(!strcmp(argv[i], "-var")){
743         if(++i >= argc) usage();
744         const char *name = argv[i];
745         if(++i >= argc) usage();
746         const char *value = argv[i];
747         tcmapput2(vars, name, value);
748       } else {
749         usage();
750       }
751     } else if(!path){
752       path = argv[i];
753     } else {
754       usage();
755     }
756   }
757   char *ibuf;
758   int isiz;
759   if(path && path[0] == '@'){
760     isiz = strlen(path) - 1;
761     ibuf = tcmemdup(path + 1, isiz);
762   } else {
763     ibuf = tcreadfile(path, -1, &isiz);
764   }
765   if(!ibuf){
766     eprintf("%s: cannot open", path ? path : "(stdin)");
767     return 1;
768   }
769   int rv = proctmpl(ibuf, isiz, vars);
770   if(path && path[0] == '@') printf("\n");
771   tcfree(ibuf);
772   return rv;
773 }
774 
775 
776 /* parse arguments of conf command */
runconf(int argc,char ** argv)777 static int runconf(int argc, char **argv){
778   int mode = 0;
779   for(int i = 2; i < argc; i++){
780     if(argv[i][0] == '-'){
781       if(!strcmp(argv[i], "-v")){
782         mode = 'v';
783       } else if(!strcmp(argv[i], "-i")){
784         mode = 'i';
785       } else if(!strcmp(argv[i], "-l")){
786         mode = 'l';
787       } else if(!strcmp(argv[i], "-p")){
788         mode = 'p';
789       } else {
790         usage();
791       }
792     } else {
793       usage();
794     }
795   }
796   int rv = procconf(mode);
797   return rv;
798 }
799 
800 
801 /* perform url command */
procurl(const char * ibuf,int isiz,bool dec,bool br,const char * base)802 static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base){
803   if(base){
804     char *obuf = tcurlresolve(base, ibuf);
805     printf("%s", obuf);
806     tcfree(obuf);
807   } else if(br){
808     TCMAP *elems = tcurlbreak(ibuf);
809     const char *elem;
810     if((elem = tcmapget2(elems, "self")) != NULL) printf("self: %s\n", elem);
811     if((elem = tcmapget2(elems, "scheme")) != NULL) printf("scheme: %s\n", elem);
812     if((elem = tcmapget2(elems, "host")) != NULL) printf("host: %s\n", elem);
813     if((elem = tcmapget2(elems, "port")) != NULL) printf("port: %s\n", elem);
814     if((elem = tcmapget2(elems, "authority")) != NULL) printf("authority: %s\n", elem);
815     if((elem = tcmapget2(elems, "path")) != NULL) printf("path: %s\n", elem);
816     if((elem = tcmapget2(elems, "file")) != NULL) printf("file: %s\n", elem);
817     if((elem = tcmapget2(elems, "query")) != NULL) printf("query: %s\n", elem);
818     if((elem = tcmapget2(elems, "fragment")) != NULL) printf("fragment: %s\n", elem);
819     tcmapdel(elems);
820   } else if(dec){
821     int osiz;
822     char *obuf = tcurldecode(ibuf, &osiz);
823     fwrite(obuf, 1, osiz, stdout);
824     tcfree(obuf);
825   } else {
826     char *obuf = tcurlencode(ibuf, isiz);
827     fwrite(obuf, 1, strlen(obuf), stdout);
828     tcfree(obuf);
829   }
830   return 0;
831 }
832 
833 
834 /* perform base command */
procbase(const char * ibuf,int isiz,bool dec)835 static int procbase(const char *ibuf, int isiz, bool dec){
836   if(dec){
837     int osiz;
838     char *obuf = tcbasedecode(ibuf, &osiz);
839     fwrite(obuf, 1, osiz, stdout);
840     tcfree(obuf);
841   } else {
842     char *obuf = tcbaseencode(ibuf, isiz);
843     fwrite(obuf, 1, strlen(obuf), stdout);
844     tcfree(obuf);
845   }
846   return 0;
847 }
848 
849 
850 /* perform quote command */
procquote(const char * ibuf,int isiz,bool dec)851 static int procquote(const char *ibuf, int isiz, bool dec){
852   if(dec){
853     int osiz;
854     char *obuf = tcquotedecode(ibuf, &osiz);
855     fwrite(obuf, 1, osiz, stdout);
856     tcfree(obuf);
857   } else {
858     char *obuf = tcquoteencode(ibuf, isiz);
859     fwrite(obuf, 1, strlen(obuf), stdout);
860     tcfree(obuf);
861   }
862   return 0;
863 }
864 
865 
866 /* perform mime command */
procmime(const char * ibuf,int isiz,bool dec,const char * ename,bool qb,bool on,bool hd,bool bd,int part)867 static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on,
868                     bool hd, bool bd, int part){
869   if(hd || bd || part > 0){
870     TCMAP *hmap = tcmapnew2(16);
871     int osiz;
872     char *obuf = tcmimebreak(ibuf, isiz, hmap, &osiz);
873     if(part > 0){
874       const char *value;
875       if(!(value = tcmapget2(hmap, "TYPE")) || !tcstrifwm(value, "multipart/") ||
876          !(value = tcmapget2(hmap, "BOUNDARY"))){
877         eprintf("not multipart");
878       } else {
879         TCLIST *parts = tcmimeparts(obuf, osiz, value);
880         if(part <= tclistnum(parts)){
881           int bsiz;
882           const char *body = tclistval(parts, part - 1, &bsiz);
883           fwrite(body, 1, bsiz, stdout);
884         } else {
885           eprintf("no such part");
886         }
887         tclistdel(parts);
888       }
889     } else {
890       if(hd){
891         TCLIST *names = tcmapkeys(hmap);
892         tclistsort(names);
893         int num = tclistnum(names);
894         for(int i = 0; i < num; i++){
895           const char *name = tclistval2(names, i);
896           printf("%s\t%s\n", name, tcmapget2(hmap, name));
897         }
898         tclistdel(names);
899         if(bd) putchar('\n');
900       }
901       if(bd) fwrite(obuf, 1, osiz, stdout);
902     }
903     tcfree(obuf);
904     tcmapdel(hmap);
905   } else if(dec){
906     char ebuf[32];
907     char *obuf = tcmimedecode(ibuf, ebuf);
908     if(on){
909       fwrite(ebuf, 1, strlen(ebuf), stdout);
910     } else {
911       fwrite(obuf, 1, strlen(obuf), stdout);
912     }
913     tcfree(obuf);
914   } else {
915     char *obuf = tcmimeencode(ibuf, ename, !qb);
916     fwrite(obuf, 1, strlen(obuf), stdout);
917     tcfree(obuf);
918   }
919   return 0;
920 }
921 
922 
923 /* perform hex command */
prochex(const char * ibuf,int isiz,bool dec)924 static int prochex(const char *ibuf, int isiz, bool dec){
925   if(dec){
926     int osiz;
927     char *obuf = tchexdecode(ibuf, &osiz);
928     fwrite(obuf, 1, osiz, stdout);
929     tcfree(obuf);
930   } else {
931     char *obuf = tchexencode(ibuf, isiz);
932     fwrite(obuf, 1, strlen(obuf), stdout);
933     tcfree(obuf);
934   }
935   return 0;
936 }
937 
938 
939 /* perform pack command */
procpack(const char * ibuf,int isiz,bool dec,bool bwt)940 static int procpack(const char *ibuf, int isiz, bool dec, bool bwt){
941   if(dec){
942     int osiz;
943     char *obuf = tcpackdecode(ibuf, isiz, &osiz);
944     if(bwt && osiz > 0){
945       int idx, step;
946       TCREADVNUMBUF(obuf, idx, step);
947       char *tbuf = tcbwtdecode(obuf + step, osiz - step, idx);
948       fwrite(tbuf, 1, osiz - step, stdout);
949       tcfree(tbuf);
950     } else {
951       fwrite(obuf, 1, osiz, stdout);
952     }
953     tcfree(obuf);
954   } else {
955     char *tbuf = NULL;
956     if(bwt){
957       int idx;
958       tbuf = tcbwtencode(ibuf, isiz, &idx);
959       char vnumbuf[sizeof(int)+1];
960       int step;
961       TCSETVNUMBUF(step, vnumbuf, idx);
962       tbuf = tcrealloc(tbuf, isiz + step + 1);
963       memmove(tbuf + step, tbuf, isiz);
964       memcpy(tbuf, vnumbuf, step);
965       isiz += step;
966       ibuf = tbuf;
967     }
968     int osiz;
969     char *obuf = tcpackencode(ibuf, isiz, &osiz);
970     fwrite(obuf, 1, osiz, stdout);
971     tcfree(obuf);
972     tcfree(tbuf);
973   }
974   return 0;
975 }
976 
977 
978 /* perform tcbs command */
proctcbs(const char * ibuf,int isiz,bool dec)979 static int proctcbs(const char *ibuf, int isiz, bool dec){
980   if(dec){
981     int osiz;
982     char *obuf = tcbsdecode(ibuf, isiz, &osiz);
983     fwrite(obuf, 1, osiz, stdout);
984     tcfree(obuf);
985   } else {
986     int osiz;
987     char *obuf = tcbsencode(ibuf, isiz, &osiz);
988     fwrite(obuf, 1, osiz, stdout);
989     tcfree(obuf);
990   }
991   return 0;
992 }
993 
994 
995 /* perform zlib command */
proczlib(const char * ibuf,int isiz,bool dec,bool gz)996 static int proczlib(const char *ibuf, int isiz, bool dec, bool gz){
997   if(dec){
998     int osiz;
999     char *obuf = gz ? tcgzipdecode(ibuf, isiz, &osiz) : tcinflate(ibuf, isiz, &osiz);
1000     if(obuf){
1001       fwrite(obuf, 1, osiz, stdout);
1002       tcfree(obuf);
1003     } else {
1004       eprintf("inflate failure");
1005     }
1006   } else {
1007     int osiz;
1008     char *obuf = gz ? tcgzipencode(ibuf, isiz, &osiz) : tcdeflate(ibuf, isiz, &osiz);
1009     if(obuf){
1010       fwrite(obuf, 1, osiz, stdout);
1011       tcfree(obuf);
1012     } else {
1013       eprintf("deflate failure");
1014     }
1015   }
1016   return 0;
1017 }
1018 
1019 
1020 /* perform bzip command */
procbzip(const char * ibuf,int isiz,bool dec)1021 static int procbzip(const char *ibuf, int isiz, bool dec){
1022   if(dec){
1023     int osiz;
1024     char *obuf = tcbzipdecode(ibuf, isiz, &osiz);
1025     if(obuf){
1026       fwrite(obuf, 1, osiz, stdout);
1027       tcfree(obuf);
1028     } else {
1029       eprintf("inflate failure");
1030     }
1031   } else {
1032     int osiz;
1033     char *obuf = tcbzipencode(ibuf, isiz, &osiz);
1034     if(obuf){
1035       fwrite(obuf, 1, osiz, stdout);
1036       tcfree(obuf);
1037     } else {
1038       eprintf("deflate failure");
1039     }
1040   }
1041   return 0;
1042 }
1043 
1044 
1045 /* perform xml command */
procxml(const char * ibuf,int isiz,bool dec,bool br)1046 static int procxml(const char *ibuf, int isiz, bool dec, bool br){
1047   if(br){
1048     TCLIST *elems = tcxmlbreak(ibuf);
1049     for(int i = 0; i < tclistnum(elems); i++){
1050       int esiz;
1051       const char *elem = tclistval(elems, i, &esiz);
1052       char *estr = tcmemdup(elem, esiz);
1053       tcstrsubchr(estr, "\t\n\r", "  ");
1054       tcstrtrim(estr);
1055       if(*estr != '\0'){
1056         if(*elem == '<'){
1057           if(tcstrfwm(estr, "<!--")){
1058             printf("COMMENT\t%s\n", estr);
1059           } else if(tcstrfwm(estr, "<![CDATA[")){
1060             printf("CDATA\t%s\n", estr);
1061           } else if(tcstrfwm(estr, "<!")){
1062             printf("DECL\t%s\n", estr);
1063           } else if(tcstrfwm(estr, "<?")){
1064             printf("XMLDECL\t%s\n", estr);
1065           } else {
1066             TCMAP *attrs = tcxmlattrs(estr);
1067             tcmapiterinit(attrs);
1068             const char *name;
1069             if((name = tcmapget2(attrs, "")) != NULL){
1070               if(tcstrfwm(estr, "</")){
1071                 printf("END");
1072               } else if(tcstrbwm(estr, "/>")){
1073                 printf("EMPTY");
1074               } else {
1075                 printf("BEGIN");
1076               }
1077               printf("\t%s", name);
1078               while((name = tcmapiternext2(attrs)) != NULL){
1079                 if(*name == '\0') continue;
1080                 printf("\t%s\t%s", name, tcmapiterval2(name));
1081               }
1082               printf("\n");
1083             }
1084             tcmapdel(attrs);
1085           }
1086         } else {
1087           char *dstr = tcxmlunescape(estr);
1088           printf("TEXT\t%s\n", dstr);
1089           tcfree(dstr);
1090         }
1091       }
1092       tcfree(estr);
1093     }
1094     tclistdel(elems);
1095   } else if(dec){
1096     char *obuf = tcxmlunescape(ibuf);
1097     fwrite(obuf, 1, strlen(obuf), stdout);
1098     tcfree(obuf);
1099   } else {
1100     char *obuf = tcxmlescape(ibuf);
1101     fwrite(obuf, 1, strlen(obuf), stdout);
1102     tcfree(obuf);
1103   }
1104   return 0;
1105 }
1106 
1107 
1108 /* perform cstr command */
proccstr(const char * ibuf,int isiz,bool dec,bool js)1109 static int proccstr(const char *ibuf, int isiz, bool dec, bool js){
1110   if(js){
1111     if(dec){
1112       char *ostr = tcjsonunescape(ibuf);
1113       printf("%s", ostr);
1114       tcfree(ostr);
1115     } else {
1116       char *ostr = tcjsonescape(ibuf);
1117       printf("%s", ostr);
1118       tcfree(ostr);
1119     }
1120   } else {
1121     if(dec){
1122       char *ostr = tccstrunescape(ibuf);
1123       printf("%s", ostr);
1124       tcfree(ostr);
1125     } else {
1126       char *ostr = tccstrescape(ibuf);
1127       printf("%s", ostr);
1128       tcfree(ostr);
1129     }
1130   }
1131   return 0;
1132 }
1133 
1134 
1135 /* perform ucs command */
procucs(const char * ibuf,int isiz,bool dec,bool un,const char * kw)1136 static int procucs(const char *ibuf, int isiz, bool dec, bool un, const char *kw){
1137   if(un){
1138     uint16_t *ary = tcmalloc(isiz * sizeof(uint16_t) + 1);
1139     int anum;
1140     tcstrutftoucs(ibuf, ary, &anum);
1141     anum = tcstrucsnorm(ary, anum, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH);
1142     char *str = tcmalloc(anum * 3 + 1);
1143     tcstrucstoutf(ary, anum, str);
1144     printf("%s", str);
1145     tcfree(str);
1146     tcfree(ary);
1147   } else if(kw){
1148     TCLIST *words = tcstrtokenize(kw);
1149     TCLIST *texts = tcstrkwic(ibuf, words, 10, TCKWMUTAB);
1150     for(int i = 0; i < tclistnum(texts); i++){
1151       printf("%s\n", tclistval2(texts, i));
1152     }
1153     tclistdel(texts);
1154     tclistdel(words);
1155   } else if(dec){
1156     uint16_t *ary = tcmalloc(isiz + 1);
1157     int anum = 0;
1158     for(int i = 0; i < isiz; i += 2){
1159       ary[anum++] = (((unsigned char *)ibuf)[i] << 8) + ((unsigned char *)ibuf)[i+1];
1160     }
1161     char *str = tcmalloc(isiz * 3 + 1);
1162     tcstrucstoutf(ary, anum, str);
1163     printf("%s", str);
1164     tcfree(str);
1165     tcfree(ary);
1166   } else {
1167     uint16_t *ary = tcmalloc(isiz * sizeof(uint16_t) + 1);
1168     int anum;
1169     tcstrutftoucs(ibuf, ary, &anum);
1170     for(int i = 0; i < anum; i++){
1171       int c = ary[i];
1172       putchar(c >> 8);
1173       putchar(c & 0xff);
1174     }
1175     tcfree(ary);
1176   }
1177   return 0;
1178 }
1179 
1180 
1181 /* perform hash command */
prochash(const char * ibuf,int isiz,bool crc,int ch)1182 static int prochash(const char *ibuf, int isiz, bool crc, int ch){
1183   if(crc){
1184     printf("%08x\n", tcgetcrc(ibuf, isiz));
1185   } else if(ch > 0){
1186     TCCHIDX *chidx = tcchidxnew(ch);
1187     printf("%d\n", tcchidxhash(chidx, ibuf, isiz));
1188     tcchidxdel(chidx);
1189   } else {
1190     char buf[48];
1191     tcmd5hash(ibuf, isiz, buf);
1192     printf("%s\n", buf);
1193   }
1194   return 0;
1195 }
1196 
1197 
1198 /* perform cipher command */
proccipher(const char * ibuf,int isiz,const char * key)1199 static int proccipher(const char *ibuf, int isiz, const char *key){
1200   char *obuf = tcmalloc(isiz + 1);
1201   const char *kbuf = "";
1202   int ksiz = 0;
1203   if(key){
1204     kbuf = key;
1205     ksiz = strlen(key);
1206   }
1207   tcarccipher(ibuf, isiz, kbuf, ksiz, obuf);
1208   fwrite(obuf, 1, isiz, stdout);
1209   tcfree(obuf);
1210   return 0;
1211 }
1212 
1213 
1214 /* perform date command */
procdate(const char * str,int jl,bool wf,bool rf)1215 static int procdate(const char *str, int jl, bool wf, bool rf){
1216   int64_t t = str ? tcstrmktime(str) : time(NULL);
1217   if(wf){
1218     char buf[48];
1219     tcdatestrwww(t, jl, buf);
1220     printf("%s\n", buf);
1221   } else if(rf){
1222     char buf[48];
1223     tcdatestrhttp(t, jl, buf);
1224     printf("%s\n", buf);
1225   } else {
1226     printf("%lld\n", (long long int)t);
1227   }
1228   return 0;
1229 }
1230 
1231 
1232 /* perform tmpl command */
proctmpl(const char * ibuf,int isiz,TCMAP * vars)1233 static int proctmpl(const char *ibuf, int isiz, TCMAP *vars){
1234   TCTMPL *tmpl = tctmplnew();
1235   tctmplload(tmpl, ibuf);
1236   char *str = tctmpldump(tmpl, vars);
1237   printf("%s", str);
1238   tcfree(str);
1239   tctmpldel(tmpl);
1240   return 0;
1241 }
1242 
1243 
1244 /* perform conf command */
procconf(int mode)1245 static int procconf(int mode){
1246   switch(mode){
1247     case 'v':
1248       printf("%s\n", tcversion);
1249       break;
1250     case 'i':
1251       printf("%s\n", _TC_APPINC);
1252       break;
1253     case 'l':
1254       printf("%s\n", _TC_APPLIBS);
1255       break;
1256     case 'p':
1257       printf("%s\n", _TC_BINDIR);
1258       break;
1259     default:
1260       printf("myconf(version): %s\n", tcversion);
1261       printf("myconf(sysname): %s\n", TCSYSNAME);
1262       printf("myconf(libver): %d\n", _TC_LIBVER);
1263       printf("myconf(formatver): %s\n", _TC_FORMATVER);
1264       printf("myconf(prefix): %s\n", _TC_PREFIX);
1265       printf("myconf(includedir): %s\n", _TC_INCLUDEDIR);
1266       printf("myconf(libdir): %s\n", _TC_LIBDIR);
1267       printf("myconf(bindir): %s\n", _TC_BINDIR);
1268       printf("myconf(libexecdir): %s\n", _TC_LIBEXECDIR);
1269       printf("myconf(appinc): %s\n", _TC_APPINC);
1270       printf("myconf(applibs): %s\n", _TC_APPLIBS);
1271       printf("myconf(bigend): %d\n", TCBIGEND);
1272       printf("myconf(usezlib): %d\n", TCUSEZLIB);
1273       printf("myconf(usebzip): %d\n", TCUSEBZIP);
1274       printf("type(bool): size=%d align=%d offset=%d max=%llu\n",
1275              (int)sizeof(bool), (int)_alignof(bool), TCALIGNOF(bool),
1276              (unsigned long long)true);
1277       printf("type(char): size=%d align=%d offset=%d max=%llu\n",
1278              (int)sizeof(char), (int)_alignof(char), TCALIGNOF(char),
1279              (unsigned long long)CHAR_MAX);
1280       printf("type(short): size=%d align=%d offset=%d max=%llu\n",
1281              (int)sizeof(short), (int)_alignof(short), TCALIGNOF(short),
1282              (unsigned long long)SHRT_MAX);
1283       printf("type(int): size=%d align=%d offset=%d max=%llu\n",
1284              (int)sizeof(int), (int)_alignof(int), TCALIGNOF(int),
1285              (unsigned long long)INT_MAX);
1286       printf("type(long): size=%d align=%d offset=%d max=%llu\n",
1287              (int)sizeof(long), (int)_alignof(long), TCALIGNOF(long),
1288              (unsigned long long)LONG_MAX);
1289       printf("type(long long): size=%d align=%d offset=%d max=%llu\n",
1290              (int)sizeof(long long), (int)_alignof(long long), TCALIGNOF(long long),
1291              (unsigned long long)LLONG_MAX);
1292       printf("type(float): size=%d align=%d offset=%d max=%g\n",
1293              (int)sizeof(float), (int)_alignof(float), TCALIGNOF(float),
1294              (double)FLT_MAX);
1295       printf("type(double): size=%d align=%d offset=%d max=%g\n",
1296              (int)sizeof(double), (int)_alignof(double), TCALIGNOF(double),
1297              (double)DBL_MAX);
1298       printf("type(long double): size=%d align=%d offset=%d max=%Lg\n",
1299              (int)sizeof(long double), (int)_alignof(long double), TCALIGNOF(long double),
1300              (long double)LDBL_MAX);
1301       printf("type(void *): size=%d align=%d offset=%d\n",
1302              (int)sizeof(void *), (int)_alignof(void *), TCALIGNOF(void *));
1303       printf("type(intptr_t): size=%d align=%d offset=%d max=%llu\n",
1304              (int)sizeof(intptr_t), (int)_alignof(intptr_t), TCALIGNOF(intptr_t),
1305              (unsigned long long)INTPTR_MAX);
1306       printf("type(size_t): size=%d align=%d offset=%d max=%llu\n",
1307              (int)sizeof(size_t), (int)_alignof(size_t), TCALIGNOF(size_t),
1308              (unsigned long long)SIZE_MAX);
1309       printf("type(ptrdiff_t): size=%d align=%d offset=%d max=%llu\n",
1310              (int)sizeof(ptrdiff_t), (int)_alignof(ptrdiff_t), TCALIGNOF(ptrdiff_t),
1311              (unsigned long long)PTRDIFF_MAX);
1312       printf("type(wchar_t): size=%d align=%d offset=%d max=%llu\n",
1313              (int)sizeof(wchar_t), (int)_alignof(wchar_t), TCALIGNOF(wchar_t),
1314              (unsigned long long)WCHAR_MAX);
1315       printf("type(sig_atomic_t): size=%d align=%d offset=%d max=%llu\n",
1316              (int)sizeof(sig_atomic_t), (int)_alignof(sig_atomic_t), TCALIGNOF(sig_atomic_t),
1317              (unsigned long long)SIG_ATOMIC_MAX);
1318       printf("type(time_t): size=%d align=%d offset=%d max=%llu\n",
1319              (int)sizeof(time_t), (int)_alignof(time_t), TCALIGNOF(time_t),
1320              (unsigned long long)_maxof(time_t));
1321       printf("type(off_t): size=%d align=%d offset=%d max=%llu\n",
1322              (int)sizeof(off_t), (int)_alignof(off_t), TCALIGNOF(off_t),
1323              (unsigned long long)_maxof(off_t));
1324       printf("type(ino_t): size=%d align=%d offset=%d max=%llu\n",
1325              (int)sizeof(ino_t), (int)_alignof(ino_t), TCALIGNOF(ino_t),
1326              (unsigned long long)_maxof(ino_t));
1327       printf("type(tcgeneric_t): size=%d align=%d offset=%d\n",
1328              (int)sizeof(tcgeneric_t), (int)_alignof(tcgeneric_t), TCALIGNOF(tcgeneric_t));
1329       printf("macro(RAND_MAX): %llu\n", (unsigned long long)RAND_MAX);
1330       printf("macro(PATH_MAX): %llu\n", (unsigned long long)PATH_MAX);
1331       printf("macro(NAME_MAX): %llu\n", (unsigned long long)NAME_MAX);
1332       printf("macro(P_tmpdir): %s\n", P_tmpdir);
1333       printf("sysconf(_SC_CLK_TCK): %ld\n", sysconf(_SC_CLK_TCK));
1334       printf("sysconf(_SC_OPEN_MAX): %ld\n", sysconf(_SC_OPEN_MAX));
1335       printf("sysconf(_SC_PAGESIZE): %ld\n", sysconf(_SC_PAGESIZE));
1336       TCMAP *info = tcsysinfo();
1337       if(info){
1338         tcmapiterinit(info);
1339         const char *name;
1340         while((name = tcmapiternext2(info)) != NULL){
1341           printf("sysinfo(%s): %s\n", name, tcmapiterval2(name));
1342         }
1343         tcmapdel(info);
1344       }
1345       struct stat sbuf;
1346       if(stat(MYCDIRSTR, &sbuf) == 0){
1347         printf("stat(st_uid): %d\n", (int)sbuf.st_uid);
1348         printf("stat(st_gid): %d\n", (int)sbuf.st_gid);
1349         printf("stat(st_blksize): %d\n", (int)sbuf.st_blksize);
1350       }
1351   }
1352   return 0;
1353 }
1354 
1355 
1356 
1357 // END OF FILE
1358