ddk: Linux vsnprintf()

git-svn-id: svn://kolibrios.org@1869 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2011-02-16 08:50:52 +00:00
parent 0e52955e0e
commit 1a1c5168f8
9 changed files with 1355 additions and 772 deletions

View File

@ -1,4 +1,5 @@
CC = gcc
AS = as
@ -24,11 +25,9 @@ NAME_SRCS:= \
linux/firmware.c \
linux/list_sort.c \
linux/dmapool.c \
linux/ctype.c \
malloc/malloc.c \
stdio/icompute.c \
stdio/vsprintf.c \
stdio/doprnt.c \
stdio/chartab.c \
string/_memmove.S \
string/_strncat.S \
string/_strncmp.S \

View File

@ -174,17 +174,5 @@ int xf86DrvMsg(int skip, int code, const char* format, ...)
return len;
}
int snprintf(char *s, size_t n, const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = vsnprintf(s, n, format, ap);
va_end(ap);
return retval;
}

View File

@ -1,261 +0,0 @@
#include "ctype.h"
char __ctype[] = {
0,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C|_S,
_C|_S,
_C|_S,
_C|_S,
_C|_S,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_C,
_S,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_N,
_N,
_N,
_N,
_N,
_N,
_N,
_N,
_N,
_N,
_P,
_P,
_P,
_P,
_P,
_P,
_P,
_U|_X,
_U|_X,
_U|_X,
_U|_X,
_U|_X,
_U|_X,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_U,
_P,
_P,
_P,
_P,
_P,
_P,
_L|_X,
_L|_X,
_L|_X,
_L|_X,
_L|_X,
_L|_X,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_L,
_P,
_P,
_P,
_P,
_C,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
};

View File

@ -1,44 +0,0 @@
/* The <ctype.h> header file defines some macros used to identify characters.
* It works by using a table stored in chartab.c. When a character is presented
* to one of these macros, the character is used as an index into the table
* (__ctype) to retrieve a byte. The relevant bit is then extracted.
*/
#ifndef _CTYPE_H
#define _CTYPE_H
extern char __ctype[]; /* property array defined in chartab.c */
#define _U 0x01 /* this bit is for upper-case letters [A-Z] */
#define _L 0x02 /* this bit is for lower-case letters [a-z] */
#define _N 0x04 /* this bit is for numbers [0-9] */
#define _S 0x08 /* this bit is for white space \t \n \f etc */
#define _P 0x10 /* this bit is for punctuation characters */
#define _C 0x20 /* this bit is for control characters */
#define _X 0x40 /* this bit is for hex digits [a-f] and [A-F]*/
/* Macros for identifying character classes. */
#define isalnum(c) ((__ctype+1)[c]&(_U|_L|_N))
#define isalpha(c) ((__ctype+1)[c]&(_U|_L))
#define iscntrl(c) ((__ctype+1)[c]&_C)
#define isgraph(c) ((__ctype+1)[c]&(_P|_U|_L|_N))
#define ispunct(c) ((__ctype+1)[c]&_P)
#define isspace(c) ((__ctype+1)[c]&_S)
#define isxdigit(c) ((__ctype+1)[c]&(_N|_X))
#define isdigit(c) ((unsigned) ((c)-'0') < 10)
#define islower(c) ((unsigned) ((c)-'a') < 26)
#define isupper(c) ((unsigned) ((c)-'A') < 26)
#define isprint(c) ((unsigned) ((c)-' ') < 95)
#define isascii(c) ((unsigned) (c) < 128)
#define toascii(c) ((c) & 0x7f)
static inline int toupper(int c)
{
return islower(c) ? c - 'a' + 'A' : c ;
}
#endif /* _CTYPE_H */

View File

