-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
134 lines (115 loc) · 4.14 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input/getArgs.h"
#include "input/getInput.h"
#include "input/getFileExtention.h"
#include "add/add.h"
#include "file/fileRW.h"
#define TRUE 1
int main(int argc, char *argv[])
{
char *baseStr = (char*)malloc(1);
char *baseInclude = (char*)malloc(1);
char *filePath;
int fileArgLoc;
if(getArgs('h', "--help", argc, argv) || argc == 1)
{
printf("chelp [options] [required]\n");
printf("options:\n");
printf(" -h --help Displays help menu\n");
printf(" -s --std Adds standard header libraries(stdio.h, stdlib.h, string.h)\n");
printf(" -n --netsocket Adds socket libraries\n");
printf(" -c --custom Prompts for any custom header files\n");
printf(" -q --quotes Adds quotes around the custom header files ex: #include \"customHeaderFile.h\" rather than #include <customHeaderFile.h>\n");
printf("Required (Unless --help is being used):\n");
printf(" -f --file Path to file you'd like to save to\n");
return 0;
}
strcpy(baseStr, "");
strcpy(baseInclude, "");
if(getArgs('s', "--std", argc, argv)) // Add the standard/basic header files for a basic C program
{
baseStr = add(baseStr, "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n");
}
if(getArgs('n', "--netsocket", argc, argv)) // Add socket based header files into file
{
baseStr = add(baseStr, "#if defined(_WIN32)\n"
"#ifndef _WIN32_WINNT\n"
"#define _WIN32_QINNT 0x0600\n\n"
"#endif\n"
"#include <winsock2.h>\n"
"#include <ws2tcpip.h>\n"
"#pragma comment(lib, \"ws2_32.lib\")\n\n"
"#else\n"
"#include <sys/types.h>\n"
"#include <sys/socket.h>\n"
"#include <netinet/in.h>\n"
"#include <arpa/inet.h>\n"
"#include <netdb.h>\n"
"#include <unistd.h>\n"
"#include <errno.h>\n\n"
"#endif\n\n\n"
"#if defined(_WIN32)\n"
"#define ISVALIDSOCKET(s) ((s) != INVALID_SOCKET)\n"
"#define CLOSESOCKET(s) closesocket(s)\n"
"#define GETSOCKETEERRNO() (WSAGetLastError())\n\n"
"#else\n"
"#define ISVALIDSOCKET(s) ((s) >= 0)\n"
"#define CLOSESOCKET(s) close(s)\n"
"#define SOCKET int\n"
"#define GETSOCKETERRNO() (errno);\n");
}
if(getArgs('c', "--custom", argc, argv))
{
while(TRUE)
{
char incBrackets[2] = {'<', '>'};
char include[] = "#include ";
if(getArgs('q', "--quotes", argc, argv))
{
incBrackets[0] = '"';
incBrackets[1] = '"';
}
include[9] = incBrackets[0];
printf("[!] Add custom header files: ");
char *header = getInput();
if(!strcmp(header, ""))
{
baseStr = add(baseStr, baseInclude);
break;
}
char *fullInclude = (char*)malloc(strlen(include) + strlen(header) + 3);
strcpy(fullInclude, include);
strcat(fullInclude, header);
char tempStr[] = {incBrackets[1], '\n', '\0'};
strcat(fullInclude, tempStr);
free(header);
baseInclude = add(baseInclude, fullInclude);
}
}
fileArgLoc = getArgs('f', "--file", argc, argv);
if(!fileArgLoc) // Get file path from CLI arguments
{
printf("[!] Need file location!\n");
return 1;
}
else
{
if(fileArgLoc + 2 > argc)
{
printf("[!] Not enough arguments");
return 1;
}
filePath = (char*)malloc(strlen(argv[fileArgLoc + 1] + 1));
strcpy(filePath, argv[fileArgLoc + 1]);
}
if(getFileExtention(filePath) == 'c')
{
baseStr = add(baseStr, "\nint main(int argc, char *argv[])\n{\n\nreturn 0;\n}");
}
writeFile(filePath, strlen(baseStr), baseStr);
free(baseStr);
free(filePath);
return 0;
}