Re: [dev] [sbase] printf(1)

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Thu, 19 Dec 2013 14:22:53 +0100

> > I'm confused about what you're trying to accomplish here.
> > How about you just copy the format part from the for loop up there and
> > throw it into the standard library's printf?
> >
>
> I agree!
> This strongly smells like NiH-syndrome. Considering we have suckless
> standard libraries like uClibc, which we could link statically to by
> default some day in the future, we can safely assume letting the stdlib

I think you are lossing something important here. When you execute
printf in a program you have the variables which you want to pass
to printf, or in the case of vprintf you have the va_list, but
in this case you don't have any of them. You cannot call to printf
(or vprintf, or sprintf, or vsprintf) with an array of pointers
to chars (argv), so you have to forgot the library routines.

I also think all the operations with fmt and end are a bit confusing
and maybe it is a bit more clear something like this (taken from a
personal project):

        void
        printf(const char *fmt, ...)
        {
                unsigned char c;
                va_list va;
                int base, sign;

                va_start(va, fmt);
                while (( c = *fmt++) != '\0') {
                        if (c == '%') {
                                sign = 0;
                                switch (*fmt++) {
                                case '%':
                                        c = '%';
                                        goto printchar;
                                case 'c':
                                        c = va_arg(va, char);
                                        goto printchar;
                                case 'o':
                                        base = 8;
                                        break;
                                case 'd':
                                        sign = 1;
                                        base = 10;
                                        break;
                                case 'p':
                                case 'x':
                                        base = 16;
                                        break;
                                case 's':
                                        puts(va_arg(va, char *));
                                        /* passthrou */
                                default:
                                        continue;
                                }
                                printn(va_arg(va, int), base, sign);
                                continue;
                        }
        printchar:
                        putchar(c);
                }
        }

Of course, it is a simplified version, and it handles operands from
the stack, but it can be easily adapated to argv.

Regards,

-- 
Roberto E. Vargas Caballero
Received on Thu Dec 19 2013 - 14:22:53 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 19 2013 - 14:24:06 CET