Skip to content

Commit

Permalink
Merge pull request #2086 from ghaerr/kfmt
Browse files Browse the repository at this point in the history
[kernel,libc] Add %#k alt option to precision timer displays
  • Loading branch information
ghaerr authored Oct 24, 2024
2 parents 12eb865 + e45f6ac commit 81f639b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
12 changes: 8 additions & 4 deletions elks/kernel/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* %D device name as %04x
* %P process ID
* %k pticks (0.838usec intervals auto displayed as us, ms or s)
* %#k pticks truncated at decimal point
*
* All except %% can be followed by a width specifier 1 -> 31 only
* and the h/l length specifiers also work where appropriate.
Expand Down Expand Up @@ -154,8 +155,8 @@ static void numout(unsigned long v, int width, unsigned int base, int type,
*--p = '0' + c;
if (!v)
break;
if (alt == ',' && ++i == 3) {
*--p = ',';
if ((alt == ',' || alt == '\'') && ++i == 3) {
*--p = alt;
i = 0;
}
}
Expand All @@ -171,8 +172,11 @@ static void numout(unsigned long v, int width, unsigned int base, int type,
if (Sign)
kputchar('-');
while (*p) {
if (n-- == Decimal) /* only for %k pticks */
if (n-- == Decimal) { /* only for %k pticks */
if (alt)
break;
kputchar('.');
}
kputchar(*p++);
}
while (Suffix) {
Expand All @@ -199,7 +203,7 @@ static void vprintk(const char *fmt, va_list p)
}

ptrfmt = alt = width = 0;
if (c == '#' || c == ',') {
if (c == '#' || c == ',' || c == '\'') {
alt = c;
c = *fmt++;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void dtostr(double val, int style, int preci, char *buf);
/* use this macro to link in libc %e,%f,%g printf/sprintf support into user program */
#define __STDIO_PRINT_FLOATS __LINK_SYMBOL(dtostr)
#endif
void ptostr(unsigned long pticks, char *buf);
void ptostr(unsigned long pticks, int alt, char *buf);

#define stdio_pending(fp) ((fp)->bufread>(fp)->bufpos)

Expand Down
7 changes: 5 additions & 2 deletions libc/misc/ptostr.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
#include <stdlib.h>

void ptostr(unsigned long v, char *outbuf)
void ptostr(unsigned long v, int alt, char *outbuf)
{
unsigned int c;
char *p;
Expand Down Expand Up @@ -40,8 +40,11 @@ void ptostr(unsigned long v, char *outbuf)

n = buf + sizeof(buf) - 1 - p; /* string length */
while (*p) {
if (n == Decimal)
if (n == Decimal) {
if (alt)
break;
*outbuf++ = '.';
}
--n;
*outbuf++ = *p++;
}
Expand Down
13 changes: 7 additions & 6 deletions libc/stdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* %x/%X hexadecimal with lower/upper case letters
* %p pointer - same as %04x
* %k pticks (0.838usec intervals auto displayed as us, ms or s)
* %#k pticks truncated at decimal point
* %efgEG optional floating point formatting using dtostr
* The following flags preceding the format type are supported:
* 0 fill with leading zeros
Expand Down Expand Up @@ -130,7 +131,7 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
unsigned long v;
int buffer_mode;
char *p;
//char hash;
int hash;
char buf[64];

/* turn off putc calling fputc every time for non or line buffered */
Expand All @@ -140,7 +141,7 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
while (*fmt) {
if (*fmt == '%') {
ljustf = 0; /* left justify flag */
//hash = 0; /* alternate output */
hash = 0; /* alternate output */
quot = 0; /* thousands grouping */
dpoint = 0; /* found decimal point */
sign = '\0'; /* sign char & status */
Expand Down Expand Up @@ -183,9 +184,9 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
quot = *fmt;
goto fmtnxt;

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

case '\0': /* early EOS */
continue;
Expand Down Expand Up @@ -249,7 +250,7 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
v = lval? va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
if (*fmt == 'k') {
if (_weakaddr(ptostr)) {
(_weakfn(ptostr))(v, p);
(_weakfn(ptostr))(v, hash, p);
preci = -1;
goto printit;
}
Expand Down

0 comments on commit 81f639b

Please sign in to comment.