aboutsummaryrefslogtreecommitdiffstats
path: root/7_1/main.c
diff options
context:
space:
mode:
Diffstat (limited to '7_1/main.c')
-rw-r--r--7_1/main.c73
1 files changed, 57 insertions, 16 deletions
diff --git a/7_1/main.c b/7_1/main.c
index 0c9ae14..9a38d63 100644
--- a/7_1/main.c
+++ b/7_1/main.c
@@ -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;
}