1 /* histfile.c - functions to manipulate the history file. */
3 /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
5 This file contains the GNU History Library (the Library), a set of
6 routines for managing the text of previously typed lines.
8 The Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
13 The Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* The goal is to make the implementation transparent, so that you
24 don't have to know what data types are used, just what functions
25 you can call. I think I have done that. */
26 #define READLINE_LIBRARY
28 #if defined (HAVE_CONFIG_H)
34 #include <sys/types.h>
36 # include <sys/file.h>
41 #if defined (HAVE_STDLIB_H)
44 # include "ansi_stdlib.h"
45 #endif /* HAVE_STDLIB_H */
47 #if defined (HAVE_UNISTD_H)
51 #if defined (HAVE_STRING_H)
55 #endif /* !HAVE_STRING_H */
62 /* If we're not compiling for __EMX__, we don't want this at all. Ever. */
75 /* Functions imported from shell.c */
76 extern char *get_env_value ();
78 extern char *xmalloc (), *xrealloc ();
80 /* Return the string that should be used in the place of this
81 filename. This only matters when you don't specify the
82 filename to read_history (), or write_history (). */
84 history_filename (filename)
87 char *return_val, *home;
90 return_val = filename ? savestring (filename) : (char *)NULL;
95 home = get_env_value ("HOME");
103 home_len = strlen (home);
105 return_val = xmalloc (2 + home_len + 8); /* strlen(".history") == 8 */
106 strcpy (return_val, home);
107 return_val[home_len] = '/';
108 strcpy (return_val + home_len + 1, ".history");
113 /* Add the contents of FILENAME to the history list, a line at a time.
114 If FILENAME is NULL, then read from ~/.history. Returns 0 if
115 successful, or errno if not. */
117 read_history (filename)
120 return (read_history_range (filename, 0, -1));
123 /* Read a range of lines from FILENAME, adding them to the history list.
124 Start reading at the FROM'th line and end at the TO'th. If FROM
125 is zero, start at the beginning. If TO is less than FROM, read
126 until the end of the file. If FILENAME is NULL, then read from
127 ~/.history. Returns 0 if successful, or errno if not. */
129 read_history_range (filename, from, to)
133 register int line_start, line_end;
134 char *input, *buffer;
135 int file, current_line;
139 buffer = (char *)NULL;
140 input = history_filename (filename);
141 file = open (input, O_RDONLY|O_BINARY, 0666);
145 /* MSDOS doesn't allow leading dots in file names. Try again
146 with the dot replaced by an underscore. */
147 if (file < 0 && !filename)
149 input[strlen (input) - 8] = '_';
150 file = open (input, O_RDONLY|O_BINARY, 0666);
153 if ((file < 0) || (fstat (file, &finfo) == -1))
156 file_size = (size_t)finfo.st_size;
158 /* check for overflow on very large files */
159 if (file_size != finfo.st_size || file_size + 1 < file_size)
167 buffer = xmalloc (file_size + 1);
169 if (read (file, buffer, file_size) != file_size)
171 if (read (file, buffer, file_size) < 0)
186 /* Set TO to larger than end of file if negative. */
190 /* Start at beginning of file, work to end. */
191 line_start = line_end = current_line = 0;
193 /* Skip lines until we are at FROM. */
194 while (line_start < file_size && current_line < from)
196 for (line_end = line_start; line_end < file_size; line_end++)
197 if (buffer[line_end] == '\n')
200 line_start = line_end + 1;
201 if (current_line == from)
206 /* If there are lines left to gobble, then gobble them now. */
207 for (line_end = line_start; line_end < file_size; line_end++)
208 if (buffer[line_end] == '\n')
210 buffer[line_end] = '\0';
212 if (buffer[line_start])
213 add_history (buffer + line_start);
217 if (current_line >= to)
220 line_start = line_end + 1;
229 /* Truncate the history file FNAME, leaving only LINES trailing lines.
230 If FNAME is NULL, then use ~/.history. */
232 history_truncate_file (fname, lines)
237 int file, chars_read;
238 char *buffer, *filename;
242 buffer = (char *)NULL;
243 filename = history_filename (fname);
244 file = open (filename, O_RDONLY|O_BINARY, 0666);
247 /* MSDOS doesn't allow leading dots in file names. Try again
248 with the dot replaced by an underscore. */
249 if (file < 0 && !fname)
251 filename[strlen (filename) - 8] = '_';
252 file = open (filename, O_RDONLY|O_BINARY, 0666);
256 if (file == -1 || fstat (file, &finfo) == -1)
259 file_size = (size_t)finfo.st_size;
261 /* check for overflow on very large files */
262 if (file_size != finfo.st_size || file_size + 1 < file_size)
271 buffer = xmalloc (file_size + 1);
272 chars_read = read (file, buffer, file_size);
278 /* Count backwards from the end of buffer until we have passed
280 for (i = chars_read - 1; lines && i; i--)
282 if (buffer[i] == '\n')
286 /* If this is the first line, then the file contains exactly the
287 number of lines we want to truncate to, so we don't need to do
288 anything. It's the first line if we don't find a newline between
289 the current value of i and 0. Otherwise, write from the start of
290 this line until the end of the buffer. */
292 if (buffer[i] == '\n')
298 /* Write only if there are more lines in the file than we want to
300 if (i && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0600)) != -1))
302 write (file, buffer + i, file_size - i);
304 #if defined (__BEOS__)
305 /* BeOS ignores O_TRUNC. */
306 ftruncate (file, file_size - i);
320 /* Workhorse function for writing history. Writes NELEMENT entries
321 from the history list to FILENAME. OVERWRITE is non-zero if you
322 wish to replace FILENAME with the entries. */
324 history_do_write (filename, nelements, overwrite)
326 int nelements, overwrite;
332 mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY;
333 output = history_filename (filename);
335 if ((file = open (output, mode, 0600)) == -1)
338 /* MSDOS doesn't allow leading dots in file names. If this is
339 the default file name, try again with the dot replaced by an
343 output[strlen (output) - 8] = '_';
344 if ((file = open (output, mode, 0600)) == -1)
356 if (nelements > history_length)
357 nelements = history_length;
359 /* Build a buffer of all the lines to write, and write them in one syscall.
360 Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */
362 HIST_ENTRY **the_history; /* local */
367 the_history = history_list ();
368 /* Calculate the total number of bytes to write. */
369 for (buffer_size = 0, i = history_length - nelements; i < history_length; i++)
370 buffer_size += 1 + strlen (the_history[i]->line);
372 /* Allocate the buffer, and fill it. */
373 buffer = xmalloc (buffer_size);
375 for (j = 0, i = history_length - nelements; i < history_length; i++)
377 strcpy (buffer + j, the_history[i]->line);
378 j += strlen (the_history[i]->line);
382 write (file, buffer, buffer_size);
393 /* Append NELEMENT entries to FILENAME. The entries appended are from
394 the end of the list minus NELEMENTs up to the end of the list. */
396 append_history (nelements, filename)
400 return (history_do_write (filename, nelements, HISTORY_APPEND));
403 /* Overwrite FILENAME with the current history. If FILENAME is NULL,
404 then write the history list to ~/.history. Values returned
405 are as in read_history ().*/
407 write_history (filename)
410 return (history_do_write (filename, history_length, HISTORY_OVERWRITE));