summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/rotx
diff options
context:
space:
mode:
Diffstat (limited to 'rushs/tinyprintf/rotx')
-rw-r--r--rushs/tinyprintf/rotx/rotx.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/rushs/tinyprintf/rotx/rotx.c b/rushs/tinyprintf/rotx/rotx.c
new file mode 100644
index 0000000..a2cb820
--- /dev/null
+++ b/rushs/tinyprintf/rotx/rotx.c
@@ -0,0 +1,59 @@
+#include <stdlib.h>
+#include <unistd.h>
+
+#define BUFFER_SIZE 10
+
+int main(int argc, char **argv)
+{
+ if (argc != 2)
+ {
+ return 0;
+ }
+
+ int rot = atoi(argv[1]);
+ int rod = rot;
+ if (rot < 0)
+ {
+ rot = (rot % 26) + 26;
+ rod = (rod % 10) + 10;
+ }
+
+ char buf[BUFFER_SIZE];
+ ssize_t r;
+
+ while ((r = read(STDIN_FILENO, buf, BUFFER_SIZE)))
+ {
+ if (r == -1)
+ {
+ return 1;
+ }
+
+ for (ssize_t i = 0; i < r; i++)
+ {
+ if (buf[i] >= 'a' && buf[i] <= 'z')
+ {
+ buf[i] = ((buf[i] - 'a') + rot) % 26 + 'a';
+ }
+ else if (buf[i] >= 'A' && buf[i] <= 'Z')
+ {
+ buf[i] = ((buf[i] - 'A') + rot) % 26 + 'A';
+ }
+ else if (buf[i] >= '0' && buf[i] <= '9')
+ {
+ buf[i] = ((buf[i] - '0') + rod) % 10 + '0';
+ }
+ }
+
+ ssize_t w = write(STDOUT_FILENO, buf, r);
+ while (w != r)
+ {
+ w += write(STDOUT_FILENO, buf, r);
+ if (w == -1)
+ {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}