[comp.sys.atari.st] Detecting Mark Williams' redirect

rogers@ncrcce.StPaul.NCR.COM (Bob Rogers) (12/08/89)

Does anybody know how I can detect (from within a Mark Williams' C program)
that standard input has been redirected (i.e., I need something like UNIX'
isatty()).

Thanks.
-- 
----
Bob Rogers                    rogers@stpaul.ncr.com  or  rogers@pnet51.cts.com
NCR Comten, St. Paul, MN      GEnie: R.C.ROGERS

dag@per2.UUCP (Daniel A. Glasser) (12/13/89)

In article <1777@ncrcce.StPaul.NCR.COM>,
	rogers@ncrcce.StPaul.NCR.COM (Bob Rogers) writes:
> Does anybody know how I can detect (from within a Mark Williams' C program)
> that standard input has been redirected (i.e., I need something like UNIX'
> isatty()).

I think that you might find that there is such a function hiding in the
Mark Williams libc.a, but I don't remember for sure.  I know that various
MWC tools use one internally (I used to work there).  If there isn't
one in the distributed library, it is fairly simple to write one.
Unfortunately I'm not at home (where all my ST stuff is), but I'll give
you some pointers from memory.  I'd suggest looking at the entry for
crts0.o in the Mark Williams C manual.

Assuming that the program is started from the Mark Williams (or some
compatible) shell or some program which uses the Mark Williams execv()
or execve() functions, something like the following should work:

------ Begin example code --------
#include <stdio.h>
int fisatty(fp)			/* function to determine if FILE is a TTY */
FILE *fp;			/* the file to look at.			  */
{
    extern char *_iovector;	/* see crts0.o entry in MWC manual	*/
    register int c;		/* a place to store a character		*/

    if (fp == NULL)		/* don't die on unopened files		*/
	return 0;		/* and don't call them ttys		*/

    c = _iovector[fileno(fp)];	/* Get the stream type from the IO Vector */
    return (c == 'C'		/* true if console (keyboard/screen) 	*/
	  || c == 'A');		/* true if serial (AUX) port		*/
    				/* false if anything else.		*/
}

int isatty(fd)		/* function to determine if a file descriptor	*/
int fd;			/* refers to a tty.  fd is the file descriptor.	*/
{
    extern char *_iovector;	/* see crts0.o entry in MWC manual	*/
    register int c;		/* a place to store a character		*/

    if (fd < 0)			/* make sure we've a valid fd		*/
	return 0;		/* if not it can't be a tty.		*/

    c = _iovector[fd];		/* Get the stream type from the IO Vector */
    return (c == 'C'		/* true if console (keyboard/screen) 	*/
	  || c == 'A');		/* true if serial (AUX) port		*/
    				/* false if anything else.		*/
}
------ End of example code --------

If you want the printer to be a TTY, add an "|| c == 'P'" to the returned
expressions.  If you don't want AUX to be a tty, remove the "|| c == 'A'"
from the returned expressions.  If you want anything but known disk files
to be called TTYs, replace the returned expressions with "(c != 'F')".
(These routines can be optimized.  I'm just trying to illustrate how to
do it.)

I believe that the range checking on the isatty parameter value should
include " || fd > MAX_FD_VALUE", but I don't know what defined symbol
to put in place of MAX_FD_VALUE, and I don't know its value off the
top of my head.

I hope this helps.
					Daniel A. Glasser
-- 
 _____________________________________________________________________________
    Daniel A. Glasser                           One of those things that goes
    uwvax!per2!dag                              "BUMP!!!(ouch)" in the night. 
 ---Persoft, Inc.---------465 Science Drive-------Madison, WI 53711-----------

rogers@ncrcce.StPaul.NCR.COM (Bob Rogers) (12/15/89)

Daniel A. Glasser responded to my query about detecting standard input 
redirection in a Mark Williams C program; he said he thought there was an
isatty() in libc.a.  Indeed there is, though it does not appear in the
documentation.  Thanks.
-- 
----
Bob Rogers                    rogers@stpaul.ncr.com  or  rogers@pnet51.cts.com
NCR Comten, St. Paul, MN      GEnie: R.C.ROGERS