+setfile(name)
+ char *name;
+{
+ FILE *ibuf;
+ int i;
+ struct stat stb;
+ char isedit = *name != '%';
+ char *who = name[1] ? name + 1 : myname;
+ static int shudclob;
+ extern char tempMesg[];
+ extern int errno;
+
+ if ((name = expand(name)) == NOSTR)
+ return -1;
+
+ if ((ibuf = Fopen(name, "r")) == NULL) {
+ if (!isedit && errno == ENOENT)
+ goto nomail;
+ perror(name);
+ return(-1);
+ }
+
+ if (fstat(fileno(ibuf), &stb) < 0) {
+ perror("fstat");
+ Fclose(ibuf);
+ return (-1);
+ }
+
+ switch (stb.st_mode & S_IFMT) {
+ case S_IFDIR:
+ Fclose(ibuf);
+ errno = EISDIR;
+ perror(name);
+ return (-1);
+
+ case S_IFREG:
+ break;
+
+ default:
+ Fclose(ibuf);
+ errno = EINVAL;
+ perror(name);
+ return (-1);
+ }
+
+ /*
+ * Looks like all will be well. We must now relinquish our
+ * hold on the current set of stuff. Must hold signals
+ * while we are reading the new file, else we will ruin
+ * the message[] data structure.
+ */
+
+ holdsigs();
+ if (shudclob)
+ quit();
+
+ /*
+ * Copy the messages into /tmp
+ * and set pointers.
+ */
+
+ readonly = 0;
+ if ((i = open(name, 1)) < 0)
+ readonly++;
+ else
+ close(i);
+ if (shudclob) {
+ fclose(itf);
+ fclose(otf);
+ }
+ shudclob = 1;
+ edit = isedit;
+ strcpy(prevfile, mailname);
+ if (name != mailname)
+ strcpy(mailname, name);
+ mailsize = fsize(ibuf);
+ if ((otf = fopen(tempMesg, "w")) == NULL) {
+ perror(tempMesg);
+ exit(1);
+ }
+ if ((itf = fopen(tempMesg, "r")) == NULL) {
+ perror(tempMesg);
+ exit(1);
+ }
+ remove(tempMesg);
+ setptr(ibuf);
+ setmsize(msgCount);
+ Fclose(ibuf);
+ relsesigs();
+ sawcom = 0;
+ if (!edit && msgCount == 0) {
+nomail:
+ fprintf(stderr, "No mail for %s\n", who);
+ return -1;
+ }
+ return(0);
+}