| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #define BUF_SIZE 256
- int main(int argc, char* argv[]) {
- if (argc <= 1) {
- printf("Too few arguments\n");
- return 1;
- }
- printf("%d\n", argc);
- for (int i = 0; i < argc; i++) {
- printf("%s\n", argv[i]);
- }
- char name[strlen(argv[1]) + 2];
- strcpy(name, argv[1]);
- strcat(name, ".c");
- char path[BUF_SIZE] = "./";
- strcat(path, name);
- int open_s = open(path, O_RDONLY, 0);
-
- if (errno == 0) {
- printf("File already exists.\n");
- return 2;
- }
- errno = 0;
- const int fd = creat(path, S_IRUSR | S_IWUSR | S_IROTH);
- int includes_n = argc - 2;
- int lines_n = 5 + includes_n;
- char* lines[lines_n] = {};
- int lines_i = 0;
- if (argc > 2) {
- for (int i = 2; i < argc; i++) {
- char line[BUF_SIZE] = "#include <";
- strcat(line, argv[i]);
- strcat(line, ".h>\n");
- lines[lines_i] = line;
- lines_i++;
- }
- lines[lines_i] = "\n";
- }
- lines[++lines_i] = "int main() {\n";
- lines[++lines_i] = "\n";
- lines[++lines_i] = " return 0;\n";
- lines[++lines_i] = "}";
- for (int i = 0; i < lines_n; i++) {
- write(fd, lines[i], strlen(lines[i]));
- }
- close(fd);
- char command[5 + strlen(name)];
- strcpy(command, "nvim ");
- strcat(command, name);
- system(command);
- return 0;
- }
|