wmmenu: Update Makefile.
[dockapps.git] / wmcalc / readln.c
blob23447e49a5a37c34318e2f6b49625277cb04b20b
1 /* readln.c - Edward H. Flora - ehflora@ksu.edu */
2 /* Last Modified: 2/24/98 */
3 /*
4 * This function reads a string from a file pointed to by fp until a
5 * new line (EOLN) or an end of file is encountered. It returns a pointer
6 * to the string. The new line character is included in the string returned
7 * by the function.
8 */
10 /* EXTERNALS:
11 ******************
12 * Functions Called:
13 * Standard Libraries.
15 * Header Files Included:
16 * Standard Includes only.
19 /* PORTABILITY:
20 ******************
21 * Coded in ANSI C, using ANSI prototypes.
24 /****** Include Files *************************************************/
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #define EOLN '\n' /* Defines the new line character */
30 #define SIZE1 10 /* Defines the increment to increase the */
31 /* string by until a newline of EOF is found */
33 /****** Function to Read a Line from fp *******************************/
34 char *readln(FILE *fp){
35 char *tmp, *t1, *t2;
36 int count = 0, s1 = SIZE1;
38 if((tmp = malloc(SIZE1*sizeof(char))) == NULL)return NULL;
39 /* If cannot allocate memory */
40 if((t1 = fgets(tmp, SIZE1, fp)) == NULL){
41 free(tmp);
42 return NULL;
43 } /* If there is no string to be read */
44 while(strchr(t1,EOLN) == NULL){ /* Loop while EOLN is not in the string */
45 count += strlen(t1);
46 if((s1 - count) > 1)break;
47 s1 +=SIZE1;
48 if((t2 = realloc(tmp, s1*sizeof(char))) == NULL){
49 free(tmp);
50 return NULL;
51 } /* If cannot allocate more memory */
52 else tmp = t2;
53 if((t1 = fgets((tmp+count),((s1 - count)),fp)) == NULL)break;
54 } /* End of While Loop */
55 if((t2 = realloc(tmp,strlen(tmp)+1)) == NULL)free(tmp);
56 return (t2);
58 /****** End of Function readln ****************************************/