-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiny.c
More file actions
1672 lines (1592 loc) · 51.8 KB
/
tiny.c
File metadata and controls
1672 lines (1592 loc) · 51.8 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
/*
* author: Jason Heflinger
* description: Tiny GCC project manager
*/
#define VERSION 1
#define MAJOR_RELEASE 1
#define MINOR_RELEASE 7
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdint.h>
#define print(...) {printf(__VA_ARGS__);printf("\n");}
#define crash(...) {printf("\033[31m[ERROR]\033[0m "); print(__VA_ARGS__);exit(1);}
#define warn(...) {printf("\033[33m[WARNING]\033[0m "); print(__VA_ARGS__);}
#define PATHLEN 4096
typedef void (*FileHandler)(const char*);
#ifdef __linux__
#include <unistd.h>
#include <dirent.h>
#include <sys/time.h>
#include <pthread.h>
#define cwd(buffer) getcwd(buffer, sizeof(buffer))
#define makedir(dir) (!mkdir(dir, 0755))
typedef pthread_t TINY_THREAD;
typedef pthread_mutex_t TINY_MUTEX;
typedef pthread_cond_t TINY_COND;
#define TINY_THREAD_RETURN_TYPE void*
#define TINY_THREAD_PARAMETER_TYPE void*
#define TINY_CREATE_THREAD(thread, func, parameters) pthread_create(&thread, NULL, (void* (*)(void*))func, parameters)
#define TINY_WAIT_THREAD(thread) pthread_join(thread, NULL)
#define TINY_CREATE_MUTEX(mutex) pthread_mutex_init(&mutex, NULL);
#define TINY_LOCK_MUTEX(mutex) pthread_mutex_lock(&mutex)
#define TINY_RELEASE_MUTEX(mutex) pthread_mutex_unlock(&mutex)
#define TINY_CREATE_COND(cond) pthread_cond_init(&cond, NULL);
#define TINY_WAIT_COND(cond, mutex) pthread_cond_wait(&cond, &mutex)
#define TINY_SIGNAL_COND(cond) pthread_cond_signal(&cond)
#define TINY_BROADCAST_COND(cond) pthread_cond_broadcast(&cond)
int threadcount() {
return (int)sysconf(_SC_NPROCESSORS_ONLN);
}
int dexists(const char* dir) {
struct stat statbuf;
if (stat(dir, &statbuf) != 0) {
return 0;
}
return S_ISDIR(statbuf.st_mode);
}
int fexists(const char* file) {
struct stat statbuf;
if (stat(file, &statbuf) != 0) {
return 0;
}
return !S_ISDIR(statbuf.st_mode);
}
void walkdir(const char* path, FileHandler func) {
DIR *dir = opendir(path);
if (!dir) {
crash("Unable to open directory \"%s\"", path);
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char full_path[PATH_MAX];
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
struct stat statbuf;
if (stat(full_path, &statbuf) != 0) {
crash("Stat call failed");
}
if (S_ISDIR(statbuf.st_mode)) {
func(full_path);
walkdir(full_path, func);
}
}
closedir(dir);
}
void walkfiles(const char* path, FileHandler func) {
DIR *dir = opendir(path);
if (!dir) {
crash("Unable to open directory \"%s\"", path);
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char full_path[PATH_MAX];
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
struct stat statbuf;
if (stat(full_path, &statbuf) != 0) {
crash("Stat call failed");
}
if (!S_ISDIR(statbuf.st_mode)) {
func(full_path);
} else {
walkfiles(full_path, func);
}
}
closedir(dir);
}
uint64_t mtime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)(tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
}
#elif __WIN32
#define WIN32_LEAN_AND_MEAN
#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOVIRTUALKEYCODES // VK_*
#define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
#define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
#define NOSYSMETRICS // SM_*
#define NOMENUS // MF_*
#define NOICONS // IDI_*
#define NOKEYSTATES // MK_*
#define NOSYSCOMMANDS // SC_*
#define NORASTEROPS // Binary and Tertiary raster ops
#define NOSHOWWINDOW // SW_*
#define OEMRESOURCE // OEM Resource values
#define NOATOM // Atom Manager routines
#define NOCLIPBOARD // Clipboard routines
#define NOCOLOR // Screen colors
#define NOCTLMGR // Control and Dialog routines
#define NODRAWTEXT // DrawText() and DT_*
#define NOGDI // All GDI defines and routines
#define NOKERNEL // All KERNEL defines and routines
#define NOUSER // All USER defines and routines
#define NOMB // MB_* and MessageBox()
#define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // typedef METAFILEPICT
#define NOMSG // typedef MSG and associated routines
#define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOSCROLL // SB_* and scrolling routines
#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
#define NOSOUND // Sound driver routines
#define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
#define NOWH // SetWindowsHook and WH_*
#define NOWINOFFSETS // GWL_*, GCL_*, associated routines
#define NOCOMM // COMM driver routines
#define NOKANJI // Kanji support stuff.
#define NOHELP // Help engine interface.
#define NOPROFILER // Profiler interface.
#define NODEFERWINDOWPOS // DeferWindowPos routines
#define NOMCX // Modem Configuration Extensions
#include <direct.h>
#include <windows.h>
#define cwd(buffer) _getcwd(buffer, sizeof(buffer))
#define makedir(dir) (!_mkdir(dir))
typedef HANDLE TINY_THREAD;
typedef CRITICAL_SECTION TINY_MUTEX;
typedef CONDITION_VARIABLE TINY_COND;
#define TINY_THREAD_RETURN_TYPE DWORD WINAPI
#define TINY_THREAD_PARAMETER_TYPE LPVOID
#define TINY_CREATE_THREAD(thread, func, parameters) { thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, (LPVOID)parameters, 0, NULL); }
#define TINY_WAIT_THREAD(thread) {WaitForSingleObject(thread, INFINITE); CloseHandle(thread);}
#define TINY_CREATE_MUTEX(mutex) InitializeCriticalSection(&mutex);
#define TINY_LOCK_MUTEX(mutex) EnterCriticalSection(&mutex)
#define TINY_RELEASE_MUTEX(mutex) LeaveCriticalSection(&mutex)
#define TINY_CREATE_COND(cond) InitializeConditionVariable(&cond);
#define TINY_WAIT_COND(cond, mutex) SleepConditionVariableCS(&cond, &mutex, INFINITE);
#define TINY_SIGNAL_COND(cond) WakeConditionVariable(&cond)
#define TINY_BROADCAST_COND(cond) WakeAllConditionVariable(&cond)
int threadcount() {
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return (int)sysinfo.dwNumberOfProcessors;
}
int dexists(const char* dir) {
struct _stat statbuf;
if (_stat(dir, &statbuf) != 0) {
return 0;
}
return (statbuf.st_mode & _S_IFDIR) != 0;
}
int fexists(const char* file) {
struct _stat statbuf;
if (_stat(file, &statbuf) != 0) {
return 0;
}
return (statbuf.st_mode & _S_IFDIR) == 0;
}
void walkdir(const char* path, FileHandler func) {
char search_path[MAX_PATH];
snprintf(search_path, MAX_PATH, "%s/*", path);
WIN32_FIND_DATAA find_data;
HANDLE hFind = FindFirstFileA(search_path, &find_data);
if (hFind == INVALID_HANDLE_VALUE) {
crash("Unable to open directory \"%s\"", path);
}
do {
const char *name = find_data.cFileName;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
char full_path[MAX_PATH];
snprintf(full_path, MAX_PATH, "%s/%s", path, name);
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
func(full_path);
walkdir(full_path, func);
}
} while (FindNextFileA(hFind, &find_data) != 0);
FindClose(hFind);
}
void walkfiles(const char* path, FileHandler func) {
char search_path[MAX_PATH];
snprintf(search_path, MAX_PATH, "%s/*", path);
WIN32_FIND_DATAA find_data;
HANDLE hFind = FindFirstFileA(search_path, &find_data);
if (hFind == INVALID_HANDLE_VALUE) {
crash("Unable to open directory \"%s\"", path);
}
do {
const char *name = find_data.cFileName;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
char full_path[MAX_PATH];
snprintf(full_path, MAX_PATH, "%s/%s", path, name);
if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
func(full_path);
} else {
walkfiles(full_path, func);
}
} while (FindNextFileA(hFind, &find_data) != 0);
FindClose(hFind);
}
uint64_t mtime() {
return (uint64_t)GetTickCount64();
}
#elif __APPLE__
#include <unistd.h>
#include <dirent.h>
#include <sys/time.h>
#include <pthread.h>
#include <limits.h>
#define cwd(buffer) getcwd(buffer, sizeof(buffer))
#define makedir(dir) (!mkdir(dir, 0755))
typedef pthread_t TINY_THREAD;
typedef pthread_mutex_t TINY_MUTEX;
typedef pthread_cond_t TINY_COND;
#define TINY_THREAD_RETURN_TYPE void*
#define TINY_THREAD_PARAMETER_TYPE void*
#define TINY_CREATE_THREAD(thread, func, parameters) pthread_create(&thread, NULL, (void* (*)(void*))func, parameters)
#define TINY_WAIT_THREAD(thread) pthread_join(thread, NULL)
#define TINY_CREATE_MUTEX(mutex) pthread_mutex_init(&mutex, NULL);
#define TINY_LOCK_MUTEX(mutex) pthread_mutex_lock(&mutex)
#define TINY_RELEASE_MUTEX(mutex) pthread_mutex_unlock(&mutex)
#define TINY_CREATE_COND(cond) pthread_cond_init(&cond, NULL);
#define TINY_WAIT_COND(cond, mutex) pthread_cond_wait(&cond, &mutex)
#define TINY_SIGNAL_COND(cond) pthread_cond_signal(&cond)
#define TINY_BROADCAST_COND(cond) pthread_cond_broadcast(&cond)
int threadcount() {
return (int)sysconf(_SC_NPROCESSORS_ONLN);
}
int dexists(const char* dir) {
struct stat statbuf;
if (stat(dir, &statbuf) != 0) {
return 0;
}
return S_ISDIR(statbuf.st_mode);
}
int fexists(const char* file) {
struct stat statbuf;
if (stat(file, &statbuf) != 0) {
return 0;
}
return !S_ISDIR(statbuf.st_mode);
}
void walkdir(const char* path, FileHandler func) {
DIR *dir = opendir(path);
if (!dir) {
crash("Unable to open directory \"%s\"", path);
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char full_path[PATH_MAX];
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
struct stat statbuf;
if (stat(full_path, &statbuf) != 0) {
crash("Stat call failed");
}
if (S_ISDIR(statbuf.st_mode)) {
func(full_path);
walkdir(full_path, func);
}
}
closedir(dir);
}
void walkfiles(const char* path, FileHandler func) {
DIR *dir = opendir(path);
if (!dir) {
crash("Unable to open directory \"%s\"", path);
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char full_path[PATH_MAX];
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
struct stat statbuf;
if (stat(full_path, &statbuf) != 0) {
crash("Stat call failed");
}
if (!S_ISDIR(statbuf.st_mode)) {
func(full_path);
} else {
walkfiles(full_path, func);
}
}
closedir(dir);
}
uint64_t mtime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)(tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
}
#else
#error "Unsupported operating system detected!"
#endif
typedef enum {
NONE = 0,
PROD = 1 << 0,
AUDIT = 1 << 1,
FAST = 1 << 2,
DEBUG = 1 << 3,
} BuildFlags;
typedef struct {
char str[PATHLEN];
void* next;
} PathList;
typedef struct {
char header[PATHLEN];
PathList* links;
PathList* secondaries;
} HeaderLink;
typedef struct {
HeaderLink* link;
void* next;
} HeaderLinkList;
typedef struct {
char* command;
char destination[PATHLEN];
char file[PATHLEN];
int index;
int basename_ptr;
int sourcei;
} ThreadParameters;
size_t s_start_time = 0;
BuildFlags s_flags = NONE;
BuildFlags s_unflags = NONE;
char s_project_directory[PATHLEN];
char s_main_file_name[PATHLEN];
int s_found_main = 0;
int s_sources_up_to_date = 1;
int s_main_up_to_date = 1;
int s_vulnerabilities = 0;
char s_main_file_path[PATHLEN] = { 0 };
char s_cwd[PATHLEN] = { 0 };
PathList* s_includes = NULL;
PathList* s_links = NULL;
PathList* s_raws = NULL;
PathList* s_defines = NULL;
PathList* s_libs = NULL;
PathList* s_sources = NULL;
PathList* s_objects = NULL;
PathList* s_changed_headers = NULL;
HeaderLinkList* s_header_links = NULL;
HeaderLinkList* s_source_links = NULL;
TINY_THREAD* s_threads = NULL;
int* s_active_threads = NULL;
TINY_MUTEX s_mutex;
int s_sourcei = 0;
int s_easymemory_detected = 0;
void pathlist_add(PathList** list, const char* path) {
PathList* new = calloc(1, sizeof(PathList));
new->next = NULL;
strncpy(new->str, path, PATHLEN);
if (*list == NULL) {
*list = new;
return;
}
PathList* current = *list;
new->next = current;
*list = new;
}
void pathlist_delete(PathList* list) {
if (list == NULL) return;
PathList* n = (PathList*)list->next;
free(list);
pathlist_delete(n);
}
size_t pathlist_len(PathList* list) {
if (list == NULL) return 0;
return 1 + pathlist_len(list->next);
}
void pathlist_construct(PathList* list, char* output) {
size_t outcounter = 0;
PathList* curr = list;
while (curr != NULL) {
strcpy(output + outcounter, curr->str);
outcounter += strlen(curr->str);
output[outcounter] = ' ';
outcounter++;
output[outcounter] = '\0';
curr = (PathList*)curr->next;
}
}
void clean_header_links() {
while (s_header_links) {
pathlist_delete(s_header_links->link->links);
pathlist_delete(s_header_links->link->secondaries);
free(s_header_links->link);
HeaderLinkList* old = s_header_links;
s_header_links = (HeaderLinkList*)s_header_links->next;
free(old);
}
}
void clean_source_links() {
while (s_source_links) {
pathlist_delete(s_source_links->link->links);
pathlist_delete(s_source_links->link->secondaries);
free(s_source_links->link);
HeaderLinkList* old = s_source_links;
s_source_links = (HeaderLinkList*)s_source_links->next;
free(old);
}
}
int header_link_exists(const char* header) {
HeaderLinkList* curr = s_header_links;
while (curr) {
if (strcmp(curr->link->header, header) == 0) {
return 1;
}
curr = (HeaderLinkList*)curr->next;
}
return 0;
}
void add_header_link(const char* header, const char* link) {
HeaderLinkList* curr = s_header_links;
while (curr) {
if (strcmp(curr->link->header, header) == 0) {
pathlist_add(&(curr->link->links), link);
return;
}
curr = (HeaderLinkList*)curr->next;
}
HeaderLinkList* new = calloc(1, sizeof(HeaderLinkList));
new->link = calloc(1, sizeof(HeaderLink));
strcpy(new->link->header, header);
pathlist_add(&(new->link->links), link);
new->next = s_header_links;
s_header_links = new;
}
void add_source_link(const char* header, const char* link) {
HeaderLinkList* curr = s_source_links;
while (curr) {
if (strcmp(curr->link->header, header) == 0) {
pathlist_add(&(curr->link->links), link);
return;
}
curr = (HeaderLinkList*)curr->next;
}
HeaderLinkList* new = calloc(1, sizeof(HeaderLinkList));
new->link = calloc(1, sizeof(HeaderLink));
strcpy(new->link->header, header);
pathlist_add(&(new->link->links), link);
new->next = s_source_links;
s_source_links = new;
}
void add_header_secondary(const char* header, const char* link) {
HeaderLinkList* curr = s_header_links;
while (curr) {
if (strcmp(curr->link->header, header) == 0) {
pathlist_add(&(curr->link->secondaries), link);
return;
}
curr = (HeaderLinkList*)curr->next;
}
HeaderLinkList* new = calloc(1, sizeof(HeaderLinkList));
new->link = calloc(1, sizeof(HeaderLink));
strcpy(new->link->header, header);
pathlist_add(&(new->link->secondaries), link);
new->next = s_header_links;
s_header_links = new;
}
int functionline(const char* line) {
int sfound = 0;
int typefound = 0;
int p1found = 0;
int p2found = 0;
int i = 0;
while (1) {
if (line[i] == '\0') return 0;
if (!sfound && line[i] != ' ') sfound = 1;
if (sfound && !typefound && line[i] == ' ') typefound = 1;
else if (typefound && line[i] == '(' && !p1found) p1found = 1;
else if (p1found && line[i] == '(') return 0;
else if (p1found && line[i] == ')') p2found = 1;
else if (p2found && line[i] == ';') return 1;
else if (p2found) return 0;
i++;
}
return 0;
}
int functionimplline(const char* line) {
int sfound = 0;
int typefound = 0;
int p1found = 0;
int p2found = 0;
int i = 0;
while (1) {
if (line[i] == '\0') return 0;
if (!sfound && line[i] != ' ') sfound = 1;
if (sfound && !typefound && line[i] == ' ') typefound = 1;
else if (typefound && line[i] == '(' && !p1found) p1found = 1;
else if (p1found && line[i] == '(') return 0;
else if (p1found && line[i] == ')') p2found = 1;
else if (p2found && line[i] == '{') return 1;
else if (p2found && line[i] != ' ') return 0;
i++;
}
return 0;
}
void easyc_audit(const char* file) {
int slen = strlen(file);
int header = (slen > 2 && (file[slen - 1] == 'h' && file[slen - 2] == '.'));
int source = (slen > 2 && (file[slen - 1] == 'c' && file[slen - 2] == '.'));
if (!header && !source) {
return;
}
int basename_ptr = 0;
for (int i = slen; i > 0; i--) {
if (file[i] == '/' || file[i] == '\\') {
basename_ptr = i + 1;
break;
}
}
FILE* fp = fopen(file, "r");
if (!fp) {
crash("Failed to open file");
}
char line[PATHLEN * 2] = { 0 };
int linecount = 0;
while (fgets(line, sizeof(line), fp)) {
linecount++;
if (strstr(line,"malloc(") || strstr(line, "calloc(") || strstr(line, "free(")) {
print("Detected unmonitored memory operation in \"%s\" on line %d", file, linecount);
s_vulnerabilities++;
}
}
fclose(fp);
}
void syntax_audit(const char* file) {
int slen = strlen(file);
int header = (slen > 2 && (file[slen - 1] == 'h' && file[slen - 2] == '.'));
int source = (slen > 2 && (file[slen - 1] == 'c' && file[slen - 2] == '.'));
if (!header && !source) {
print("Detected abnormal file type in project: \"%s\"", file);
s_vulnerabilities++;
return;
}
int basename_ptr = 0;
for (int i = slen; i > 0; i--) {
if (file[i] == '/' || file[i] == '\\') {
basename_ptr = i + 1;
break;
}
}
FILE* fp = fopen(file, "r");
if (!fp) {
crash("Failed to open file");
}
char line[PATHLEN * 2] = { 0 };
int linecount = 0;
int prev_empty = 0;
int header_closed = 0;
while (fgets(line, sizeof(line), fp)) {
linecount++;
int linelen = strlen(line);
if (linelen >= PATHLEN) {
print("Detected excessively long line in \"%s\" on line %d", file, linecount);
s_vulnerabilities++;
}
int empty_line = 1;
for (int i = 0; i < linelen; i++) {
if (line[i] != '\n' && line[i] != ' ' && line[i] != '\t' && line[i] != '\r') {
empty_line = 0;
break;
}
}
if (empty_line && prev_empty) {
print("Detected excessive whitespace in \"%s\" on line %d", file, linecount);
s_vulnerabilities++;
prev_empty = 0;
} else if (empty_line) {
prev_empty = 1;
} else {
prev_empty = 0;
}
if (linecount == 1 && header) {
char hbuf[PATHLEN] = { 0 };
snprintf(hbuf, PATHLEN, "#ifndef %s", file + basename_ptr);
int hlen = strlen(hbuf);
for (int i = 8; i < hlen; i++) {
if (hbuf[i] >= 'a' && hbuf[i] <= 'z') hbuf[i] = hbuf[i] + ('A' - 'a');
else if (hbuf[i] == '.') hbuf[i] = '_';
}
char t = line[hlen];
line[hlen] = '\0';
if (strcmp(hbuf, line) != 0) {
print("Detected missing or incorrect header guard in \"%s\" on line %d", file, linecount);
s_vulnerabilities++;
}
line[hlen] = t;
} else if (linecount == 2 && header) {
char hbuf[PATHLEN] = { 0 };
snprintf(hbuf, PATHLEN, "#define %s", file + basename_ptr);
int hlen = strlen(hbuf);
for (int i = 8; i < hlen; i++) {
if (hbuf[i] >= 'a' && hbuf[i] <= 'z') hbuf[i] = hbuf[i] + ('A' - 'a');
else if (hbuf[i] == '.') hbuf[i] = '_';
}
char t = line[hlen];
line[hlen] = '\0';
if (strcmp(hbuf, line) != 0) {
print("Detected missing or incorrect header guard in \"%s\" on line %d", file, linecount);
s_vulnerabilities++;
}
line[hlen] = t;
}
if (header) {
char endbuf[7] = { 0 };
if (linelen >= 6) memcpy(endbuf, line, 6);
if (linelen >= 6 && strcmp("#endif", endbuf) == 0) header_closed = 1;
else if (!empty_line) header_closed = 0;
}
if (header) {
if (functionline(line)) {
char implbuf[PATHLEN] = { 0 };
char badimplbuf[PATHLEN] = { 0 };
for (int i = 0; i < PATHLEN; i++) {
if (line[i] == ';') {
implbuf[i] = ' ';
implbuf[i + 1] = '{';
implbuf[i + 2] = '\0';
badimplbuf[i] = '{';
badimplbuf[i + 1] = '\0';
break;
} else {
implbuf[i] = line[i];
badimplbuf[i] = line[i];
}
}
int implfound = 0;
int badimplfound = 0;
char srcfilepath[PATHLEN] = { 0 };
strcpy(srcfilepath, file);
srcfilepath[slen - 1] = 'c';
if (fexists(srcfilepath)) {
FILE* sfp = fopen(srcfilepath, "r");
if (!sfp) {
crash("Unable to open file");
}
char srcline[PATHLEN * 2] = { 0 };
int srclc = 0;
int srcbilc = 0;
while (fgets(srcline, sizeof(srcline), sfp)) {
srclc++;
if (strstr(srcline, implbuf)) implfound = 1;
if (strstr(srcline, badimplbuf)) {
badimplfound = 1;
srcbilc = srclc;
}
}
if (!implfound) {
if (badimplfound) {
print("The function implementation in \"%s\" on line %d is improperly formatted - please have a space in between the function and the curly brace.", srcfilepath, srcbilc);
s_vulnerabilities++;
} else {
print("Unable to find an implementation for the function on line %d of header \"%s\" in the corresponding source file", linecount, file);
s_vulnerabilities++;
}
}
fclose(sfp);
} else {
print("Unable to find a corresponding source for the header \"%s\"", file);
s_vulnerabilities++;
}
}
}
if (header) {
if (strstr(line, "#include")) {
if (strstr(line, "easymemory.h")) s_easymemory_detected = 1;
char ibuf[PATHLEN] = { 0 };
int ind = 0;
int toggle = 0;
for (int i = 0; i < linelen; i++) {
if (line[i] == '\"' || line[i] == '<' || line[i] == '>') toggle = !toggle;
else if (toggle) {
ibuf[ind] = line[i];
ind++;
if (line[i] == '/' || line[i] == '\\') {
ind = 0;
memset(ibuf, 0, PATHLEN);
}
}
}
add_header_link(file + basename_ptr, ibuf);
}
}
if (source) {
if (line[0] != 0 && line[0] != '\n' && line[0] != '\r' && line[0] != ' ' && line[0] != '\t') {
if (!strstr(line, "static") &&
!strstr(line, "}") &&
!strstr(line, "#")) {
if (strstr(line, "=")) {
print("The global variable detected in \"%s\" on line %d is not translation protected - please make it static.", file, linecount);
s_vulnerabilities++;
}
if (functionimplline(line) && !strstr(line, "int main(")) {
char implbuf[PATHLEN] = { 0 };
strcpy(implbuf, line);
int implcorrect = 0;
for (int i = 0; i < PATHLEN; i++) {
if (line[i] == '{') {
if (i > 0 && line[i - 1] == ' ') {
implbuf[i - 1] = ';';
implbuf[i] = 0;
} else {
implbuf[i] = ';';
implbuf[i + 1] = 0;
}
implcorrect = 1;
}
}
if (implcorrect) {
char hfilepath[PATHLEN] = { 0 };
strcpy(hfilepath, file);
hfilepath[slen - 1] = 'h';
int needs_static = 1;
if (fexists(hfilepath)) {
FILE* hfp = fopen(hfilepath, "r");
if (!hfp) {
crash("Unable to open file");
}
char hline[PATHLEN * 2] = { 0 };
int hlc = 0;
while (fgets(hline, sizeof(hline), hfp)) {
hlc++;
if (strstr(hline, implbuf)) {
needs_static = 0;
break;
}
}
fclose(hfp);
}
if (needs_static) {
print("The global function detected in \"%s\" on line %d is not translation protected - please make it static.", file, linecount);
s_vulnerabilities++;
}
}
}
}
}
if (strstr(line, "#include")) {
if (strstr(line, "easymemory.h")) s_easymemory_detected = 1;
char ibuf[PATHLEN] = { 0 };
int ind = 0;
int toggle = 0;
for (int i = 0; i < linelen; i++) {
if (line[i] == '\"' || line[i] == '<' || line[i] == '>') toggle = !toggle;
else if (toggle) {
ibuf[ind] = line[i];
ind++;
if (line[i] == '/' || line[i] == '\\') {
ind = 0;
memset(ibuf, 0, PATHLEN);
}
}
}
add_source_link(file + basename_ptr, ibuf);
}
}
}
if (header && !header_closed) {
print("Detected missing #endif to close header guard in \"%s\"", file);
s_vulnerabilities++;
}
if (linecount == 0) {
print("Detected empty file \"%s\"", file);
s_vulnerabilities++;
}
fclose(fp);
}
void copyfile(const char* src, const char* dst) {
FILE* srcFile = fopen(src, "rb");
if (!srcFile) {
crash("Failed to open source file");
}
FILE *dstFile = fopen(dst, "wb");
if (!dstFile) {
fclose(srcFile);
crash("Failed to open destination file \"%s\"", dst);
}
char buffer[4096];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), srcFile)) > 0) {
if (fwrite(buffer, 1, bytes, dstFile) != bytes) {
fclose(srcFile);
fclose(dstFile);
crash("Critical write error during copying");
}
}
fclose(srcFile);
fclose(dstFile);
}
int filecmp(const char* path1, const char* path2) {
FILE* f1 = fopen(path1, "rb");
FILE* f2 = fopen(path2, "rb");
if (!f1 || !f2) {
fclose(f1);
fclose(f2);
crash("failure during opening files");
}
struct stat stat1, stat2;
if (stat(path1, &stat1) != 0 || stat(path2, &stat2) != 0) {
fclose(f1);
fclose(f2);
crash("failure during stating files");
}
if (stat1.st_size != stat2.st_size) {
fclose(f1);
fclose(f2);
return 0;
}
char buf1[4096], buf2[4096];
size_t bytes1, bytes2;
int equal = 1;
while ((bytes1 = fread(buf1, 1, 4096, f1)) > 0 &&
(bytes2 = fread(buf2, 1, 4096, f2)) > 0) {
if (bytes1 != bytes2 || memcmp(buf1, buf2, bytes1) != 0) {
equal = 0;
break;
}
}
fclose(f1);
fclose(f2);
return equal;
}
void affirmdir(const char* dir) {
if (!dexists(dir) && !makedir(dir)) {
crash("Unable to affirm directory %s", dir);
}
}
void affirm_to_cache(const char* dir) {
char buffer[PATHLEN] = { 0 };
snprintf(buffer, PATHLEN, "build/cache/%s", dir);
affirmdir(buffer);
}
void add_to_sources(const char* file) {
size_t slen = strlen(file);
if (slen > 2 && file[slen - 1] == 'c' && file[slen - 2] == '.')
pathlist_add(&s_sources, file);
}
void verify_header(const char* file) {
size_t slen = strlen(file);
if (slen > 2 && (file[slen - 1] != 'h' || file[slen - 2] != '.')) return;
int basename_ptr = 0;
for (int i = slen; i > 0; i--) {
if (file[i] == '/' || file[i] == '\\') {
basename_ptr = i + 1;
break;
}
}
char destination[PATHLEN] = { 0 };
snprintf(destination, PATHLEN, "build/cache/%s", file);
if (!fexists(destination)) {
pathlist_add(&s_changed_headers, file + basename_ptr);
copyfile(file, destination);
} else {
if (!filecmp(file, destination)) {
pathlist_add(&s_changed_headers, file + basename_ptr);
copyfile(file, destination);
}
}
}
void accumulate_header(const char* file) {
size_t slen = strlen(file);
if (slen > 2 && (file[slen - 1] != 'h' || file[slen - 2] != '.')) return;
int basename_ptr = 0;
for (int i = slen; i > 0; i--) {
if (file[i] == '/' || file[i] == '\\') {
basename_ptr = i + 1;
break;
}
}
PathList* curr = s_changed_headers;
while (curr != NULL) {
if (strcmp(file + basename_ptr, curr->str) == 0) return;
curr = (PathList*)curr->next;
}
FILE* fp = fopen(file, "r");
if (!fp) {
crash("Unable to open file \"%s\"", file);
}
char line[PATHLEN * 2] = { 0 };
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, "#include") != NULL) {
curr = s_changed_headers;
while (curr != NULL) {
if (strstr(line, curr->str) != NULL) {
pathlist_add(&s_changed_headers, file + basename_ptr);
fclose(fp);
return;
}
curr = (PathList*)curr->next;
}
}
}
fclose(fp);
}
void async_compile_progress_update(int index, int action, const char* name) {
if (action == 0) {
print("- [%s] \033[33m(compiling...)\033[0m", name);
} else {
print("\033[%dA\033[2K- [%s] \033[32mOK\033[0m", s_sourcei - index, name);
printf("\033[%dB", (s_sourcei - index) - 1);
}
}
void async_compile(void* params) {
ThreadParameters* tp = (ThreadParameters*)params;
int result = system(tp->command);
if (result == 0) {
copyfile(tp->file, tp->destination);
TINY_LOCK_MUTEX(s_mutex);
async_compile_progress_update(tp->sourcei, 1, tp->file + tp->basename_ptr);
TINY_RELEASE_MUTEX(s_mutex);
} else {
TINY_LOCK_MUTEX(s_mutex);
print("Building source \"%s\" \033[31mfailed\033[0m", tp->file + tp->basename_ptr);
exit(1);
}
TINY_LOCK_MUTEX(s_mutex);
s_active_threads[tp->index] = 2;