Browse Source

blib wip

master
Mathieu Serandour 6 months ago
parent
commit
e2087528b3
  1. 15
      blib/include/sprintf.h
  2. 387
      blib/include/stdio.h
  3. 85
      blib/include/stdio_printf.h
  4. 189
      blib/src/sprintf.c
  5. 84
      blib/src/stdio.c

15
blib/include/sprintf.h

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
extern int sprintf(char *str, const char *format, ...);
extern int snprintf(char *str, size_t size, const char *format, ...);
extern int vsprintf(char *str, const char *format, va_list ap);
extern int vsnprintf(char *str, size_t size, const char *format, va_list ap);
extern int snprintf (char *__restrict __s, size_t __maxlen,
const char *__restrict __format, ...)

387
blib/include/stdio.h

@ -0,0 +1,387 @@
/* Define ISO C stdio on top of C++ iostreams.
Copyright (C) 1991-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/*
* ISO C99 Standard: 7.19 Input/output <stdio.h>
*/
#ifndef _STDIO_H
#define _STDIO_H 1
#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
typedef struct FILE FILE;
typedef unsigned long long fpos_t;
/* The possibilities for the third argument to `setvbuf'. */
#define _IOFBF 0 /* Fully buffered. */
#define _IOLBF 1 /* Line buffered. */
#define _IONBF 2 /* No buffering. */
/* Default buffer size. */
#define BUFSIZ 8192
/* The value returned by fgetc and similar functions to indicate the
end of the file. */
#define EOF (-1)
/* The possibilities for the third argument to `fseek'.
These values should not be changed. */
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
#define FOPEN_MAX 32
#ifndef __restrict
#ifdef __cplusplus
# define __restrict __restrict__
#else
# define __restrict restrict
#endif
#endif
/* Standard streams. */
extern FILE *stdin; /* Standard input stream. */
extern FILE *stdout; /* Standard output stream. */
extern FILE *stderr; /* Standard error output stream. */
/* C89/C99 say they're macros. Make them happy. */
#define stdin stdin
#define stdout stdout
#define stderr stderr
// unimplementable yet
#if 0
/* Remove file FILENAME. */
extern int remove (const char *__filename);
/* Rename file OLD to NEW. */
extern int rename (const char *__old, const char *__new);
/* Create a temporary file and open it read/write.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *tmpfile (void);
/* Generate a temporary filename. */
extern char *tmpnam (char *__s);
/* This is the reentrant variant of `tmpnam'. The only difference is
that it does not allow S to be NULL. */
extern char *tmpnam_r (char *__s);
/* Generate a unique temporary filename using up to five characters of PFX
if it is not NULL. The directory to put this file in is searched for
as follows: First the environment variable "TMPDIR" is checked.
If it contains the name of a writable directory, that directory is used.
If not and if DIR is not NULL, that value is checked. If that fails,
P_tmpdir is tried and finally "/tmp". The storage for the filename
is allocated by `malloc'. */
extern char *tempnam (const char *__dir, const char *__pfx)
__THROW __attribute_malloc__;
#ifdef __USE_MISC
/* Faster versions when locking is not required.
This function is not part of POSIX and therefore no official
cancellation point. But due to similarity with an POSIX interface
or due to the implementation it is a cancellation point and
therefore not marked with __THROW. */
extern int fflush_unlocked (FILE *__stream);
#endif
#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2)
/* Create a new stream that refers to a memory buffer. */
extern FILE *fmemopen (void *__s, size_t __len, const char *__modes)
;
/* Open a stream that writes into a malloc'd buffer that is expanded as
necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
and the number of characters written on fflush or fclose. */
extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc);
#endif
/* Read a character from stdin.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int getchar (void);
/* These are defined in POSIX.1:1996.
These functions are possible cancellation points and therefore not
marked with __THROW. */
extern int getc_unlocked (FILE *__stream);
extern int getchar_unlocked (void);
/* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
(and null-terminate it). *LINEPTR is a pointer returned from malloc (or
NULL), pointing to *N characters of space. It is realloc'd as
necessary. Returns the number of characters read (not including the
null terminator), or -1 on error or EOF.
These functions are not part of POSIX and therefore no official
cancellation point. But due to similarity with an POSIX interface
or due to the implementation they are cancellation points and
therefore not marked with __THROW. */
extern size_t __getdelim (char **__restrict __lineptr,
size_t *__restrict __n, int __delimiter,
FILE *__restrict __stream);
extern size_t getdelim (char **__restrict __lineptr,
size_t *__restrict __n, int __delimiter,
FILE *__restrict __stream);
#ifdef __USE_POSIX199506
/* These are defined in POSIX.1:1996. */
/* Acquire ownership of STREAM. */
extern void flockfile (FILE *__stream);
/* Try to acquire ownership of STREAM but do not block if it is not
possible. */
extern int ftrylockfile (FILE *__stream);
/* Relinquish the ownership granted for STREAM. */
extern void funlockfile (FILE *__stream);
#endif /* POSIX */
/* Create a new stream connected to a pipe running the given command.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *popen (const char *__command, const char *__modes);
/* Close a stream opened by popen and return the status of its child.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int pclose (FILE *__stream);
/* Return the name of the controlling terminal. */
extern char *ctermid (char *__s);
#endif
/* Close STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fclose (FILE *__stream);
/* Flush STREAM, or all streams if STREAM is NULL.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fflush (FILE *__stream);
/* Open a file and create a new stream for it.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *fopen (const char *__restrict __filename,
const char *__restrict __modes);
/* Open a file, replacing an existing stream with it.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *freopen (const char *__restrict __filename,
const char *__restrict __modes,
FILE *__restrict __stream);
/* Create a new stream that refers to an existing system file descriptor. */
extern FILE *fdopen (int __fd, const char *__modes);
/* If BUF is NULL, make STREAM unbuffered.
Else make it use buffer BUF, of size BUFSIZ. */
extern void setbuf (FILE *__restrict __stream, char *__restrict __buf);
/* Make STREAM use buffering mode MODE.
If BUF is not NULL, use N bytes of it for buffering;
else allocate an internal buffer N bytes long. */
extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
int __modes, size_t __n);
#ifdef __USE_MISC
/* If BUF is NULL, make STREAM unbuffered.
Else make it use SIZE bytes of BUF for buffering. */
extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf,
size_t __size);
/* Make STREAM line-buffered. */
extern void setlinebuf (FILE *__stream);
#endif
#include <stdio_printf.h>
/* Read a character from STREAM.
These functions are possible cancellation points and therefore not
marked with __THROW. */
extern int fgetc (FILE *__stream);
extern int getc (FILE *__stream);
/* Write a character to STREAM.
These functions are possible cancellation points and therefore not
marked with __THROW.
These functions is a possible cancellation point and therefore not
marked with __THROW. */
extern int fputc (int __c, FILE *__stream);
extern int putc (int __c, FILE *__stream);
/* Write a character to stdout.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int putchar (int __c);
/* Get a newline-terminated string of finite length from STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream);
/* Write a string to STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fputs (const char *__restrict __s, FILE *__restrict __stream);
/* Write a string, followed by a newline, to stdout.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int puts (const char *__s);
/* Push a character back onto the input buffer of STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int ungetc (int __c, FILE *__stream);
/* Read chunks of generic data from STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern size_t fread (void *__restrict __ptr, size_t __size,
size_t __n, FILE *__restrict __stream);
/* Write chunks of generic data to STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern size_t fwrite (const void *__restrict __ptr, size_t __size,
size_t __n, FILE *__restrict __s);
/* Seek to a certain position on STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fseek (FILE *__stream, long int __off, int __whence);
/* Return the current position of STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern long int ftell (FILE *__stream);
/* Rewind to the beginning of STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void rewind (FILE *__stream);
/* The Single Unix Specification, Version 2, specifies an alternative,
more adequate interface for the two functions above which deal with
file offset. `long int' is not the right type. These definitions
are originally defined in the Large File Support API. */
#define fseeko fseek
#define ftello ftell
/* Get STREAM's position.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
/* Set STREAM's position.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fsetpos (FILE *__stream, const fpos_t *__pos);
/* Clear the error and EOF indicators for STREAM. */
extern void clearerr (FILE *__stream);
/* Return the EOF indicator for STREAM. */
extern int feof (FILE *__stream);
/* Return the error indicator for STREAM. */
extern int ferror (FILE *__stream);
/* Print a message describing the meaning of the value of errno.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void perror (const char *__s);
/* Return the system file descriptor for STREAM. */
extern int fileno (FILE *__stream);
#endif /* <stdio.h> included. */

