📄 eefile.c
字号:
/* ELLE - Copyright 1982, 1984, 1987 by Ken Harrenstien, SRI International * This software is quasi-public; it may be used freely with * like software, but may NOT be sold or made part of licensed * products without permission of the author. *//* * EEFILE File reading/writing functions */#include "elle.h"#include <stdio.h> /* Use "standard" I/O package for writing */#ifndef BUFSIZ#define BUFSIZ BUFSIZE /* Some places use wrong name in stdio.h */#endif /*-BUFSIZ*/#if V6 struct stat { int st_dev; int st_ino; char *st_mode; char st_nlink; char st_uid; char st_gid; char st_size0; char st_size; int st_addr[8]; long st_atime; long st_mtime; };#define ENOENT (2) /* Syscall error - no such file or dir */#else#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#endif /*-V6*/#if TOPS20#include <sys/file.h> /* Get open mode bits */#endifextern char *strerror(); /* Return error string for errno */extern struct buffer *make_buf(), *find_buf();char *fncons(), *last_fname();int hoardfd = -1; /* Retain a FD here to ensure we can always write *//* Flags for iwritfile() */#define WF_SMASK 07 /* Source Mask */#define WF_SBUFF 0 /* source: Buffer */#define WF_SREG 1 /* source: Region */#define WF_SKILL 2 /* source: Last Kill */#define WF_ASK 010 /* Ask for filename to write to */static int iwritfile();/* EFUN: "Find File" *//* Ask user for a filename and do a find_file for it. * If buffer exists for that filename, select that buffer. * Else create a buffer for it, and read in the file if it exists. */f_ffile(){ int find_file();#if IMAGEN hack_file("Visit file: ", find_file);#else hack_file("Find file: ", find_file);#endif /*-IMAGEN*/}/* EFUN: "Read File" *//* User read_file function, asks user for a filename and reads it */f_rfile() { u_r_file("Read file: "); }/* EFUN: "Visit File" *//* Same as Read File, with different prompt. */f_vfile() { u_r_file("Visit file: "); }u_r_file(prompt)char *prompt;{ register char *f_name; register struct buffer *b; if((f_name = ask (prompt))==0) /* prompt user for filename */ return; /* user punted... */ b = cur_buf; if(*f_name == '\0') { if (b -> b_fn == 0) ding("No default file name."); else read_file(b -> b_fn); } else read_file(f_name); chkfree(f_name);}/* EFUN: "Insert File" *//* Asks for a filename and inserts the file at current location. * Point is left at beginning, and the mark at the end. */f_ifile(){ int ins_file(); hack_file("Insert file: ", ins_file);}/* EFUN: "Save File" *//* Save current buffer to its default file name */f_sfile(){ if(cur_buf->b_flags&B_MODIFIED) return(iwritfile(WF_SBUFF)); /* Write buffer, don't ask */ else { saynow("(No changes need to be written)"); return(1); }}#if FX_SAVEFILES || FX_WFEXIT/* EFUN: "Save All Files" *//* F_SAVEFILES - Offer to save all modified files. * With argument, doesn't ask. * Returns 0 if user aborts or if an error happened. */f_savefiles(){ register struct buffer *b, *savb; register int res = 1; char *ans; savb = cur_buf; for (b = buf_head; res && b; b = b->b_next) if ((b->b_flags & B_MODIFIED) && b->b_fn) { if(exp_p) /* If arg, */ { chg_buf(b); /* just save, don't ask */ res = f_sfile(); continue; /* Check next buffer */ } /* Ask user whether to save */ ans = ask("Buffer %s contains changes - write out? ", b->b_name); if(ans == 0) { res = 0; /* User aborted */ break; } if (upcase(*ans) == 'Y') { chg_buf(b); res = f_sfile(); /* Save File */ } chkfree(ans); } chg_buf(savb); return(res);}#endif /*FX_SAVEFILES||FX_WFEXIT*//* EFUN: "Write File" *//* Write out the buffer to an output file. */f_wfile(){ return iwritfile(WF_ASK|WF_SBUFF);}/* EFUN: "Write Region" *//* Write region out to a file */f_wreg(){ return iwritfile(WF_ASK|WF_SREG); /* Ask, write region */}#if FX_WLASTKILL/* EFUN: "Write Last Kill" (not EMACS) *//* Write current kill buffer out to a file.** This is mainly for MINIX.*/extern int kill_ptr; /* From EEF3 */extern SBSTR *kill_ring[];f_wlastkill(){ return iwritfile(WF_ASK|WF_SKILL);}#endif/* HACK_FILE - intermediate subroutine */hack_file(prompt, rtn)char *prompt;int (*rtn)();{ register char *f_name; if((f_name = ask(prompt)) == 0) return; if (*f_name != '\0') /* Check for null answer */ (*rtn)(f_name); chkfree(f_name);}/* FIND_FILE(f_name) * If there is a buffer whose fn == f_name, select that buffer. * Else create one with name of the last section of f_name and * read the file into that buffer. */find_file(f_name)register char *f_name;{ register struct buffer *b; register char *ans; char *lastn; int fd;#if IMAGEN char real_name[128]; /* File name w/ expanded ~ and $ */ expand_file(real_name, f_name); f_name = real_name;#endif /*IMAGEN*/ for (b = buf_head; b; b = b -> b_next) if(b->b_fn && (strcmp (b -> b_fn, f_name) == 0)) break; if (b) /* if we found one */ { sel_buf(b); /* go there */ return; /* and we are done */ } if((fd = open(f_name,0)) < 0) /* See if file exists */ { if(errno != ENOENT) /* No, check reason */ { ferr_ropn(); /* Strange error, complain */ return; /* and do nothing else. */ } } else close(fd); /* Found! Close FD, since the */ /* read_file rtn will re-open. */ lastn = last_fname(f_name); /* Find buffer name */ b = find_buf(lastn); /* Is there a buffer of that name? */ if (b && (ex_blen(b) || b->b_fn)) { ans = ask("Buffer %s contains %s, which buffer shall I use? ", b -> b_name, b->b_fn ? b->b_fn : "something"); if(ans == 0) return; /* Aborted */ if (*ans != '\0') /* if null answer, use b */ b = make_buf(ans); /* else use ans */ chkfree(ans); } else b = make_buf(lastn); sel_buf(b); if(fd < 0) /* If file doesn't exist, */ { set_fn(f_name); /* just say "new" and set filename */ return; /* and return right away. */ } if (read_file(f_name)==0) /* File exists, read it in! */ { if(b->b_fn) /* Failed... if filename, */ { chkfree(b->b_fn); /* flush the filename. */ b->b_fn = 0; } }}/* READ_FILE(f_name) * Reads file into current buffer, flushing any * previous contents (if buffer modified, will ask about saving) * Returns 0 if failed. */read_file(f_name)char *f_name;{#if IMAGEN struct stat s; char real_name[128]; /* File name w/ expanded ~ and $ */#endif /*IMAGEN*/ if(!zap_buffer()) /* Flush the whole buffer */ return; /* Unless user aborts */#if IMAGEN expand_file(real_name, f_name); f_name = real_name; /* Hack, hack! */#endif /*IMAGEN*/ set_fn(f_name); if (ins_file(f_name)==0) return 0; f_bufnotmod(); /* Say not modified now */#if IMAGEN stat(f_name, &s); /* Get file stat */ cur_buf->b_mtime = s.st_mtime; /* and pick out last-modified time */#endif /*IMAGEN*/ return 1;}/* INS_FILE(f_name) * Inserts file named f_name into current buffer at current point * Point is not moved; mark is set to end of inserted stuff. * Returns 0 if failed, 1 if won. */ins_file (f_name)char *f_name;{ register int ifd; register SBSTR *sd; chroff insdot; /* To check for range of mods */#if IMAGEN char real_name[128]; /* File name w/ expanded ~ and $ */ expand_file(real_name, f_name); f_name = real_name;#endif /*IMAGEN*/#if !(TOPS20) if((ifd = open(f_name,0)) < 0)#else if((ifd = open(f_name,O_RDONLY|O_UNCONVERTED)) < 0)#endif /*TOPS20*/ { ferr_ropn(); /* Can't open, complain */ return 0; /* no redisplay */ } errno = 0; if((sd = sb_fduse(ifd)) == 0) { if (ifd >= SB_NFILES) dingtoo(" Cannot read - too many internal files"); else if (errno) ferr_ropn(); else errbarf("SB rtn cannot read file?"); close(ifd); return 0; } sb_sins(cur_buf,sd); insdot = e_dot(); f_setmark(); /* Set mark at current ptr */ if(cur_dot != insdot) /* If pointer was advanced, */ buf_tmat(insdot); /* then stuff was inserted */ e_gocur(); return 1;}ferr_ropn() { ferr(" Cannot read"); }ferr_wopn() { ferr(" Cannot write"); }ferr(str)char *str;{ saytoo(str); saytoo(" - "); dingtoo(strerror(errno));}/* IWRITFILE - auxiliary for writing files.** Returns 1 if write successful, 0 if not.*/static intiwritfile(flags)int flags;{ register struct buffer *b; register char *o_name; /* output file name */ int styp = flags & WF_SMASK; /* Source type, one of WF_Sxxx */ char *prompt;#ifdef STDWRITE register FILE *o_file; /* output file pointer */ char obuf[BUFSIZ]; chroff dotcnt;#endif /*STDWRITE*/ int ofd; /* output file FD */ SBSTR *sd; char fname[FNAMSIZ]; /* To avoid chkfree hassle */ char newname[FNAMSIZ]; /* for robustness */ char oldname[FNAMSIZ]; /* ditto */ int res; struct stat statb; int statres;#if IMAGEN struct stat s; char real_name[128]; /* File name w/ expanded ~ and $ */#endif /*IMAGEN*/ res = 1; /* Let's keep track of success */ /* Check for existence of source, and set prompt string */ switch(styp) { case WF_SBUFF: prompt = "Write File: "; break; case WF_SREG: if(!mark_p) { dingtoo(" No Mark!"); return(0); } prompt = "Write Region: "; break;#if FX_WLASTKILL case WF_SKILL: if(!kill_ring[kill_ptr]) { dingtoo("No killed stuff"); return(0); } prompt = "Write Last Kill: "; break;#endif default: /* Internal error */ errbarf("bad iwritfile arg"); return 0; } if (flags&WF_ASK) { if((o_name = ask(prompt))==0) return(0); /* User punted. */ strcpy(&fname[0], o_name); /* Copy filename onto stack */ chkfree(o_name); } o_name = &fname[0]; b = cur_buf; if (!(flags&WF_ASK) || (*o_name == '\0')) { if (b->b_fn == 0) { ding("No default file name."); return(0); } strcpy(o_name, b->b_fn); }#if IMAGEN expand_file(real_name, o_name); o_name = real_name; /* Hack, hack */#endif /*IMAGEN*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -