summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/my_c_tail
diff options
context:
space:
mode:
Diffstat (limited to 'rushs/tinyprintf/my_c_tail')
-rw-r--r--rushs/tinyprintf/my_c_tail/main.c10
-rw-r--r--rushs/tinyprintf/my_c_tail/my_c_tail.c46
-rw-r--r--rushs/tinyprintf/my_c_tail/my_c_tail.h6
3 files changed, 62 insertions, 0 deletions
diff --git a/rushs/tinyprintf/my_c_tail/main.c b/rushs/tinyprintf/my_c_tail/main.c
new file mode 100644
index 0000000..ba33337
--- /dev/null
+++ b/rushs/tinyprintf/my_c_tail/main.c
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+
+#include "my_c_tail.h"
+
+int main(int argc, char *argv[])
+{
+ if (argc > 1)
+ stdintail(atoi(argv[1]));
+ return 0;
+}
diff --git a/rushs/tinyprintf/my_c_tail/my_c_tail.c b/rushs/tinyprintf/my_c_tail/my_c_tail.c
new file mode 100644
index 0000000..790240c
--- /dev/null
+++ b/rushs/tinyprintf/my_c_tail/my_c_tail.c
@@ -0,0 +1,46 @@
+#include "my_c_tail.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+
+void stdintail(unsigned int n)
+{
+ char **lines = calloc(2000, sizeof(char *));
+ lines[0] = malloc(350 * sizeof(char));
+ size_t m = 0;
+ char c;
+ size_t i = 0;
+ while (read(STDIN_FILENO, &c, 1))
+ {
+ if (c == '\n')
+ {
+ lines[m][i] = '\0';
+ lines[++m] = malloc(350 * sizeof(char));
+ i = 0;
+ }
+ else
+ {
+ lines[m][i++] = c;
+ }
+ }
+
+ size_t j;
+ if (m > n)
+ {
+ for (size_t i = 0; i < m - n; i++)
+ free(lines[i]);
+ j = m - n;
+ }
+ else
+ j = 0;
+
+ for (; j < m; j++)
+ {
+ for (size_t i = 0; lines[j][i]; i++)
+ write(STDOUT_FILENO, &(lines[j][i]), 1);
+ write(STDOUT_FILENO, "\n", 1);
+ free(lines[j]);
+ }
+ free(lines[m]);
+ free(lines);
+}
diff --git a/rushs/tinyprintf/my_c_tail/my_c_tail.h b/rushs/tinyprintf/my_c_tail/my_c_tail.h
new file mode 100644
index 0000000..172c844
--- /dev/null
+++ b/rushs/tinyprintf/my_c_tail/my_c_tail.h
@@ -0,0 +1,6 @@
+#ifndef MY_C_TAIL_H
+#define MY_C_TAIL_H
+
+void stdintail(unsigned int n);
+
+#endif // MY_C_TAIL_H