| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #define BUF_SIZE 256
- typedef struct node {
- char* string;
- struct node* next;
- } fifo;
- void fifo_append(fifo* head, char* new_string);
- char* fifo_read(fifo** head);
- int main(int argc, char* argv[]) {
- if (argc <= 1) {
- printf("Too few arguments\n");
- return 1;
- }
- printf("%d\n", argc);
- char name[strlen(argv[1]) + 2];
- strcpy(name, argv[1]);
- strcat(name, ".c");
- char path[BUF_SIZE] = "./";
- strcat(path, name);
- 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 = 4 + includes_n;
- char* lines[BUF_SIZE];
- int lines_i = 0;
- if (argc > 2) {
- for (int i = 2; i < argc; i++) {
- char* line = (char*) malloc(BUF_SIZE);
- strcpy(line, "#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\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;
- }
|