Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / ttym.c
1 /* ttym.c - miscellaneous routines */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: ttym.c,v 1.6 1992/02/03 17:57:22 jromine Exp $";
4 #endif  lint
5
6 #include <pwd.h>
7 #ifndef NSIG
8 #include <signal.h>
9 #endif  NSIG
10 #include <sys/time.h>
11
12 static int  ttyf ();
13
14 /* \f */
15
16 static  ttym (fd, command, line, user, vec)
17 int     fd;
18 char   *command,
19        *line,
20        *user,
21       **vec;
22 {
23     TYPESIG     (*pstat) ();
24     char   *ap,
25            *term,
26            *myself,
27            *getlogin (),
28            *rindex (),
29            *ttyname ();
30     struct passwd  *pw;
31 #ifndef __STDC__
32 #ifdef  SYS5
33     struct passwd  *getpwuid ();
34 #endif
35 #endif /* !__STDC__ */
36
37     if ((term = ap = ttyname (2)) && (term = rindex (term, '/')))
38         term++;
39     if (term == NULL || *term == NULL)
40         term = ap;
41     if ((myself = getlogin ()) == NULL || *myself == NULL)
42         myself = (pw = getpwuid (getuid ())) ? pw -> pw_name : NULL;
43
44     pstat = signal (SIGPIPE, SIG_IGN);
45     (void) write (fd, command, strlen (command));
46     (void) write (fd, "", 1);
47
48     if (term)
49         (void) write (fd, term, strlen (term));
50     (void) write (fd, "", 1);
51
52     if (myself)
53         (void) write (fd, myself, strlen (myself));
54     (void) write (fd, "", 1);
55
56     if (line && *line)
57         (void) write (fd, line, strlen (line));
58     (void) write (fd, "", 1);
59
60     if (user && *user)
61         (void) write (fd, user, strlen (user));
62     (void) write (fd, "", 1);
63
64     if (vec)
65         while (ap = *vec++) {
66             (void) write (fd, ap, strlen (ap));
67             (void) write (fd, "", 1);
68         }
69
70     (void) write (fd, "", 1);
71     (void) signal (SIGPIPE, pstat);
72 }
73
74 /* \f */
75
76 static int  ttyv (fd)
77 int     fd;
78 {
79     int     ifds,
80             nbits;
81     char    c;
82     struct timeval tv;
83
84     ifds = 1 << fd;
85     nbits = getdtablesize();
86     tv.tv_sec = SMLWAIT;
87     tv.tv_usec = 0;
88     if (select (nbits, &ifds, (int *) 0, (int *) 0, &tv) <= 0
89             || read (fd, &c, 1) != 1)
90         return NOTOK;
91     if (c == NULL)
92         return fd;
93     putc (c, stderr);
94
95     (void) ttyf (fd, stderr);
96     return NOTOK;
97 }
98
99
100 static int  ttyf (fd, f)
101 int     fd;
102 FILE * f;
103 {
104     int     i;
105     char    buffer[BUFSIZ];
106
107     while ((i = read (fd, buffer, sizeof buffer)) > 0)
108         (void) fwrite (buffer, sizeof (char), i, f);
109     return i;
110 }