[comp.os.minix] What should [fs]scanf return?

poole@forty2.UUCP (Simon Poole) (01/16/89)

Somehow I've got the feeling that this has been hashed thru before, but
I probably wasn't paying attention then. Consider the following code
fragment:

........
    printf("%d\n",sscanf("123","%d",&dummy));
    printf("%d\n",sscanf("123","%*d",&dummy));   
    printf("%d\n",sscanf("123:456","%d:%d",&dummy,&dummy));
    printf("%d\n",sscanf("123:456","%*d:%*d",&dummy,&dummy));
........

What should it print?

Minix-ST 1.1*    BSD 4.2    I think it should be   

    1             1                1
    1             -1               0
    3             2                2
    3             -1               0


K&R first edition says (about scanf):

'It returns as its value the number of sucessfully matched and assigned
 input items'

The fixes to get the behaviour in the third row are quite simple, but is it
correct?

* Applies to the PC version too as far as I know.

-- 
----------------------------------------------------------------------------
UUCP:   ...mcvax!cernvax!forty2!poole			Simon Poole
BITNET: K538915@CZHRZU1A
----------------------------------------------------------------------------

poole@forty2.UUCP (Simon Poole) (01/16/89)

In article <588@forty2.UUCP> poole@forty2.UUCP (Simon Poole) writes:
......
>    printf("%d\n",sscanf("123","%d",&dummy));
>    printf("%d\n",sscanf("123","%*d",&dummy));   
>    printf("%d\n",sscanf("123:456","%d:%d",&dummy,&dummy));
>    printf("%d\n",sscanf("123:456","%*d:%*d",&dummy,&dummy));
.....
Should naturally be:

    printf("%d\n",sscanf("123","%d",&dummy));
    printf("%d\n",sscanf("123","%*d"));   
    printf("%d\n",sscanf("123:456","%d:%d",&dummy,&dummy));
    printf("%d\n",sscanf("123:456","%*d:%*d"));

Sorry...
-- 
----------------------------------------------------------------------------
UUCP:   ...mcvax!cernvax!forty2!poole			Simon Poole
BITNET: K538915@CZHRZU1A
----------------------------------------------------------------------------

kirkenda@psu-cs.UUCP (Steve Kirkendall) (01/19/89)

The following applies to MINIX-ST.  The MINIX-PC crowd should probably worry
about this too, though...

Simon Poole is right; the scanf() functions do return unconventional
(i.e. incorrect) values.

Even worse though: The scanf() functions seem to use a defective version of
ungetc().  I wrote a program (a font editor) that had to read in some hex
numbers (the values of the font16[] and font8[] arrays from kernel/stfnt.c).
Fscanf() was getting stuck in the middle of the file.  On a hunch, I modified
my program to print the value of ftell() wherever fscanf() failed.  It was
failing at file offsets 1024 and 12288 - both are block boundaries. The scanf()
functions can't always handle block boundaries.

My work-around was to use fgets() and sscanf().

BTW, is there any interest in new fonts for MINIX-ST?  Should I post my font
editor?  Or mail it to somebody?
-- 
"If I seem insensitive to what you're going through,
	keep in mind that's the way I am." -- Mr. Spock

dal@midgard.Midgard.MN.ORG (Dale Schumacher) (01/20/89)

The following question appeared in comp.os.minix recently.  I thought
it would get a better/faster answer in comp.lang.c, thus this crossposting.

In article <588@forty2.UUCP> poole@forty2.UUCP (Simon Poole) writes:
|Somehow I've got the feeling that this has been hashed thru before, but
|I probably wasn't paying attention then. Consider the following code
|fragment:
|
|........
|    printf("%d\n",sscanf("123","%d",&dummy));
|    printf("%d\n",sscanf("123","%*d",&dummy));   
|    printf("%d\n",sscanf("123:456","%d:%d",&dummy,&dummy));
|    printf("%d\n",sscanf("123:456","%*d:%*d"));
|........
|
|What should it print?
|
|Minix-ST 1.1     BSD 4.2    I think it should be   
|
|    1             1                1
|    1             -1               0
|    3             2                2
|    3             -1               0
|
|
|K&R first edition says (about scanf):
|
|'It returns as its value the number of sucessfully matched and assigned
| input items'

guy@auspex.UUCP (Guy Harris) (01/21/89)

>...I think it should be...

You're right.  It should be.  (And it is, on SunOS 4.0, which means it
probably is on S5R3 as well, since the 4.0 "scanf" code is derived from
the S5R3 "scanf" code....)

I don't know why BSD is confused; perhaps it treats suppressed
assignments as errors, or assumes that just because there were no
succesful assignments (because there were no assignments at all) the
call must have failed.  That's wrong.  I assume Minix is confused
because it 1) counts even suppressed assignments and 2) counts literal
matches (i.e., the ":" matched the ":"); that's also wrong. 

charles@hpcvca.HP.COM (Charles Brown) (01/26/89)

> Minix-ST 1.1*    BSD 4.2    I think it should be   
>     1             1                1
>     1             -1               0
>     3             2                2
>     3             -1               0
> UUCP:   ...mcvax!cernvax!forty2!poole			Simon Poole

For what it is worth, HP-UX 6.2 on an HP9000s350 gives the BSD4.2
results.  The man page implies (but does not state) that this routine
comes from SYS5.  However, HP-UX contains enough BSDisms that I am not
sure.
--
	Charles Brown		charles%hpcvca@hplabs.hp.com
	Not representing my employer.