Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

696 Te rminal I/O Chapter 18


When we run the program from Figure18.14, we get the following output:
$./a.out
fd 0: tty
fd 1: tty
fd 2: tty
$./a.out </etc/passwd 2>/dev/null
fd 0: not a tty
fd 1: tty
fd 2: not a tty

Example —ttynameFunction


Thettynamefunction (Figure18.15) is longer, as wehave to search all the device
entries, looking for a match.
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
struct devdir {
struct devdir *d_next;
char *d_name;
};
static struct devdir *head;
static struct devdir *tail;
static char pathname[_POSIX_PATH_MAX + 1];

static void
add(char *dirname)
{
struct devdir *ddp;
int len;

len = strlen(dirname);
/*
*Skip ., .., and /dev/fd.
*/
if ((dirname[len-1] == ’.’) && (dirname[len-2] == ’/’ ||
(dirname[len-2] == ’.’ && dirname[len-3] == ’/’)))
return;
if (strcmp(dirname, "/dev/fd") == 0)
return;
if ((ddp = malloc(sizeof(struct devdir))) == NULL)
return;
Free download pdf