Skip to content

Commit

Permalink
muri now encoding and decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
msoulier committed Nov 3, 2024
1 parent 4ef376c commit 4244014
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions muri.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,49 @@

int
main(int argc, char *argv[]) {
int encode = 1;
int read_stdin = 1;
int maximum_read = 10*1024*1024;

char *usage = "Usage: muri [-h] [-d]\n reads from stdin, encode is default, -d to decode\n";

if (argc > 1) {
for (int i = 1; i < argc; ++i) {
if (strncmp(argv[i], "-h", 2) == 0) {
fprintf(stderr, usage);
return 1;
} else if (strncmp(argv[i], "-d", 2) == 0) {
encode = 0;
}
}
}

char *input_buffer = malloc(sizeof(char)*maximum_read);
assert( input_buffer != NULL );
if (argc < 2) {
if (fgets(input_buffer, maximum_read, stdin) == NULL) {
return 1;
}
// Throw away the newline.
if (input_buffer[strnlen(input_buffer, maximum_read)-1] == '\n') {
input_buffer[strnlen(input_buffer, maximum_read)-1] = '\0';
} // FIXME if not then we didn't read all of stdin

// Read from stdin.
if (fgets(input_buffer, maximum_read, stdin) == NULL) {
return 1;
}
// Throw away the newline.
if (input_buffer[strnlen(input_buffer, maximum_read)-1] == '\n') {
input_buffer[strnlen(input_buffer, maximum_read)-1] = '\0';
} else {
if (strncmp(argv[1], "-h", 2) == 0) {
fprintf(stderr, "Usage: %s [string]\nIf [string] not provided, reads from stdin.\n", argv[0]);
return 1;
} else {
// string is on the command-line
strncpy(input_buffer, argv[1], maximum_read);
}
fprintf(stderr, "ERROR: Input too large.\n");
// FIXME: realloc or allow configurable buffer size
return 1;
}

int return_value = 0;
int output_length = 0;
char *output = uriencode(input_buffer, &output_length);
char *output = NULL;

if (encode) {
output = uriencode(input_buffer, &output_length);
} else {
output = uridecode(input_buffer, &output_length);
}

if (output != NULL) {
printf("%s\n", output);
} else {
Expand Down

0 comments on commit 4244014

Please sign in to comment.