📄 tmp.c
字号:
nlines = 1; /* there is 1 line in the file */ nbytes = 1; }#if MSDOS || TOS /* each line has an extra CR that we didn't count yet */ nbytes += nlines;#endif /* report the number of lines in the file */ msg("\"%s\" %s %ld line%s, %ld char%s", origname, (tstflag(file, READONLY) ? "[READONLY]" : ""), nlines, nlines == 1 ? "" : "s", nbytes, nbytes == 1 ? "" : "s"); } /* initialize the cursor to start of line 1 */ cursor = MARK_FIRST; /* close the original file */ close(origfd); /* any other messages? */ if (tstflag(file, HADNUL)) { msg("This file contained NULs. They've been changed to \\x80 chars"); } if (tstflag(file, ADDEDNL)) { msg("Newline characters have been inserted to break up long lines"); }#ifndef CRUNCH if (tstflag(file, HADBS)) { msg("Backspace characters deleted due to ':set beautify'"); }#endif storename(origname);#ifndef NO_MODELINES if (nlines > 10) { do_modelines(1L, 5L); do_modelines(nlines - 4L, nlines); } else { do_modelines(1L, nlines); }#endif /* force all blocks out onto the disk, to support file recovery */ blksync(); return 0;}/* This function copies the temp file back onto an original file. * Returns TRUE if successful, or FALSE if the file could NOT be saved. */int tmpsave(filename, bang) char *filename; /* the name to save it to */ int bang; /* forced write? */{ int fd; /* fd of the file we're writing to */ REG int len; /* length of a text block */ REG BLK *this; /* a text block */ long bytes; /* byte counter */ REG int i; /* if no filename is given, assume the original file name */ if (!filename || !*filename) { filename = origname; } /* if still no file name, then fail */ if (!*filename) { msg("Don't know a name for this file -- NOT WRITTEN"); return FALSE; } /* can't rewrite a READONLY file */#if AMIGA if (!strcmp(filename, origname) && tstflag(file, READONLY) && !bang)#else if (!strcmp(filename, origname) && *o_readonly && !bang)#endif { msg("\"%s\" [READONLY] -- NOT WRITTEN", filename); return FALSE; } /* open the file */ if (*filename == '>' && filename[1] == '>') { filename += 2; while (*filename == ' ' || *filename == '\t') { filename++; }#ifdef O_APPEND fd = open(filename, O_WRONLY|O_APPEND);#else fd = open(filename, O_WRONLY); lseek(fd, 0L, 2);#endif } else { /* either the file must not exist, or it must be the original * file, or we must have a bang, or "writeany" must be set. */ if (strcmp(filename, origname) && access(filename, 0) == 0 && !bang#ifndef CRUNCH && !*o_writeany#endif ) { msg("File already exists - Use :w! to overwrite"); return FALSE; }#if VMS /* Create a new VMS version of this file. */ { char *strrchr(), *ptr = strrchr(filename,';'); if (ptr) *ptr = '\0'; /* Snip off any ;number in the name */ }#endif fd = creat(filename, FILEPERMS); } if (fd < 0) { msg("Can't write to \"%s\" -- NOT WRITTEN", filename); return FALSE; } /* write each text block to the file */ bytes = 0L; for (i = 1; i < MAXBLKS && (this = blkget(i)) && this->c[0]; i++) { for (len = 0; len < BLKSIZE && this->c[len]; len++) { } if (twrite(fd, this->c, len) < len) { msg("Trouble writing to \"%s\"", filename); if (!strcmp(filename, origname)) { setflag(file, MODIFIED); } close(fd); return FALSE; } bytes += len; } /* reset the "modified" flag, but not the "undoable" flag */ clrflag(file, MODIFIED); significant = FALSE; /* report lines & characters */#if MSDOS || TOS bytes += nlines; /* for the inserted carriage returns */#endif msg("Wrote \"%s\" %ld lines, %ld characters", filename, nlines, bytes); /* close the file */ close(fd); return TRUE;}/* This function deletes the temporary file. If the file has been modified * and "bang" is FALSE, then it returns FALSE without doing anything; else * it returns TRUE. * * If the "autowrite" option is set, then instead of returning FALSE when * the file has been modified and "bang" is false, it will call tmpend(). */int tmpabort(bang) int bang;{ /* if there is no file, return successfully */ if (tmpfd < 0) { return TRUE; } /* see if we must return FALSE -- can't quit */ if (!bang && tstflag(file, MODIFIED)) { /* if "autowrite" is set, then act like tmpend() */ if (*o_autowrite) return tmpend(bang); else return FALSE; } /* delete the tmp file */ cutswitch(); strcpy(prevorig, origname); prevline = markline(cursor); *origname = '\0'; origtime = 0L; blkinit(); nlines = 0; initflags(); return TRUE;}/* This function saves the file if it has been modified, and then deletes * the temporary file. Returns TRUE if successful, or FALSE if the file * needs to be saved but can't be. When it returns FALSE, it will not have * deleted the tmp file, either. */int tmpend(bang) int bang;{ /* save the file if it has been modified */ if (tstflag(file, MODIFIED) && !tmpsave((char *)0, FALSE) && !bang) { return FALSE; } /* delete the tmp file */ tmpabort(TRUE); return TRUE;}/* If the tmp file has been changed, then this function will force those * changes to be written to the disk, so that the tmp file will survive a * system crash or power failure. */#if AMIGA || MSDOS || TOSsync(){ /* MS-DOS and TOS don't flush their buffers until the file is closed, * so here we close the tmp file and then immediately reopen it. */ close(tmpfd); tmpfd = open(tmpname, O_RDWR | O_BINARY); return 0;}#endif/* This function stores the file's name in the second block of the temp file. * SLEAZE ALERT! SLEAZE ALERT! The "tmpblk" buffer is probably being used * to store the arguments to a command, so we can't use it here. Instead, * we'll borrow the buffer that is used for "shift-U". */storename(name) char *name; /* the name of the file - normally origname */{#ifndef CRUNCH int len; char *ptr;#endif /* we're going to clobber the U_text buffer, so reset U_line */ U_line = 0L; if (!name) { strncpy(U_text, "", BLKSIZE); U_text[1] = 127; }#ifndef CRUNCH else if (*name != SLASH) { /* get the directory name */ ptr = getcwd(U_text, BLKSIZE); if (ptr != U_text) { strcpy(U_text, ptr); } /* append a slash to the directory name */ len = strlen(U_text); U_text[len++] = SLASH; /* append the filename, padded with heaps o' NULs */ strncpy(U_text + len, *name ? name : "foo", BLKSIZE - len); }#endif else { /* copy the filename into U_text */ strncpy(U_text, *name ? name : "foo", BLKSIZE); } if (tmpfd >= 0) { /* write the name out to second block of the temp file */ lseek(tmpfd, (long)BLKSIZE, 0); write(tmpfd, U_text, (unsigned)BLKSIZE); } return 0;}/* This function handles deadly signals. It restores sanity to the terminal * preserves the current temp file, and deletes any old temp files. */int deathtrap(sig) int sig; /* the deadly signal that we caught */{ char *why; /* restore the terminal's sanity */ endwin();#ifdef CRUNCH why = "-Elvis died";#else /* give a more specific description of how Elvis died */ switch (sig) {# ifdef SIGHUP case SIGHUP: why = "-the modem lost its carrier"; break;# endif# ifndef DEBUG# ifdef SIGILL case SIGILL: why = "-Elvis hit an illegal instruction"; break;# endif# ifdef SIGBUS case SIGBUS: why = "-Elvis had a bus error"; break;# endif# if defined(SIGSEGV) && !defined(TOS) case SIGSEGV: why = "-Elvis had a segmentation violation"; break;# endif# ifdef SIGSYS case SIGSYS: why = "-Elvis munged a system call"; break;# endif# endif /* !DEBUG */# ifdef SIGPIPE case SIGPIPE: why = "-the pipe reader died"; break;# endif# ifdef SIGTERM case SIGTERM: why = "-Elvis was terminated"; break;# endif# if !MINIX# ifdef SIGUSR1 case SIGUSR1: why = "-Elvis was killed via SIGUSR1"; break;# endif# ifdef SIGUSR2 case SIGUSR2: why = "-Elvis was killed via SIGUSR2"; break;# endif# endif default: why = "-Elvis died"; break; }#endif /* if we had a temp file going, then preserve it */ if (tmpnum > 0 && tmpfd >= 0) { close(tmpfd); sprintf(tmpblk.c, "%s \"%s\" %s", PRESERVE, why, tmpname); system(tmpblk.c); } /* delete any old temp files */ cutend(); /* exit with the proper exit status */ exit(sig);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -