[comp.unix.programmer] ls & file pipes

avalon@coombs.anu.edu.au (avalon) (02/20/91)

G'day, can anyone tell me how 'ls' knows whether a
file is a regular file or a pipe ?

When i do a stat(2) call on the file, it is returned
as a regular file.  What tricks does ls perform ?

Also, a question about curses, whenever I call one of
the input routines, say getstr(), wscanw(), etc,
the program hangs while reading data in read(2).
I have called initscr() and various other curses calls
BEFORE getting to these (successfully too :/ ).
The only thing the program responds to after this is
manual sending of signals to the process :-(.
Do i need to make any *other* calls to get these
to work or is it a deeper, more serious problem ?
My environ. is a pyramid 9825, OSx5.1, BSD universe.

thanks,
-avalon

p.s. sorry for the crosspost but the curses question/
problem is still unresolved after posting to c.u.q.

mike (02/21/91)

In an article, coombs.anu.edu.au!avalon (avalon) writes:
>G'day, can anyone tell me how 'ls' knows whether a
>file is a regular file or a pipe ?
>
>When i do a stat(2) call on the file, it is returned
>as a regular file.  What tricks does ls perform ?

A file that is a named pipe shoould have S_IFIFO set in st_mode;
such as:

	if ( stat(file,&buf) != -1 && buf.st_mode & S_IFIFO )
		printf("%s is a pipe\n",file);

Cheers,
-- 
Michael Stefanik, MGI Inc., Los Angeles| Opinions stated are not even my own.
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
Remember folks: If you can't flame MS-DOS, then what _can_ you flame?

art@pilikia.pegasus.com (Art Neilson) (02/21/91)

In article <avalon.666979740@coombs> avalon@coombs.anu.edu.au (avalon) writes:
>G'day, can anyone tell me how 'ls' knows whether a
>file is a regular file or a pipe ?
>
>When i do a stat(2) call on the file, it is returned
>as a regular file.  What tricks does ls perform ?

Here's an example of testing if a file is a pipe or not:

	if (stat(pathname, &st) == -1) {
		perror("stat");
		exit(1);
	}

	if ((st.st_mode & S_IFMT) == S_IFIFO)
		printf("%s is a fifo\n", pathname);

-- 
Arthur W. Neilson III		| INET: art@pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pegasus!pilikia!art

rstevens@noao.edu (Rich Stevens) (02/21/91)

In article <471@bria> uunet!bria!mike writes:
>
>A file that is a named pipe shoould have S_IFIFO set in st_mode;
>such as:
>
>	if ( stat(file,&buf) != -1 && buf.st_mode & S_IFIFO )
                                      ^^^^^^^^^^^^^^^^^^^^^

You can't do this.  You *must* mask the st_mode member with S_IFMT
and then test for equality with the various file types.  Better yet,
use the POSIX-style S_ISxxx() macros.  The piece above should be

		((buf.st_mode & S_IFMT) == S_IFIFO)

For example, on SunOS "st_mode & S_IFREG" will be true for a regular file,
a sybolic link, and a socket.  Take a look at <sys.stat.h>.  Only 4 bits
are used to code 7 different values.

	Rich Stevens