]>
diplodocus.org Git - nmh/blob - uip/rcvtty.c
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
10 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
11 * a number of other things also exist. Please check.
12 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
16 #include <h/signals.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
27 #endif /* HAVE_GETUTXENT */
30 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
31 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
32 %{subject}%<{body}<<%{body}>>%>"
34 static struct swit switches
[] = {
38 { "form formatfile", 0 },
40 { "format string", 5 },
42 { "width columns", 0 },
60 static int newline
= 1;
63 static char *form
= NULL
;
64 static char *format
= NULL
;
69 char *getusername(void);
74 static void alrmser (int);
75 static int message_fd (char **);
76 static int header_fd (void);
77 static void alert (char *, int);
81 main (int argc
, char **argv
)
84 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
85 char **argp
, **arguments
, *vec
[MAXARGS
];
88 setlocale(LC_ALL
, "");
90 invo_name
= r1bindex (argv
[0], '/');
92 /* read user profile/context */
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++)) {
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
110 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
122 if (!(form
= *argp
++) || *form
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
127 if (!(format
= *argp
++) || *format
== '-')
128 adios (NULL
, "missing argument to %s", argp
[-2]);
133 if (!(cp
= *argp
++) || *cp
== '-')
134 adios(NULL
, "missing argument to %s", argp
[-2]);
156 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
159 user
= getusername();
163 while ((utp
= getutxent()) != NULL
) {
164 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
165 && utp
->ut_line
[0] != 0
166 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
167 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
172 #endif /* HAVE_GETUTXENT */
175 return 0; /* dead code to satisfy the compiler */
189 message_fd (char **vec
)
192 int bytes
, fd
, seconds
;
196 fd
= mkstemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
)));
199 if ((child_id
= vfork()) == NOTOK
) {
203 } else if (child_id
) {
205 if (!setjmp (myctx
)) {
206 SIGNAL (SIGALRM
, alrmser
);
207 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
209 /* amount of time to wait depends on message size */
211 /* give at least 5 minutes */
213 } else if (bytes
>= 90000) {
214 /* but 30 minutes should be long enough */
217 seconds
= (bytes
/ 60) + 300;
219 alarm ((unsigned int) seconds
);
220 pidwait(child_id
, OK
);
223 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
227 * Ruthlessly kill the child and anything
228 * else in its process group.
230 killpg(child_id
, SIGKILL
);
238 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
241 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
242 execvp (vec
[0], vec
);
244 return 1; /* dead code to satisfy compiler */
255 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
256 if (tfile
== NULL
) return NOTOK
;
261 /* get new format string */
262 nfs
= new_fs (form
, format
, SCANFMT
);
263 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0);
265 write (fd
, "\n\r", 2);
266 write (fd
, scanl
, strlen (scanl
));
268 write (fd
, "\007", 1);
275 alert (char *tty
, int md
)
278 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
281 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
284 * The mask depends on whether we are checking for
285 * write permission based on `biff' or `mesg'.
287 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
288 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
291 if (!setjmp (myctx
)) {
292 SIGNAL (SIGALRM
, alrmser
);
294 td
= open (ttyspec
, O_WRONLY
);
303 lseek (md
, (off_t
) 0, SEEK_SET
);
305 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
306 if (write (td
, buffer
, i
) != i
)