[comp.unix.wizards] Obscure code.

waldorf@venice.SEDD.TRW.COM (jerry waldorf) (03/28/90)

	I am porting some code from a sun to hp and ran across
this:

static int scanit(str, fmt, args)
register char *str;
char *fmt;
char **args;
{
	FILE _strbuf;

	_strbuf._flag = _IOREAD | _IOSTRG;
	_strbuf._ptr = _strbuf._base = (unsigned char *) str;
	_strbuf._cnt = 0;
	while(*str++)
		_strbuf._cnt++;
	_strbuf._bufsiz = _strbuf._cnt;
	return(_doscan(&_strbuf, fmt, args));
}

	What exactly does this do and where do I find _doscan?

	Any hints would be greatly appreciated.  I am trying to
make this code portable.  Thanks in advance.

rcb@ccpv1.ncsu.edu (Randy Buckland) (03/28/90)

waldorf@venice.SEDD.TRW.COM (jerry waldorf) writes:
>static int scanit(str, fmt, args)
>register char *str;
>char *fmt;
>char **args;
>{
>	FILE _strbuf;

>	_strbuf._flag = _IOREAD | _IOSTRG;
>	_strbuf._ptr = _strbuf._base = (unsigned char *) str;
>	_strbuf._cnt = 0;
>	while(*str++)
>		_strbuf._cnt++;
>	_strbuf._bufsiz = _strbuf._cnt;
>	return(_doscan(&_strbuf, fmt, args));
>}

>	What exactly does this do and where do I find _doscan?

_doscan is an internal routine of {scanf, fscnaf, sscanf}. This code is
part of "sscanf". The doscan expects a FILE struct with a buffer already
read in and will scan the chars in the buffer. scanf and fscanf call it 
with different arguments and for sscanf a fake FILE struct is built to
allow it to call the same routine. _doscan will read the chars in the
buffer (the passed string) and if that is not enough, will attempt to read
more chars from the file. This will fail and doscan will return with what
it has.
Randy Buckland
North Carolina State University
(919) 737-2517
rcb@ccpv1.ncsu.edu

tml@hemuli.tik.vtt.fi (Tor Lillqvist) (03/29/90)

In article <1990Mar28.131215.14795@ncsuvx.ncsu.edu> rcb@ccpv1.ncsu.edu (Randy Buckland) writes:
>waldorf@venice.SEDD.TRW.COM (jerry waldorf) writes:

>>	_strbuf._flag = _IOREAD | _IOSTRG;

>_doscan is an internal routine of {scanf, fscnaf, sscanf}. 

>for sscanf a fake FILE struct is built to
>allow it to call the same routine.

One more point if you are porting to HP-UX: There is no _IOSTRG flag,
instead you should put _NFILE in _strbuf._file (i.e. an illegal file
descriptor).  Or something like that, disassemble the sscanf code
(with adb) to be sure.
-- 
Tor Lillqvist,
working, but not speaking, for the Technical Research Centre of Finland

lm@snafu.Sun.COM (Larry McVoy) (03/29/90)

In article <423@venice.SEDD.TRW.COM> waldorf@venice.sedd.trw.com (jerry waldorf) writes:
>	return(_doscan(&_strbuf, fmt, args));
>	What exactly does this do and where do I find _doscan?

_doscan() is a libc routine that printf like functions use.
---
What I say is my opinion.  I am not paid to speak for Sun, I'm paid to hack.
    Besides, I frequently read news when I'm drjhgunghc, err, um, drunk.
Larry McVoy, Sun Microsystems     (415) 336-7627       ...!sun!lm or lm@sun.com