-/* $NetBSD: hack.topl.c,v 1.10 2009/06/07 20:13:18 dholland Exp $ */
+/* $NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $ */
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: hack.topl.c,v 1.10 2009/06/07 20:13:18 dholland Exp $");
+__RCSID("$NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $");
#endif /* not lint */
#include <stdlib.h>
#include "hack.h"
#include "extern.h"
-char toplines[BUFSZ];
-xchar tlx, tly; /* set by pline; used by addtopl */
+static char toplines[BUFSZ];
+static xchar tlx, tly; /* set by pline; used by addtopl */
-struct topl {
+static struct topl {
struct topl *next_topl;
char *topl_text;
} *old_toplines, *last_redone_topl;
#define OTLMAX 20 /* max nr of old toplines remembered */
+static void redotoplin(void);
+static void xmore(const char *);
+
int
doredotopl(void)
{
return (0);
}
-void
+static void
redotoplin(void)
{
home();
!strcmp(toplines, old_toplines->topl_text))
return;
last_redone_topl = 0;
- tl = (struct topl *)
- alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 1));
+ tl = alloc(strlen(toplines) + sizeof(*tl) + 1);
tl->next_topl = old_toplines;
tl->topl_text = (char *) (tl + 1);
(void) strcpy(tl->topl_text, toplines);
tl = tl->next_topl;
}
if (tl && tl->next_topl) {
- free((char *) tl->next_topl);
+ free(tl->next_topl);
tl->next_topl = 0;
}
}
}
/* s = allowed chars besides space/return */
-void
+static void
xmore(const char *s)
{
if (flags.toplin) {
{
char pbuf[BUFSZ];
char *bp = pbuf, *tl;
- int n, n0;
+ int n, n0, tlpos, dead;
if (!line || !*line)
return;
if (flags.toplin == 1)
more();
remember_topl();
+ dead = 0;
toplines[0] = 0;
- while (n0) {
+ while (n0 && !dead) {
if (n0 >= CO) {
/* look for appropriate cut point */
n0 = 0;
if (!n0)
n0 = CO - 2;
}
- (void) strncpy((tl = eos(toplines)), bp, n0);
+ tlpos = strlen(toplines);
+ tl = toplines + tlpos;
+ /* avoid overflow */
+ if (tlpos + n0 > (int)sizeof(toplines) - 1) {
+ n0 = sizeof(toplines) - 1 - tlpos;
+ dead = 1;
+ }
+ (void) memcpy(tl, bp, n0);
tl[n0] = 0;
bp += n0;
n0 = strlen(bp);
if (n0 && tl[0])
- (void) strcat(tl, "\n");
+ (void) strlcat(toplines, "\n", sizeof(toplines));
}
redotoplin();
}