Skip to content

Commit

Permalink
Merge pull request #2011 from ghaerr/fmt
Browse files Browse the repository at this point in the history
[libc] Minimize differences between vfprintf.c and tiny_printf.c
  • Loading branch information
ghaerr authored Sep 13, 2024
2 parents 72be181 + b7ed9c7 commit 2a9d258
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ on:
- '**'
- '!.github/workflows/cross.yml'
- '!.github/workflows/ow-libc.yml'
- '!tools/**'
pull_request:
paths:
- '!tools/*'
pull_request:
paths:
- '**'
- '!.github/workflows/cross.yml'
- '!.github/workflows/ow-libc.yml'
- '!tools/**'
- '!tools/*'

jobs:
build:
Expand Down
32 changes: 16 additions & 16 deletions elkscmd/lib/tiny_vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>

static unsigned char bufout[80];
Expand Down Expand Up @@ -79,7 +79,7 @@ static void __fputc(int ch, FILE *fp)
* the number of characters output.
*/
static int
prtfld(FILE *op, unsigned char *buf, int ljustf, char pad, int width, int preci)
__fmt(FILE *op, unsigned char *buf, int ljustf, int width, int preci, char pad)
{
int cnt = 0, len;
unsigned char ch;
Expand Down Expand Up @@ -107,7 +107,7 @@ prtfld(FILE *op, unsigned char *buf, int ljustf, char pad, int width, int preci)
}
else if (len)
{
ch = *buf++; /* main field */
ch = *buf++; /* main field */
--len;
}
else
Expand All @@ -121,13 +121,14 @@ prtfld(FILE *op, unsigned char *buf, int ljustf, char pad, int width, int preci)
return cnt;
}