@ -1,311 +0,0 @@
/*
* doprnt.c - print formatted output
*/
/* $Header$ */
#include "ctype.h"
#include "stdio.h"
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
#define NOFLOAT
#define putc(c, p) (--(p)->_count >= 0 ? \
(int) (*(p)->_ptr++ = (c)) : EOF)
/* gnum() is used to get the width and precision fields of a format. */
static const char *
gnum(register const char *f, int *ip, va_list *app)
{
register int i, c;
if (*f == '*') {
*ip = va_arg((*app), int);
f++;
} else {
i = 0;
while ((c = *f - '0') >= 0 && c <= 9) {
i = i*10 + c;
f++;
}
*ip = i;
}
return f;
}
#if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */
#elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG)
#else
#error garbage pointer size
#define set_pointer(flags) /* compilation might continue */
#endif
/* print an ordinal number */
static char *
o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
{
long signed_val;
unsigned long unsigned_val;
char *old_s = s;
int base;
switch (flags & (FL_SHORT | FL_LONG)) {
case FL_SHORT:
if (is_signed) {
signed_val = (short) va_arg(*ap, int);
} else {
unsigned_val = (unsigned short) va_arg(*ap, unsigned);
}
break;
case FL_LONG:
if (is_signed) {
signed_val = va_arg(*ap, long);
} else {
unsigned_val = va_arg(*ap, unsigned long);
}
break;
default:
if (is_signed) {
signed_val = va_arg(*ap, int);
} else {
unsigned_val = va_arg(*ap, unsigned int);
}
break;
}
if (is_signed) {
if (signed_val < 0) {
*s++ = '-';
signed_val = -signed_val;
} else if (flags & FL_SIGN) *s++ = '+';
else if (flags & FL_SPACE) *s++ = ' ';
unsigned_val = signed_val;
}
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0';
if (!unsigned_val && c != 'p') {
if (!precision)
return s;
} else if (((flags & FL_ALT) && (c == 'x' || c == 'X'))
|| c == 'p') {
*s++ = '0';
*s++ = (c == 'X' ? 'X' : 'x');
}
switch (c) {
case 'b': base = 2; break;
case 'o': base = 8; break;
case 'd':
case 'i':
case 'u': base = 10; break;
case 'x':
case 'X':
case 'p': base = 16; break;
}
s = _i_compute(unsigned_val, base, s, precision);
if (c == 'X')
while (old_s != s) {
*old_s = toupper(*old_s);
old_s++;
}
return s;
}
int
_doprnt(register const char *fmt, va_list ap, FILE *stream)
{
register char *s;
register int j;
int i, c, width, precision, zfill, flags, between_fill;
int nrchars=0;
const char *oldfmt;
char *s1, buf[1025];
while (c = *fmt++) {
if (c != '%') {
if (c == '\n') {
if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
}
if (putc(c, stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
continue;
}
flags = 0;
do {
switch(*fmt) {
case '-': flags |= FL_LJUST; break;
case '+': flags |= FL_SIGN; break;
case ' ': flags |= FL_SPACE; break;
case '#': flags |= FL_ALT; break;
case '0': flags |= FL_ZEROFILL; break;
default: flags |= FL_NOMORE; continue;
}
fmt++;
} while(!(flags & FL_NOMORE));
oldfmt = fmt;
fmt = gnum(fmt, &width, &ap);
if (fmt != oldfmt) flags |= FL_WIDTHSPEC;
if (*fmt == '.') {
fmt++; oldfmt = fmt;
fmt = gnum(fmt, &precision, &ap);
if (precision >= 0) flags |= FL_PRECSPEC;
}
if ((flags & FL_WIDTHSPEC) && width < 0) {
width = -width;
flags |= FL_LJUST;
}
if (!(flags & FL_WIDTHSPEC)) width = 0;
if (flags & FL_SIGN) flags &= ~FL_SPACE;
if (flags & FL_LJUST) flags &= ~FL_ZEROFILL;
s = s1 = buf;
switch (*fmt) {
case 'h': flags |= FL_SHORT; fmt++; break;
case 'l': flags |= FL_LONG; fmt++; break;
case 'L': flags |= FL_LONGDOUBLE; fmt++; break;
}
switch (c = *fmt++) {
default:
if (c == '\n') {
if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
}
if (putc(c, stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
continue;
case 'n':
if (flags & FL_SHORT)
*va_arg(ap, short *) = (short) nrchars;
else if (flags & FL_LONG)
*va_arg(ap, long *) = (long) nrchars;
else
*va_arg(ap, int *) = (int) nrchars;
continue;
case 's':
s1 = va_arg(ap, char *);
if (s1 == NULL)
s1 = "(null)";
s = s1;
while (precision || !(flags & FL_PRECSPEC)) {
if (*s == '\0')
break;
s++;
precision--;
}
break;
case 'p':
set_pointer(flags);
/* fallthrough */
case 'b':
case 'o':
case 'u':
case 'x':
case 'X':
if (!(flags & FL_PRECSPEC)) precision = 1;
else if (c != 'p') flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 0);
break;
case 'd':
case 'i':
flags |= FL_SIGNEDCONV;
if (!(flags & FL_PRECSPEC)) precision = 1;
else flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 1);
break;
case 'c':
*s++ = va_arg(ap, int);
break;
#ifndef NOFLOAT
case 'G':
case 'g':
if ((flags & FL_PRECSPEC) && (precision == 0))
precision = 1;
case 'f':
case 'E':
case 'e':
if (!(flags & FL_PRECSPEC))
precision = 6;
if (precision >= sizeof(buf))
precision = sizeof(buf) - 1;
flags |= FL_SIGNEDCONV;
s = _f_print(&ap, flags, s, c, precision);
break;
#endif /* NOFLOAT */
case 'r':
ap = va_arg(ap, va_list);
fmt = va_arg(ap, char *);
continue;
}
zfill = ' ';
if (flags & FL_ZEROFILL) zfill = '0';
j = s - s1;
/* between_fill is true under the following conditions:
* 1- the fill character is '0'
* and
* 2a- the number is of the form 0x... or 0X...
* or
* 2b- the number contains a sign or space
*/
between_fill = 0;
if ((flags & FL_ZEROFILL)
&& (((c == 'x' || c == 'X') && (flags & FL_ALT) && j > 1)
|| (c == 'p')
|| ((flags & FL_SIGNEDCONV)
&& ( *s1 == '+' || *s1 == '-' || *s1 == ' '))))
between_fill++;
if ((i = width - j) > 0)
if (!(flags & FL_LJUST)) { /* right justify */
nrchars += i;
if (between_fill) {
if (flags & FL_SIGNEDCONV) {
j--; nrchars++;
if (putc(*s1++, stream) == EOF)
return nrchars ? -nrchars : -1;
} else {
j -= 2; nrchars += 2;
if ((putc(*s1++, stream) == EOF)
|| (putc(*s1++, stream) == EOF))
return nrchars ? -nrchars : -1;
}
}
do {
if (putc(zfill, stream) == EOF)
return nrchars ? -nrchars : -1;
} while (--i);
}
nrchars += j;
while (--j >= 0) {
if (putc(*s1++, stream) == EOF)
return nrchars ? -nrchars : -1;
}
if (i > 0) nrchars += i;
while (--i >= 0)
if (putc(zfill, stream) == EOF)
return nrchars ? -nrchars : -1;
}
return nrchars;
}

View File

@ -1,21 +0,0 @@
/*
* icompute.c - compute an integer
*/
/* $Header$ */
#include "loc_incl.h"
/* This routine is used in doprnt.c as well as in tmpfile.c and tmpnam.c. */
char *
_i_compute(unsigned long val, int base, char *s, int nrdigits)
{
int c;
c= val % base ;
val /= base ;
if (val || nrdigits > 1)
s = _i_compute(val, base, s, nrdigits - 1);
*s++ = (c>9 ? c-10+'a' : c+'0');
return s;
}

View File

@ -1,37 +0,0 @@
/*
* loc_incl.h - local include file for stdio library
*/
/* $Header$ */
#include "stdio.h"
#define io_testflag(p,x) ((p)->_flags & (x))
#include <stdarg.h>
#ifdef _ANSI
int _doprnt(const char *format, va_list ap, FILE *stream);
int _doscan(FILE * stream, const char *format, va_list ap);
char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
char *_f_print(va_list *ap, int flags, char *s, char c, int precision);
void __cleanup(void);
#ifndef NOFLOAT
char *_ecvt(long double value, int ndigit, int *decpt, int *sign);
char *_fcvt(long double value, int ndigit, int *decpt, int *sign);
#endif /* NOFLOAT */
#endif
#define FL_LJUST 0x0001 /* left-justify field */
#define FL_SIGN 0x0002 /* sign in signed conversions */
#define FL_SPACE 0x0004 /* space in signed conversions */
#define FL_ALT 0x0008 /* alternate form */
#define FL_ZEROFILL 0x0010 /* fill with zero's */
#define FL_SHORT 0x0020 /* optional h */
#define FL_LONG 0x0040 /* optional l */
#define FL_LONGDOUBLE 0x0080 /* optional L */
#define FL_WIDTHSPEC 0x0100 /* field width is specified */
#define FL_PRECSPEC 0x0200 /* precision is specified */
#define FL_SIGNEDCONV 0x0400 /* may contain a sign */
#define FL_NOASSIGN 0x0800 /* do not assign (in scanf) */
#define FL_NOMORE 0x1000 /* all flags collected */

View File

@ -1,46 +0,0 @@
/*
* stdio.h - input/output definitions
*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
#ifndef _STDIO_H
#define _STDIO_H
/*
* Focus point of all stdio activity.
*/
typedef struct __iobuf {
int _count;
int _fd;
int _flags;
int _bufsiz;
unsigned char *_buf;
unsigned char *_ptr;
} FILE;
#define _IOFBF 0x000
#define _IOREAD 0x001
#define _IOWRITE 0x002
#define _IONBF 0x004
#define _IOMYBUF 0x008
#define _IOEOF 0x010
#define _IOERR 0x020
#define _IOLBF 0x040
#define _IOREADING 0x080
#define _IOWRITING 0x100
#define _IOAPPEND 0x200
#define _IOFIFO 0x400
/* The following definitions are also in <unistd.h>. They should not
* conflict.
*/
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define EOF (-1)
#endif /* _STDIO_H */

File diff suppressed because it is too large Load Diff