85
blib/include/stdio_printf.h

@ -0,0 +1,85 @@
#pragma once
#ifndef _STDIO_H
#error "Never use <stdio_printf.h> directly; include <stdio.h> instead."
#endif
/* Write formatted output to STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fprintf (FILE *__restrict __stream,
const char *__restrict __format, ...);
/* Write formatted output to stdout.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int printf (const char *__restrict __format, ...);
/* Write formatted output to S. */
extern int sprintf (char *__restrict __s,
const char *__restrict __format, ...)NL;
#include "sprintf.h"
extern int vfprintf (FILE *__restrict __s, const char *__restrict __format,
__gnuc_va_list __arg);
/* Write formatted output to stdout from argument list ARG.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg);
/* Write formatted output to S from argument list ARG. */
/* Maximum chars of output to write in MAXLEN. */
extern int snprintf (char *__restrict __s, size_t __maxlen,
const char *__restrict __format, ...)
__THROWNL __attribute__ ((__format__ (__printf__, 3, 4)));
extern int vsnprintf (char *__restrict __s, size_t __maxlen,
const char *__restrict __format, __gnuc_va_list __arg)
__THROWNL __attribute__ ((__format__ (__printf__, 3, 0)));
/* Write formatted output to a file descriptor. */
extern int vdprintf (int __fd, const char *__restrict __fmt,
__gnuc_va_list __arg)
__attribute__ ((__format__ (__printf__, 2, 0)));
extern int dprintf (int __fd, const char *__restrict __fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
/* Read formatted input from STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fscanf (FILE *__restrict __stream,
const char *__restrict __format, ...) __wur;
/* Read formatted input from stdin.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int scanf (const char *__restrict __format, ...) __wur;
/* Read formatted input from S. */
extern int sscanf (const char *__restrict __s,
const char *__restrict __format, ...);
/* Read formatted input from S into argument list ARG.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int vfscanf (FILE *__restrict __s, const char *__restrict __format,
__gnuc_va_list __arg)
__attribute__ ((__format__ (__scanf__, 2, 0))) __wur;
/* Read formatted input from stdin into argument list ARG.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg)
__attribute__ ((__format__ (__scanf__, 1, 0))) __wur;
/* Read formatted input from S into argument list ARG. */
extern int vsscanf (const char *__restrict __s,
const char *__restrict __format, __gnuc_va_list __arg)
__THROW __attribute__ ((__format__ (__scanf__, 2, 0)));

189
blib/src/sprintf.c

@ -0,0 +1,189 @@
#include "sprintf.h"
#include "math.h"
#include "string.h"
#include "../drivers/terminal/terminal.h"
#define SIGN(X) X > 0 ? 1 : -1
#define ARG(TYPE) (TYPE)va_arg(ap, TYPE)
static char* utohex(char* str, uint64_t x, int min_digits) {
size_t digits = 1;
uint64_t _x = x;
// _x < 0xf
while(_x & ~0xf) {
_x >>= 4;
digits++;
}
if((int)digits < min_digits)
digits = min_digits;
char* end = str+digits;
char* ptr = end;
for(int i = digits;i>0;i--) {
int d = x & 0x0f;
if(d < 0xa)
d += '0';
else
d += 'a'-10;
*(--ptr) = d;
x >>= 4;
}
*end = '\0';
return end;
}
// unsigned to string
// return a ptr to the end of the string
static char* utos(char* str, uint64_t x, int min_digits) {
size_t digits = 1;
uint64_t _x = x;
while(_x > 9) {
_x /= 10;
digits++;
}
if((int)digits < min_digits)
digits = min_digits;
char* end = str+digits;
char* ptr = end;
for(int i = digits;i>0;i--) {
*(--ptr) = '0' + x%10;
x /= 10;
}
*end = '\0';
return end;
}
// signed to string
static char* itos(char* str, int64_t x, int min_digits) {
if(x < 0) {
*(str++) = '-';
x *= -1;
}
return utos(str, x, min_digits);
}
int vsprintf(char *str, const char *format, va_list ap) {
char cf;
int args_put = 0;
int persent = 0;
int digits = -1;
// bool value: '%l..'
int _long = 0;
while(1) {
cf = *(format++);
if(cf == '\0')
break;
if(persent) {
if(cf == '.')
continue;
else if(cf == 'l') {
_long = 1;
continue;
}
int i = cf - '0';
if(i >= 0 && i <= 9) {
if(digits < 0)
digits = 0;
digits = 10 * digits + i;
}
else {
switch(cf) {
default:
// invalid
break;
case 'u':
if(!_long)
str = utos(str, ARG(uint32_t), digits);
else
str = utos(str, ARG(uint64_t), digits);
break;
case 'd':
case 'i':
if(!_long)
str = itos(str, ARG(int32_t), digits);
else
str = itos(str, ARG(int64_t), digits);
break;
case 'h':
case 'x':
if(!_long)
str = utohex(str, ARG(uint32_t), digits);
else
str = utohex(str, ARG(uint64_t), digits);
break;
case 's':
{
const char* arg = ARG(const char*);
if(digits >= 0) {
strncpy(str, arg, digits);
if((int) strlen(arg) < digits)
str += strlen(arg);
else
str += digits;
}
else {
strcpy(str, arg);
str += strlen(arg);
}
}
break;
case 'c':
*(str++) = (char)ARG(int);
break;
}
digits = -1;
persent = 0;
_long = 0;
args_put++;
}
}
else if(cf == '%') {
if(persent) {
// allow %% in fmt to print '%'
persent = 0;
*(str++) = '%';
}
persent = 1;
}
else {
*(str++) = cf;
}
}
*str = '\0';
return args_put;
}
int sprintf(char* str, const char* format, ...) {
va_list ap;
int res;
va_start(ap, format);
res = vsprintf(str,format,ap);
va_end(ap);
return res;
}

84
blib/src/stdio.c

@ -0,0 +1,84 @@
#include <stdio.h>
#include <unistd.h>
#include <alloc.h>
struct FILE {
int fd;
int flags;
int mode;
int ungetc;
int error;
int eof;
int pos;
int size;
char* buffer;
size_t buf_off;
};
FILE *fopen (const char *restrict filename,
const char *restrict modes
) {
int fd = open(filename, 0, 0);
if(fd < 0) {
return NULL;
}
FILE* file = malloc(sizeof(FILE));
if(!file) {
return NULL;
}
file->fd = fd;
file->flags = 0;
file->mode = 0;
file->ungetc = 0;
file->error = 0;
file->eof = 0;
file->pos = 0;
file->size = 0;
file->buf_off = 0;
file->buffer = malloc(BUFSIZ);
return file;
}
int fflush (FILE *stream) {
if(stream->buf_off) {
write(stream->fd, stream->buffer, stream->buf_off);
stream->buf_off = 0;
}
}
int fclose (FILE* stream) {
fflush(stream);
close(stream->fd);
free(stream->buffer);
free(stream);
}
FILE *freopen (const char* restrict filename,
const char* restrict modes,
FILE* restrict stream
) {
fflush(stream);
close(stream->fd);
stream->fd = open(filename, 0, 0);
if(stream->fd < 0) {
return NULL;
}
return stream;
}
void setbuf (FILE *restrict stream, char *restrict buf) {
free(stream->buffer);
stream->buffer = buf;
}
Loading…
Cancel
Save