int vfprintf(FILE *op, const char *fmt, va_list ap)
int
vfprintf(FILE *op, const char *fmt, va_list ap)
{
int cnt = 0;
int i, width, preci, radix;
int ljustf, lval, pad, dpoint;
int i, cnt = 0, ljustf, lval;
int preci, width, radix;
char pad, dpoint;
char *ptmp;
char tmp[64];
char tmp[64];

while (*fmt)
{
Expand All @@ -136,17 +137,18 @@ int vfprintf(FILE *op, const char *fmt, va_list ap)
ljustf = 0; /* left justify flag */
dpoint = 0; /* found decimal point */
lval = 0;
pad = ' '; /* justification padding char */
width = -1; /* min field width */
preci = -1; /* max data width */
pad = ' '; /* justification padding char */
radix = 10; /* number base */
ptmp = tmp; /* pointer to area to print */
fmtnxt:
i = 0;
for(;;)
{
++fmt;
if(*fmt < '0' || *fmt > '9' ) break;
if(*fmt < '0' || *fmt > '9' )
break;
i = (i * 10) + (*fmt - '0');
if (dpoint)
preci = i;
Expand All @@ -171,20 +173,18 @@ int vfprintf(FILE *op, const char *fmt, va_list ap)

case 'l': /* long data */
lval = 1;
case 'h': /* short data */
case 'h': /* short data */
goto fmtnxt;

case 'd': /* Signed decimal */
ptmp = ltostr((long) ((lval)
? va_arg(ap, long)
: va_arg(ap, int)), 10);
ptmp = ltostr((long) ((lval) ? va_arg(ap, long) : va_arg(ap, int)), 10);
goto printit;

case 'o': /* Unsigned octal */
radix = 8;
goto usproc;

case 'x': /* Unsigned hexadecimal */
case 'x': /* Unsigned hexadecimal */
radix = 16;
/* fall thru */

Expand All @@ -204,7 +204,7 @@ int vfprintf(FILE *op, const char *fmt, va_list ap)
ptmp = va_arg(ap, char*);
nopad:
printit:
cnt += prtfld(op, (unsigned char *)ptmp, ljustf, pad, width, preci);
cnt += __fmt(op, (unsigned char *)ptmp, ljustf, width, preci, pad);
break;

default: /* unknown character */
Expand Down
56 changes: 29 additions & 27 deletions libc/stdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

#ifndef __HAS_NO_FLOATS__
Expand All @@ -36,16 +36,16 @@
*/
#endif

static int
printfield(FILE *op, unsigned char *buf, int ljustf, char sign, char pad, int width,
int preci, int buffer_mode)
/*
* Output the given field in the manner specified by the arguments. Return
* the number of characters output.
*/
static int
__fmt(FILE *op, unsigned char *buf, int ljustf, int width, int preci, char pad,
char sign, int buffer_mode)
{
register int cnt = 0, len;
register unsigned char ch;
int cnt = 0, len;
unsigned char ch;

len = strlen((char *)buf);

Expand Down Expand Up @@ -80,7 +80,8 @@ printfield(FILE *op, unsigned char *buf, int ljustf, char sign, char pad, int wi
{
if (sign)
{
showsign:ch = sign; /* sign */
showsign:
ch = sign; /* sign */
sign = '\0';
}
else
Expand All @@ -96,19 +97,20 @@ printfield(FILE *op, unsigned char *buf, int ljustf, char sign, char pad, int wi
if( ch == '\n' && buffer_mode == _IOLBF ) fflush(op);
}

return (cnt);
return cnt;
}

int
vfprintf(FILE *op, const char *fmt, va_list ap)
{
register int i, cnt = 0, ljustf, lval;
int preci, dpoint, width;
char pad, sign, radix, hash;
register char *ptmp;
int i, cnt = 0, ljustf, lval;
int preci, width, radix;
char pad, dpoint;
char sign, hash;
unsigned long l;
int buffer_mode;
char tmp[64];
char *ptmp;
char tmp[64];

/* This speeds things up a bit for unbuffered */
buffer_mode = (op->mode&__MODE_BUF);
Expand All @@ -120,21 +122,22 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
{
if( buffer_mode == _IONBF ) fflush(op);
ljustf = 0; /* left justify flag */
hash = 0;
dpoint = 0; /* found decimal point */
sign = '\0'; /* sign char & status */
pad = ' '; /* justification padding char */
width = -1; /* min field width */
dpoint = 0; /* found decimal point */
preci = -1; /* max data width */
radix = 10; /* number base */
ptmp = tmp; /* pointer to area to print */
hash = 0;
lval = (sizeof(int)==sizeof(long)); /* long value flag */
fmtnxt:
i = 0;
for(;;)
{
++fmt;
if(*fmt < '0' || *fmt > '9' ) break;
if(*fmt < '0' || *fmt > '9' )
break;
i = (i * 10) + (*fmt - '0');
if (dpoint)
preci = i;
Expand Down Expand Up @@ -188,10 +191,7 @@ vfprintf(FILE *op, const char *fmt, va_list ap)

case 'd': /* Signed decimal */
case 'i':
ptmp = ltostr((long) ((lval)
? va_arg(ap, long)
: va_arg(ap, int)),
10);
ptmp = ltostr((long) ((lval) ? va_arg(ap, long) : va_arg(ap, int)), 10);
goto printit;

case 'b': /* Unsigned binary */
Expand Down Expand Up @@ -227,11 +227,14 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
/* if precision timing not linked in, display as unsigned */
}
ptmp = ultostr(l, radix);
if( hash && radix == 8 ) { width = strlen(ptmp)+1; pad='0'; }
if( hash && radix == 8 ) {
width = strlen(ptmp)+1;
pad = '0';
}
goto printit;

case '#':
hash=1;
hash = 1;
goto fmtnxt;

case 'c': /* Character */
Expand All @@ -246,8 +249,8 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
sign = '\0';
pad = ' ';
printit:
cnt += printfield(op, (unsigned char *)ptmp, ljustf,
sign, pad, width, preci, buffer_mode);
cnt += __fmt(op, (unsigned char *)ptmp, ljustf, width, preci, pad,
sign, buffer_mode);
break;

#ifndef __HAS_NO_FLOATS__
Expand All @@ -256,8 +259,7 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
case 'g':
case 'E':
case 'G':
if (_weakaddr(dtostr))
{
if (_weakaddr(dtostr)) {
(_weakfn(dtostr))(va_arg(ap, double), *fmt, preci, ptmp);
preci = -1;
goto printit;
Expand All @@ -281,5 +283,5 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
op->mode |= buffer_mode;
if( buffer_mode == _IONBF ) fflush(op);
if( buffer_mode == _IOLBF ) op->bufwrite = op->bufstart;
return (cnt);
return cnt;
}

0 comments on commit 2a9d258

Please sign in to comment.