File: util.c

package info (click to toggle)
noflushd 2.7.5-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 792 kB
  • ctags: 302
  • sloc: ansic: 2,537; sh: 1,142; makefile: 85
file content (163 lines) | stat: -rw-r--r-- 2,921 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * <util.c>
 * Convenience functions for noflushd.
 * 
 * (C) 2000 Daniel Kobras <kobras@linux.de>
 * 
 * $Id: util.c,v 1.8 2003/09/09 20:56:49 nold Exp $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */             

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "bug.h"
#include "util.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>



char *get_line(FILE *fp)
{
	char *c, *buf = NULL;
	const size_t inc = 1024;
	size_t	size = 0;

	if (!fp || feof(fp))
		return NULL;
	
	do {
		size += inc;
		buf = (char *) realloc(buf, size);
		if (!buf)
			goto _out;
		
		c = buf+(size-inc);
		if (!fgets(c, inc, fp) && size==inc) {
			free(buf);
			buf=NULL;
			goto _out;
		}
		size = strlen(buf);
	} while (!feof(fp) && buf[size-1] != '\n');
		
_out:
	return buf;
}

void release_line(char *line)
{
	free(line);
}

/* There's obvious room for optimisations here. */
void next_line(FILE *fp)
{
	release_line(get_line(fp));
}
static char *skip_space(char *str)
{
	while (*str && isspace((int) *str))
		str++;
	
	return str;
}

static char *skip_nospace(char *str)
{
	while (*str && !isspace((int) *str))
		str++;

	return str;
}

static char *__get_entry(char *str, int num)
{
	goto _in;
	
	while (*str && num--) {
		str = skip_nospace(str);
_in:
		str = skip_space(str);
	};

	return *str ? str : NULL;
}
	
char *_get_entry(char *str, int num)
{
	char *end;

	if (!(str = __get_entry(str, num)))
		return NULL;
	end = skip_nospace(str);
	*end = '\0';
	return str;
}

char *get_entry(char *str, int num)
{
	char *end, *newstr;
	size_t len;
	
	if (!(str = __get_entry(str, num)))
		return NULL;
	end = skip_nospace(str);
	len = end-str;
	newstr = malloc(len+1);
	if (!newstr)
		return NULL;
	strncpy(newstr, str, len);
	newstr[len] = '\0';
	return newstr;
}

void release_entry(char *str)
{
	free(str);
}

char *_get_remains(char *str, int num)
{
	return __get_entry(str, num);
}

char *get_remains(char *str, int num)
{
	char *start;
	int len;
	
	if (!(start = __get_entry(str, num)))
		return NULL;
	start = strdup(start);
	len = strlen(start);
	if (len > 0 && start[len-1]=='\n')
		start[len-1]='\0';

	return start;
}
	
void release_remains(char *str)
{
	free(str);
}