Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!gatech!bloom-beacon!think!ames!lll-tis!lll-lcc!ptsfa!ihnp4!homxb!mtuxo!mtune!codas!usfvax2!chips
From: chips@usfvax2.UUCP (Chip Salzenberg)
Newsgroups: comp.lang.c
Subject: Re: Distinguished pointers (was Re: Weird syscall returns)
Message-ID: <794@usfvax2.UUCP>
Date: Sat, 18-Jul-87 20:17:11 EDT
Article-I.D.: usfvax2.794
Posted: Sat Jul 18 20:17:11 1987
Date-Received: Sun, 19-Jul-87 19:48:18 EDT
References: <1158@copper.TEK.COM> <6858@auspyr.UUCP> <17171@cca.CCA.COM> <846@bsu-cs.UUCP>
Organization: AT Engineering, Tampa, FL
Lines: 69
Summary: No need for (foo *) -1

In article <846@bsu-cs.UUCP>, dhesi@bsu-cs.UUCP writes:
> In article <6109@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) 
> writes:
> >There are several UNIX library routines in various implementations
> >that attempt to return a -1 value for a function whose return type
> >is (char *). . . .
> >Phase 2 -- change these functions to return
> >NULL (of the appropriate type) on failure.
> 
> On the contrary, I think we need more distinguished pointer values, not
> just a single zero or NULL value.  I have a set of custom I/O routines
> that use the pointer value NOFILE to indicate that no file could be
> opened (equivalent to (char *) 0 in current C implementations) and
> another pointer value NULLFILE to indicate that the custom I/O library
> routines should ignore all output to this file.
> [contention that (FILE *) -1 should be portable, so it can be used as
> an alternative invalid pointer]

That's easy to do, and there's no need for "another NULL".
Declare a global variable of type FILE and use its address as a "magic
number".  For example:

--- C code follows ---

FILE nullfile;		/* This is never opened, read, etc. */

...
	FILE *logfile;
	logfile = myopen("logfile", "w");
...
	mywrite(logfile, buf, bufsiz);
...

FILE *myopen(fname, fmode);
{
	if (no_disk_io)
		return (&nullfile);
	else
		return (fopen(fname, fmode));
}

int mywrite(fp, buf, len)
FILE *fp;
char *buf;
int len;
{
	if (fp == NULL)
		big_problem();	/* :-) */
	else if (fp == &nullfile)
		return (len);	/* do nothing */
	else
		return (fwrite(fp, 1, len, buf));
}

--- End of C code ---

> Consider again how gets(3) indicates end-of-file and error.  If there
> were two distinguished pointer values, one could test for both
> end-of-file and for error without using the botched-up errno.

But where would it end?  No, if there must be an invalid pointer -- which
there is :-) -- then it must be one of a kind.

> Rahul Dhesi         UUCP:  {ihnp4,seismo}!{iuvax,pur-ee}!bsu-cs!dhesi

-- 
Chip Salzenberg            UUCP: "uunet!ateng!chip"  or  "chips@usfvax2.UUCP"
A.T. Engineering, Tampa    Fidonet: 137/42    CIS: 73717,366
"Use the Source, Luke!"	   My opinions do not necessarily agree with anything.