/* * Read the file "fname" into the current buffer. Make all of the text * in the buffer go away, after checking for unsaved changes. This is * called by the "read" command, the "visit" command, and the mainline * (for "mg file"). */ int readin(char *fname) { struct mgwin *wp; int status, i, ro = FALSE; PF *ael; /* might be old */ if (bclear(curbp) != TRUE) return (TRUE); /* Clear readonly. May be set by autoexec path */ curbp->b_flag &= ~BFREADONLY; if ((status = insertfile(fname, fname, TRUE)) != TRUE) { ewprintf("File is not readable: %s", fname); return (FALSE); } for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { if (wp->w_bufp == curbp) { if ((fisdir(fname)) != TRUE) { wp->w_dotp = wp->w_linep = bfirstlp(curbp); wp->w_doto = 0; wp->w_markp = NULL; wp->w_marko = 0; } } } /* * Call auto-executing function if we need to. */ if ((ael = find_autoexec(fname)) != NULL) { for (i = 0; ael[i] != NULL; i++) (*ael[i])(0, 1); free(ael); } /* no change */ curbp->b_flag &= ~BFCHG; /* * We need to set the READONLY flag after we insert the file, * unless the file is a directory. */ if (access(fname, W_OK) && errno != ENOENT) ro = TRUE; if (fisdir(fname) == TRUE) ro = TRUE; if (ro == TRUE) curbp->b_flag |= BFREADONLY; if (startrow) { gotoline(FFARG, startrow); startrow = 0; } undo_add_modified(); return (status); }
/* ARGSUSED */ int filewrite(int f, int n) { struct stat statbuf; int s; char fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25]; char *adjfname, *bufp; FILE *ffp; if (getbufcwd(fname, sizeof(fname)) != TRUE) fname[0] = '\0'; if ((bufp = eread("Write file: ", fname, NFILEN, EFDEF | EFNEW | EFCR | EFFILE)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); adjfname = adjustname(fname, TRUE); if (adjfname == NULL) return (FALSE); /* Check if file exists; write checks done later */ if (stat(adjfname, &statbuf) == 0) { if (S_ISDIR(statbuf.st_mode)) { dobeep(); ewprintf("%s is a directory", adjfname); return (FALSE); } snprintf(tmp, sizeof(tmp), "File `%s' exists; overwrite", adjfname); if ((s = eyorn(tmp)) != TRUE) return (s); } /* old attributes are no longer current */ bzero(&curbp->b_fi, sizeof(curbp->b_fi)); if ((s = writeout(&ffp, curbp, adjfname)) == TRUE) { (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname)); if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE) (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd)); if (augbname(bn, curbp->b_fname, sizeof(bn)) == FALSE) return (FALSE); free(curbp->b_bname); if ((curbp->b_bname = strdup(bn)) == NULL) return (FALSE); (void)fupdstat(curbp); curbp->b_flag &= ~BFCHG; upmodes(curbp); undo_add_boundary(FFRAND, 1); undo_add_modified(); } return (s); }
/* * Save the contents of the buffer argument into its associated file. Do * nothing if there have been no changes (is this a bug, or a feature?). * Error if there is no remembered file name. If this is the first write * since the read or visit, then a backup copy of the file is made. * Allow user to select whether or not to make backup files by looking at * the value of makebackup. */ int buffsave(struct buffer *bp) { int s; FILE *ffp; /* return, no changes */ if ((bp->b_flag & BFCHG) == 0) { ewprintf("(No changes need to be saved)"); return (TRUE); } /* must have a name */ if (bp->b_fname[0] == '\0') { dobeep(); ewprintf("No file name"); return (FALSE); } /* Ensure file has not been modified elsewhere */ /* We don't use the ignore flag here */ if (fchecktime(bp) != TRUE) { if ((s = eyesno("File has changed on disk since last save. " "Save anyway")) != TRUE) return (s); } if (makebackup && (bp->b_flag & BFBAK)) { s = fbackupfile(bp->b_fname); /* hard error */ if (s == ABORT) return (FALSE); /* softer error */ if (s == FALSE && (s = eyesno("Backup error, save anyway")) != TRUE) return (s); } if ((s = writeout(&ffp, bp, bp->b_fname)) == TRUE) { (void)fupdstat(bp); bp->b_flag &= ~(BFCHG | BFBAK); upmodes(bp); undo_add_boundary(FFRAND, 1); undo_add_modified(); } return (s); }
/* * Read the file "fname" into the current buffer. Make all of the text * in the buffer go away, after checking for unsaved changes. This is * called by the "read" command, the "visit" command, and the mainline * (for "mg file"). */ int readin(char *fname) { struct mgwin *wp; struct stat statbuf; int status, ro = FALSE; char dp[NFILEN]; /* might be old */ if (bclear(curbp) != TRUE) return (TRUE); /* Clear readonly. May be set by autoexec path */ curbp->b_flag &= ~BFREADONLY; if ((status = insertfile(fname, fname, TRUE)) != TRUE) { dobeep(); ewprintf("File is not readable: %s", fname); return (FALSE); } for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { if (wp->w_bufp == curbp) { wp->w_dotp = wp->w_linep = bfirstlp(curbp); wp->w_doto = 0; wp->w_markp = NULL; wp->w_marko = 0; } } /* no change */ curbp->b_flag &= ~BFCHG; /* * Set the buffer READONLY flag if any of following are true: * 1. file is a directory. * 2. file is read-only. * 3. file doesn't exist and directory is read-only. */ if (fisdir(fname) == TRUE) { ro = TRUE; } else if ((access(fname, W_OK) == -1)) { if (errno != ENOENT) { ro = TRUE; } else if (errno == ENOENT) { (void)xdirname(dp, fname, sizeof(dp)); (void)strlcat(dp, "/", sizeof(dp)); /* Missing directory; keep buffer rw, like emacs */ if (stat(dp, &statbuf) == -1 && errno == ENOENT) { if (eyorn("Missing directory, create") == TRUE) (void)do_makedir(dp); } else if (access(dp, W_OK) == -1 && errno == EACCES) { ewprintf("File not found and directory" " write-protected"); ro = TRUE; } } } if (ro == TRUE) curbp->b_flag |= BFREADONLY; if (startrow) { gotoline(FFARG, startrow); startrow = 0; } undo_add_modified(); return (status); }