[comp.lang.c] Determining if a file is a device

wnp@killer.UUCP (Wolf Paul) (10/05/87)

In trying to implement a function which emulates the UNIX stat(2)
system call I have run into a problem:

How can I determine whether a filename passed to a C function
is a regular file or the name of a device?

I realize that I could hard-code the default device names (i.e. CON,
PRN, COM1, LPT1, etc.) into my function, but that would fail as soon
as there were a user-installed device driver using a non-standard name.

Is there a DOS function which will let me determine this? Can the DOS
IOCTL function be used without opening the named file?

In a related question, given a file handle (file descriptor), is there
any way to determine the name of the file? Specifically, can I determine
which device file handle 0 (STDIN) refers to, i.e. CON or possibly COM1?
This would be required to implement a tty(1) command, to determine
whether access is local (via th CONsole), or remotely, via a COM port.

Thanks for any hints - please post or e-mail as you see fit.

Wolf Paul
ihnp4!killer!wnp

connery@bnrmtv.UUCP (Glenn Connery) (10/06/87)

In article <1738@killer.UUCP>, wnp@killer.UUCP (Wolf Paul) writes:
> In trying to implement a function which emulates the UNIX stat(2)
> system call I have run into a problem:
> 
> How can I determine whether a filename passed to a C function
> is a regular file or the name of a device?
> 

The DOS IOCTL call can be used to determine if a handle corresponds to
a device or a file.  Don't know how to determine this info without openning
the file I'm afraid (aside from something really gory).  For example the
following fragment will determine if the stdout handle is attached to
a file or a device...

 #include <stdio.h>
 #include <dos.h>
  
 /*
     IsConsole - determine if stdout is connected to console or not
  
     returns TRUE if stdout is the console, FALSE if not
 */
  
 int IsConsole()
 {
     union REGS r;
  
     r.x.ax = 0x4400;
     r.x.bx = 1;
     intdos(&r, &r);
     return((r.x.dx & 1) && (r.x.dx & 0x80));     /* isdev && iscin */
 }
 
-- 

Glenn Connery, Bell Northern Research, Mountain View, CA
{hplabs,amdahl,3comvax}!bnrmtv!connery