diff --git a/.clinerules/workspace_rules.md b/.clinerules/workspace_rules.md new file mode 100644 index 0000000..d84c98c --- /dev/null +++ b/.clinerules/workspace_rules.md @@ -0,0 +1,4 @@ + +When building, use build.sh, not make. + +Use it as follows: build.sh -m "useful comment on changes being made" \ No newline at end of file diff --git a/debug b/debug new file mode 100755 index 0000000..0c48976 Binary files /dev/null and b/debug differ diff --git a/debug.c b/debug.c new file mode 100644 index 0000000..7231826 --- /dev/null +++ b/debug.c @@ -0,0 +1 @@ +int main() { printf("Testing direct filename: %d\n", strncmp("97d9d82b5414a9439102f3811fb90ab1d6368a00d33229a18b306476f9d04f82.pad", "97", 2)); return 0; } diff --git a/otp.c b/otp.c index 01fc1a0..d0663ff 100644 --- a/otp.c +++ b/otp.c @@ -365,41 +365,53 @@ uint64_t parse_size_string(const char* size_str) { char* find_pad_by_prefix(const char* prefix) { DIR* dir = opendir(PADS_DIR); - if (!dir) return NULL; + if (!dir) { + printf("Error: Cannot open pads directory\n"); + return NULL; + } struct dirent* entry; char* matches[100]; // Store up to 100 matches int match_count = 0; // Check if it's a number (for interactive menu selection) + // Only treat as number if it's a single digit (1-9) to avoid conflicts with hex prefixes char* endptr; int selection = strtol(prefix, &endptr, 10); - if (*endptr == '\0' && selection > 0) { + if (*endptr == '\0' && selection > 0 && selection <= 9 && strlen(prefix) == 1) { // It's a number, find the nth pad int current = 0; rewinddir(dir); while ((entry = readdir(dir)) != NULL && match_count == 0) { - if (strstr(entry->d_name, ".pad") && strlen(entry->d_name) == 68) { // 64 char chksum + ".pad" - current++; - if (current == selection) { - matches[match_count] = malloc(65); - strncpy(matches[match_count], entry->d_name, 64); - matches[match_count][64] = '\0'; - match_count = 1; - } + // Skip . and .. entries, and only process .pad files + if (entry->d_name[0] == '.') continue; + if (!strstr(entry->d_name, ".pad")) continue; + if (strlen(entry->d_name) != 68) continue; // 64 char chksum + ".pad" + + current++; + if (current == selection) { + matches[match_count] = malloc(65); + strncpy(matches[match_count], entry->d_name, 64); + matches[match_count][64] = '\0'; + match_count = 1; + break; } } } else { // Find pads that start with the prefix size_t prefix_len = strlen(prefix); while ((entry = readdir(dir)) != NULL && match_count < 100) { - if (strstr(entry->d_name, ".pad") && strlen(entry->d_name) == 68) { - if (strncmp(entry->d_name, prefix, prefix_len) == 0) { - matches[match_count] = malloc(65); - strncpy(matches[match_count], entry->d_name, 64); - matches[match_count][64] = '\0'; - match_count++; - } + // Skip . and .. entries, and only process .pad files + if (entry->d_name[0] == '.') continue; + if (!strstr(entry->d_name, ".pad")) continue; + if (strlen(entry->d_name) != 68) continue; // 64 char chksum + ".pad" + + // Compare prefix with the filename (checksum part) + if (strncmp(entry->d_name, prefix, prefix_len) == 0) { + matches[match_count] = malloc(65); + strncpy(matches[match_count], entry->d_name, 64); + matches[match_count][64] = '\0'; + match_count++; } } } @@ -418,14 +430,35 @@ char* find_pad_by_prefix(const char* prefix) { printf("Multiple matches found for '%s':\n", prefix); for (int i = 0; i < match_count; i++) { printf("%d. %.16s...\n", i + 1, matches[i]); - if (i > 0) free(matches[i]); } - printf("Please be more specific.\n"); - char* result = matches[0]; - for (int i = 1; i < match_count; i++) { + printf("Please be more specific or enter a number (1-%d): ", match_count); + fflush(stdout); + + char choice_input[64]; + if (fgets(choice_input, sizeof(choice_input), stdin)) { + choice_input[strcspn(choice_input, "\n")] = 0; + + // Check if it's a number + char* endptr; + int choice = strtol(choice_input, &endptr, 10); + if (*endptr == '\0' && choice >= 1 && choice <= match_count) { + // Valid choice, return selected pad + char* result = matches[choice - 1]; + // Free the others + for (int i = 0; i < match_count; i++) { + if (i != choice - 1) { + free(matches[i]); + } + } + return result; + } + } + + // Invalid choice or no input, free all and return NULL + for (int i = 0; i < match_count; i++) { free(matches[i]); } - return result; + return NULL; } }