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