BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionLibC / commit / 03048a95d88cc7a78171393371f5c22a0250a014 / stdio

// Related

OrionLibC

Barry Importing existing Orion LibC 03048a9 (2 years, 2 months ago)
diff --git a/stdio/dbgprintf.c b/stdio/dbgprintf.c
new file mode 100644
index 0000000..c1f5729
--- /dev/null
+++ b/stdio/dbgprintf.c
@@ -0,0 +1,30 @@
+#include <stdarg.h>
+#include <sys/syscall.h>
+
+int vsprintf(char *buf, const char *fmt, va_list args);
+
+/* Send message to Kernel Output */
+void
+dbgprintf(char *fmt, ...)
+{
+	char buf[1024];
+	va_list args;
+	va_start(args, fmt);
+	vsprintf(buf, fmt, args);
+	va_end(args);
+
+	int ret;
+	char *p = buf;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_DBGPRINTF),
+	             "c" (1), "S" (&p));
+}
+
+/* Send an unformatted message to Kernel Output */
+void
+dbgputs(char *str)
+{
+	int ret;
+	asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_DBGPRINTF),
+	             "c" (1), "S" (&str));
+
+}
diff --git a/stdio/fopen.c b/stdio/fopen.c
new file mode 100644
index 0000000..69aecca
--- /dev/null
+++ b/stdio/fopen.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+/* Open a file */
+FILE *
+fopen(const char *name, const char *mode)
+{
+	int fd;
+	FILE *fp;
+
+	if (*mode != 'r' && *mode != 'w' && *mode != 'a')
+		return NULL;
+
+	fp = malloc(sizeof(FILE));
+	if (!fp)
+		return NULL;
+
+	if (*mode == 'w')
+		fd = create(name, 0666);
+	else if (*mode == 'a')
+		fd = open(name, O_CREATE | O_WRONLY, 0);
+	else
+		fd = open(name, O_RDONLY, 0);
+	if (fd < 0) {
+		free(fp);
+		return NULL;
+	}
+	if (*mode == 'a')
+		lseek(fd, 0, SEEK_END);
+
+	fp->fd = fd;
+	fp->count = 0;
+	fp->buf = NULL;
+	fp->flags = (*mode == 'r') ? FILE_READ : FILE_WRITE;
+
+	return fp;
+}
diff --git a/stdio/getc.c b/stdio/getc.c
new file mode 100644
index 0000000..e1a8a8f
--- /dev/null
+++ b/stdio/getc.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/* Allocate and fill a file's buffer */
+static int
+_fillbuf(FILE *fp)
+{
+	int bufsize;
+
+	if ((fp->flags & (FILE_READ | FILE_EOF | FILE_ERR)) != FILE_READ)
+		return EOF;
+	bufsize = (fp->flags & FILE_UNBUF) ? 1 : 1024;
+
+	/* Create buffer */
+	if (fp->buf == NULL) {
+		fp->buf = malloc(bufsize);
+		if (!fp->buf)
+			return EOF;
+	}
+	fp->ptr = fp->buf;
+	fp->count = read(fp->fd, fp->ptr, bufsize);
+	if (--fp->count < 0) {
+		if (fp->count == -1)
+			fp->flags |= FILE_EOF;
+		else
+			fp->flags |= FILE_ERR;
+		fp->count = 0;
+		return EOF;
+	}
+	return (unsigned char) *fp->ptr++;
+}
+
+/* Get a character from a stream */
+inline int
+getc(FILE *stream)
+{
+	if (--stream->count >= 0)
+		return (unsigned char) *stream->ptr++;
+	return _fillbuf(stream);
+}
+
+/* Get a character from standard input */
+int
+getchar(void)
+{
+	getc(stdin);
+}
diff --git a/stdio/perror.c b/stdio/perror.c
new file mode 100644
index 0000000..c852582
--- /dev/null
+++ b/stdio/perror.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <errno.h>
+
+int errno;
+const char *errorList[] = {
+	[ENONE]   = "No error",
+	[EPERM]   = "Operation not permitted",
+	[ENOENT]  = "No such file or directory",
+	[ESRCH]   = "No such process",
+	[EINVAL]  = "Invalid argument",
+	[EBADF]   = "Bad file descriptor",
+	[ENOEXEC] = "Exec format error",
+	[EMFILE]  = "Too many open files",
+	[EFAULT]  = "Bad address",
+	[EISDIR]  = "Is a directory",
+	[ENOTDIR] = "Not a directory",
+	[EACCES]  = "Permission denied",
+	[ENODEV]  = "No such device",
+	[EEXIST]  = "File exists",
+	[ENXIO]   = "No such device or address",
+	[ENOTBLK] = "Block device required",
+	[ENOMEM]  = "Cannot allocate memory",
+	[ECHILD]  = "No child processes",
+	[ENOTTY]  = "Inappropriate ioctl for device",
+};
+
+/* Display an error message for errno */
+void
+perror(const char *s)
+{
+	if (errno < 0 || errno >= (sizeof(errorList)/sizeof(errorList[0])))
+		return;
+	if (!s)
+		printf("%s\n", errorList[errno]);
+	else if (!s[0])
+		printf("%s\n", errorList[errno]);
+	else
+		printf("%s: %s\n", s, errorList[errno]);
+}
diff --git a/stdio/printf.c b/stdio/printf.c
new file mode 100644
index 0000000..de0d9a4
--- /dev/null
+++ b/stdio/printf.c
@@ -0,0 +1,21 @@
+#include <unistd.h>
+#include <stdarg.h>
+#include <sys/syscall.h>
+#include <stdio.h>
+
+int vsprintf(char *buf, const char *fmt, va_list args);
+
+/* Write a formatted string to stdout */
+int
+printf(const char *fmt, ...)
+{
+	int len;
+	char buf[8196];
+	va_list args;
+	va_start(args, fmt);
+	len = vsprintf(buf, fmt, args);
+	va_end(args);
+
+	len = write(STDOUT_FILENO, buf, len);
+	return len;
+}
diff --git a/stdio/puts.c b/stdio/puts.c
new file mode 100644
index 0000000..45113ef
--- /dev/null
+++ b/stdio/puts.c
@@ -0,0 +1,21 @@
+#include <unistd.h>
+#include <string.h>
+
+/* Write a string to stdout */
+int
+puts(const char *str)
+{
+	char end[] = "\n";
+	int len = strlen(str);
+	len = write(STDOUT_FILENO, (void *) str, len);
+	return len + write(STDOUT_FILENO, end, 1);
+}
+
+/* Write a character to stdout */
+int
+putchar(int c)
+{
+	char str[] = {(char) c, 0};
+	write(STDOUT_FILENO, str, 1);
+	return c;
+}
diff --git a/stdio/vsprintf.c b/stdio/vsprintf.c
new file mode 100644
index 0000000..88e2f9f
--- /dev/null
+++ b/stdio/vsprintf.c
@@ -0,0 +1,244 @@
+#include <stdarg.h>
+#include <string.h>
+
+#define is_digit(c)	((c) >= '0' && (c) <= '9')
+
+/* Do not convert */
+static int
+skip_atoi(const char **s)
+{
+	int i = 0;
+
+	while (is_digit(**s))
+		i = i*10 + *((*s)++) - '0';
+
+	return i;
+}
+
+#define ZEROPAD	1		/* pad with zero */
+#define SIGN	2		/* unsigned/signed long */
+#define PLUS	4		/* show plus */
+#define SPACE	8		/* space if plus */
+#define LEFT	16		/* left justified */
+#define SPECIAL	32		/* 0x */
+#define SMALL	64		/* use 'abcdef' instead of 'ABCDEF' */
+
+#define do_div(n,base) ({ \
+int __res; \
+__asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
+__res; })
+
+/* Convert a number to ASCII */
+static char *
+number(char *str, int num, int base, int size, int precision, int type)
+{
+	char c, sign, tmp[36];
+	const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+	int i;
+
+	if (type & SMALL)
+		digits = "0123456789abcdefghijklmnopqrstuvwxyz";
+	if (type & LEFT)
+		type &= ~ZEROPAD;
+	if (base < 2 || base > 36)
+		return 0;
+	c = (type & ZEROPAD) ? '0' : ' ' ;
+	if (type & SIGN && num < 0) {
+		sign = '-';
+		num = -num;
+	} else {
+		sign = (type & PLUS) ? '+' : ((type & SPACE) ? ' ' : 0);
+	}
+	if (sign)
+		size--;
+	if (type & SPECIAL)
+		if (base == 16)
+			size -= 2;
+		else if (base == 8)
+			size--;
+	i = 0;
+	if (num == 0)
+		tmp[i++] = '0';
+	else while (num != 0)
+		tmp[i++] = digits[do_div(num,base)];
+	if (i > precision)
+		precision = i;
+	size -= precision;
+	if (!(type & (ZEROPAD + LEFT)))
+		while (size-- > 0)
+			*str++ = ' ';
+	if (sign)
+		*str++ = sign;
+	if (type & SPECIAL)
+		if (base == 8) {
+			*str++ = '0';
+		} else if (base == 16) {
+			*str++ = '0';
+			*str++ = digits[33];
+		}
+	if (!(type & LEFT))
+		while (size-- > 0)
+			*str++ = c;
+	while (i < precision--)
+		*str++ = '0';
+	while (i-- > 0)
+		*str++ = tmp[i];
+	while (size-- > 0)
+		*str++ = ' ';
+	return str;
+}
+
+/* Print formatted to a buffer */
+int
+vsprintf(char *buf, const char *fmt, va_list args)
+{
+	int len, i;
+	char *str, *s;
+	int *ip, flags;
+	int field_width, precision, qualifier;
+
+	for (str = buf; *fmt; fmt++) {
+		if (*fmt != '%') {
+			*str++ = *fmt;
+			continue;
+		}
+
+		/* Process flags */
+		flags = 0;
+repeat:
+		fmt++;
+		switch (*fmt) {
+		case '-':
+			flags |= LEFT;
+			goto repeat;
+		case '+':
+			flags |= PLUS;
+			goto repeat;
+		case ' ':
+			flags |= SPACE;
+			goto repeat;
+		case '#':
+			flags |= SPECIAL;
+			goto repeat;
+		case '0':
+			flags |= ZEROPAD;
+			goto repeat;
+		}
+
+		/* Get field width */
+		field_width = -1;
+		if (is_digit(*fmt)) {
+			field_width = skip_atoi(&fmt);
+		} else if (*fmt == '*') {
+			field_width = va_arg(args, int);
+			fmt++;
+			if (field_width < 0) {
+				field_width = -field_width;
+				flags |= LEFT;
+			}
+		}
+
+		/* Get the precision */
+		precision = -1;
+		if (*fmt == '.') {
+			fmt++;
+			if (is_digit(*fmt)) {
+				precision = skip_atoi(&fmt);
+			} else if (*fmt == '*') {
+				precision = va_arg(args, int);
+				fmt++;
+			}
+			if (precision < 0)
+				precision = 0;
+		}
+
+		/* Get the conversion qualifier */
+		qualifier = -1;
+		if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
+			qualifier = *fmt;
+			fmt++;
+		}
+
+		switch (*fmt) {
+		case 'c':
+			if (!(flags & LEFT))
+				while (--field_width > 0)
+					*str++ = ' ';
+			*str++ = (unsigned char) va_arg(args, int);
+			while (--field_width > 0)
+				*str++ = ' ';
+			break;
+
+		case 's':
+			s = va_arg(args, char *);
+			len = strlen(s);
+			if (precision < 0)
+				precision = len;
+			else if (len > precision)
+				len = precision;
+
+			if (!(flags & LEFT))
+				while (len < field_width--)
+					*str++ = ' ';
+			for (i = 0; i < len; ++i)
+				*str++ = *s++;
+			while (len < field_width--)
+				*str++ = ' ';
+			break;
+
+		case 'o':
+			str = number(str, va_arg(args, unsigned long), 8,
+				field_width, precision, flags);
+			break;
+
+		case 'p':
+			if (field_width == -1) {
+				field_width = 8;
+				flags |= ZEROPAD;
+			}
+			str = number(str,
+				(unsigned long) va_arg(args, void *), 16,
+				field_width, precision, flags);
+			break;
+
+		case 'x':
+			flags |= SMALL;
+			/* FALLTHROUGH */
+		case 'X':
+			str = number(str, va_arg(args, unsigned long), 16,
+				field_width, precision, flags);
+			break;
+
+		case 'd': /* FALLTHROUGH */
+		case 'i':
+			flags |= SIGN;
+			/* FALLTHROUGH */
+		case 'u':
+			str = number(str, va_arg(args, unsigned long), 10,
+				field_width, precision, flags);
+			break;
+
+		case 'f':
+			str = number(str,
+				(unsigned long) va_arg(args, double), 10,
+				field_width, precision, flags);
+			break;
+
+		case 'n':
+			ip = va_arg(args, int *);
+			*ip = (str - buf);
+			break;
+
+		default:
+			if (*fmt != '%')
+				*str++ = '%';
+			if (*fmt)
+				*str++ = *fmt;
+			else
+				--fmt;
+			break;
+		}
+	}
+	*str = '\0';
+	return str-buf;
+}