[comp.sys.hp] Extraneous error code under 3.1?

jthomas@nmsu.edu (James Thomas) (11/18/89)

System: HP 9000/840 under HP-UX 3.1

When we run the following code in either form (with the fgets or fscanf),
the first fgets/fscanf call returns an error code of 25 .  If that is
cleared, following calls do not touch errno .  Would someone else please
try this and report back your results?

Thank you,
Jim Thomas

------------------------------ 8< ------------------------------

#include <stdio.h>
	extern	int	errno;

main ()
{
  FILE* fd;
  char s[20];
  
  fd = fopen ("tmp","r");
  printf("using fscanf errno = %d",errno);
/*  fgets(s,20,fd); */
  fscanf(fd,"%s",s);
  printf("\n after read errno = %d\n",errno);
  printf(" string is %s\n",s);
}

mjs@hpfcso.HP.COM (Marc Sabatella) (11/20/89)

>When we run the following code in either form (with the fgets or fscanf),
>the first fgets/fscanf call returns an error code of 25 .  If that is
>cleared, following calls do not touch errno .  Would someone else please
>try this and report back your results?

None of this is surprising; none of the library functions in your program are
supposed to set errno to any reasonable value.  The system calls which are
used internally often do use and modify it, however.  In general, if the man
page for a function doesn't say it puts an error code into "errno", assume it
trashes it; and furthermore, even for those functions which do return codes in
"errno", the value is only valid only if the actual function return indictaes
that an error occurred (generally, when a system call returns -1).

--------------
Marc Sabatella
HP Colorado Language Lab
marc%hpfcrt@hplabs.hp.com

decot@hpisod2.HP.COM (Dave Decot) (11/22/89)

> >When we run the following code in either form (with the fgets or fscanf),
> >the first fgets/fscanf call returns an error code of 25 .  If that is
> >cleared, following calls do not touch errno .  Would someone else please
> >try this and report back your results?
> 
> None of this is surprising; none of the library functions in your program are
> supposed to set errno to any reasonable value.  The system calls which are
> used internally often do use and modify it, however.  In general, if the man
> page for a function doesn't say it puts an error code into "errno", assume it
> trashes it; and furthermore, even for those functions which do return codes
> in "errno", the value is only valid only if the actual function return
> indictaes that an error occurred (generally, when a system call returns -1).

All of the above is completely correct, for HP-UX 3.1.    However, please
note that all of the functions called in the program in question *do*
set errno to a reasonable value in error situations in HP-UX 7.0 or later.

Dave

frank@zen.co.uk (Frank Wales) (11/23/89)

In article <JTHOMAS.89Nov17162045@tecate.nmsu.edu> jthomas@nmsu.edu
(James Thomas) writes:
>System: HP 9000/840 under HP-UX 3.1

[Exactly our set-up here; the problem you describe is a generic Unix
 problem, however, and not unique to HP-UX.]

>When we run the following code in either form (with the fgets or fscanf),
>the first fgets/fscanf call returns an error code of 25 .  If that is
>cleared, following calls do not touch errno .  Would someone else please
>try this and report back your results?
>[example program deleted]

The behaviour you have noted is what I would expect, and is not a
problem.  Unless a system call or library function actually *returns* a
failure code as documented for the call or function in question, the
value of errno is undefined, and can be anything.  Many library calls
cause errors to happen as part of their normal operation, but none are
obliged to reset errno to zero if they eventually succeed.  For example,
fgets/fscanf probably perform an ioctl on their input as part of stdio set-up
operations during their first invocation, and in the case in question,
this would fail (errno 25==ENOTTY).

In short, errno should *only* be used to discover more information about
an already-reported error; it should not be used to detect the error in
the first place.
--
Frank Wales, Systems Manager,        [frank@zen.co.uk<->mcvax!zen.co.uk!frank]
Zengrange Ltd., Greenfield Rd., Leeds, ENGLAND, LS9 8DB. (+44) 532 489048 x217