1 /* -*- mode: C; c-basic-offset: 4; -*- */
2 /*
3    bogosort - sorts or doesn't sort files or its standard input
4 
5    Copyright (C) 2000, 2001, 2002 Ulrik Haugen
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 /* $Id: getlines.c,v 1.3 2002/05/04 20:22:22 qha Exp $ */
22 
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 
27 #include <sys/types.h>
28 #include "system.h"
29 
30 #include <stdio.h>
31 #include "getlines.h"
32 
33 #define CHARSTEP 80
34 #define LINESTEP 1024
35 
36 char *xmalloc();
37 char *xrealloc();
38 char *xstrdup();
39 
40 /*
41  * getlines: return a pointer to a newly allocated array of newly allocated
42  * strings containing steam. the last string is followed by a NULL-pointer.
43  */
44 char **
getlines(FILE * stream)45 getlines(FILE *stream)
46 {
47     int numlines = 0, allocated = 0;
48     char *s = NULL;
49     char **ss = NULL;
50 
51     /* read all lines available */
52     while ((s = get_line(stream)) != NULL) {
53 	if (numlines == allocated)		/* make more room if needed */
54 	    ss = (char **)xrealloc(ss,
55 		    (allocated += LINESTEP) * sizeof (char *));
56 	ss[numlines++] = s;
57     }
58 
59     if (numlines == allocated)			/* make more room if needed */
60 	ss = (char **)xrealloc(ss, ++allocated * sizeof (char *));
61     ss[numlines] = NULL;
62 
63     return ss;
64 }
65 
66 
67 /*
68  * getmorelines: fill out lines whith stream and return a pointer to the
69  * reallocated array of some old and some newly allocated strings. the last
70  * string is followed by a NULL-pointer.
71  */
72 char **
getmorelines(FILE * stream,char ** lines)73 getmorelines(FILE *stream, char **lines)
74 {
75     int numlines = 0, allocated = 0;
76     char *s = NULL;
77     char **ss = lines;
78 
79     /* count the lines in ss */
80     for (; ss[numlines] != NULL; numlines++) ;
81     allocated = numlines + 1;	/* this is a lie, but it's close enough */
82 
83     /* read all lines available */
84     while ((s = get_line(stream)) != NULL) {
85 	if (numlines == allocated)		/* make more room if needed */
86 	    ss = (char **)xrealloc(ss,
87 		    (allocated += LINESTEP) * sizeof (char *));
88 	ss[numlines++] = s;
89     }
90 
91     if (numlines == allocated)			/* make more room if needed */
92 	ss = (char **)xrealloc(ss, ++allocated * sizeof (char *));
93     ss[numlines] = NULL;
94 
95     return ss;
96 }
97 
98 /*
99  * getline: return a pointer to a newly allocated string containing the next
100  * line in stream, return NULL on EOF
101  */
102 char *
get_line(FILE * stream)103 get_line(FILE *stream)
104 {
105     int c, numchars = 0, allocated = 0;
106     char *s = NULL;
107 
108     /* read all chars available */
109     for (c = getc(stream); (c != EOF) && (c != '\n'); c = getc(stream)) {
110 	if (numchars == allocated)		/* make more room if needed */
111 	    s = xrealloc(s, (allocated += CHARSTEP) * sizeof (char));
112 	s[numchars++] = (char) c;
113     }
114 
115     if ((c == EOF) && (numchars == 0)) return NULL;
116 
117     if (numchars == allocated)			/* make more room if needed */
118 	s = xrealloc(s, (allocated += 2) * sizeof (char));
119     s[numchars++] = '\n';
120     s[numchars] = '\0';
121 
122     return s;
123 }
124