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