Compare commits

...

1 Commits

4 changed files with 60 additions and 22 deletions

View File

@@ -0,0 +1,4 @@
When building, use build.sh, not make.
Use it as follows: build.sh -m "useful comment on changes being made"

BIN
debug Executable file

Binary file not shown.

1
debug.c Normal file
View File

@@ -0,0 +1 @@
int main() { printf("Testing direct filename: %d\n", strncmp("97d9d82b5414a9439102f3811fb90ab1d6368a00d33229a18b306476f9d04f82.pad", "97", 2)); return 0; }

77
otp.c
View File

@@ -365,41 +365,53 @@ uint64_t parse_size_string(const char* size_str) {
char* find_pad_by_prefix(const char* prefix) { char* find_pad_by_prefix(const char* prefix) {
DIR* dir = opendir(PADS_DIR); DIR* dir = opendir(PADS_DIR);
if (!dir) return NULL; if (!dir) {
printf("Error: Cannot open pads directory\n");
return NULL;
}
struct dirent* entry; struct dirent* entry;
char* matches[100]; // Store up to 100 matches char* matches[100]; // Store up to 100 matches
int match_count = 0; int match_count = 0;
// Check if it's a number (for interactive menu selection) // 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; char* endptr;
int selection = strtol(prefix, &endptr, 10); 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 // It's a number, find the nth pad
int current = 0; int current = 0;
rewinddir(dir); rewinddir(dir);
while ((entry = readdir(dir)) != NULL && match_count == 0) { while ((entry = readdir(dir)) != NULL && match_count == 0) {
if (strstr(entry->d_name, ".pad") && strlen(entry->d_name) == 68) { // 64 char chksum + ".pad" // Skip . and .. entries, and only process .pad files
current++; if (entry->d_name[0] == '.') continue;
if (current == selection) { if (!strstr(entry->d_name, ".pad")) continue;
matches[match_count] = malloc(65); if (strlen(entry->d_name) != 68) continue; // 64 char chksum + ".pad"
strncpy(matches[match_count], entry->d_name, 64);
matches[match_count][64] = '\0'; current++;
match_count = 1; 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 { } else {
// Find pads that start with the prefix // Find pads that start with the prefix
size_t prefix_len = strlen(prefix); size_t prefix_len = strlen(prefix);
while ((entry = readdir(dir)) != NULL && match_count < 100) { while ((entry = readdir(dir)) != NULL && match_count < 100) {
if (strstr(entry->d_name, ".pad") && strlen(entry->d_name) == 68) { // Skip . and .. entries, and only process .pad files
if (strncmp(entry->d_name, prefix, prefix_len) == 0) { if (entry->d_name[0] == '.') continue;
matches[match_count] = malloc(65); if (!strstr(entry->d_name, ".pad")) continue;
strncpy(matches[match_count], entry->d_name, 64); if (strlen(entry->d_name) != 68) continue; // 64 char chksum + ".pad"
matches[match_count][64] = '\0';
match_count++; // 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); printf("Multiple matches found for '%s':\n", prefix);
for (int i = 0; i < match_count; i++) { for (int i = 0; i < match_count; i++) {
printf("%d. %.16s...\n", i + 1, matches[i]); printf("%d. %.16s...\n", i + 1, matches[i]);
if (i > 0) free(matches[i]);
} }
printf("Please be more specific.\n"); printf("Please be more specific or enter a number (1-%d): ", match_count);
char* result = matches[0]; fflush(stdout);
for (int i = 1; i < match_count; i++) {
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]); free(matches[i]);
} }
return result; return NULL;
} }
} }