commit b47037f66e3bec4279a1890c978346cf66105a20 Author: hasslesstech Date: Wed Dec 11 20:29:59 2024 +0200 add basic code diff --git a/inc/swapper.h b/inc/swapper.h new file mode 100644 index 0000000..b03ad45 --- /dev/null +++ b/inc/swapper.h @@ -0,0 +1,9 @@ +#ifndef __SWAPPER_HEADER +#define __SWAPPER_HEADER + +#define __SWAPPER_MAX_STR_LENGTH 65535 + +int swapper_length_difference(char *, char *); +void swapper_swap(char *, char *); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..6602f06 --- /dev/null +++ b/src/main.c @@ -0,0 +1,17 @@ +#include +#include "swapper.h" + +int main(int argc, char **argv) +{ + if (3 != argc) + return 1; + + if (swapper_length_difference(argv[1], argv[2])) + return 2; + + swapper_swap(argv[1], argv[2]); + + printf("%s\n%s\n", argv[1], argv[2]); + + return 0; +} diff --git a/src/swapper.c b/src/swapper.c new file mode 100644 index 0000000..3f4192c --- /dev/null +++ b/src/swapper.c @@ -0,0 +1,48 @@ +#include +#include "swapper.h" + +#define ABS(val) ( (val) >= 0 ? (val) : -(val) ) +#define ALIGN8(val) ( (((val) >> 3) << 3) ) + +union tmp { + long int l; + char c; +}; + +static int strlen_c(char *a) +{ + int len; + + for (len = 0; len < __SWAPPER_MAX_STR_LENGTH; len++) { + if (a[len] == '\0') + break; + } + + return len; +} + +int swapper_length_difference(char *a, char *b) +{ + int len_a = strlen_c(a); + int len_b = strlen_c(b); + + return ABS(len_a - len_b); +} + +void swapper_swap(char *a, char *b) { + int len = strlen_c(a); + + union tmp t; + + for (int i = 0; i < (len >> 3); i++) { + t.l = ((long int*) a)[i]; + ((long int*) a)[i] = ((long int*) b)[i]; + ((long int*) b)[i] = t.l; + } + + for (int i = ALIGN8(len); i < len; i++) { + t.c = a[i]; + a[i] = b[i]; + b[i] = t.c; + } +}