aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2025-11-11 12:30:47 -0500
committerBen Winston2025-11-11 12:38:18 -0500
commit22d10da8be61a0b3be19b228b015fe1e5a965dbc (patch)
treef47897bc896318b6ed355eaa8df2421beae972c1
parente5fbef8e416fe89d1fe2f7840383c26528b6b8d4 (diff)
define a format in a header file, parse command to the format
-rw-r--r--later.c87
-rw-r--r--later.h12
2 files changed, 72 insertions, 27 deletions
diff --git a/later.c b/later.c
index 5f59577..81eda3b 100644
--- a/later.c
+++ b/later.c
@@ -3,6 +3,59 @@
#include <string.h>
#include <unistd.h>
+#include "later.h"
+
+
+LaterItem_t* new_item() {
+ LaterItem_t* item = (LaterItem_t*)malloc(sizeof(LaterItem_t));
+ item->directory = NULL;
+ item->path = NULL;
+ item->command = NULL;
+ return item;
+}
+
+void free_item(LaterItem_t* item) {
+ if (item->directory) free(item->directory);
+ if (item->path) free(item->path);
+ if (item->command) free(item->command);
+ free(item);
+}
+
+void build_item(LaterItem_t* item, char** raw_command, int raw_command_length) {
+ // get the current directory
+ char directory[255];
+ getcwd(directory, sizeof(directory)-1);
+ item->directory = strdup(directory);
+
+ // get the current path
+ char* path = getenv("PATH");
+ item->path = path;
+
+ // copy the command
+ char* command = (char*)malloc(2048);
+ int command_idx = 0;
+ char* cur;
+ for (int raw_idx = 0; raw_idx < raw_command_length; raw_idx++) {
+ cur = raw_command[raw_idx];
+ command_idx += strlen(cur);
+ strcat(command, cur);
+ command[command_idx] = ' ';
+ command_idx++;
+ }
+ item->command = command;
+}
+
+void print_item(LaterItem_t* item) {
+ /*
+ * Format:
+ * DIR <pwd>
+ * PATH <path>
+ * DO <command>
+ * --
+ */
+ printf("DIR %s\nPATH %s\nDO %s\n--\n", item->directory, item->path, item->command);
+}
+
int main(int argc, char** argv) {
/* how it could work
@@ -10,12 +63,6 @@ int main(int argc, char** argv) {
* - wait for --
* - after --, read everything in and save to the LATERFILE
* - make sure to also save the PWD
- *
- * Format is:
- * FROM <pwd>
- * <command>
- * --
- *
*/
char* laterfile_path = getenv("LATERFILE");
if (!laterfile_path || strlen(laterfile_path) == 0) {
@@ -23,7 +70,9 @@ int main(int argc, char** argv) {
return 1;
}
- FILE* laterfile = fopen(laterfile_path, "a");
+ // open the file for reading/appending
+ FILE* laterfile = fopen(laterfile_path, "a+");
+ fseek(laterfile, 0, SEEK_END);
// loop through the arguments
int argi = 1;
@@ -36,26 +85,10 @@ int main(int argc, char** argv) {
}
argi++; // go past the --
-
- // determine the current working directory
- char pwd[255];
- getcwd(pwd, 254);
-
- // TODO check err
-
- // write the output
- fwrite("FROM ", 1, 5, laterfile);
- fwrite(pwd, 1, strlen(pwd), laterfile);
- fwrite("\n", 1, 1, laterfile);
-
- // write out the command
- for (int i = argi; i < argc; i++) {
- fwrite(argv[i], 1, strlen(argv[i]), laterfile);
- fwrite(" ", 1, 1, laterfile);
- }
-
- // write the terminal
- fwrite("\n--\n", 1, 4, laterfile);
+
+ LaterItem_t* item = new_item();
+ build_item(item, argv+argi, argc-argi);
+ print_item(item);
return 0;
}
diff --git a/later.h b/later.h
new file mode 100644
index 0000000..875b81c
--- /dev/null
+++ b/later.h
@@ -0,0 +1,12 @@
+#ifndef __LATER_H__
+#define __LATER_H__
+
+typedef struct LaterItem LaterItem_t;
+
+struct LaterItem {
+ char* directory;
+ char* path;
+ char* command;
+};
+
+#endif