| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #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;
- }
|