cfile.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #define BUF_SIZE 256
  8. typedef struct node {
  9. char* string;
  10. struct node* next;
  11. } fifo;
  12. void fifo_append(fifo* head, char* new_string);
  13. char* fifo_read(fifo** head);
  14. int main(int argc, char* argv[]) {
  15. if (argc <= 1) {
  16. printf("Too few arguments\n");
  17. return 1;
  18. }
  19. printf("%d\n", argc);
  20. char name[strlen(argv[1]) + 2];
  21. strcpy(name, argv[1]);
  22. strcat(name, ".c");
  23. char path[BUF_SIZE] = "./";
  24. strcat(path, name);
  25. open(path, O_RDONLY, 0);
  26. if (errno == 0) {
  27. printf("File already exists.\n");
  28. return 2;
  29. }
  30. errno = 0;
  31. const int fd = creat(path, S_IRUSR | S_IWUSR | S_IROTH);
  32. int includes_n = argc - 2;
  33. int lines_n = 4 + includes_n;
  34. char* lines[BUF_SIZE];
  35. int lines_i = 0;
  36. if (argc > 2) {
  37. for (int i = 2; i < argc; i++) {
  38. char* line = (char*) malloc(BUF_SIZE);
  39. strcpy(line, "#include <");
  40. strcat(line, argv[i]);
  41. strcat(line, ".h>\n");
  42. lines[lines_i] = line;
  43. lines_i++;
  44. }
  45. lines[lines_i] = "\n";
  46. }
  47. lines[++lines_i] = "int main() {\n\n";
  48. lines[++lines_i] = " return 0;\n";
  49. lines[++lines_i] = "}";
  50. for (int i = 0; i < lines_n; i++) {
  51. write(fd, lines[i], strlen(lines[i]));
  52. }
  53. close(fd);
  54. char command[5 + strlen(name)];
  55. strcpy(command, "nvim ");
  56. strcat(command, name);
  57. system(command);
  58. return 0;
  59. }