1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)fio.c 8.1 (Berkeley) 6/6/93"; 36 #endif /* not lint */ 37 38 #include "rcv.h" 39 #include <sys/file.h> 40 #include <sys/wait.h> 41 42 #include <unistd.h> 43 #include <paths.h> 44 #include <errno.h> 45 #include "extern.h" 46 47 /* 48 * Mail -- a mail program 49 * 50 * File I/O. 51 */ 52 53 /* 54 * Set up the input pointers while copying the mail file into /tmp. 55 */ 56 void 57 setptr(ibuf) 58 register FILE *ibuf; 59 { 60 extern char *tmpdir; 61 register int c, count; 62 register char *cp, *cp2; 63 struct message this; 64 FILE *mestmp; 65 off_t offset; 66 int maybe, inhead; 67 char linebuf[LINESIZE]; 68 69 /* Get temporary file. */ 70 (void)sprintf(linebuf, "%s/mail.XXXXXX", tmpdir); 71 if ((c = mkstemp(linebuf)) == -1 || 72 (mestmp = Fdopen(c, "r+")) == NULL) { 73 (void)fprintf(stderr, "mail: can't open %s\n", linebuf); 74 exit(1); 75 } 76 (void)unlink(linebuf); 77 78 msgCount = 0; 79 maybe = 1; 80 inhead = 0; 81 offset = 0; 82 this.m_flag = MUSED|MNEW; 83 this.m_size = 0; 84 this.m_lines = 0; 85 this.m_block = 0; 86 this.m_offset = 0; 87 for (;;) { 88 if (fgets(linebuf, LINESIZE, ibuf) == NULL) { 89 if (append(&this, mestmp)) { 90 perror("temporary file"); 91 exit(1); 92 } 93 makemessage(mestmp); 94 return; 95 } 96 count = strlen(linebuf); 97 (void) fwrite(linebuf, sizeof *linebuf, count, otf); 98 if (ferror(otf)) { 99 perror("/tmp"); 100 exit(1); 101 } 102 linebuf[count - 1] = 0; 103 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) { 104 msgCount++; 105 if (append(&this, mestmp)) { 106 perror("temporary file"); 107 exit(1); 108 } 109 this.m_flag = MUSED|MNEW; 110 this.m_size = 0; 111 this.m_lines = 0; 112 this.m_block = blockof(offset); 113 this.m_offset = offsetof(offset); 114 inhead = 1; 115 } else if (linebuf[0] == 0) { 116 inhead = 0; 117 } else if (inhead) { 118 for (cp = linebuf, cp2 = "status";; cp++) { 119 if ((c = *cp2++) == 0) { 120 while (isspace(*cp++)) 121 ; 122 if (cp[-1] != ':') 123 break; 124 while (c = *cp++) 125 if (c == 'R') 126 this.m_flag |= MREAD; 127 else if (c == 'O') 128 this.m_flag &= ~MNEW; 129 inhead = 0; 130 break; 131 } 132 if (*cp != c && *cp != toupper(c)) 133 break; 134 } 135 } 136 offset += count; 137 this.m_size += count; 138 this.m_lines++; 139 maybe = linebuf[0] == 0; 140 } 141 } 142 143 /* 144 * Drop the passed line onto the passed output buffer. 145 * If a write error occurs, return -1, else the count of 146 * characters written, including the newline. 147 */ 148 int 149 putline(obuf, linebuf) 150 FILE *obuf; 151 char *linebuf; 152 { 153 register int c; 154 155 c = strlen(linebuf); 156 (void) fwrite(linebuf, sizeof *linebuf, c, obuf); 157 (void) putc('\n', obuf); 158 if (ferror(obuf)) 159 return (-1); 160 return (c + 1); 161 } 162 163 /* 164 * Read up a line from the specified input into the line 165 * buffer. Return the number of characters read. Do not 166 * include the newline at the end. 167 */ 168 int 169 readline(ibuf, linebuf, linesize) 170 FILE *ibuf; 171 char *linebuf; 172 int linesize; 173 { 174 register int n; 175 176 clearerr(ibuf); 177 if (fgets(linebuf, linesize, ibuf) == NULL) 178 return -1; 179 n = strlen(linebuf); 180 if (n > 0 && linebuf[n - 1] == '\n') 181 linebuf[--n] = '\0'; 182 return n; 183 } 184 185 /* 186 * Return a file buffer all ready to read up the 187 * passed message pointer. 188 */ 189 FILE * 190 setinput(mp) 191 register struct message *mp; 192 { 193 194 fflush(otf); 195 if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) { 196 perror("fseek"); 197 panic("temporary file seek"); 198 } 199 return (itf); 200 } 201 202 /* 203 * Take the data out of the passed ghost file and toss it into 204 * a dynamically allocated message structure. 205 */ 206 void 207 makemessage(f) 208 FILE *f; 209 { 210 register size = (msgCount + 1) * sizeof (struct message); 211 212 if (message != 0) 213 free((char *) message); 214 if ((message = (struct message *) malloc((unsigned) size)) == 0) 215 panic("Insufficient memory for %d messages", msgCount); 216 dot = message; 217 size -= sizeof (struct message); 218 fflush(f); 219 (void) lseek(fileno(f), (off_t)sizeof *message, 0); 220 if (read(fileno(f), (char *) message, size) != size) 221 panic("Message temporary file corrupted"); 222 message[msgCount].m_size = 0; 223 message[msgCount].m_lines = 0; 224 Fclose(f); 225 } 226 227 /* 228 * Append the passed message descriptor onto the temp file. 229 * If the write fails, return 1, else 0 230 */ 231 int 232 append(mp, f) 233 struct message *mp; 234 FILE *f; 235 { 236 return fwrite((char *) mp, sizeof *mp, 1, f) != 1; 237 } 238 239 /* 240 * Delete a file, but only if the file is a plain file. 241 */ 242 int 243 rm(name) 244 char *name; 245 { 246 struct stat sb; 247 248 if (stat(name, &sb) < 0) 249 return(-1); 250 if (!S_ISREG(sb.st_mode)) { 251 errno = EISDIR; 252 return(-1); 253 } 254 return(unlink(name)); 255 } 256 257 static int sigdepth; /* depth of holdsigs() */ 258 static int omask; 259 /* 260 * Hold signals SIGHUP, SIGINT, and SIGQUIT. 261 */ 262 void 263 holdsigs() 264 { 265 266 if (sigdepth++ == 0) 267 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)); 268 } 269 270 /* 271 * Release signals SIGHUP, SIGINT, and SIGQUIT. 272 */ 273 void 274 relsesigs() 275 { 276 277 if (--sigdepth == 0) 278 sigsetmask(omask); 279 } 280 281 /* 282 * Determine the size of the file possessed by 283 * the passed buffer. 284 */ 285 off_t 286 fsize(iob) 287 FILE *iob; 288 { 289 struct stat sbuf; 290 291 if (fstat(fileno(iob), &sbuf) < 0) 292 return 0; 293 return sbuf.st_size; 294 } 295 296 /* 297 * Evaluate the string given as a new mailbox name. 298 * Supported meta characters: 299 * % for my system mail box 300 * %user for user's system mail box 301 * # for previous file 302 * & invoker's mbox file 303 * +file file in folder directory 304 * any shell meta character 305 * Return the file name as a dynamic string. 306 */ 307 char * 308 expand(name) 309 register char *name; 310 { 311 char xname[PATHSIZE]; 312 char cmdbuf[PATHSIZE]; /* also used for file names */ 313 register int pid, l; 314 register char *cp, *shell; 315 int pivec[2]; 316 struct stat sbuf; 317 extern union wait wait_status; 318 319 /* 320 * The order of evaluation is "%" and "#" expand into constants. 321 * "&" can expand into "+". "+" can expand into shell meta characters. 322 * Shell meta characters expand into constants. 323 * This way, we make no recursive expansion. 324 */ 325 switch (*name) { 326 case '%': 327 findmail(name[1] ? name + 1 : myname, xname); 328 return savestr(xname); 329 case '#': 330 if (name[1] != 0) 331 break; 332 if (prevfile[0] == 0) { 333 printf("No previous file\n"); 334 return NOSTR; 335 } 336 return savestr(prevfile); 337 case '&': 338 if (name[1] == 0 && (name = value("MBOX")) == NOSTR) 339 name = "~/mbox"; 340 /* fall through */ 341 } 342 if (name[0] == '+' && getfold(cmdbuf) >= 0) { 343 sprintf(xname, "%s/%s", cmdbuf, name + 1); 344 name = savestr(xname); 345 } 346 /* catch the most common shell meta character */ 347 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) { 348 sprintf(xname, "%s%s", homedir, name + 1); 349 name = savestr(xname); 350 } 351 if (!anyof(name, "~{[*?$`'\"\\")) 352 return name; 353 if (pipe(pivec) < 0) { 354 perror("pipe"); 355 return name; 356 } 357 sprintf(cmdbuf, "echo %s", name); 358 if ((shell = value("SHELL")) == NOSTR) 359 shell = _PATH_CSHELL; 360 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR); 361 if (pid < 0) { 362 close(pivec[0]); 363 close(pivec[1]); 364 return NOSTR; 365 } 366 close(pivec[1]); 367 l = read(pivec[0], xname, BUFSIZ); 368 close(pivec[0]); 369 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) { 370 fprintf(stderr, "\"%s\": Expansion failed.\n", name); 371 return NOSTR; 372 } 373 if (l < 0) { 374 perror("read"); 375 return NOSTR; 376 } 377 if (l == 0) { 378 fprintf(stderr, "\"%s\": No match.\n", name); 379 return NOSTR; 380 } 381 if (l == BUFSIZ) { 382 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name); 383 return NOSTR; 384 } 385 xname[l] = 0; 386 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--) 387 ; 388 cp[1] = '\0'; 389 if (index(xname, ' ') && stat(xname, &sbuf) < 0) { 390 fprintf(stderr, "\"%s\": Ambiguous.\n", name); 391 return NOSTR; 392 } 393 return savestr(xname); 394 } 395 396 /* 397 * Determine the current folder directory name. 398 */ 399 int 400 getfold(name) 401 char *name; 402 { 403 char *folder; 404 405 if ((folder = value("folder")) == NOSTR) 406 return (-1); 407 if (*folder == '/') 408 strcpy(name, folder); 409 else 410 sprintf(name, "%s/%s", homedir, folder); 411 return (0); 412 } 413 414 /* 415 * Return the name of the dead.letter file. 416 */ 417 char * 418 getdeadletter() 419 { 420 register char *cp; 421 422 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR) 423 cp = expand("~/dead.letter"); 424 else if (*cp != '/') { 425 char buf[PATHSIZE]; 426 427 (void) sprintf(buf, "~/%s", cp); 428 cp = expand(buf); 429 } 430 return cp; 431 } 432