]> pd.if.org Git - pd_readline/blob - history.c
More work.
[pd_readline] / history.c
1
2
3 /*  history.c                                          */ 
4 /*  Command history.                                   */ 
5 /*  This code is released to the public domain.        */ 
6 /*  "Share and enjoy...."  ;)                          */  
7 /*  See the UNLICENSE file for details.                */ 
8
9 #include <string.h>   
10 #include <stdio.h> 
11 #include <stdlib.h> 
12
13
14 /*  Helper function, to let us see if a file */ 
15 /*  exists in the current directory.  */ 
16 int fexists(char *fname)
17 {  
18    FILE *fptr;  
19    fptr = fopen(fname, "r") ;     
20    if ( !fptr )  return -1 ;  /* File does not exist in dir. */                  
21    fclose(fptr);  
22    return 0;    /* File DOES exist in dir.  */   
23
24
25
26 /* Helper function to chop newlines off the lines read in. */ 
27 /* Without this being done, an extra newline is inserted */ 
28 /* (which is usually not what is wanted). */ 
29 char *chop(char *s)
30 {
31   s[strcspn(s,"\n")] = '\0'; 
32   return s; 
33 }
34
35
36 /* An array to store the command-history file in. */ 
37 /* Only 20 lines are read. */ 
38 char hist[20][80];  
39
40
41
42 /* Read the file into the array of strings.  */ 
43 void readhistory(char *fname) 
44
45    int retval = fexists(fname); 
46         
47    int i; 
48    if (retval == 0) { 
49           /* File exists, so open it. */  
50           /* We open it in read-write mode so we can */ 
51           /* append new commands to it. */ 
52           FILE *fptr;  
53           fptr = fopen(fname, "rw"); 
54                            
55           for(i=0; i<20; i++)  
56           {  
57                 chop(fgets(hist[i], 80, fptr) );  
58           }
59           
60    }  /* retval == 0  */           
61         
62         else puts("Error! File does not exist. \n");  
63         
64 }       
65
66
67
68