]>
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>
26 #endif /* HAVE_GETUTXENT */
29 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
30 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
31 %{subject}%<{body}<<%{body}>>%>"
33 #define RCVTTY_SWITCHES \
34 X("biff", 0, BIFFSW) \
35 X("form formatfile", 0, FORMSW) \
36 X("format string", 5, FMTSW) \
37 X("width columns", 0, WIDTHSW) \
38 X("newline", 0, NLSW) \
39 X("nonewline", 0, NNLSW) \
41 X("nobell", 0, NBELSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(RCVTTY
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
55 static int newline
= 1;
58 static char *form
= NULL
;
59 static char *format
= NULL
;
64 char *getusername(void);
69 static void alrmser (int);
70 static int message_fd (char **);
71 static int header_fd (void);
72 static void alert (char *, int);
76 main (int argc
, char **argv
)
79 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
80 char **argp
, **arguments
, *vec
[MAXARGS
];
83 setlocale(LC_ALL
, "");
85 invo_name
= r1bindex (argv
[0], '/');
87 /* read user profile/context */
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
105 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
117 if (!(form
= *argp
++) || *form
== '-')
118 adios (NULL
, "missing argument to %s", argp
[-2]);
122 if (!(format
= *argp
++) || *format
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
128 if (!(cp
= *argp
++) || *cp
== '-')
129 adios(NULL
, "missing argument to %s", argp
[-2]);
151 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
154 user
= getusername();
158 while ((utp
= getutxent()) != NULL
) {
159 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
160 && utp
->ut_line
[0] != 0
161 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
162 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
167 #endif /* HAVE_GETUTXENT */
183 message_fd (char **vec
)
186 int bytes
, fd
, seconds
;
190 fd
= mkstemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
)));
193 if ((child_id
= fork()) == NOTOK
) {
197 } else if (child_id
) {
199 if (!setjmp (myctx
)) {
200 SIGNAL (SIGALRM
, alrmser
);
201 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
203 /* amount of time to wait depends on message size */
205 /* give at least 5 minutes */
207 } else if (bytes
>= 90000) {
208 /* but 30 minutes should be long enough */
211 seconds
= (bytes
/ 60) + 300;
213 alarm ((unsigned int) seconds
);
214 pidwait(child_id
, OK
);
217 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
221 * Ruthlessly kill the child and anything
222 * else in its process group.
224 killpg(child_id
, SIGKILL
);
232 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
235 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
236 execvp (vec
[0], vec
);
248 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
249 if (tfile
== NULL
) return NOTOK
;
254 /* get new format string */
255 nfs
= new_fs (form
, format
, SCANFMT
);
256 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0);
259 write (fd
, "\n\r", 2);
260 write (fd
, scanl
, strlen (scanl
));
262 write (fd
, "\007", 1);
269 alert (char *tty
, int md
)
272 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
275 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
278 * The mask depends on whether we are checking for
279 * write permission based on `biff' or `mesg'.
281 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
282 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
285 if (!setjmp (myctx
)) {
286 SIGNAL (SIGALRM
, alrmser
);
288 td
= open (ttyspec
, O_WRONLY
);
297 lseek (md
, (off_t
) 0, SEEK_SET
);
299 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
300 if (write (td
, buffer
, i
) != i
)