head 1.27; access; symbols netbsd-10-0-RELEASE:1.27 netbsd-10-0-RC6:1.27 netbsd-10-0-RC5:1.27 netbsd-10-0-RC4:1.27 netbsd-10-0-RC3:1.27 netbsd-10-0-RC2:1.27 netbsd-10-0-RC1:1.27 netbsd-10:1.27.0.36 netbsd-10-base:1.27 netbsd-9-3-RELEASE:1.27 cjep_sun2x-base1:1.27 cjep_sun2x:1.27.0.34 cjep_sun2x-base:1.27 cjep_staticlib_x-base1:1.27 netbsd-9-2-RELEASE:1.27 cjep_staticlib_x:1.27.0.32 cjep_staticlib_x-base:1.27 netbsd-9-1-RELEASE:1.27 phil-wifi-20200421:1.27 phil-wifi-20200411:1.27 is-mlppp:1.27.0.30 is-mlppp-base:1.27 phil-wifi-20200406:1.27 netbsd-8-2-RELEASE:1.27 netbsd-9-0-RELEASE:1.27 netbsd-9-0-RC2:1.27 netbsd-9-0-RC1:1.27 phil-wifi-20191119:1.27 netbsd-9:1.27.0.28 netbsd-9-base:1.27 phil-wifi-20190609:1.27 netbsd-8-1-RELEASE:1.27 netbsd-8-1-RC1:1.27 pgoyette-compat-merge-20190127:1.27 pgoyette-compat-20190127:1.27 pgoyette-compat-20190118:1.27 pgoyette-compat-1226:1.27 pgoyette-compat-1126:1.27 pgoyette-compat-1020:1.27 pgoyette-compat-0930:1.27 pgoyette-compat-0906:1.27 netbsd-7-2-RELEASE:1.27 pgoyette-compat-0728:1.27 netbsd-8-0-RELEASE:1.27 phil-wifi:1.27.0.26 phil-wifi-base:1.27 pgoyette-compat-0625:1.27 netbsd-8-0-RC2:1.27 pgoyette-compat-0521:1.27 pgoyette-compat-0502:1.27 pgoyette-compat-0422:1.27 netbsd-8-0-RC1:1.27 pgoyette-compat-0415:1.27 pgoyette-compat-0407:1.27 pgoyette-compat-0330:1.27 pgoyette-compat-0322:1.27 pgoyette-compat-0315:1.27 netbsd-7-1-2-RELEASE:1.27 pgoyette-compat:1.27.0.24 pgoyette-compat-base:1.27 netbsd-7-1-1-RELEASE:1.27 matt-nb8-mediatek:1.27.0.22 matt-nb8-mediatek-base:1.27 perseant-stdc-iso10646:1.27.0.20 perseant-stdc-iso10646-base:1.27 netbsd-8:1.27.0.18 netbsd-8-base:1.27 prg-localcount2-base3:1.27 prg-localcount2-base2:1.27 prg-localcount2-base1:1.27 prg-localcount2:1.27.0.16 prg-localcount2-base:1.27 pgoyette-localcount-20170426:1.27 bouyer-socketcan-base1:1.27 pgoyette-localcount-20170320:1.27 netbsd-7-1:1.27.0.14 netbsd-7-1-RELEASE:1.27 netbsd-7-1-RC2:1.27 netbsd-7-nhusb-base-20170116:1.27 bouyer-socketcan:1.27.0.12 bouyer-socketcan-base:1.27 pgoyette-localcount-20170107:1.27 netbsd-7-1-RC1:1.27 pgoyette-localcount-20161104:1.27 netbsd-7-0-2-RELEASE:1.27 localcount-20160914:1.27 netbsd-7-nhusb:1.27.0.10 netbsd-7-nhusb-base:1.27 pgoyette-localcount-20160806:1.27 pgoyette-localcount-20160726:1.27 pgoyette-localcount:1.27.0.8 pgoyette-localcount-base:1.27 netbsd-7-0-1-RELEASE:1.27 netbsd-7-0:1.27.0.6 netbsd-7-0-RELEASE:1.27 netbsd-7-0-RC3:1.27 netbsd-7-0-RC2:1.27 netbsd-7-0-RC1:1.27 netbsd-5-2-3-RELEASE:1.26 netbsd-5-1-5-RELEASE:1.26 netbsd-6-0-6-RELEASE:1.26 netbsd-6-1-5-RELEASE:1.26 netbsd-7:1.27.0.4 netbsd-7-base:1.27 yamt-pagecache-base9:1.27 yamt-pagecache-tag8:1.26 netbsd-6-1-4-RELEASE:1.26 netbsd-6-0-5-RELEASE:1.26 tls-earlyentropy:1.27.0.2 tls-earlyentropy-base:1.27 riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.27 riastradh-drm2-base3:1.26 netbsd-6-1-3-RELEASE:1.26 netbsd-6-0-4-RELEASE:1.26 netbsd-5-2-2-RELEASE:1.26 netbsd-5-1-4-RELEASE:1.26 netbsd-6-1-2-RELEASE:1.26 netbsd-6-0-3-RELEASE:1.26 netbsd-5-2-1-RELEASE:1.26 netbsd-5-1-3-RELEASE:1.26 netbsd-6-1-1-RELEASE:1.26 riastradh-drm2-base2:1.26 riastradh-drm2-base1:1.26 riastradh-drm2:1.26.0.54 riastradh-drm2-base:1.26 netbsd-6-1:1.26.0.60 netbsd-6-0-2-RELEASE:1.26 netbsd-6-1-RELEASE:1.26 khorben-n900:1.26.0.58 netbsd-6-1-RC4:1.26 netbsd-6-1-RC3:1.26 agc-symver:1.26.0.56 agc-symver-base:1.26 netbsd-6-1-RC2:1.26 netbsd-6-1-RC1:1.26 yamt-pagecache-base8:1.26 netbsd-5-2:1.26.0.52 netbsd-6-0-1-RELEASE:1.26 yamt-pagecache-base7:1.26 netbsd-5-2-RELEASE:1.26 netbsd-5-2-RC1:1.26 matt-nb6-plus-nbase:1.26 yamt-pagecache-base6:1.26 netbsd-6-0:1.26.0.50 netbsd-6-0-RELEASE:1.26 netbsd-6-0-RC2:1.26 tls-maxphys:1.26.0.48 tls-maxphys-base:1.27 matt-nb6-plus:1.26.0.46 matt-nb6-plus-base:1.26 netbsd-6-0-RC1:1.26 yamt-pagecache-base5:1.26 yamt-pagecache-base4:1.26 netbsd-6:1.26.0.44 netbsd-6-base:1.26 netbsd-5-1-2-RELEASE:1.26 netbsd-5-1-1-RELEASE:1.26 yamt-pagecache-base3:1.26 yamt-pagecache-base2:1.26 yamt-pagecache:1.26.0.42 yamt-pagecache-base:1.26 cherry-xenmp:1.26.0.40 cherry-xenmp-base:1.26 bouyer-quota2-nbase:1.26 bouyer-quota2:1.26.0.38 bouyer-quota2-base:1.26 matt-mips64-premerge-20101231:1.26 matt-nb5-mips64-premerge-20101231:1.26 matt-nb5-pq3:1.26.0.36 matt-nb5-pq3-base:1.26 netbsd-5-1:1.26.0.34 netbsd-5-1-RELEASE:1.26 netbsd-5-1-RC4:1.26 matt-nb5-mips64-k15:1.26 netbsd-5-1-RC3:1.26 netbsd-5-1-RC2:1.26 netbsd-5-1-RC1:1.26 netbsd-5-0-2-RELEASE:1.26 matt-nb5-mips64-premerge-20091211:1.26 matt-premerge-20091211:1.26 matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.26 matt-nb4-mips64-k7-u2a-k9b:1.26 matt-nb5-mips64-u1-k1-k5:1.26 matt-nb5-mips64:1.26.0.32 netbsd-5-0-1-RELEASE:1.26 jym-xensuspend-nbase:1.26 netbsd-5-0:1.26.0.30 netbsd-5-0-RELEASE:1.26 netbsd-5-0-RC4:1.26 netbsd-5-0-RC3:1.26 netbsd-5-0-RC2:1.26 jym-xensuspend:1.26.0.28 jym-xensuspend-base:1.26 netbsd-5-0-RC1:1.26 netbsd-5:1.26.0.26 netbsd-5-base:1.26 matt-mips64-base2:1.26 matt-mips64:1.26.0.24 mjf-devfs2:1.26.0.22 mjf-devfs2-base:1.26 netbsd-4-0-1-RELEASE:1.26 wrstuden-revivesa-base-3:1.26 wrstuden-revivesa-base-2:1.26 wrstuden-fixsa-newbase:1.26 wrstuden-revivesa-base-1:1.26 yamt-pf42-base4:1.26 yamt-pf42-base3:1.26 hpcarm-cleanup-nbase:1.26 yamt-pf42-baseX:1.26 yamt-pf42-base2:1.26 wrstuden-revivesa:1.26.0.20 wrstuden-revivesa-base:1.26 yamt-pf42:1.26.0.18 yamt-pf42-base:1.26 keiichi-mipv6:1.26.0.16 keiichi-mipv6-base:1.26 matt-armv6-nbase:1.26 matt-armv6-prevmlocking:1.26 wrstuden-fixsa-base-1:1.26 netbsd-4-0:1.26.0.14 netbsd-4-0-RELEASE:1.26 cube-autoconf:1.26.0.12 cube-autoconf-base:1.26 netbsd-4-0-RC5:1.26 netbsd-4-0-RC4:1.26 netbsd-4-0-RC3:1.26 netbsd-4-0-RC2:1.26 netbsd-4-0-RC1:1.26 matt-armv6:1.26.0.10 matt-armv6-base:1.26 matt-mips64-base:1.26 hpcarm-cleanup:1.26.0.8 hpcarm-cleanup-base:1.26 netbsd-3-1-1-RELEASE:1.24 netbsd-3-0-3-RELEASE:1.24 wrstuden-fixsa:1.26.0.6 wrstuden-fixsa-base:1.26 abandoned-netbsd-4-base:1.26 abandoned-netbsd-4:1.26.0.2 netbsd-3-1:1.24.0.6 netbsd-3-1-RELEASE:1.24 netbsd-3-0-2-RELEASE:1.24 netbsd-3-1-RC4:1.24 netbsd-3-1-RC3:1.24 netbsd-3-1-RC2:1.24 netbsd-3-1-RC1:1.24 netbsd-4:1.26.0.4 netbsd-4-base:1.26 netbsd-3-0-1-RELEASE:1.24 netbsd-3-0:1.24.0.4 netbsd-3-0-RELEASE:1.24 netbsd-3-0-RC6:1.24 netbsd-3-0-RC5:1.24 netbsd-3-0-RC4:1.24 netbsd-3-0-RC3:1.24 netbsd-3-0-RC2:1.24 netbsd-3-0-RC1:1.24 netbsd-2-0-3-RELEASE:1.23 netbsd-2-1:1.23.0.6 netbsd-2-1-RELEASE:1.23 netbsd-2-1-RC6:1.23 netbsd-2-1-RC5:1.23 netbsd-2-1-RC4:1.23 netbsd-2-1-RC3:1.23 netbsd-2-1-RC2:1.23 netbsd-2-1-RC1:1.23 netbsd-2-0-2-RELEASE:1.23 netbsd-3:1.24.0.2 netbsd-3-base:1.24 netbsd-2-0-1-RELEASE:1.23 netbsd-2:1.23.0.4 netbsd-2-base:1.23 netbsd-2-0-RELEASE:1.23 netbsd-2-0-RC5:1.23 netbsd-2-0-RC4:1.23 netbsd-2-0-RC3:1.23 netbsd-2-0-RC2:1.23 netbsd-2-0-RC1:1.23 netbsd-2-0:1.23.0.2 netbsd-2-0-base:1.23 netbsd-1-6-PATCH002-RELEASE:1.21 netbsd-1-6-PATCH002:1.21 netbsd-1-6-PATCH002-RC4:1.21 netbsd-1-6-PATCH002-RC3:1.21 netbsd-1-6-PATCH002-RC2:1.21 netbsd-1-6-PATCH002-RC1:1.21 netbsd-1-6-PATCH001:1.21 netbsd-1-6-PATCH001-RELEASE:1.21 netbsd-1-6-PATCH001-RC3:1.21 netbsd-1-6-PATCH001-RC2:1.21 netbsd-1-6-PATCH001-RC1:1.21 fvdl_fs64_base:1.21 netbsd-1-6-RELEASE:1.21 netbsd-1-6-RC3:1.21 netbsd-1-6-RC2:1.21 netbsd-1-6-RC1:1.21 netbsd-1-6:1.21.0.6 netbsd-1-6-base:1.21 netbsd-1-5-PATCH003:1.21 netbsd-1-5-PATCH002:1.21 netbsd-1-5-PATCH001:1.21 netbsd-1-5-RELEASE:1.21 netbsd-1-5-BETA2:1.21 netbsd-1-5-BETA:1.21 netbsd-1-4-PATCH003:1.19 netbsd-1-5-ALPHA2:1.21 netbsd-1-5:1.21.0.4 netbsd-1-5-base:1.21 minoura-xpg4dl:1.21.0.2 minoura-xpg4dl-base:1.21 netbsd-1-4-PATCH002:1.19 wrstuden-devbsize-19991221:1.19 wrstuden-devbsize:1.19.0.6 wrstuden-devbsize-base:1.19 comdex-fall-1999:1.19.0.4 comdex-fall-1999-base:1.19 netbsd-1-4-PATCH001:1.19 netbsd-1-4-RELEASE:1.19 netbsd-1-4:1.19.0.2 netbsd-1-4-base:1.19 netbsd-1-3-PATCH003:1.17 netbsd-1-3-PATCH003-CANDIDATE2:1.17 netbsd-1-3-PATCH003-CANDIDATE1:1.17 netbsd-1-3-PATCH003-CANDIDATE0:1.17 netbsd-1-3-PATCH002:1.17 netbsd-1-3-PATCH001:1.17 netbsd-1-3-RELEASE:1.17 netbsd-1-3-BETA:1.17 netbsd-1-3:1.17.0.2 netbsd-1-3-base:1.17 netbsd-1-2-PATCH001:1.15 netbsd-1-2-RELEASE:1.15 netbsd-1-2-BETA:1.15 netbsd-1-2-base:1.15 netbsd-1-2:1.15.0.6 netbsd-1-1-PATCH001:1.15 netbsd-1-1-RELEASE:1.15 netbsd-1-1:1.15.0.2 netbsd-1-1-base:1.15 netbsd-1-0-PATCH06:1.13 netbsd-1-0-PATCH05:1.13 netbsd-1-0-PATCH04:1.13 netbsd-1-0-PATCH03:1.13 netbsd-1-0-PATCH02:1.13 netbsd-1-0-PATCH1:1.13 netbsd-1-0-PATCH0:1.13 netbsd-1-0-RELEASE:1.13 netbsd-1-0:1.13.0.2 netbsd-1-0-base:1.13 netbsd-0-9-RELEASE:1.7 netbsd-0-9-BETA:1.7 netbsd-0-9-ALPHA2:1.7 netbsd-0-9-ALPHA:1.7 netbsd-0-9:1.7.0.2 netbsd-0-9-base:1.7 netbsd-0-8:1.2 netbsd-alpha-1:1.2 alm-ed-0-2:1.2; locks; strict; comment @ * @; 1.27 date 2014.03.23.05.06.42; author dholland; state Exp; branches 1.27.26.1; next 1.26; 1.26 date 2006.03.17.14.37.14; author rumble; state Exp; branches 1.26.42.1 1.26.48.1; next 1.25; 1.25 date 2005.06.26.19.10.49; author christos; state Exp; branches; next 1.24; 1.24 date 2005.02.17.16.29.26; author xtraeme; state Exp; branches; next 1.23; 1.23 date 2003.10.19.01.52.45; author wiz; state Exp; branches; next 1.22; 1.22 date 2003.09.05.20.16.50; author itojun; state Exp; branches; next 1.21; 1.21 date 2000.04.17.23.37.50; author christos; state Exp; branches; next 1.20; 1.20 date 2000.04.04.17.07.29; author thorpej; state Exp; branches; next 1.19; 1.19 date 98.11.04.13.41.32; author christos; state Exp; branches; next 1.18; 1.18 date 98.07.28.05.31.24; author mycroft; state Exp; branches; next 1.17; 1.17 date 97.07.20.06.35.35; author thorpej; state Exp; branches; next 1.16; 1.16 date 97.03.22.03.02.17; author lukem; state Exp; branches; next 1.15; 1.15 date 95.04.23.10.07.28; author cgd; state Exp; branches; next 1.14; 1.14 date 95.03.21.09.04.35; author cgd; state Exp; branches; next 1.13; 1.13 date 94.03.23.04.22.09; author alm; state Exp; branches; next 1.12; 1.12 date 94.03.23.04.04.19; author alm; state Exp; branches; next 1.11; 1.11 date 94.02.20.06.56.21; author alm; state Exp; branches; next 1.10; 1.10 date 93.11.23.04.41.48; author alm; state Exp; branches; next 1.9; 1.9 date 93.11.12.10.48.50; author alm; state Exp; branches; next 1.8; 1.8 date 93.08.01.18.59.50; author mycroft; state Exp; branches; next 1.7; 1.7 date 93.07.02.10.02.28; author alm; state Exp; branches; next 1.6; 1.6 date 93.05.08.10.49.55; author alm; state Exp; branches; next 1.5; 1.5 date 93.04.23.02.08.51; author alm; state Exp; branches; next 1.4; 1.4 date 93.04.15.04.58.34; author alm; state Exp; branches; next 1.3; 1.3 date 93.04.14.12.22.15; author alm; state Exp; branches; next 1.2; 1.2 date 93.04.10.11.46.42; author cgd; state Exp; branches; next 1.1; 1.1 date 93.04.08.01.07.18; author cgd; state Exp; branches; next ; 1.27.26.1 date 2020.04.21.18.41.03; author martin; state dead; branches; next 1.27.26.2; commitid 86tA4aEmdr3VCh5C; 1.27.26.2 date 2020.04.21.19.37.31; author martin; state Exp; branches; next ; commitid x6IB64bYH9UmWh5C; 1.26.42.1 date 2014.05.22.11.26.23; author yamt; state Exp; branches; next ; 1.26.48.1 date 2014.08.19.23.45.11; author tls; state Exp; branches; next ; desc @@ 1.27 log @sprintf considered harmful @ text @/* $NetBSD: buf.c,v 1.26 2006/03/17 14:37:14 rumble Exp $ */ /* buf.c: This file contains the scratch-file buffer routines for the ed line editor. */ /*- * Copyright (c) 1993 Andrew Moore, Talke Studio. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 * SUCH DAMAGE. */ #include #ifndef lint #if 0 static char *rcsid = "@@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp"; #else __RCSID("$NetBSD: buf.c,v 1.26 2006/03/17 14:37:14 rumble Exp $"); #endif #endif /* not lint */ #include #include #include #include #include #include "ed.h" FILE *sfp; /* scratch file pointer */ off_t sfseek; /* scratch file position */ int seek_write; /* seek before writing */ line_t buffer_head; /* incore buffer */ /* get_sbuf_line: get a line of text from the scratch file; return pointer to the text */ char * get_sbuf_line(line_t *lp) { static char *sfbuf = NULL; /* buffer */ static int sfbufsz = 0; /* buffer size */ int len, ct; if (lp == &buffer_head) return NULL; seek_write = 1; /* force seek on write */ /* out of position */ if (sfseek != lp->seek) { sfseek = lp->seek; if (fseek(sfp, sfseek, SEEK_SET) < 0) { fprintf(stderr, "%s\n", strerror(errno)); seterrmsg("cannot seek temp file"); return NULL; } } len = lp->len; REALLOC(sfbuf, sfbufsz, len + 1, NULL); if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) { fprintf(stderr, "%s\n", strerror(errno)); seterrmsg("cannot read temp file"); return NULL; } sfseek += len; /* update file position */ sfbuf[len] = '\0'; return sfbuf; } /* put_sbuf_line: 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 */ char * put_sbuf_line(char *cs) { line_t *lp; int len, ct; char *s; if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) { fprintf(stderr, "%s\n", strerror(errno)); seterrmsg("out of memory"); return NULL; } /* assert: cs is '\n' terminated */ for (s = cs; *s != '\n'; s++) ; if (s - cs >= LINECHARS) { seterrmsg("line too long"); free(lp); return NULL; } len = s - cs; /* out of position */ if (seek_write) { if (fseek(sfp, 0L, SEEK_END) < 0) { fprintf(stderr, "%s\n", strerror(errno)); seterrmsg("cannot seek temp file"); free(lp); return NULL; } sfseek = ftell(sfp); seek_write = 0; } /* assert: SPL1() */ if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) { sfseek = -1; fprintf(stderr, "%s\n", strerror(errno)); seterrmsg("cannot write temp file"); free(lp); return NULL; } lp->len = len; lp->seek = sfseek; add_line_node(lp); sfseek += len; /* update file position */ return ++s; } /* add_line_node: add a line node in the editor buffer after the current line */ void add_line_node(line_t *lp) { line_t *cp; cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */ INSQUE(lp, cp); addr_last++; current_addr++; } /* get_line_node_addr: return line number of pointer */ long get_line_node_addr(line_t *lp) { line_t *cp = &buffer_head; long n = 0; while (cp != lp && (cp = cp->q_forw) != &buffer_head) n++; if (n && cp == &buffer_head) { seterrmsg("invalid address"); return ERR; } return n; } /* get_addressed_line_node: return pointer to a line node in the editor buffer */ line_t * get_addressed_line_node(long n) { static line_t *lp = &buffer_head; static long on = 0; SPL1(); if (n > on) { if (n <= (on + addr_last) >> 1) { for (; on < n; on++) lp = lp->q_forw; } else { lp = buffer_head.q_back; for (on = addr_last; on > n; on--) lp = lp->q_back; } } else { if (n >= on >> 1) { for (; on > n; on--) lp = lp->q_back; } else { lp = &buffer_head; for (on = 0; on < n; on++) lp = lp->q_forw; } } SPL0(); return lp; } char *sfn = NULL; /* scratch file name */ /* open_sbuf: open scratch file */ int open_sbuf(void) { int u, fd; const char *tmp; size_t s; isbinary = newline_added = 0; fd = -1; u = umask(077); if ((tmp = getenv("TMPDIR")) == NULL) tmp = _PATH_TMP; if ((s = strlen(tmp)) == 0 || tmp[s - 1] == '/') (void)asprintf(&sfn, "%sed.XXXXXX", tmp); else (void)asprintf(&sfn, "%s/ed.XXXXXX", tmp); if (sfn == NULL) { warn(NULL); seterrmsg("could not allocate memory"); umask(u); return ERR; } if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) { if (fd != -1) close(fd); warn("%s", sfn); seterrmsg("cannot open temp file"); umask(u); return ERR; } umask(u); return 0; } /* close_sbuf: close scratch file */ int close_sbuf(void) { if (sfp) { if (fclose(sfp) < 0) { fprintf(stderr, "%s: %s\n", sfn, strerror(errno)); seterrmsg("cannot close temp file"); return ERR; } sfp = NULL; if (sfn) { unlink(sfn); free(sfn); sfn = NULL; } } sfseek = seek_write = 0; return 0; } /* quit: remove_lines scratch file and exit */ void quit(int n) { if (sfp) { fclose(sfp); if (sfn) { unlink(sfn); free(sfn); sfn = NULL; } } exit(n); /* NOTREACHED */ } unsigned char ctab[256]; /* character translation table */ /* init_buffers: open scratch buffer; initialize line queue */ void init_buffers(void) { int i = 0; /* Read stdin one character at a time to avoid i/o contention with shell escapes invoked by nonterminal input, e.g., ed - < 0; us++) *us = ctab[*us]; return s; } @ 1.27.26.1 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.27 2014/03/23 05:06:42 dholland Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.27 2014/03/23 05:06:42 dholland Exp $"); @ 1.27.26.2 log @Ooops, restore accidently removed files from merge mishap @ text @@ 1.26 log @Handle asprintf returning NULL. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.25 2005/06/26 19:10:49 christos Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.25 2005/06/26 19:10:49 christos Exp $"); d73 1 a73 1 sprintf(errmsg, "cannot seek temp file"); d81 1 a81 1 sprintf(errmsg, "cannot read temp file"); d101 1 a101 1 sprintf(errmsg, "out of memory"); d108 1 a108 1 sprintf(errmsg, "line too long"); d117 1 a117 1 sprintf(errmsg, "cannot seek temp file"); d128 1 a128 1 sprintf(errmsg, "cannot write temp file"); d163 1 a163 1 sprintf(errmsg, "invalid address"); d225 1 a225 1 sprintf(errmsg, "could not allocate memory"); d235 1 a235 1 sprintf(errmsg, "cannot open temp file"); d251 1 a251 1 sprintf(errmsg, "cannot close temp file"); @ 1.26.48.1 log @Rebase to HEAD as of a few days ago. @ text @d1 1 a1 1 /* $NetBSD$ */ d36 1 a36 1 __RCSID("$NetBSD$"); d73 1 a73 1 seterrmsg("cannot seek temp file"); d81 1 a81 1 seterrmsg("cannot read temp file"); d101 1 a101 1 seterrmsg("out of memory"); d108 1 a108 1 seterrmsg("line too long"); d117 1 a117 1 seterrmsg("cannot seek temp file"); d128 1 a128 1 seterrmsg("cannot write temp file"); d163 1 a163 1 seterrmsg("invalid address"); d225 1 a225 1 seterrmsg("could not allocate memory"); d235 1 a235 1 seterrmsg("cannot open temp file"); d251 1 a251 1 seterrmsg("cannot close temp file"); @ 1.26.42.1 log @sync with head. for a reference, the tree before this commit was tagged as yamt-pagecache-tag8. this commit was splitted into small chunks to avoid a limitation of cvs. ("Protocol error: too many arguments") @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.26 2006/03/17 14:37:14 rumble Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.26 2006/03/17 14:37:14 rumble Exp $"); d73 1 a73 1 seterrmsg("cannot seek temp file"); d81 1 a81 1 seterrmsg("cannot read temp file"); d101 1 a101 1 seterrmsg("out of memory"); d108 1 a108 1 seterrmsg("line too long"); d117 1 a117 1 seterrmsg("cannot seek temp file"); d128 1 a128 1 seterrmsg("cannot write temp file"); d163 1 a163 1 seterrmsg("invalid address"); d225 1 a225 1 seterrmsg("could not allocate memory"); d235 1 a235 1 seterrmsg("cannot open temp file"); d251 1 a251 1 seterrmsg("cannot close temp file"); @ 1.25 log @sprinkle a little const, and now everything compiles with WARNS=3 @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.24 2005/02/17 16:29:26 xtraeme Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.24 2005/02/17 16:29:26 xtraeme Exp $"); d223 7 @ 1.24 log @Kill __P(), use ANSI function declarations. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.23 2003/10/19 01:52:45 wiz Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.23 2003/10/19 01:52:45 wiz Exp $"); d209 1 a209 1 char *tmp; @ 1.23 log @Fix typos in comments. From Jared Yanovich via jmc@@openbsd. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.22 2003/09/05 20:16:50 itojun Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.22 2003/09/05 20:16:50 itojun Exp $"); d58 1 a58 2 get_sbuf_line(lp) line_t *lp; d93 1 a93 2 put_sbuf_line(cs) char *cs; d142 1 a142 2 add_line_node(lp) line_t *lp; d146 1 a146 1 cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */ d155 1 a155 2 get_line_node_addr(lp) line_t *lp; d172 1 a172 2 get_addressed_line_node(n) long n; d206 1 a206 1 open_sbuf() d239 1 a239 1 close_sbuf() d261 1 a261 2 quit(n) int n; d280 1 a280 1 init_buffers() d301 1 a301 5 translit_text(s, len, from, to) char *s; int len; int from; int to; @ 1.22 log @plug memory leak. PR bin/22694 @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.21 2000/04/17 23:37:50 christos Exp $ */ d3 1 a3 1 /* buf.c: This file contains the scratch-file buffer rountines for the d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.21 2000/04/17 23:37:50 christos Exp $"); @ 1.21 log @Obey $TMPDIR for creating the scratch file @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.20 2000/04/04 17:07:29 thorpej Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.20 2000/04/04 17:07:29 thorpej Exp $"); d111 1 d120 1 d131 1 @ 1.20 log @Back out the incorrect change in revision 1.11 of main.c, and actually fix the problem it meant to address by putting extern declarations into ed.h. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.19 1998/11/04 13:41:32 christos Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.19 1998/11/04 13:41:32 christos Exp $"); d43 2 d204 1 a204 1 char sfn[15] = ""; /* scratch file name */ d211 2 d217 9 a225 1 strcpy(sfn, "/tmp/ed.XXXXXX"); d250 5 a254 1 unlink(sfn); d268 5 a272 1 unlink(sfn); @ 1.19 log @add braces @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.18 1998/07/28 05:31:24 mycroft Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.18 1998/07/28 05:31:24 mycroft Exp $"); a200 2 extern int newline_added; @ 1.18 log @Delint. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.17 1997/07/20 06:35:35 thorpej Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.17 1997/07/20 06:35:35 thorpej Exp $"); d164 2 a165 2 } return n; d178 2 a179 2 if (n > on) if (n <= (on + addr_last) >> 1) d182 1 a182 1 else { d187 2 a188 2 else if (n >= on >> 1) d191 1 a191 1 else { d196 1 @ 1.17 log @- Use __RCSID() and __COPYRIGHT(). - Fix (all but two) compiler warnings. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.16 1997/03/22 03:02:17 lukem Exp $ */ d36 1 a36 1 __RCSID("$NetBSD: buf.c,v 1.16 1997/03/22 03:02:17 lukem Exp $"); d256 1 @ 1.16 log @use mkstemp() instead of mktemp() @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.15 1995/04/23 10:07:28 cgd Exp $ */ d31 1 d36 1 a36 1 static char rcsid[] = "$NetBSD: buf.c,v 1.15 1995/04/23 10:07:28 cgd Exp $"; d42 2 @ 1.15 log @clean up RCS Ids slightly, etc. @ text @d1 1 a1 1 /* $NetBSD: buf.c,v 1.14 1995/03/21 09:04:35 cgd Exp $ */ d35 1 a35 1 static char rcsid[] = "$NetBSD: buf.c,v 1.14 1995/03/21 09:04:35 cgd Exp $"; d206 1 a206 1 int u; d209 1 d212 4 a215 2 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) { fprintf(stderr, "%s: %s\n", sfn, strerror(errno)); @ 1.14 log @convert to new RCS id conventions. @ text @d1 1 a1 1 /* $NetBSD$ */ d30 1 d35 1 a35 1 static char rcsid[] = "$NetBSD$"; @ 1.13 log @Renamed insque @ text @d1 2 d31 1 d33 3 @ 1.12 log @Use umask 077 to open buffer file @ text @d135 1 a135 1 insque(lp, cp); @ 1.11 log @`s3' applies the last substitution to the 3rd (or nth) match. Split up the code to several files. @ text @d33 1 d199 2 d202 1 d207 1 d210 1 @ 1.10 log @Added a line-addressing test so this isn't overlooked again. Added BACKWARDS directive to Makefile for compatibility. Completely changed the name space as per FSF's request. Fixed a line-addressing bug introduced by previous `fix', i.e., space preceding a digit acts a plus (+) operator, as in `1 1' == `2', and a plus operator by itself acts as +1, but a space by itself should not equal a plus operator, i.e., `1 ' != `1+' == `2'. @ text @d29 1 a29 2 /* static char sccsid[] = "@@(#)buf.c 5.5 (Talke Studio) 3/28/93"; */ static char rcsid[] = "$Id: buf.c,v 1.17 1993/11/20 12:46:24 alm Exp $"; a31 3 #include #include #include a32 1 #include a35 1 extern char errmsg[]; a37 2 char *sfbuf = NULL; /* scratch file input buffer */ int sfbufsz = 0; /* scratch file input buffer size */ d40 1 a40 1 line_t line0; /* initial node of line queue */ d48 3 d53 1 a53 1 if (lp == &line0) d66 1 a66 1 CKBUF(sfbuf, sfbufsz, len + 1, NULL); a77 3 extern long current_addr; extern long addr_last; d134 1 a134 1 insqueue(lp, cp); d145 1 a145 1 line_t *cp = &line0; d148 1 a148 1 while (cp != lp && (cp = cp->next) != &line0) d150 1 a150 1 if (n && cp == &line0) { d163 1 a163 1 static line_t *lp = &line0; d170 1 a170 1 lp = lp->next; d172 1 a172 1 lp = line0.prev; d174 1 a174 1 lp = lp->prev; d179 1 a179 1 lp = lp->prev; d181 1 a181 1 lp = &line0; d183 1 a183 1 lp = lp->next; d190 2 d198 1 d248 7 d257 1 a257 1 requeue(&line0, &line0); @ 1.9 log @fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2 add global-active routines to replace some bad hacks remove obsolescent Addison-Wesley copyrights @ text @d29 2 a30 1 static char sccsid[] = "@@(#)buf.c 5.5 (Talke Studio) 3/28/93"; d50 1 a50 1 /* gettxt: get a line of text from the scratch file; return pointer d53 1 a53 1 gettxt(lp) d83 2 a84 2 extern long curln; extern long lastln; d86 1 a86 1 /* puttxt: write a line of text to the scratch file and add a line node d89 1 a89 1 puttxt(cs) d108 1 a108 1 len = (s - cs); d119 1 a119 1 /* assert: spl1() */ d128 1 a128 1 lpqueue(lp); d134 1 a134 1 /* lpqueue: add a line node in the editor buffer after the current line */ d136 1 a136 1 lpqueue(lp) d141 1 a141 1 cp = getlp(curln); /* this getlp last! */ d143 2 a144 2 lastln++; curln++; d148 1 a148 1 /* getaddr: return line number of pointer */ d150 1 a150 1 getaddr(lp) d166 1 a166 1 /* getlp: return pointer to a line node in the editor buffer */ d168 1 a168 1 getlp(n) d174 1 a174 1 spl1(); d176 1 a176 1 if (n <= (on + lastln) >> 1) d181 1 a181 1 for (on = lastln; on > n; on--) d193 1 a193 1 spl0(); d200 3 a202 2 /* sbopen: open scratch file */ sbopen() d214 3 a216 2 /* sbclose: close scratch file */ sbclose() d232 1 a232 1 /* quit: remove scratch file and exit */ d247 1 a247 1 /* inited: open scratch buffer; initialize line queue */ d249 1 a249 1 inited() d253 1 a253 1 if (sbopen() < 0) d261 1 a261 1 /* translit: translate characters in a string */ d263 1 a263 1 translit(s, len, from, to) @ 1.8 log @Add RCS identifiers. @ text @d4 1 a4 1 * Copyright (c) 1992 The Regents of the University of California. a6 3 * This code is derived from software contributed to Berkeley by * Rodney Ruddock of the University of Guelph. * a14 7 * 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. d16 1 a16 1 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND d19 1 a19 1 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE a27 1 d29 1 a29 2 /*static char sccsid[] = "from: @@(#)buf.c 5.5 (Berkeley) 3/28/93";*/ static char rcsid[] = "$Id: $"; d69 1 a69 1 len = lp->len & ~ACTV; d107 1 a107 1 len = (s - cs) & ~ACTV; d244 1 a244 1 /* init_buf: open scratch buffer; initialize line queue */ d246 1 a246 1 init_buf() a275 2 @ 1.7 log @fixed undo within a global command (would corrupt the buffer) @ text @d40 2 a41 1 static char sccsid[] = "@@(#)buf.c 5.5 (Berkeley) 3/28/93"; @ 1.6 log @added support for lines of arbitrary length added support for 8-bit data, including NULs @ text @a51 1 extern line_t line0; d58 1 d168 5 a172 1 return (cp != &line0) ? n : 0; d251 38 @ 1.5 log @added man page (draft) fixed `!' command final code cleanup @ text @a42 1 #include d55 2 a65 1 static char buf[MAXLINE]; d77 1 a77 1 return (char *) ERR; d81 2 a82 1 if ((ct = fread(buf, sizeof(char), len, sfp)) < 0 || ct != len) { d85 1 a85 1 return (char *) ERR; d88 2 a89 2 buf[len] = '\0'; return buf; d109 1 a109 1 return (char *) ERR; d114 4 d124 1 a124 1 return (char *) ERR; d134 1 a134 1 return (char *) ERR; d140 1 a140 1 return (*++s == '\0') ? NULL : s; d151 1 a151 1 cp = getptr(curln); /* this getptr last! */ d172 1 a172 4 extern int mutex; extern int sigflags; /* getptr: return pointer to a line node in the editor buffer */ d174 1 a174 1 getptr(n) d242 1 a243 1 fclose(sfp); @ 1.4 log @added an efficient and relatively clean move (`m' command) added ustack checking @ text @d65 1 a65 1 static char txtbuf[MAXLINE]; d81 1 a81 1 if ((ct = fread(txtbuf, sizeof(char), len, sfp)) < 0 || ct != len) { d87 2 a88 2 txtbuf[len] = '\0'; return txtbuf; @ 1.3 log @implemented line marks as pointers marks to lines that are moved, deleted or otherwise changed are cleared undo restores cleared marks added strerror diagnostics @ text @d230 1 @ 1.2 log @updated to alm's ed-0.2 @ text @d75 1 d82 1 d106 1 d117 1 d127 1 d153 14 d209 1 d221 5 a225 1 fclose(sfp); @ 1.1 log @added ed, from Andrew Moore, alm@@netcom.com @ text @d52 1 d75 1 a75 1 fprintf(stderr, "cannot seek temp file\n"); d81 1 a81 1 fprintf(stderr, "cannot read temp file\n"); d104 1 a104 1 fprintf(stderr, "out of memory\n"); d114 1 a114 1 fprintf(stderr, "cannot seek temp file\n"); d123 1 a123 1 fprintf(stderr, "cannot write temp file\n"); d161 1 a161 1 if (n <= (on + lastln) >> 1) { d164 1 a164 1 } else { d170 1 a170 1 if (n >= on >> 1) { d173 1 a173 1 } else { d190 1 a190 1 fprintf(stderr, "cannot open temp file\n"); d196 1 a200 1 unlink(sfn); d203 1 @