]>
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
));
170 return 0; /* dead code to satisfy the compiler */
177 #ifndef RELIABLE_SIGNALS
178 SIGNAL (SIGALRM
, alrmser
);
186 message_fd (char **vec
)
189 int bytes
, fd
, seconds
;
194 fd
= mkstemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
)));
196 unlink (mktemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
))));
197 if ((fd
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == NOTOK
)
202 if ((child_id
= vfork()) == NOTOK
) {
206 } else if (child_id
) {
208 if (!setjmp (myctx
)) {
209 SIGNAL (SIGALRM
, alrmser
);
210 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
212 /* amount of time to wait depends on message size */
214 /* give at least 5 minutes */
216 } else if (bytes
>= 90000) {
217 /* but 30 minutes should be long enough */
220 seconds
= (bytes
/ 60) + 300;
222 alarm ((unsigned int) seconds
);
223 pidwait(child_id
, OK
);
226 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
230 * Ruthlessly kill the child and anything
231 * else in its process group.
233 KILLPG(child_id
, SIGKILL
);
241 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
244 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
245 execvp (vec
[0], vec
);
247 return 1; /* dead code to satisfy compiler */
255 char *nfs
, tmpfil
[BUFSIZ
];
257 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
258 if ((fd
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == NOTOK
)
264 /* get new format string */
265 nfs
= new_fs (form
, format
, SCANFMT
);
266 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0);
268 write (fd
, "\n\r", 2);
269 write (fd
, scanl
, strlen (scanl
));
271 write (fd
, "\007", 1);
278 alert (char *tty
, int md
)
281 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
284 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
287 * The mask depends on whether we are checking for
288 * write permission based on `biff' or `mesg'.
290 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
291 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
294 if (!setjmp (myctx
)) {
295 SIGNAL (SIGALRM
, alrmser
);
297 td
= open (ttyspec
, O_WRONLY
);
306 lseek (md
, (off_t
) 0, SEEK_SET
);
308 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
309 if (write (td
, buffer
, i
) != i
)