]>
diplodocus.org Git - nmh/blob - uip/rcvtty.c
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
10 #include <h/rcvmail.h>
11 #include <h/scansbr.h>
12 #include <zotnet/tws/tws.h>
19 # define UTMP_FILE _PATH_UTMP
21 # define UTMP_FILE "/etc/utmp"
26 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
27 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
28 %{subject}%<{body}<<%{body}>>%>"
30 static struct swit switches
[] = {
34 { "form formatfile", 0 },
36 { "format string", 5 },
38 { "width columns", 0 },
56 static int newline
= 1;
59 static char *form
= NULL
;
60 static char *format
= NULL
;
65 char *getusername(void);
70 static RETSIGTYPE
alrmser (int);
71 static int message_fd (char **);
72 static int header_fd (void);
73 static void alert (char *, int);
77 main (int argc
, char **argv
)
80 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
81 char **argp
, **arguments
, *vec
[MAXARGS
];
86 setlocale(LC_ALL
, "");
88 invo_name
= r1bindex (argv
[0], '/');
90 /* read user profile/context */
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
108 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
109 print_help (buf
, switches
, 1);
112 print_version(invo_name
);
120 if (!(form
= *argp
++) || *form
== '-')
121 adios (NULL
, "missing argument to %s", argp
[-2]);
125 if (!(format
= *argp
++) || *format
== '-')
126 adios (NULL
, "missing argument to %s", argp
[-2]);
131 if (!(cp
= *argp
++) || *cp
== '-')
132 adios(NULL
, "missing argument to %s", argp
[-2]);
154 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
157 user
= getusername();
158 if ((uf
= fopen (UTMP_FILE
, "r")) == NULL
)
161 while (fread ((char *) &ut
, sizeof(ut
), 1, uf
) == 1)
162 if (ut
.ut_name
[0] != 0
163 && strncmp (user
, ut
.ut_name
, sizeof(ut
.ut_name
)) == 0) {
164 strncpy (tty
, ut
.ut_line
, sizeof(ut
.ut_line
));
176 #ifndef RELIABLE_SIGNALS
177 SIGNAL (SIGALRM
, alrmser
);
185 message_fd (char **vec
)
188 int bytes
, fd
, seconds
;
192 unlink (mktemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
))));
193 if ((fd
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == NOTOK
)
197 if ((child_id
= vfork()) == NOTOK
) {
201 } else if (child_id
) {
203 if (!setjmp (myctx
)) {
204 SIGNAL (SIGALRM
, alrmser
);
205 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
207 /* amount of time to wait depends on message size */
209 /* give at least 5 minutes */
211 } else if (bytes
>= 90000) {
212 /* but 30 minutes should be long enough */
215 seconds
= (bytes
/ 60) + 300;
217 alarm ((unsigned int) seconds
);
218 pidwait(child_id
, OK
);
221 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
225 * Ruthlessly kill the child and anything
226 * else in its process group.
228 KILLPG(child_id
, SIGKILL
);
236 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
239 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
240 execvp (vec
[0], vec
);
249 char *nfs
, tmpfil
[BUFSIZ
];
251 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
252 if ((fd
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == NOTOK
)
258 /* get new format string */
259 nfs
= new_fs (form
, format
, SCANFMT
);
260 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0);
262 write (fd
, "\n\r", 2);
263 write (fd
, scanl
, strlen (scanl
));
265 write (fd
, "\007", 1);
272 alert (char *tty
, int md
)
275 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
278 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
281 * The mask depends on whether we are checking for
282 * write permission based on `biff' or `mesg'.
284 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
285 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
288 if (!setjmp (myctx
)) {
289 SIGNAL (SIGALRM
, alrmser
);
291 td
= open (ttyspec
, O_WRONLY
);
300 lseek (md
, (off_t
) 0, SEEK_SET
);
302 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
303 if (write (td
, buffer
, i
) != i
)