#include #include #include #include #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 * PATH * DO * -- */ 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 * - read in tokens * - wait for -- * - after --, read everything in and save to the LATERFILE * - make sure to also save the PWD */ char* laterfile_path = getenv("LATERFILE"); if (!laterfile_path || strlen(laterfile_path) == 0) { printf("where's your LATERFILE?\n"); return 1; } // open the file for reading/appending FILE* laterfile = fopen(laterfile_path, "a+"); fseek(laterfile, 0, SEEK_END); // loop through the arguments int argi = 1; while (argi < argc && strncmp(argv[argi], "--", 2) != 0) argi++; if (argi == argc) { printf("unrecognized arguments\n"); return 1; } argi++; // go past the -- LaterItem_t* item = new_item(); build_item(item, argv+argi, argc-argi); print_item(item); return 0; }