]>
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 #define RCVTTY_SWITCHES \
35 X("biff", 0, BIFFSW) \
36 X("form formatfile", 0, FORMSW) \
37 X("format string", 5, FMTSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("newline", 0, NLSW) \
40 X("nonewline", 0, NNLSW) \
42 X("nobell", 0, NBELSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(RCVTTY
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
56 static int newline
= 1;
59 static char *form
= NULL
;
60 static char *format
= NULL
;
65 char *getusername(void);
70 static void 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
];
84 setlocale(LC_ALL
, "");
86 invo_name
= r1bindex (argv
[0], '/');
88 /* read user profile/context */
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
106 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
118 if (!(form
= *argp
++) || *form
== '-')
119 adios (NULL
, "missing argument to %s", argp
[-2]);
123 if (!(format
= *argp
++) || *format
== '-')
124 adios (NULL
, "missing argument to %s", argp
[-2]);
129 if (!(cp
= *argp
++) || *cp
== '-')
130 adios(NULL
, "missing argument to %s", argp
[-2]);
152 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
155 user
= getusername();
159 while ((utp
= getutxent()) != NULL
) {
160 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
161 && utp
->ut_line
[0] != 0
162 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
163 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
168 #endif /* HAVE_GETUTXENT */
171 return 0; /* dead code to satisfy the compiler */
185 message_fd (char **vec
)
188 int bytes
, fd
, seconds
;
192 fd
= mkstemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
)));
195 if ((child_id
= fork()) == NOTOK
) {
199 } else if (child_id
) {
201 if (!setjmp (myctx
)) {
202 SIGNAL (SIGALRM
, alrmser
);
203 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
205 /* amount of time to wait depends on message size */
207 /* give at least 5 minutes */
209 } else if (bytes
>= 90000) {
210 /* but 30 minutes should be long enough */
213 seconds
= (bytes
/ 60) + 300;
215 alarm ((unsigned int) seconds
);
216 pidwait(child_id
, OK
);
219 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
223 * Ruthlessly kill the child and anything
224 * else in its process group.
226 killpg(child_id
, SIGKILL
);
234 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
237 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
238 execvp (vec
[0], vec
);
240 return 1; /* dead code to satisfy compiler */
251 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
252 if (tfile
== NULL
) return NOTOK
;
257 /* get new format string */
258 nfs
= new_fs (form
, format
, SCANFMT
);
259 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
)