9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Ori Bernstein <ori@eigenstate.org>
To: 9fans <9fans@9fans.net>
Subject: Re: [9fans] uvlong does not work on 9legacy raspberry pi image
Date: Mon, 19 May 2025 11:25:51 -0400	[thread overview]
Message-ID: <20250519112551.bf1c9146262cabdf12bbbd62@eigenstate.org> (raw)
In-Reply-To: <6217A342E4752D83738A502BE6EDD751@eigenstate.org>

...Wait. I just realized that 9front has fixed this on
these commits. I should have remembered because I was
the one that fixed it...

Initial commit to ape:

        73f38fc5460cb68662dd237022bda636ad734045

Sync to /sys/src:

        bc1cc79225f0b006dd66d4fd81030d06f83bfca2

On Mon, 19 May 2025 10:26:15 -0400
ori@eigenstate.org wrote:

> Quoth hahahahacker2009 <hahahahacker2009@gmail.com>:
> > I'm writing a program that use uvlong on 9legacy raspberry pi.
> > uvlong n;
> > scanf("%lld", &n);
> > print("%lld", n);
> > But after compiling the program and input a small number (8), it print 0. I modified it and compile with pcc, but it print a very big number then.
> > unsigned long works. What's the problem with uvlong?
> 
> the problem is with scanf; it doesn't handle that case:
> 
> static int icvt_n(FILE *f, va_list *args, int store, int width, int type){
>         if(store){
>                 --ncvt; /* this assignment doesn't count! */
>                 switch(type){
>                 case 'h': *va_arg(*args, short *)=nread; break;
>                 case 'n': *va_arg(*args, int *)=nread; break;
>                 case 'l':
>                 case 'L': *va_arg(*args, long *)=nread; break;
>                 }
>         }
>         return 1;
> }
> 
> A patch to fix this would be welcome, but Plan 9 code doesn't
> tend to use scanf; Unix code is also best off avoiding scanf,
> it's an API that feels convienient but has a number of pitfalls.
> Specifically, if there's a format mismatch, the remainder of the
> input remains buffered, and you need fiddly error handling code
> to consume it.
> 
> Here's probably how I'd write it:
> 
> char *e;
> uvlong n;
> 
> ln = Brdstr(bfd, '\n', 1);
> n = strtoull(ln, &e, 0);
> if(*e != '\0')
>         print("trailing junk\n");
> printf("%llud\n", n);
> free(ln);
> 
> it's a little more code, but it keeps working as the code gets
> more functional.
> 
> If I want to handle more complex input, I'd tend to reach for
> tokenize(2) -- which loosely the format a lot of programs use.
> For example:
> 
> char *e, *sp[4];
> uvlong n1, n2;
> int n;
> 
> ln = Brdstr(bfd, '\n', 1);
> n = tokenize(ln, sp, nelem(sp));
> switch(n){
> case 1:
>         if(strcmp(sp[0], "greet") != 0)
>                 sysfatal("unknown command");
>         print("hello world\n");
>         break;
> case 3:
>         if(strcmp(sp[0], "sum") != 0)
>                 sysfatal("unknown command");
>         n1 = strtoull(sp[1], &e, 0);
>         if(*e != 0)
>                 sysfatal("invalid number\n");
>         n2 = strtoull(sp[2], &e, 0);
>         if(*e != 0)
>                 sysfatal("invalid number\n");
>         print("2*arg: %lld\n", n1+n2);
>         break;
> default:
>         sysfatal("invalid line");
>         break;
> }
> free(ln);
> 
> (Subtle point: I have n+1 entries in sp[] to detect that
> too many fields were passed).
> 


-- 
Ori Bernstein <ori@eigenstate.org>

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T3df06e15ad1bc104-M51109fdec96a1bb86f620204
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

  parent reply	other threads:[~2025-05-19 17:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-19  5:14 hahahahacker2009
     [not found] ` <6217A342E4752D83738A502BE6EDD751@eigenstate.org>
2025-05-19 15:25   ` Ori Bernstein [this message]
     [not found] ` <ffeeafae-605d-45e6-aa42-951535b717f9@fjrhome.net>
     [not found]   ` <9f406cbd-623c-4772-ad00-7fb15605260d@sirjofri.de>
2025-05-19 23:37     ` Frank D. Engel, Jr.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250519112551.bf1c9146262cabdf12bbbd62@eigenstate.org \
    --to=ori@eigenstate.org \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).