summaryrefslogtreecommitdiff
path: root/xyo-networks
diff options
context:
space:
mode:
Diffstat (limited to 'xyo-networks')
-rw-r--r--xyo-networks/.gitignore2
-rw-r--r--xyo-networks/Makefile6
-rw-r--r--xyo-networks/build.sh5
-rw-r--r--xyo-networks/default.nix7
-rw-r--r--xyo-networks/printf.c91
-rw-r--r--xyo-networks/printf.h3
6 files changed, 114 insertions, 0 deletions
diff --git a/xyo-networks/.gitignore b/xyo-networks/.gitignore
new file mode 100644
index 0000000..29af1e7
--- /dev/null
+++ b/xyo-networks/.gitignore
@@ -0,0 +1,2 @@
+printf
+*.o
diff --git a/xyo-networks/Makefile b/xyo-networks/Makefile
new file mode 100644
index 0000000..a7502e4
--- /dev/null
+++ b/xyo-networks/Makefile
@@ -0,0 +1,6 @@
+
+printf: printf.c
+ gcc -Wall -Wextra -Werror -O2 -std=c99 -pedantic -o printf printf.c
+
+clean:
+ rm -f printf printf.o
diff --git a/xyo-networks/build.sh b/xyo-networks/build.sh
new file mode 100644
index 0000000..af769a9
--- /dev/null
+++ b/xyo-networks/build.sh
@@ -0,0 +1,5 @@
+source $stdenv/setup
+
+PATH="$coreutils/bin:$gcc/bin:$PATH"
+mkdir $out
+make
diff --git a/xyo-networks/default.nix b/xyo-networks/default.nix
new file mode 100644
index 0000000..ee63f97
--- /dev/null
+++ b/xyo-networks/default.nix
@@ -0,0 +1,7 @@
+with (import <nixpkgs> {});
+
+stdenv.mkDerivation {
+ name = "my_printf";
+ builder = "${bash}/bin/bash";
+ src = ./.;
+}
diff --git a/xyo-networks/printf.c b/xyo-networks/printf.c
new file mode 100644
index 0000000..4fa165c
--- /dev/null
+++ b/xyo-networks/printf.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include "printf.h"
+
+/*
+ * my_printf("str: %s\nchr: %c\nhex: %x\nint: %d\noct: %o",
+ 1, 1, 1, 1, 1);
+*/
+
+int
+main()
+{
+ my_printf("str: %s\nchr: %c\nhex: %x\nint: %d\noct: %o",
+ "hi", 'a', 0x32, 10, 8);
+ return 0;
+}
+
+void
+my_printf(char *format, ...)
+{
+ char *c; // a char in the `format` string
+ va_list args;
+
+ va_start(args, format);
+
+ for (c = format; *c != '\0'; c++)
+ {
+ while (*c != '%')
+ {
+ putchar(*c);
+ c++;
+ }
+
+ c++;
+
+ switch(*c)
+ {
+ case 'c':
+ {
+ putchar(va_arg(args, int));
+ putchar('\n');
+ break;
+ }
+
+ case 'd':
+ {
+ puts(convert(va_arg(args, int), 10));
+ break;
+ }
+
+ case 'o':
+ {
+ puts(convert(va_arg(args, unsigned int), 8));
+ break;
+ }
+
+ case 'x':
+ {
+ puts(convert(va_arg(args, unsigned int), 16));
+ break;
+ }
+
+ case 's':
+ {
+ puts(va_arg(args, char*));
+ break;
+ }
+ }
+ }
+
+ va_end(args);
+}
+
+
+char*
+convert(unsigned int num, int base)
+{
+ static char rep[]= "0123456789ABCDEF";
+ static char buffer[50];
+ char *ptr;
+
+ ptr = &buffer[49];
+ *ptr = '\0';
+
+ do {
+ *--ptr = rep[num%base];
+ num /= base;
+ } while (num != 0);
+
+ return(ptr);
+}
diff --git a/xyo-networks/printf.h b/xyo-networks/printf.h
new file mode 100644
index 0000000..27ced90
--- /dev/null
+++ b/xyo-networks/printf.h
@@ -0,0 +1,3 @@
+
+void my_printf(char*, ...);
+char* convert(unsigned int, int);