summaryrefslogtreecommitdiff
path: root/history.c
diff options
context:
space:
mode:
authorToby Goodwin <tjg@star.le.ac.uk>2003-08-13 13:51:14 +0100
committerToby Goodwin <toby@paccrat.org>2015-05-16 21:22:10 +0100
commitd884971193ef4934424b77ae862e8b0ecf70bab3 (patch)
tree700b8fd63be483aaa2a737036a3f7833fdf7b26a /history.c
parent77b18786228a8cf9f58aee09677440c61dc218a9 (diff)
release: rc-1.7.1
Diffstat (limited to 'history.c')
-rw-r--r--history.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/history.c b/history.c
index 2993a69..1f22f2e 100644
--- a/history.c
+++ b/history.c
@@ -78,6 +78,8 @@ static char *sub(char *s, char *old, char *new) {
char *t, *u;
t = isin(s, old);
+ if (!t)
+ return s;
u = newstr();
*t = '\0';
@@ -206,9 +208,9 @@ static char *readhistoryfile(char **last) {
exit(1);
}
- size = 0;
- count = 0;
- buf = ealloc(size = CHUNKSIZE);
+ size = CHUNKSIZE;
+ buf = ealloc(size);
+ buf[0] = '\0'; count = 1; /* sentinel */
while ((nread = fread(buf + count, sizeof (char), size - count, histfile)) > 0) {
count += nread;
if (size - count == 0)
@@ -222,32 +224,30 @@ static char *readhistoryfile(char **last) {
return buf;
}
-static char *getcommand() {
+static char *getcommand(void) {
char *s, *t;
static char *hist = NULL, *last;
if (hist == NULL) {
hist = readhistoryfile(&last);
- *--last = '\0'; /* trim final newline */
+ *--last = '\0'; /* replaces final newline */
+ ++hist; /* start beyond sentinel */
}
again: s = last;
if (s < hist)
return NULL;
- while (*--s != '\n')
- if (s <= hist) {
- last = hist - 1;
- return hist;
- }
+ while (s >= hist && *s != '\n')
+ --s;
*s = '\0';
last = s++;
/*
* if the command contains the "me" character at the start of the line
- * or after any of [`{|()@] then try again
+ * or after any of [`{|()@/] then try again
*/
- for (t = s; *t != '\0'; t++)
+ for (t = s; *t != '\0'; ++t)
if (*t == me) {
char *u = t - 1;
while (u >= s && (*u == ' ' || *u == '\t'))