diff options
Diffstat (limited to '7_1')
-rw-r--r-- | 7_1/Makefile | 30 | ||||
-rw-r--r-- | 7_1/main.c | 73 | ||||
-rw-r--r-- | 7_1/test_file | 1 |
3 files changed, 70 insertions, 34 deletions
diff --git a/7_1/Makefile b/7_1/Makefile index f7aaf17..072ca2e 100644 --- a/7_1/Makefile +++ b/7_1/Makefile @@ -6,24 +6,13 @@ all: test .PHONY: test clean test: task7_1 - - ./$< - @./$< 69 - @./$< 429 - @./$< 828 - @./$< 425729 - @./$< 235723572579 - @./$< 000111222333 - @./$< 1048575 - @./$< 1048576 - @./$< -31273891273891273 - @./$< -000000000000000000 - @./$< -2313423 - @./$< -123 - - ./$< -9223372036854775809 - @./$< -9223372036854775807 - - ./$< -9223372036854775808 - @./$< -1048576 - @./$< -1048575 + -./$< + @./$< 69 429 828 425729 235723572579 000111222333 1048575 1048576 -31273891273891273 \ + -000000000000000000 -2313423 -123 -9223372036854775809 -9223372036854775807 -9223372036854775808 -1048576 -1048575 + ./$< test_file + @cat test_file + @$(MAKE) clean + task7_1: main.c @@ -31,3 +20,8 @@ task7_1: main.c clean: $(RM) task7_1 + $(eval TMP := $(shell mktemp)) + @touch $(TMP) + @head -n 1 test_file > $(TMP) + @cat $(TMP) > test_file + @rm -f $(TMP) @@ -1,11 +1,13 @@ -#include<stdlib.h> -#include<stdio.h> +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#define LONGMIN 0x8000000000000000 #define RED "\x1b[91m" #define GRN "\x1b[92m" #define RST "\x1b[0m" -/* breaks (segfaults) if x = 0x8000000000000000 */ char* tobin(long int x) { size_t i = 0, j = 0; char buffer[64]; @@ -22,27 +24,66 @@ char* tobin(long int x) { return ret; } -int main(int argc, char* argv[]) { +void handle_main_arg(int argc, char *argv[]) { long number; char *bin; + int i; - if (argc != 2) { - fprintf(stderr, RED "Error: Invalid amount of arguments\n" RST "Usage: " GRN "%s <number>\n" RST , argv[0]); - return EXIT_FAILURE; + /* Iterate over every argument and print conversions to stdout */ + for (i = 1; i < argc; ++i) { + number = atol(argv[i]); + + /* Since I am negating a number using the branchless trick, + * Multiplying this number by -1 still gives you a negative number because of the overflow + * Unfortunately this has to be handled separately */ + if (number == LONGMIN) { + fprintf(stderr, RED "Error: " RST "The number %li does not fit in 64 bits\n", number); + continue; + } + + bin = tobin(number); + printf("Binary for %-20li: %s\n", number, bin); + free(bin); } +} - number = atol(argv[1]); +int handle_main_noarg(char *filename) { + FILE *f; + long num, pos, max; + char *buf; - /* Since I am negating a number using the branchless trick, - * Multiplying this number by -1 still gives you a negative number because of the overflow - * Unfortunately this has to be handled separately */ - if (number == 0x8000000000000000) { - puts(RED "Error: " RST "The number does not fit in 64 bits"); + f = fopen(filename, "r+"); + if (!f) { + fprintf(stderr, RED "Error: could not open file %s" RST " (errno %i: %s)\n", filename, errno, strerror(errno)); return EXIT_FAILURE; } + fseek(f, 0, SEEK_END); + max = ftell(f); + fseek(f, 0, SEEK_SET); + while (fscanf(f, "%li", &num) == 1) { + if (num == LONGMIN) { + fprintf(stderr, RED "Error: " RST "The number %li does not fit in 64 bits\n", num); + continue; + } + pos = ftell(f); /* Remember position in file */ + if (pos >= max) break; + fseek(f, 0, SEEK_END); /* Go to end of file */ + buf = tobin(num); /* Perform conversion */ + fprintf(f, "%s\n", buf); /* Write the conversion there */ + fseek(f, pos, SEEK_SET); /* Restore file position*/ + free(buf); + } + fclose(f); + return EXIT_SUCCESS; +} - bin = tobin(number); - printf("Binary for %-20li: %s\n", number, bin); - free(bin); +int main(int argc, char* argv[]) { + char * filename; + if (argc < 2) { + fprintf(stderr, RED "Error: Invalid arguments.\n" RST "Usage: " GRN "%s filename" RST " or " GRN "%s num1 num2 num3...\n" RST, *argv, *argv); + return EXIT_FAILURE; + } + if (argc == 2) return handle_main_noarg(argv[1]); + handle_main_arg(argc, argv); return EXIT_SUCCESS; } diff --git a/7_1/test_file b/7_1/test_file new file mode 100644 index 0000000..eefa9a3 --- /dev/null +++ b/7_1/test_file @@ -0,0 +1 @@ +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -1 -2 -3 -4 -5 -6 -7 -8 |