]>
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 extern m_getfld_state_t gstate
;
36 static struct swit switches
[] = {
40 { "form formatfile", 0 },
42 { "format string", 5 },
44 { "width columns", 0 },
62 static int newline
= 1;
65 static char *form
= NULL
;
66 static char *format
= NULL
;
71 char *getusername(void);
76 static void alrmser (int);
77 static int message_fd (char **);
78 static int header_fd (void);
79 static void alert (char *, int);
83 main (int argc
, char **argv
)
86 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
87 char **argp
, **arguments
, *vec
[MAXARGS
];
90 setlocale(LC_ALL
, "");
92 invo_name
= r1bindex (argv
[0], '/');
94 /* read user profile/context */
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
101 while ((cp
= *argp
++)) {
103 switch (smatch (++cp
, switches
)) {
105 ambigsw (cp
, switches
);
112 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
113 print_help (buf
, switches
, 1);
116 print_version(invo_name
);
124 if (!(form
= *argp
++) || *form
== '-')
125 adios (NULL
, "missing argument to %s", argp
[-2]);
129 if (!(format
= *argp
++) || *format
== '-')
130 adios (NULL
, "missing argument to %s", argp
[-2]);
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios(NULL
, "missing argument to %s", argp
[-2]);
158 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
161 user
= getusername();
165 while ((utp
= getutxent()) != NULL
) {
166 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
167 && utp
->ut_line
[0] != 0
168 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
169 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
174 #endif /* HAVE_GETUTXENT */
177 return 0; /* dead code to satisfy the compiler */
191 message_fd (char **vec
)
194 int bytes
, fd
, seconds
;
198 fd
= mkstemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
)));
201 if ((child_id
= vfork()) == NOTOK
) {
205 } else if (child_id
) {
207 if (!setjmp (myctx
)) {
208 SIGNAL (SIGALRM
, alrmser
);
209 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
211 /* amount of time to wait depends on message size */
213 /* give at least 5 minutes */
215 } else if (bytes
>= 90000) {
216 /* but 30 minutes should be long enough */
219 seconds
= (bytes
/ 60) + 300;
221 alarm ((unsigned int) seconds
);
222 pidwait(child_id
, OK
);
225 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
229 * Ruthlessly kill the child and anything
230 * else in its process group.
232 killpg(child_id
, SIGKILL
);
240 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
243 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
244 execvp (vec
[0], vec
);
246 return 1; /* dead code to satisfy compiler */
257 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
258 if (tfile
== NULL
) return NOTOK
;
263 /* get new format string */
264 nfs
= new_fs (form
, format
, SCANFMT
);
265 m_getfld_state_init (&gstate
);
266 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0);
267 m_getfld_state_destroy (&gstate
);
269 write (fd
, "\n\r", 2);
270 write (fd
, scanl
, strlen (scanl
));
272 write (fd
, "\007", 1);
279 alert (char *tty
, int md
)
282 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
285 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
288 * The mask depends on whether we are checking for
289 * write permission based on `biff' or `mesg'.
291 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
292 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
295 if (!setjmp (myctx
)) {
296 SIGNAL (SIGALRM
, alrmser
);
298 td
= open (ttyspec
, O_WRONLY
);
307 lseek (md
, (off_t
) 0, SEEK_SET
);
309 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
310 if (write (td
, buffer
, i
) != i
)