fgetlr.c (2663B)
1 /************************************************************************ 2 * * 3 * Copyright (c) 1982, Fred Fish * 4 * All Rights Reserved * 5 * * 6 * This software and/or documentation is released for public * 7 * distribution for personal, non-commercial use only. * 8 * Limited rights to use, modify, and redistribute are hereby * 9 * granted for non-commercial purposes, provided that all * 10 * copyright notices remain intact and all changes are clearly * 11 * documented. The author makes no warranty of any kind with * 12 * respect to this product and explicitly disclaims any implied * 13 * warranties of merchantability or fitness for any particular * 14 * purpose. * 15 * * 16 ************************************************************************ 17 */ 18 19 20 /* 21 * LIBRARY FUNCTION 22 * 23 * fgetlr get logical record from a file 24 * 25 * KEY WORDS 26 * 27 * fgetlr 28 * string functions 29 * 30 * SYNOPSIS 31 * 32 * char *fgetlr(bp,bpsize,fp) 33 * char *bp; 34 * int bpsize; 35 * FILE *fp; 36 * 37 * DESCRIPTION 38 * 39 * Reads the next logical record from stream "fp" into buffer "bp" 40 * until next unescaped newline, "bpsize" minus one characters 41 * have been read, end of file, or read error. 42 * The last character read is followed by a NULL. 43 * 44 * A logical record may span several physical records by having 45 * each newline escaped with the standard C escape character 46 * (backslash). 47 * 48 * This is particularly useful for things like the termcap 49 * file, where a single entry is too long for one physical 50 * line, yet needs to be treated as a single record. 51 * 52 * Returns its first argument unless an end of file or read 53 * error occurs prior to any characters being read. 54 * 55 * BUGS 56 * 57 * The only way to know if read was terminated due to buffer size 58 * limitation is to test for a newline before the terminating 59 * null. 60 * 61 */ 62 63 #include <stdio.h> 64 65 /* 66 * PSEUDO CODE 67 * 68 * Begin fgetlr 69 * If read fails then 70 * Return NULL. 71 * Else 72 * Find out how many characters were read. 73 * Initialize pointer to terminating null. 74 * If last char read was newline then 75 * If newline was escaped then 76 * Replace backslash with the newline. 77 * Replace newline with null. 78 * Read and append more. 79 * End if 80 * End if 81 * Return buffer pointer. 82 * End if 83 * End fgetlr 84 * 85 */ 86 87 char *fgetlr(bp,bpsize,fp) 88 char *bp; 89 int bpsize; 90 FILE *fp; 91 { 92 int numch; 93 char *cp; 94 95 if (fgets(bp,bpsize,fp) == NULL) { 96 return(NULL); 97 } else { 98 numch = strlen(bp); 99 cp = &bp[numch]; 100 if (*--cp == '\n') { 101 if (numch > 1 && *--cp == '\\') { 102 *cp++ = '\n'; 103 *cp = NULL; 104 fgetlr(cp,bpsize-numch+1,fp); 105 } 106 } 107 return(bp); 108 } 109 }