| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #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;
- fifo* lines_head = (fifo*) malloc(sizeof(fifo));
- lines_head->string = "";
- lines_head->next = NULL;
- if (argc > 2) {
- for (int i = 2; i < argc; i++) {
- char line[BUF_SIZE] = "#include <";
- strcat(line, argv[i]);
- strcat(line, ".h>\n");
- fifo_append(lines_head, line);
- }
- fifo_append(lines_head, "\n");
- }
- fifo_append(lines_head, "int main() {\n\n");
- fifo_append(lines_head, " return 0;\n");
- fifo_append(lines_head, "}");
- for (int i = 0; i < lines_n; i++) {
- write(fd, fifo_read(&lines_head), strlen(lines_head->string));
- }
- close(fd);
- char command[5 + strlen(name)];
- strcpy(command, "nvim ");
- strcat(command, name);
- system(command);
- return 0;
- }
- void fifo_append(fifo* head, char* string) {
- fifo* last = head;
- char* new_string = (char*) malloc(sizeof(char) * strlen(string));
- strcpy(new_string, string);
- while (last->next != NULL)
- last = last->next;
- fifo* new_node = (fifo*) malloc(sizeof(fifo));
- new_node->string = new_string;
- new_node->next = NULL;
- last->next = new_node;
- }
- char* fifo_read(fifo** head) {
- char* return_value = (*head)->string;
- fifo* prev_head = *head;
- *head = (*head)->next;
- free(prev_head);
- return return_value;
- }
|