histfile.c (21308) | histfile.c (26497) |
---|---|
1/* histfile.c - functions to manipulate the history file. */ 2 3/* Copyright (C) 1989, 1992 Free Software Foundation, Inc. 4 5 This file contains the GNU History Library (the Library), a set of 6 routines for managing the text of previously typed lines. 7 8 The Library is free software; you can redistribute it and/or modify --- 38 unchanged lines hidden (view full) --- 47#endif 48 49#if defined (HAVE_STRING_H) 50# include <string.h> 51#else 52# include <strings.h> 53#endif /* !HAVE_STRING_H */ 54 | 1/* histfile.c - functions to manipulate the history file. */ 2 3/* Copyright (C) 1989, 1992 Free Software Foundation, Inc. 4 5 This file contains the GNU History Library (the Library), a set of 6 routines for managing the text of previously typed lines. 7 8 The Library is free software; you can redistribute it and/or modify --- 38 unchanged lines hidden (view full) --- 47#endif 48 49#if defined (HAVE_STRING_H) 50# include <string.h> 51#else 52# include <strings.h> 53#endif /* !HAVE_STRING_H */ 54 |
55#if defined (__EMX__) 56# ifndef O_BINARY 57# define O_BINARY 0 58# endif 59#else /* !__EMX__ */ 60 /* If we're not compiling for __EMX__, we don't want this at all. Ever. */ 61# undef O_BINARY 62# define O_BINARY 0 63#endif /* !__EMX__ */ 64 |
|
55#include <errno.h> 56#if !defined (errno) 57extern int errno; 58#endif /* !errno */ 59 60#include "history.h" 61#include "histlib.h" 62 | 65#include <errno.h> 66#if !defined (errno) 67extern int errno; 68#endif /* !errno */ 69 70#include "history.h" 71#include "histlib.h" 72 |
73/* Functions imported from shell.c */ 74extern char *get_env_value (); 75 |
|
63extern char *xmalloc (), *xrealloc (); 64 65/* Return the string that should be used in the place of this 66 filename. This only matters when you don't specify the 67 filename to read_history (), or write_history (). */ 68static char * 69history_filename (filename) 70 char *filename; 71{ 72 char *return_val, *home; 73 int home_len; 74 75 return_val = filename ? savestring (filename) : (char *)NULL; 76 77 if (return_val) 78 return (return_val); 79 | 76extern char *xmalloc (), *xrealloc (); 77 78/* Return the string that should be used in the place of this 79 filename. This only matters when you don't specify the 80 filename to read_history (), or write_history (). */ 81static char * 82history_filename (filename) 83 char *filename; 84{ 85 char *return_val, *home; 86 int home_len; 87 88 return_val = filename ? savestring (filename) : (char *)NULL; 89 90 if (return_val) 91 return (return_val); 92 |
80 home = getenv ("HOME"); | 93 home = get_env_value ("HOME"); |
81 82 if (home == 0) 83 { 84 home = "."; 85 home_len = 1; 86 } 87 else 88 home_len = strlen (home); --- 27 unchanged lines hidden (view full) --- 116 int from, to; 117{ 118 register int line_start, line_end; 119 char *input, *buffer = (char *)NULL; 120 int file, current_line; 121 struct stat finfo; 122 123 input = history_filename (filename); | 94 95 if (home == 0) 96 { 97 home = "."; 98 home_len = 1; 99 } 100 else 101 home_len = strlen (home); --- 27 unchanged lines hidden (view full) --- 129 int from, to; 130{ 131 register int line_start, line_end; 132 char *input, *buffer = (char *)NULL; 133 int file, current_line; 134 struct stat finfo; 135 136 input = history_filename (filename); |
124 file = open (input, O_RDONLY, 0666); | 137 file = open (input, O_RDONLY|O_BINARY, 0666); |
125 126 if ((file < 0) || (fstat (file, &finfo) == -1)) 127 goto error_and_exit; 128 129 buffer = xmalloc ((int)finfo.st_size + 1); 130 131 if (read (file, buffer, finfo.st_size) != finfo.st_size) 132 { --- 56 unchanged lines hidden (view full) --- 189 If FNAME is NULL, then use ~/.history. */ 190int 191history_truncate_file (fname, lines) 192 char *fname; 193 register int lines; 194{ 195 register int i; 196 int file, chars_read; | 138 139 if ((file < 0) || (fstat (file, &finfo) == -1)) 140 goto error_and_exit; 141 142 buffer = xmalloc ((int)finfo.st_size + 1); 143 144 if (read (file, buffer, finfo.st_size) != finfo.st_size) 145 { --- 56 unchanged lines hidden (view full) --- 202 If FNAME is NULL, then use ~/.history. */ 203int 204history_truncate_file (fname, lines) 205 char *fname; 206 register int lines; 207{ 208 register int i; 209 int file, chars_read; |
197 char *buffer = (char *)NULL, *filename; | 210 char *buffer, *filename; |
198 struct stat finfo; 199 | 211 struct stat finfo; 212 |
213 buffer = (char *)NULL; |
|
200 filename = history_filename (fname); | 214 filename = history_filename (fname); |
201 file = open (filename, O_RDONLY, 0666); | 215 file = open (filename, O_RDONLY|O_BINARY, 0666); |
202 203 if (file == -1 || fstat (file, &finfo) == -1) 204 goto truncate_exit; 205 206 buffer = xmalloc ((int)finfo.st_size + 1); 207 chars_read = read (file, buffer, finfo.st_size); 208 close (file); 209 --- 17 unchanged lines hidden (view full) --- 227 if (buffer[i] == '\n') 228 { 229 i++; 230 break; 231 } 232 233 /* Write only if there are more lines in the file than we want to 234 truncate to. */ | 216 217 if (file == -1 || fstat (file, &finfo) == -1) 218 goto truncate_exit; 219 220 buffer = xmalloc ((int)finfo.st_size + 1); 221 chars_read = read (file, buffer, finfo.st_size); 222 close (file); 223 --- 17 unchanged lines hidden (view full) --- 241 if (buffer[i] == '\n') 242 { 243 i++; 244 break; 245 } 246 247 /* Write only if there are more lines in the file than we want to 248 truncate to. */ |
235 if (i && ((file = open (filename, O_WRONLY|O_TRUNC, 0666)) != -1)) | 249 if (i && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0666)) != -1)) |
236 { 237 write (file, buffer + i, finfo.st_size - i); 238 close (file); 239 } 240 241 truncate_exit: 242 243 FREE (buffer); --- 6 unchanged lines hidden (view full) --- 250 from the history list to FILENAME. OVERWRITE is non-zero if you 251 wish to replace FILENAME with the entries. */ 252static int 253history_do_write (filename, nelements, overwrite) 254 char *filename; 255 int nelements, overwrite; 256{ 257 register int i; | 250 { 251 write (file, buffer + i, finfo.st_size - i); 252 close (file); 253 } 254 255 truncate_exit: 256 257 FREE (buffer); --- 6 unchanged lines hidden (view full) --- 264 from the history list to FILENAME. OVERWRITE is non-zero if you 265 wish to replace FILENAME with the entries. */ 266static int 267history_do_write (filename, nelements, overwrite) 268 char *filename; 269 int nelements, overwrite; 270{ 271 register int i; |
258 char *output = history_filename (filename); | 272 char *output; |
259 int file, mode; 260 | 273 int file, mode; 274 |
261 mode = overwrite ? O_WRONLY | O_CREAT | O_TRUNC : O_WRONLY | O_APPEND; | 275 mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY; 276 output = history_filename (filename); |
262 263 if ((file = open (output, mode, 0666)) == -1) 264 { 265 FREE (output); 266 return (errno); 267 } 268 269 if (nelements > history_length) --- 55 unchanged lines hidden --- | 277 278 if ((file = open (output, mode, 0666)) == -1) 279 { 280 FREE (output); 281 return (errno); 282 } 283 284 if (nelements > history_length) --- 55 unchanged lines hidden --- |