/* buf.c: This file contains the scratch-file buffer rountines for the
* Copyright (c) 1992 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Rodney Ruddock of the University of Guelph.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)buf.c 5.5 (Berkeley) 3/28/93";
FILE *sfp
; /* scratch file pointer */
char *sfbuf
= NULL
; /* scratch file input buffer */
int sfbufsz
= 0; /* scratch file input buffer size */
off_t sfseek
; /* scratch file position */
int seek_write
; /* seek before writing */
line_t line0
; /* initial node of line queue */
/* gettxt: get a line of text from the scratch file; return pointer
seek_write
= 1; /* force seek on write */
if (sfseek
!= lp
->seek
) {
if (fseek(sfp
, sfseek
, SEEK_SET
) < 0) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "cannot seek temp file");
CKBUF(sfbuf
, sfbufsz
, len
+ 1, NULL
);
if ((ct
= fread(sfbuf
, sizeof(char), len
, sfp
)) < 0 || ct
!= len
) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "cannot read temp file");
sfseek
+= len
; /* update file position */
/* puttxt: write a line of text to the scratch file and add a line node
to the editor buffer; return a pointer to the end of the text */
if ((lp
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "out of memory");
/* assert: cs is '\n' terminated */
for (s
= cs
; *s
!= '\n'; s
++)
if (s
- cs
>= LINECHARS
) {
sprintf(errmsg
, "line too long");
if (fseek(sfp
, 0L, SEEK_END
) < 0) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "cannot seek temp file");
if ((ct
= fwrite(cs
, sizeof(char), len
, sfp
)) < 0 || ct
!= len
) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "cannot write temp file");
sfseek
+= len
; /* update file position */
/* lpqueue: add a line node in the editor buffer after the current line */
cp
= getlp(curln
); /* this getlp last! */
/* getaddr: return line number of pointer */
while (cp
!= lp
&& (cp
= cp
->next
) != &line0
)
sprintf(errmsg
, "invalid address");
/* getlp: return pointer to a line node in the editor buffer */
static line_t
*lp
= &line0
;
if (n
<= (on
+ lastln
) >> 1)
for (on
= lastln
; on
> n
; on
--)
for (on
= 0; on
< n
; on
++)
char sfn
[15] = ""; /* scratch file name */
/* sbopen: open scratch file */
strcpy(sfn
, "/tmp/ed.XXXXXX");
if (mktemp(sfn
) == NULL
|| (sfp
= fopen(sfn
, "w+")) == NULL
) {
fprintf(stderr
, "%s: %s\n", sfn
, strerror(errno
));
sprintf(errmsg
, "cannot open temp file");
/* sbclose: close scratch file */
fprintf(stderr
, "%s: %s\n", sfn
, strerror(errno
));
sprintf(errmsg
, "cannot close temp file");
/* quit: remove scratch file and exit */
unsigned char ctab
[256]; /* character translation table */
/* init_buf: open scratch buffer; initialize line queue */
for (i
= 0; i
< 256; i
++)
/* translit: translate characters in a string */
translit(s
, len
, from
, to
)
ctab
[i
] = i
; /* restore table to initial state */
for (us
= (unsigned char *) s
; len
-- > 0; us
++)