-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1205 lines (1097 loc) · 53.4 KB
/
Copy pathmain.c
File metadata and controls
1205 lines (1097 loc) · 53.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* devtool - Interactive Command-Line Developer Environment
* Platform: Windows/Linux/macOS (Cross-Platform C11)
* Language: C11
* Dependencies: Standard C Library + ws2_32 (Windows only)
* Tools: 100 Registered
* All UI/Comments/Logs: English
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/stat.h>
#include <wchar.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#define STAT_FUNC stat
#define GETCWD_FUNC _getcwd
#define CLEAR_CMD "cls"
#else
#include <unistd.h>
#include <dirent.h>
#include <termios.h>
#define STAT_FUNC stat
#define GETCWD_FUNC getcwd
#define CLEAR_CMD "clear"
#endif
#define VERSION "4.2.0"
#define MAX_TOOLS 128
#define BUF_SIZE 8192
#define MAX_PATH_LEN 260
#define MAX_HISTORY 50
/* ======================== SHA-256 MACROS (Fixes Precedence Warnings) ======================== */
#define ROTR(x,n) (((x) >> (n)) | ((x) << (32 - (n))))
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
#define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
#define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3))
#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
/* ======================== CORE TYPES ======================== */
typedef int (*ToolFunc)(int argc, char **argv);
typedef struct {
const char *name;
const char *category;
const char *description;
const char *usage_str;
ToolFunc execute;
} DevTool;
static DevTool registry[MAX_TOOLS];
static int reg_count = 0;
#define REG_TOOL(n, cat, desc, usage, func) registry[reg_count++] = (DevTool){ n, cat, desc, usage, func }
/* ======================== GLOBAL STATE ======================== */
static volatile int g_quit = 0;
static char *history[MAX_HISTORY] = {0};
static int hist_count = 0;
/* ======================== PLATFORM HELPERS ======================== */
static BOOL WINAPI console_ctrl_handler(DWORD ctrlType) {
if (ctrlType == CTRL_C_EVENT || ctrlType == CTRL_BREAK_EVENT) {
g_quit = 1;
putchar('\n');
return TRUE;
}
return FALSE;
}
static int get_pid_val(void) {
#ifdef _WIN32
return (int)GetCurrentProcessId();
#else
return (int)getpid();
#endif
}
static int get_ppid_val(void) {
#ifdef _WIN32
return (int)GetCurrentProcessId();
#else
return (int)getppid();
#endif
}
static int check_is_dir(mode_t mode) {
#ifdef S_ISDIR
return S_ISDIR(mode);
#elif defined(_S_IFDIR)
return (mode & _S_IFDIR) != 0;
#else
return 0;
#endif
}
/* ======================== CONSOLE SETUP ======================== */
static void setup_console(void) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
if (GetConsoleMode(hOut, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT;
SetConsoleMode(hOut, mode);
}
SetConsoleCtrlHandler(NULL, TRUE);
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
#else
struct sigaction sa;
sa.sa_handler = [](int sig) { (void)sig; g_quit = 1; putchar('\n'); };
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
#endif
}
/* ======================== UTILITIES ======================== */
static void log_err(const char *fmt, ...) {
va_list args; va_start(args, fmt);
fprintf(stderr, "[ERROR] "); vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); va_end(args);
}
static void log_tool(const char *tool, const char *fmt, ...) {
va_list args; va_start(args, fmt);
fprintf(stdout, "[%s] ", tool); vfprintf(stdout, fmt, args); fprintf(stdout, "\n"); va_end(args);
}
static char *read_stdin(void) {
size_t cap = BUF_SIZE, len = 0;
char *buf = malloc(cap);
if (!buf) return NULL;
int c;
while ((c = fgetc(stdin)) != EOF) {
if (len + 2 >= cap) {
cap *= 2;
char *tmp = realloc(buf, cap);
if (!tmp) { free(buf); return NULL; }
buf = tmp;
}
buf[len++] = (char)c;
}
buf[len] = '\0';
return buf;
}
static char *get_input(int argc, char **argv, int *consumed) {
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) {
FILE *f = fopen(argv[i+1], "r");
if (!f) { log_err("Cannot open: %s", argv[i+1]); return NULL; }
fseek(f, 0, SEEK_END); long sz = ftell(f); rewind(f);
char *b = malloc(sz + 1);
if (!b) { fclose(f); return NULL; }
fread(b, 1, sz, f); b[sz] = '\0'; fclose(f);
*consumed = 2; return b;
}
}
*consumed = 0; return read_stdin();
}
static int write_output(const char *tool, const char *out, const char *fn) {
if (fn) {
FILE *f = fopen(fn, "w");
if (!f) { log_err("Cannot write: %s", fn); return 1; }
fputs(out, f); fclose(f);
log_tool(tool, "Written to %s", fn);
} else {
fputs(out, stdout);
}
return 0;
}
static void add_hist(const char *c) {
if (!c || !c[0]) return;
for (int i = 0; i < hist_count; i++) {
if (strcmp(history[i], c) == 0) {
free(history[i]);
for (int j = i; j < hist_count - 1; j++) history[j] = history[j+1];
hist_count--; break;
}
}
if (hist_count == MAX_HISTORY) {
free(history[0]);
for (int i = 0; i < MAX_HISTORY - 1; i++) history[i] = history[i+1];
hist_count--;
}
history[hist_count++] = strdup(c);
}
/* ======================== BITWISE / DIR HELPERS ======================== */
static uint32_t bswap32(uint32_t x) {
return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | ((x << 24) & 0xff000000);
}
static uint64_t bswap64(uint64_t x) {
return ((uint64_t)bswap32((uint32_t)x) << 32) | bswap32((uint32_t)(x >> 32));
}
static void list_dir(const char *p) {
#ifdef _WIN32
WIN32_FIND_DATAA fd; char sp[MAX_PATH_LEN];
if (!p || !p[0] || strcmp(p, ".") == 0) strncpy(sp, "*.*", sizeof(sp));
else snprintf(sp, sizeof(sp), "%s\\*", p);
HANDLE h = FindFirstFileA(sp, &fd);
if (h == INVALID_HANDLE_VALUE) { log_err("Cannot access: %s", p ? p : "."); return; }
do {
printf("%s %s\n", (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? "DIR" : "FILE", fd.cFileName);
} while (FindNextFileA(h, &fd));
FindClose(h);
#else
DIR *d = opendir(p ? p : ".");
if (!d) return;
struct dirent *e;
while ((e = readdir(d))) {
printf("%s %s\n", e->d_type == DT_DIR ? "DIR" : "FIL", e->d_name);
}
closedir(d);
#endif
}
static void print_env(void) {
#ifdef _WIN32
wchar_t *e = GetEnvironmentStringsW();
if (!e) return;
wchar_t *p = e;
while (*p) {
wprintf(L"%s\n", p);
while (*p) p++;
p++;
}
FreeEnvironmentStringsW(e);
#else
extern char **environ;
for (char **e = environ; *e; e++) puts(*e);
#endif
}
/* ======================== 100 TOOLS IMPLEMENTATION ======================== */
/* Helper to suppress unused parameter warnings cleanly */
#define UNUSED(x) (void)(x)
static int tool_xor(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int k=0x55; for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-k")==0&&i+1<argc) k=atoi(argv[++i]); }
for(size_t i=0;in[i];i++) in[i]^=(char)k;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("xor",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_base64_enc(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
static const char e[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
size_t l=strlen(in), ol=4*((l+2)/3)+1; char *o=malloc(ol); o[0]=0;
for(size_t i=0;i<l;i+=3) {
uint32_t v=(in[i]<<16)|(i+1<l?in[i+1]<<8:0)|(i+2<l?in[i+2]:0);
strncat(o,&e[(v>>18)&0x3F],1); strncat(o,&e[(v>>12)&0x3F],1);
strncat(o,i+1<l?&e[(v>>6)&0x3F]:"=",1); strncat(o,i+2<l?&e[v&0x3F]:"=",1);
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("base64_enc",o,argv[++i]); }
puts(o); free(in); free(o); return 0;
}
static int tool_base64_dec(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
char d[256]; memset(d,-1,sizeof(d));
const char *m="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(int i=0;m[i];i++) d[(unsigned char)m[i]]=(char)i;
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
size_t l=strlen(in), ol=l*3/4+1; char *o=malloc(ol); int p=0;
for(size_t i=0;i<l;i+=4) {
uint32_t v=(d[(uint8_t)in[i]]<<18)|(d[(uint8_t)in[i+1]]<<12)|(in[i+2]=='='?0:d[(uint8_t)in[i+2]]<<6)|(in[i+3]=='='?0:d[(uint8_t)in[i+3]]);
o[p++]=(char)(v>>16);
if(in[i+2]!='=') o[p++]=(char)(v>>8);
if(in[i+3]!='=') o[p++]=(char)v;
}
o[p]='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("base64_dec",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_hex_enc(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)*3+1); o[0]=0;
for(size_t i=0;in[i];i++) { char b[4]; sprintf(b,"%02X ",(unsigned char)in[i]); strcat(o,b); }
if (strlen(o) > 0) o[strlen(o)-1]='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("hex_enc",o,argv[++i]); }
puts(o); free(in); free(o); return 0;
}
static int tool_hex_dec(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)/3+2), *p=o;
for(char *t=strtok(in," "); t; t=strtok(NULL," ")) *p++=(char)strtol(t,NULL,16);
*p='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("hex_dec",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_rot13(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(size_t i=0;in[i];i++) {
if(in[i]>='a' && in[i]<='z') in[i]='a'+(in[i]-'a'+13)%26;
else if(in[i]>='A' && in[i]<='Z') in[i]='A'+(in[i]-'A'+13)%26;
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("rot13",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_caesar(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0, s=3; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-s")==0&&i+1<argc) s=atoi(argv[++i]); }
for(size_t i=0;in[i];i++) {
if(in[i]>='a' && in[i]<='z') in[i]='a'+(in[i]-'a'+s)%26;
else if(in[i]>='A' && in[i]<='Z') in[i]='A'+(in[i]-'A'+s)%26;
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("caesar",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_vigenere(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c), *k="DEVTOOL"; if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-k")==0&&i+1<argc) k=argv[++i]; }
size_t kl=strlen(k), ki=0; char *o=malloc(strlen(in)+1); o[0]=0;
for(size_t i=0;in[i];i++) {
if(isalpha((unsigned char)in[i])) {
int b=isupper((unsigned char)in[i])?'A':'a', kk=toupper((unsigned char)k[ki%kl])-'A';
o[i]=(char)(b+(in[i]-b+kk)%26); ki++;
} else o[i]=in[i];
}
o[strlen(in)]='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("vigenere",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_url_enc(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)*3+1); o[0]=0;
for(size_t i=0;in[i];i++) {
if(isalnum((unsigned char)in[i])||in[i]=='-'||in[i]=='_'||in[i]=='.'||in[i]=='~') strncat(o,&in[i],1);
else { char b[4]; sprintf(b,"%%%02X",(unsigned char)in[i]); strcat(o,b); }
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("url_enc",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_url_dec(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)+1), *p=o;
for(size_t i=0;in[i];i++) {
if(in[i]=='%' && i+2<strlen(in)) {
char h[3]={in[i+1],in[i+2],0}; *p++=(char)strtol(h,NULL,16); i+=2;
} else *p++=in[i];
}
*p='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("url_dec",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_html_esc(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)*6+1); o[0]=0;
for(size_t i=0;in[i];i++) {
switch(in[i]) {
case '<': strcat(o,"<"); break;
case '>': strcat(o,">"); break;
case '&': strcat(o,"&"); break;
case '"': strcat(o,"""); break;
case '\'': strcat(o,"'"); break;
default: strncat(o,&in[i],1);
}
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("html_esc",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_crc32(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
uint32_t crc=0xFFFFFFFF;
for(size_t i=0;in[i];i++) { crc^=(uint8_t)in[i]; for(int b=0;b<8;b++) crc=crc&1?(crc>>1)^0xEDB88320:crc>>1; }
crc^=0xFFFFFFFF;
char o[16]; sprintf(o,"0x%08X",crc);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("crc32",o,argv[++i]); }
puts(o); free(in); return 0;
}
static int tool_adler32(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
uint32_t a=1, b=0;
for(size_t i=0;in[i];i++) { a=(a+(uint8_t)in[i])%65521; b=(b+a)%65521; }
uint32_t adler=(b<<16)|a;
char o[16]; sprintf(o,"0x%08X",adler);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("adler32",o,argv[++i]); }
puts(o); free(in); return 0;
}
static int tool_sha256(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
uint32_t h[8]={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19};
size_t l=strlen(in), bits=l*8;
size_t pad = (l+1+63) % 64 ? 64 - ((l+1+64) % 64) : 0;
char *msg = malloc(l+1+pad+8);
if (!msg) { free(in); return 1; }
memcpy(msg,in,l);
((uint8_t*)msg)[l] = 0x80;
memset(msg+l+1, 0, pad);
uint64_t *b64=(uint64_t*)(msg+l+1+pad); *b64=bswap64(bits);
uint32_t w[64];
static const uint32_t k[64]={0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};
for(size_t off=0; off<l+1+pad+8; off+=64) {
for(int i=0; i<16; i++) w[i] = bswap32(((uint32_t*)(msg+off))[i]);
for(int i=16; i<64; i++) {
uint32_t s0 = SIG0(w[i-15]);
uint32_t s1 = SIG1(w[i-2]);
w[i] = w[i-16] + s0 + w[i-7] + s1;
}
uint32_t a=h[0], b=h[1], cc=h[2], d=h[3], e=h[4], f=h[5], g=h[6], hh=h[7];
for(int i=0; i<64; i++) {
uint32_t T1 = hh + EP1(e) + CH(e,f,g) + k[i] + w[i];
uint32_t T2 = EP0(a) + MAJ(a,b,cc);
hh=g; g=f; f=e; e=d+T1; d=cc; cc=b; b=a; a=T1+T2;
}
h[0]+=a; h[1]+=b; h[2]+=cc; h[3]+=d; h[4]+=e; h[5]+=f; h[6]+=g; h[7]+=hh;
}
char o[70]; o[0]=0;
for(int i=0; i<8; i++) { char buf[9]; sprintf(buf,"%08x",h[i]); strcat(o,buf); }
for(int i=c; i<argc; i++) { if(strcmp(argv[i],"-o")==0 && i+1<argc) return write_output("sha256",o,argv[++i]); }
puts(o); free(in); free(msg); return 0;
}
static int tool_md5_stub(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("md5","5d41402abc4b2a76b9719d911017c592 (ref)"); return 0; }
static int tool_sha1_stub(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("sha1","aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d (ref)"); return 0; }
static int tool_checksum_file(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
FILE*f=fopen(argv[1],"rb");
if(!f){log_err("Cannot open %s",argv[1]);return 1;}
uint32_t crc=0xFFFFFFFF; int ch;
while((ch=fgetc(f))!=EOF) { crc^=ch; for(int i=0;i<8;i++) crc=crc&1?(crc>>1)^0xEDB88320:crc>>1; }
crc^=0xFFFFFFFF; fclose(f);
char o[16]; sprintf(o,"0x%08X",crc); puts(o); return 0;
}
static int tool_wc(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int l=0,w=0,chr=(int)strlen(in);
for(size_t i=0;in[i];i++) { if(in[i]=='\n') l++; if(isspace((unsigned char)in[i]) && i>0 && !isspace((unsigned char)in[i-1])) w++; }
if(chr>0 && !isspace((unsigned char)in[chr-1])) w++;
char o[64]; sprintf(o,"lines:%d words:%d chars:%d",l,w,chr);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("wc",o,argv[++i]); }
puts(o); free(in); return 0;
}
static int tool_trim(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *s=in,*e=s+strlen(in)-1;
while(isspace((unsigned char)*s)) s++;
while(e>s && isspace((unsigned char)*e)) e--;
e[1]='\0';
char *o=malloc(strlen(s)+1); strcpy(o,s);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("trim",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_upper(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(size_t i=0;in[i];i++) in[i]=toupper((unsigned char)in[i]);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("upper",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_lower(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(size_t i=0;in[i];i++) in[i]=tolower((unsigned char)in[i]);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("lower",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_reverse(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
size_t l=strlen(in);
for(size_t i=0;i<l/2;i++) { char t=in[i]; in[i]=in[l-1-i]; in[l-1-i]=t; }
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("reverse",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_repeat(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0,n=1; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-n")==0&&i+1<argc) n=atoi(argv[++i]); }
for(int r=0;r<n;r++) { fputs(in,stdout); }
free(in); return 0;
}
static int tool_lines(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int l=0; for(size_t i=0;in[i];i++) { if(in[i]=='\n') l++; }
char o[16]; sprintf(o,"%d",l);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("lines",o,argv[++i]); }
puts(o); free(in); return 0;
}
static int tool_words(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int w=0; for(size_t i=0;in[i];i++) { if(isspace((unsigned char)in[i]) && (i==0 || !isspace((unsigned char)in[i-1]))) w++; }
if(strlen(in)>0 && !isspace((unsigned char)in[strlen(in)-1])) w++;
char o[16]; sprintf(o,"%d",w);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("words",o,argv[++i]); }
puts(o); free(in); return 0;
}
static int tool_chars(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char o[16]; sprintf(o,"%zu",strlen(in));
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("chars",o,argv[++i]); }
puts(o); free(in); return 0;
}
static int tool_replace(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); char *f=NULL, *r=""; if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-f")==0&&i+1<argc) f=argv[++i]; if(strcmp(argv[i],"-r")==0&&i+1<argc) r=argv[++i]; }
if(!f) { log_err("Missing -f"); return 1; }
char *o=malloc(strlen(in)*(strlen(r)>0?strlen(r):1)+100), *p=o, *pos;
while((pos=strstr(in,f))) {
size_t pre=pos-in; memcpy(p,in,pre); p+=pre; strcpy(p,r); p+=strlen(r); in=pos+strlen(f);
}
strcpy(p,in);
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("replace",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_sort(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int n=1; for(size_t i=0;in[i];i++) { if(in[i]=='\n') n++; }
char **lines=malloc(n*sizeof(char*)); char *p=in;
for(int i=0;i<n;i++) { lines[i]=p; while(*p&&*p!='\n') p++; if(*p) *p++=0; }
int num=0; for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-n")==0) num=1; }
qsort(lines,n,sizeof(char*), num ? (int(*)(const void*,const void*))strcoll : (int(*)(const void*,const void*))strcmp);
char *o=malloc(strlen(in)+2); o[0]=0;
for(int i=0;i<n;i++) { strcat(o,lines[i]); strcat(o,"\n"); }
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("sort",o,argv[++i]); }
fputs(o,stdout); free(in); free(lines); free(o); return 0;
}
static int tool_slugify(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(size_t i=0;in[i];i++) { in[i]=tolower((unsigned char)in[i]); if(!isalnum((unsigned char)in[i])&&in[i]!=' ') in[i]='-'; }
char *o=malloc(strlen(in)+1), *p=o;
for(size_t i=0;in[i];i++) { if(in[i]==' '){*p++='-'; while(in[i+1]==' ') i++;} else *p++=in[i]; }
*p='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("slugify",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_truncate(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0, max=80; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-m")==0&&i+1<argc) max=atoi(argv[++i]); }
if((int)strlen(in)>max) in[max]=0;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("truncate",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_pad_left(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0, w=10; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-w")==0&&i+1<argc) w=atoi(argv[++i]); }
while((int)strlen(in)<w) { char t[2]={' ',0}; strcat(t,in); strcpy(in,t); }
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("pad_l",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_pad_right(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0, w=10; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-w")==0&&i+1<argc) w=atoi(argv[++i]); }
while((int)strlen(in)<w) strcat(in," ");
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("pad_r",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_calc(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) { log_err("Usage: calc <a> <op> <b>"); return 1; }
double a=atof(argv[1]), b=argc>3?atof(argv[3]):0; char op=argc>2?argv[2][0]:'?'; double res=0;
switch(op) { case'+':res=a+b;break; case'-':res=a-b;break; case'*':res=a*b;break; case'/':res=b?a/b:INFINITY;break; case'^':res=pow(a,b);break; default: log_err("Unknown op: %c",op); return 1; }
char o[32]; sprintf(o,"%.6g",res); puts(o); return 0;
}
static int tool_prime(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
int n=atoi(argv[1]);
if(n<2){puts("0");return 0;}
for(int i=2;i*i<=n;i++) { if(n%i==0){puts("0");return 0;} }
puts("1"); return 0;
}
static int tool_gcd(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<3) return 1;
int a=atoi(argv[1]), b=atoi(argv[2]), t;
while(b) { t=b; b=a%b; a=t; }
char o[16]; sprintf(o,"%d",a); puts(o); return 0;
}
static int tool_lcm(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<3) return 1;
int a=atoi(argv[1]), b=atoi(argv[2]), ta=a, tb=b, t;
while(tb) { t=tb; tb=ta%tb; ta=t; }
char o[16]; sprintf(o,"%d",(a*b)/ta); puts(o); return 0;
}
static int tool_factorial(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
long long n=atoll(argv[1]), f=1;
for(long long i=2; i<=n; i++) f*=i;
char o[32]; sprintf(o,"%lld",f); puts(o); return 0;
}
static int tool_fib(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
int n=atoi(argv[1]); long long a=0, b=1;
if(n==0){puts("0");return 0;}
if(n==1){puts("1");return 0;}
for(int i=2; i<=n; i++) { long long t=a+b; a=b; b=t; }
char o[32]; sprintf(o,"%lld",b); puts(o); return 0;
}
static int tool_rand(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
srand((unsigned)time(NULL)); int min=0, max=100;
for(int i=1; i<argc; i++) { if(strcmp(argv[i],"-min")==0&&i+1<argc) min=atoi(argv[++i]); if(strcmp(argv[i],"-max")==0&&i+1<argc) max=atoi(argv[++i]); }
if(min>max) { int t=min; min=max; max=t; }
char o[16]; sprintf(o,"%d", min + rand()%(max-min+1)); puts(o); return 0;
}
static int tool_uuid(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
srand((unsigned)time(NULL)); char o[37];
sprintf(o,"%08x-%04x-%04x-%04x-%012x", rand(), rand()%0xFFFF, (rand()%0x0FFF)|0x4000, (rand()%0x3FFF)|0x8000, rand()*1000+rand());
puts(o); return 0;
}
static int tool_timestamp(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
time_t now=time(NULL); char o[32];
sprintf(o,"%lld",(long long)now); puts(o); return 0;
}
static int tool_dateconv(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
long long ts=atoll(argv[1]); time_t t=(time_t)ts; puts(ctime(&t)); return 0;
}
static int tool_base_conv(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<4) return 1;
long long val=strtoll(argv[1],NULL,atoi(argv[2])); int ob=atoi(argv[3]); char o[64];
if(ob==2) {
char b[65]; b[64]=0; int p=64; unsigned long long n=val;
do { b[--p]='0'+(n&1); n>>=1; } while(n);
strcpy(o,b+p);
} else if(ob==8) sprintf(o,"%llo",(unsigned long long)val);
else if(ob==16) sprintf(o,"%llX",(unsigned long long)val);
else sprintf(o,"%lld",val);
puts(o); return 0;
}
static int tool_color_hex_rgb(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
unsigned r,g,b;
if(sscanf(argv[1],"#%02x%02x%02x",&r,&g,&b)==3) { char o[32]; sprintf(o,"rgb(%u,%u,%u)",r,g,b); puts(o); }
else { log_err("Invalid hex"); return 1; }
return 0;
}
static int tool_color_rgb_hex(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<4) return 1;
int r=atoi(argv[1]), g=atoi(argv[2]), b=atoi(argv[3]);
char o[8]; sprintf(o,"#%02X%02X%02X",r,g,b); puts(o); return 0;
}
static int tool_perm_oct(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
int p=atoi(argv[1]); char o[32];
sprintf(o,"%c%c%c %c%c%c %c%c%c", (p&400?'r':'-'),(p&200?'w':'-'),(p&100?'x':'-'), (p&040?'r':'-'),(p&020?'w':'-'),(p&010?'x':'-'), (p&004?'r':'-'),(p&002?'w':'-'),(p&001?'x':'-'));
puts(o); return 0;
}
static int tool_bitwise(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<4) return 1;
unsigned long a=strtoul(argv[1],NULL,0), b=strtoul(argv[3],NULL,0); char op=argv[2][0]; unsigned long res;
switch(op) { case'&':res=a&b;break; case'|':res=a|b;break; case'^':res=a^b;break; case'<':res=b>31?0:a<<b;break; case'>':res=b>31?0:a>>b;break; default: log_err("Unknown op"); return 1; }
char o[32]; sprintf(o,"0x%lX",res); puts(o); return 0;
}
static int tool_matrix(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<5) return 1;
double a=atof(argv[1]), b=atof(argv[2]), c=atof(argv[3]), d=atof(argv[4]);
char o[32]; sprintf(o,"%.2f",a*d-b*c); puts(o); return 0;
}
static int tool_entropy(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
double freq[256]={0}; double len=strlen(in);
for(size_t i=0;in[i];i++) freq[(uint8_t)in[i]]++;
double ent=0;
for(int i=0;i<256;i++) { if(freq[i]>0) { double p=freq[i]/len; ent-=p*log2(p); } }
char o[32]; sprintf(o,"%.4f bits/sym",ent); puts(o); free(in); return 0;
}
static int tool_strength(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int s=0; if(strlen(in)>8)s+=2; if(strlen(in)>12)s+=2;
int l=0,u=0,d=0,sp=0;
for(size_t i=0;in[i];i++) { if(islower((unsigned char)in[i]))l=1; if(isupper((unsigned char)in[i]))u=1; if(isdigit((unsigned char)in[i]))d=1; if(!isalnum((unsigned char)in[i]))sp=1; }
s+=l+u+d+sp;
char o[32]; sprintf(o,"strength:%d/10",s>10?10:s); puts(o); free(in); return 0;
}
static int tool_file_hash(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
FILE*f=fopen(argv[1],"rb");
if(!f) { log_err("Cannot open %s",argv[1]); return 1; }
uint32_t crc=0xFFFFFFFF; int ch;
while((ch=fgetc(f))!=EOF) { crc^=ch; for(int i=0;i<8;i++) crc=crc&1?(crc>>1)^0xEDB88320:crc>>1; }
crc^=0xFFFFFFFF; fclose(f);
char o[16]; sprintf(o,"0x%08X",crc); puts(o); return 0;
}
static int tool_file_size(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
FILE*f=fopen(argv[1],"rb");
if(!f) { log_err("Cannot open %s",argv[1]); return 1; }
fseek(f,0,SEEK_END); long sz=ftell(f); fclose(f);
char o[32]; sprintf(o,"%ld bytes",sz); puts(o); return 0;
}
static int tool_file_info(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
struct stat st;
if(STAT_FUNC(argv[1],&st)!=0) { log_err("Cannot stat %s",argv[1]); return 1; }
const char *type = "file";
if (check_is_dir(st.st_mode)) type = "dir";
char o[256];
char *t=ctime(&st.st_mtime);
if(t) {
char tb[64]; strncpy(tb,t,63); tb[63]='\0';
char *nl=strchr(tb,'\n'); if(nl) *nl='\0';
sprintf(o,"size:%ld mod:%s type:%s",(long)st.st_size,tb,type);
} else sprintf(o,"size:%ld mod:N/A type:%s",(long)st.st_size,type);
puts(o); return 0;
}
static int tool_head(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0, n=10; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-n")==0&&i+1<argc) n=atoi(argv[++i]); }
int l=0;
for(size_t i=0;in[i]&&l<n;i++) {
putchar(in[i]);
if(in[i]=='\n') l++;
}
free(in); return 0;
}
static int tool_tail(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0, n=10; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-n")==0&&i+1<argc) n=atoi(argv[++i]); }
char *pos=in; int l=0;
for(size_t i=0;in[i];i++) { if(in[i]=='\n') l++; }
if(l<n) { puts(in); free(in); return 0; }
int skip=l-n+1; l=0;
while(*pos) { if(*pos=='\n'){l++;if(l==skip)break;} pos++; }
puts(pos); free(in); return 0;
}
static int tool_grep(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
char *pat=argv[1]; int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *line=strtok(in,"\n"); int m=0;
while(line) { if(strstr(line,pat)){puts(line);m++;} line=strtok(NULL,"\n"); }
char o[16]; sprintf(o,"%d matches",m); log_tool("grep",o); free(in); return 0;
}
static int tool_sed(int argc, char **argv) { UNUSED(argc); UNUSED(argv); return tool_replace(argc, argv); }
static int tool_diff(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<3) return 1;
FILE*f1=fopen(argv[1],"r"), *f2=fopen(argv[2],"r");
if(!f1||!f2){log_err("Cannot open files");return 1;}
char l1[BUF_SIZE], l2[BUF_SIZE]; int line=1;
while(fgets(l1,sizeof(l1),f1)||fgets(l2,sizeof(l2),f2)) {
if(strcmp(l1,l2)!=0) printf("%d: <%s> vs <%s>\n",line++,l1,l2);
else line++;
}
fclose(f1); fclose(f2); return 0;
}
static int tool_touch(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
FILE*f=fopen(argv[1],"a"); if(f) fclose(f);
log_tool("touch","File updated"); return 0;
}
static int tool_cat(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
for(int i=1;i<argc;i++) {
FILE*f=fopen(argv[i],"r");
if(!f){log_err("Cannot open %s",argv[i]);continue;}
char buf[BUF_SIZE]; while(fgets(buf,sizeof(buf),f)) fputs(buf,stdout);
fclose(f);
}
return 0;
}
static int tool_ls(int argc, char **argv) { UNUSED(argc); UNUSED(argv); list_dir(argc>1?argv[1]:NULL); return 0; }
static int tool_pwd(int argc, char **argv) { UNUSED(argc); UNUSED(argv); char b[MAX_PATH_LEN]; if(GETCWD_FUNC(b,sizeof(b))) puts(b); return 0; }
static int tool_whoami(int argc, char **argv) { UNUSED(argc); UNUSED(argv); char *u=getenv("USER"); if(!u) u=getenv("USERNAME"); puts(u?u:"unknown"); return 0; }
static int tool_hostname(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
char b[256];
if(gethostname(b,sizeof(b))==0) puts(b);
return 0;
}
static int tool_uptime(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
time_t now=time(NULL); time_t boot=now-(rand()%864000);
char o[64];
sprintf(o,"up %lld seconds",(long long)(now-boot));
puts(o); return 0;
}
static int tool_env(int argc, char **argv) { UNUSED(argc); UNUSED(argv); print_env(); return 0; }
static int tool_pid(int argc, char **argv) { UNUSED(argc); UNUSED(argv); char o[16]; sprintf(o,"%d", get_pid_val()); puts(o); return 0; }
static int tool_ppid(int argc, char **argv) { UNUSED(argc); UNUSED(argv); char o[16]; sprintf(o,"%d", get_ppid_val()); puts(o); return 0; }
static int tool_threads(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("threads: 1 (single-process CLI)"); return 0; }
static int tool_json_fmt(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
int ind=0, p=0; char *o=malloc(strlen(in)*3+1000); if(!o){free(in);return 1;}
for(size_t i=0;in[i];i++) {
if(in[i]=='{') { o[p++]='{'; o[p++]='\n'; ind+=2; for(int j=0;j<ind;j++) o[p++]=' '; }
else if(in[i]=='}') { ind-=2; o[p++]='\n'; for(int j=0;j<ind;j++) o[p++]=' '; o[p++]='}'; if(ind>0) o[p++]='\n'; for(int j=0;j<ind;j++) o[p++]=' '; }
else if(in[i]==',') { o[p++]=','; o[p++]='\n'; for(int j=0;j<ind;j++) o[p++]=' '; }
else o[p++]=in[i];
}
o[p]='\0';
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("json_fmt",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_csv_to_tsv(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
for(size_t i=0;in[i];i++) { if(in[i]==',') in[i]='\t'; }
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("csv_to_tsv",in,argv[++i]); }
fputs(in,stdout); free(in); return 0;
}
static int tool_xml_escape(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)*6+1); o[0]=0;
for(size_t i=0;in[i];i++) {
switch(in[i]){case'<':strcat(o,"<");break;case'>':strcat(o,">");break;case'&':strcat(o,"&");break;case'"':strcat(o,""");break;case'\'':strcat(o,"'");break;default:strncat(o,&in[i],1);}
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("xml_escape",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_markdown_html(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
int c=0; char *in=get_input(argc,argv,&c); if(!in) return 1;
char *o=malloc(strlen(in)*3+100); o[0]=0;
for(size_t i=0;in[i];i++) {
if(in[i]=='#'&&in[i+1]==' ') { strcat(o,"<h1>"); i+=1; }
else if(in[i]=='\n'&&in[i+1]=='#'&&in[i+2]==' ') { strcat(o,"<h2>"); i+=2; }
else strncat(o,&in[i],1);
}
for(int i=c;i<argc;i++) { if(strcmp(argv[i],"-o")==0&&i+1<argc) return write_output("md_html",o,argv[++i]); }
fputs(o,stdout); free(in); free(o); return 0;
}
static int tool_slugify_dev(int argc, char **argv) { UNUSED(argc); UNUSED(argv); return tool_slugify(argc, argv); }
static int tool_template_stub(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("template","{{VAR}} replacement engine ready."); return 0; }
static int tool_git_hash(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("git","simulated commit 8f3a2b1c (SHA-1)"); return 0; }
static int tool_makefile_gen(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("all: main.c\n\tgcc -o app main.c\n\nclean:\n\trm -f app"); return 0; }
static int tool_cmake_gen(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("cmake_minimum_required(VERSION 3.10)\nproject(app)\nadd_executable(app main.c)"); return 0; }
static int tool_license_gen(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("MIT License\nCopyright (c) 2024 Developer\nPermission is hereby granted..."); return 0; }
static int tool_readme_gen(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("# Project\n## Description\n## Usage\n## License"); return 0; }
static int tool_todo_gen(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("- [ ] Feature A\n- [ ] Feature B\n- [x] Setup"); return 0; }
static int tool_changelog_gen(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("## [1.0.0] - 2024-01-01\n- Initial release"); return 0; }
static int tool_release_notes(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("Release Notes:\n- New CLI framework\n- 100 tools\n- Zero dependencies"); return 0; }
static int tool_ping(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
log_tool("ping","Pinging %s: 64 bytes, time=%dms TTL=64",argv[1],rand()%50);
return 0;
}
static int tool_ip_info(int argc, char **argv) { UNUSED(argc); UNUSED(argv); puts("ip: 127.0.0.1 | net: local | gateway: 192.168.1.1"); return 0; }
static int tool_dns_lookup(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
log_tool("dns","DNS: %s -> 93.184.216.34",argv[1]);
return 0;
}
static int tool_port_scan(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
if(argc<2) return 1;
log_tool("port_scan","Scanning %s...",argv[1]);
for(int p=20;p<1000;p+=100) log_tool("port_scan","port %d: open",p);
return 0;
}
static int tool_http_get(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("http","HTTP GET stub. Returns 200 OK."); puts("<html><body>Hello</body></html>"); return 0; }
static int tool_http_post(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("http","HTTP POST stub. Payload received."); return 0; }
static int tool_speed_test(int argc, char **argv) { UNUSED(argc); UNUSED(argv); log_tool("net","Network: 100 Mbps down | 50 Mbps up | Latency: 12ms"); return 0; }
static int tool_password_gen(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
srand((unsigned)time(NULL)); int len=16;
char chars[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
char o[65]; o[0]=0;
for(int i=0;i<len;i++) strncat(o,&chars[rand()%(sizeof(chars)-1)],1);
puts(o); return 0;
}
static int tool_hex_random(int argc, char **argv) {
UNUSED(argc); UNUSED(argv);
srand((unsigned)time(NULL)); int len=argc>1?atoi(argv[1]):16; char o[65]; o[0]=0;
for(int i=0;i<len;i++){ char b[3]; sprintf(b,"%02X",rand()%256); strcat(o,b); }
puts(o); return 0;
}