Mercurial > noffle
comparison src/util.c @ 43:2842f50feb55 noffle
[svn] * client.c, client.h, common.h, config.c, config.h, content.c, content.h,
control.c, control.h, database.c, database.h, dynamicstring.c,
dynamicstring.h, fetch.c, fetch.h, fetchlist.c, fetchlist.h, group.c,
group.h, itemlist.c, itemlist.h, lock.c, lock.h, log.c, log.h, noffle.c,
online.c, online.h, outgoing.c, outgoing.h, over.c, over.h, post.c, post.h,
protocol.c, protocol.h, pseudo.c, pseudo.h, request.c, request.h, server.c,
server.h, util.c, util.h, wildmat.c, wildmat.h: Moved files to the
subdirectory src/
* Makefile.am, acconfig.h, configure.in, docs/Makefile.am, src/Makefile.am,
Makefile.in, aclocal.m4, config.h.in, configure, install-sh, missing,
mkinstalldirs, stamp-h.in, docs/Makefile.in, src/Makefile.in: Added files.
They are used by aclocal, autoheader, autoconf and automake.
* src/config.c, src/config.h: Renamed to configfile.c and configfile.h,
because configure will generate a config.h file itself.
* src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c,
src/group.c, src/lock.c, src/noffle.c, src/online.c, src/outgoing.c,
src/over.c, src/pseudo.c, src/request.c, src/server.c, src/util.c:
Changed '#include "config.h"' to '#include "configfile.h"'.
* src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c,
src/group.c, src/lock.c, src/online.c, src/outgoing.c, src/post.c,
src/protocol.c, src/request.c, src/server.c: Files now #include <config.h>.
Added missing <stdio.h>. This removes the warnings about snprintf() not
being declared.
* Makefile: Removed. This is now generated by configure.
author | uh1763 |
---|---|
date | Fri, 05 May 2000 22:45:56 +0100 |
parents | |
children | 32ba1198c6fa |
comparison
equal
deleted
inserted
replaced
42:2467ff423c15 | 43:2842f50feb55 |
---|---|
1 /* | |
2 util.c | |
3 | |
4 $Id: util.c 49 2000-05-05 21:45:56Z uh1763 $ | |
5 */ | |
6 | |
7 #include "util.h" | |
8 #include <errno.h> | |
9 #include <ctype.h> | |
10 #include <sys/types.h> | |
11 #include <sys/stat.h> | |
12 #include <fcntl.h> | |
13 #include <time.h> | |
14 #include <unistd.h> | |
15 #include "configfile.h" | |
16 #include "log.h" | |
17 #include "wildmat.h" | |
18 | |
19 static const char * | |
20 nextWhiteSpace( const char *p ) | |
21 { | |
22 while ( *p && ! isspace( *p ) ) | |
23 ++p; | |
24 return p; | |
25 } | |
26 | |
27 static const char * | |
28 nextNonWhiteSpace( const char *p ) | |
29 { | |
30 while ( *p && isspace( *p ) ) | |
31 ++p; | |
32 return p; | |
33 } | |
34 | |
35 const char * | |
36 Utl_restOfLn( const char *line, unsigned int token ) | |
37 { | |
38 unsigned int i; | |
39 const char *p; | |
40 | |
41 p = line; | |
42 for ( i = 0; i < token; ++i ) | |
43 { | |
44 p = nextNonWhiteSpace( p ); | |
45 p = nextWhiteSpace( p ); | |
46 } | |
47 p = nextNonWhiteSpace( p ); | |
48 return p; | |
49 } | |
50 | |
51 const char * | |
52 Utl_getLn( Str result, const char *pos ) | |
53 { | |
54 int len = 0; | |
55 const char *p = pos; | |
56 | |
57 if ( ! p ) | |
58 return NULL; | |
59 while ( *p != '\n' ) | |
60 { | |
61 if ( *p == '\0' ) | |
62 { | |
63 if ( len > 0 ) | |
64 Log_err( "Line not terminated by newline: '%s'", pos ); | |
65 return NULL; | |
66 } | |
67 *(result++) = *(p++); | |
68 ++len; | |
69 if ( len >= MAXCHAR - 1 ) | |
70 { | |
71 *result = '\0'; | |
72 Log_err( "Utl_getLn: line too long: %s", result ); | |
73 return ++p; | |
74 } | |
75 } | |
76 *result = '\0'; | |
77 return ++p; | |
78 | |
79 } | |
80 | |
81 const char * | |
82 Utl_ungetLn( const char *str, const char *p ) | |
83 { | |
84 if ( str == p ) | |
85 return FALSE; | |
86 --p; | |
87 if ( *p != '\n' ) | |
88 { | |
89 Log_dbg( "Utl_ungetLn: not at beginning of line" ); | |
90 return NULL; | |
91 } | |
92 --p; | |
93 while ( TRUE ) | |
94 { | |
95 if ( p == str ) | |
96 return p; | |
97 if ( *p == '\n' ) | |
98 return p + 1; | |
99 --p; | |
100 } | |
101 } | |
102 | |
103 const char * | |
104 Utl_getHeaderLn( Str result, const char *p ) | |
105 { | |
106 const char * res = Utl_getLn( result, p ); | |
107 | |
108 /* Look for followon line if this isn't a blank line. */ | |
109 if ( res != NULL && !isspace( result[ 0 ] ) ) | |
110 while ( res != NULL && res[ 0 ] != '\n' && isspace( res[ 0 ] ) ) | |
111 { | |
112 Str nextLine; | |
113 const char *here; | |
114 char *next; | |
115 | |
116 here = res; | |
117 res = Utl_getLn( nextLine, res ); | |
118 next = Utl_stripWhiteSpace( nextLine ); | |
119 | |
120 if ( next[ 0 ] != '\0' ) | |
121 { | |
122 Utl_catStr( result, " " ); | |
123 Utl_catStr( result, next ); | |
124 } | |
125 else | |
126 { | |
127 res = here; | |
128 break; | |
129 } | |
130 } | |
131 | |
132 return res; | |
133 } | |
134 | |
135 void | |
136 Utl_toLower( Str line ) | |
137 { | |
138 char *p; | |
139 | |
140 p = line; | |
141 while ( *p ) | |
142 { | |
143 *p = tolower( *p ); | |
144 ++p; | |
145 } | |
146 } | |
147 | |
148 char * | |
149 Utl_stripWhiteSpace( char *line ) | |
150 { | |
151 char *p; | |
152 | |
153 while ( isspace( *line ) ) | |
154 ++line; | |
155 p = line + strlen( line ) - 1; | |
156 while ( isspace( *p ) ) | |
157 { | |
158 *p = '\0'; | |
159 --p; | |
160 } | |
161 return line; | |
162 } | |
163 | |
164 void | |
165 Utl_stripComment( char *line ) | |
166 { | |
167 for ( ; *line != '\0'; line++ ) | |
168 if ( *line =='#' ) | |
169 { | |
170 *line = '\0'; | |
171 break; | |
172 } | |
173 } | |
174 | |
175 void | |
176 Utl_cpyStr( Str dst, const char *src ) | |
177 { | |
178 dst[ 0 ] = '\0'; | |
179 strncat( dst, src, MAXCHAR ); | |
180 } | |
181 | |
182 void | |
183 Utl_cpyStrN( Str dst, const char *src, size_t n ) | |
184 { | |
185 if ( n > MAXCHAR ) | |
186 n = MAXCHAR; | |
187 dst[ 0 ] = '\0'; | |
188 strncat( dst, src, n ); | |
189 } | |
190 | |
191 void | |
192 Utl_catStr( Str dst, const char *src ) | |
193 { | |
194 strncat( dst, src, MAXCHAR - strlen( dst ) ); | |
195 } | |
196 | |
197 void | |
198 Utl_catStrN( Str dst, const char *src, size_t n ) | |
199 { | |
200 if ( n > MAXCHAR - strlen( dst ) ) | |
201 n = MAXCHAR - strlen( dst ); | |
202 strncat( dst, src, n ); | |
203 } | |
204 | |
205 void | |
206 Utl_stamp( Str file ) | |
207 { | |
208 FILE *f; | |
209 time_t t; | |
210 | |
211 time( &t ); | |
212 if ( ! ( f = fopen( file, "w" ) ) ) | |
213 { | |
214 Log_err( "Could not open %s for writing (%s)", | |
215 file, strerror( errno ) ); | |
216 return; | |
217 } | |
218 fprintf( f, "%lu\n", t ); | |
219 fclose( f ); | |
220 } | |
221 | |
222 Bool | |
223 Utl_getStamp( time_t *result, Str file ) | |
224 { | |
225 FILE *f; | |
226 | |
227 if ( ! ( f = fopen( file, "r" ) ) ) | |
228 return FALSE; | |
229 if ( fscanf( f, "%lu", result ) != 1 ) | |
230 { | |
231 Log_err( "File %s corrupted", file ); | |
232 fclose( f ); | |
233 return FALSE; | |
234 } | |
235 fclose( f ); | |
236 return TRUE; | |
237 } | |
238 | |
239 void | |
240 Utl_rfc822Date( time_t t, Str res ) | |
241 { | |
242 strftime( res, MAXCHAR,"%a, %d %b %Y %H:%M:%S %z", localtime( &t ) ); | |
243 } | |
244 | |
245 void | |
246 Utl_allocAndCpy( char **dst, const char *src ) | |
247 { | |
248 int len = strlen( src ); | |
249 if ( ! ( *dst = (char *)malloc( len + 1 ) ) ) | |
250 { | |
251 Log_err( "Cannot allocate string with length %lu", strlen( src ) ); | |
252 exit( EXIT_FAILURE ); | |
253 } | |
254 memcpy( *dst, src, len + 1 ); | |
255 } |