]>
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>
17 #include <h/rcvmail.h>
18 #include <h/scansbr.h>
29 # define UTMP_FILE _PATH_UTMP
31 # define UTMP_FILE "/etc/utmp"
37 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
38 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
39 %{subject}%<{body}<<%{body}>>%>"
41 static struct swit switches
[] = {
45 { "form formatfile", 0 },
47 { "format string", 5 },
49 { "width columns", 0 },
67 static int newline
= 1;
70 static char *form
= NULL
;
71 static char *format
= NULL
;
76 char *getusername(void);
81 static RETSIGTYPE
alrmser (int);
82 static int message_fd (char **);
83 static int header_fd (void);
84 static void alert (char *, int);
88 main (int argc
, char **argv
)
91 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
92 char **argp
, **arguments
, *vec
[MAXARGS
];
101 setlocale(LC_ALL
, "");
103 invo_name
= r1bindex (argv
[0], '/');
105 /* read user profile/context */
108 mts_init (invo_name
);
109 arguments
= getarguments (invo_name
, argc
, argv
, 1);
112 while ((cp
= *argp
++)) {
114 switch (smatch (++cp
, switches
)) {
116 ambigsw (cp
, switches
);
123 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
124 print_help (buf
, switches
, 1);
127 print_version(invo_name
);
135 if (!(form
= *argp
++) || *form
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
140 if (!(format
= *argp
++) || *format
== '-')
141 adios (NULL
, "missing argument to %s", argp
[-2]);
146 if (!(cp
= *argp
++) || *cp
== '-')
147 adios(NULL
, "missing argument to %s", argp
[-2]);
169 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
172 user
= getusername();
176 while ((utp
= getutent()) != NULL
) {
178 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
179 utp
->ut_type
== USER_PROCESS
183 && utp
->ut_line
[0] != 0
184 && strncmp (user
, utp
->ut_name
, sizeof(utp
->ut_name
)) == 0) {
185 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
191 if ((uf
= fopen (UTMP_FILE
, "r")) == NULL
)
193 while (fread ((char *) &ut
, sizeof(ut
), 1, uf
) == 1)
194 if (ut
.ut_name
[0] != 0
195 && strncmp (user
, ut
.ut_name
, sizeof(ut
.ut_name
)) == 0) {
196 strncpy (tty
, ut
.ut_line
, sizeof(ut
.ut_line
));
203 return 0; /* dead code to satisfy the compiler */
210 #ifndef RELIABLE_SIGNALS
211 SIGNAL (SIGALRM
, alrmser
);
219 message_fd (char **vec
)
222 int bytes
, fd
, seconds
;
226 fd
= mkstemp (strncpy (tmpfil
, "/tmp/rcvttyXXXXX", sizeof(tmpfil
)));
229 if ((child_id
= vfork()) == NOTOK
) {
233 } else if (child_id
) {
235 if (!setjmp (myctx
)) {
236 SIGNAL (SIGALRM
, alrmser
);
237 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
239 /* amount of time to wait depends on message size */
241 /* give at least 5 minutes */
243 } else if (bytes
>= 90000) {
244 /* but 30 minutes should be long enough */
247 seconds
= (bytes
/ 60) + 300;
249 alarm ((unsigned int) seconds
);
250 pidwait(child_id
, OK
);
253 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
257 * Ruthlessly kill the child and anything
258 * else in its process group.
260 killpg(child_id
, SIGKILL
);
268 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
271 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
272 execvp (vec
[0], vec
);
274 return 1; /* dead code to satisfy compiler */
285 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
286 if (tfile
== NULL
) return NOTOK
;
291 /* get new format string */
292 nfs
= new_fs (form
, format
, SCANFMT
);
293 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0);
295 write (fd
, "\n\r", 2);
296 write (fd
, scanl
, strlen (scanl
));
298 write (fd
, "\007", 1);
305 alert (char *tty
, int md
)
308 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
311 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
314 * The mask depends on whether we are checking for
315 * write permission based on `biff' or `mesg'.
317 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
318 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
321 if (!setjmp (myctx
)) {
322 SIGNAL (SIGALRM
, alrmser
);
324 td
= open (ttyspec
, O_WRONLY
);
333 lseek (md
, (off_t
) 0, SEEK_SET
);
335 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
336 if (write (td
, buffer
, i
) != i
)