Browse Source

New tool cr

Aaro Saila 1 year ago
parent
commit
42b8c56c61
2 changed files with 79 additions and 0 deletions
  1. 72 0
      cr/cr.c
  2. 7 0
      cr/makefile

+ 72 - 0
cr/cr.c

@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAX_ARG_LENGTH 1000
+
+#define VERSION "1.0"
+
+// TODO: Check file modification date and only recompile if binary is older.
+
+typedef struct {
+  char full_path[MAX_ARG_LENGTH];
+  char name[MAX_ARG_LENGTH];
+  char exec_name[MAX_ARG_LENGTH];
+  char dest_path[MAX_ARG_LENGTH + 6]; // /tmp/ + exec_name + \0
+} file_info;
+
+void parse_flags(char argv[][MAX_ARG_LENGTH], file_info* fi) {
+  if (strcmp(argv[1], "--version") == 0) {
+    printf("cr version: %s\n", VERSION);
+    exit(0);
+  }
+
+  strncpy(fi->full_path, argv[1], (sizeof(fi->full_path) / sizeof(char)) - 1);
+  const char* delim = "/";
+  char* first_token = strtok(argv[1], delim);
+
+  if (first_token == NULL) {
+    strncpy(fi->name, argv[1], (sizeof(fi->full_path) / sizeof(char)) - 1);
+  } else {
+    char* prev_token = first_token;
+    char* cur_token = first_token;
+    while (cur_token != NULL) {
+      prev_token = cur_token;
+      cur_token = strtok(NULL, delim);
+    }
+
+    strncpy(fi->name, prev_token, (sizeof(fi->full_path) / sizeof(char)) - 1);
+  }
+
+  strncpy(fi->exec_name, fi->name, sizeof(fi->exec_name) / sizeof(char));
+  fi->exec_name[strlen(fi->exec_name) - 2] = '\0';
+  strncpy(fi->dest_path, "/tmp/", (sizeof(fi->dest_path) / sizeof(char)) - 1);
+  strncat(fi->dest_path, fi->exec_name, (sizeof(fi->dest_path) / sizeof(char)) - 1);
+}
+
+int main(int argc, char* argv[]) {
+  if (argc < 2) {
+    printf("ERROR: Path to C file must be specified.\n");
+    return 1;
+  }
+
+  file_info fi;
+  char argv_copy[argc][MAX_ARG_LENGTH];
+  for (int i = 0; i < argc; i++) {
+    strncpy(argv_copy[i], argv[i], MAX_ARG_LENGTH);
+  }
+  parse_flags(argv_copy, &fi);
+
+  const int cmd_max_len = strlen("gcc ") + sizeof(fi.full_path) / sizeof(char) + strlen(" -o ") + sizeof(fi.dest_path) / sizeof(char);
+  char cmd[cmd_max_len];
+  strcpy(cmd, "gcc ");
+  strcat(cmd, fi.full_path);
+  strcat(cmd, " -o ");
+  strcat(cmd, fi.dest_path);
+  strcat(cmd, "\0");
+
+  system(cmd);
+  system(fi.dest_path);
+  
+  return 0;
+}

+ 7 - 0
cr/makefile

@@ -0,0 +1,7 @@
+.PHONY: compile
+compile:
+	gcc -Wall -Werror -pedantic -Og -ggdb -o cr cr.c
+
+.PHONY: release
+release:
+	gcc -Wall -Werror -pedantic -O3 -o cr cr.c