From 692f65b7f030e8dc18473189b1acbf5f6135e330 Mon Sep 17 00:00:00 2001 From: Laan Tungir Date: Thu, 14 Aug 2025 10:28:50 -0400 Subject: [PATCH] Version v0.2.68 - Implemented stdin pipe support for OTP program --- otp.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/otp.c b/otp.c index 3e7748d..215de95 100644 --- a/otp.c +++ b/otp.c @@ -50,6 +50,11 @@ static char current_pads_dir[512] = DEFAULT_PADS_DIR; int main(int argc, char* argv[]); int interactive_mode(void); int command_line_mode(int argc, char* argv[]); +int pipe_mode(int argc, char* argv[], const char* piped_text); + +// Stdin detection functions +int has_stdin_data(void); +char* read_stdin_text(void); // OTP thumb drive detection function int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size); @@ -124,7 +129,18 @@ int main(int argc, char* argv[]) { current_pads_dir[sizeof(current_pads_dir) - 1] = '\0'; } - if (argc == 1) { + // Check for piped input + if (argc == 1 && has_stdin_data()) { + // No arguments but has piped data - enter pipe mode for interactive pad selection + char* piped_text = read_stdin_text(); + if (piped_text) { + int result = pipe_mode(argc, argv, piped_text); + free(piped_text); + return result; + } + // If reading stdin failed, fall back to interactive mode + return interactive_mode(); + } else if (argc == 1) { return interactive_mode(); } else { return command_line_mode(argc, argv); @@ -2323,6 +2339,83 @@ void get_pad_path(const char* chksum, char* pad_path, char* state_path) { snprintf(state_path, 1024, "%s/%s.state", current_pads_dir, chksum); } +// Stdin detection functions implementation +int has_stdin_data(void) { + // Check if stdin is a pipe/redirect (not a terminal) + if (!isatty(STDIN_FILENO)) { + return 1; + } + return 0; +} + +char* read_stdin_text(void) { + size_t capacity = 4096; + size_t length = 0; + char* buffer = malloc(capacity); + + if (!buffer) { + return NULL; + } + + char chunk[1024]; + while (fgets(chunk, sizeof(chunk), stdin)) { + size_t chunk_len = strlen(chunk); + + // Ensure we have enough capacity + while (length + chunk_len >= capacity) { + capacity *= 2; + char* new_buffer = realloc(buffer, capacity); + if (!new_buffer) { + free(buffer); + return NULL; + } + buffer = new_buffer; + } + + strcpy(buffer + length, chunk); + length += chunk_len; + } + + // Remove trailing newline if present + if (length > 0 && buffer[length - 1] == '\n') { + buffer[length - 1] = '\0'; + length--; + } + + // If empty, free and return NULL + if (length == 0) { + free(buffer); + return NULL; + } + + return buffer; +} + +int pipe_mode(int argc, char* argv[], const char* piped_text) { + (void)argc; // Suppress unused parameter warning + (void)argv; // Suppress unused parameter warning + + printf("Piped text received: \"%s\"\n\n", piped_text); + + // List available pads for selection + int pad_count = list_available_pads(); + if (pad_count == 0) { + printf("No pads available. Generate a pad first.\n"); + return 1; + } + + printf("\nEnter pad selection (number, checksum, or prefix): "); + char pad_input[MAX_HASH_LENGTH]; + if (!fgets(pad_input, sizeof(pad_input), stdin)) { + printf("Error: Failed to read pad selection\n"); + return 1; + } + pad_input[strcspn(pad_input, "\n")] = 0; + + // Encrypt the piped text + return encrypt_text(pad_input, piped_text); +} + // OTP thumb drive detection function implementation int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) { const char* mount_dirs[] = {"/media", "/run/media", "/mnt", NULL};