Mercurial > noffle
annotate util.c @ 26:526a4c34ee2e noffle
[svn] Applied patch from Jim Hague: support for local groups / new command
line options --create and --cancel.
author | enz |
---|---|
date | Sat, 29 Apr 2000 15:45:56 +0100 |
parents | 04124a4423d4 |
children | 8e972daaeab9 |
rev | line source |
---|---|
0 | 1 /* |
2 util.c | |
3 | |
26
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
4 $Id: util.c 32 2000-04-29 14:45:56Z enz $ |
0 | 5 */ |
6 | |
7 #include "util.h" | |
8 #include <errno.h> | |
9 #include <ctype.h> | |
10 #include <fnmatch.h> | |
11 #include <sys/types.h> | |
12 #include <sys/stat.h> | |
13 #include <fcntl.h> | |
14 #include <time.h> | |
15 #include <unistd.h> | |
16 #include "config.h" | |
17 #include "log.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 | |
26
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
103 const char * |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
104 Utl_getHeaderLn( Str result, const char *p ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
105 { |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
106 const char * res = Utl_getLn( result, p ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
107 |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
108 /* Look for followon line if this isn't a blank line. */ |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
109 if ( res != NULL && !isspace( result[ 0 ] ) ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
110 while ( res != NULL && res[ 0 ] != '\n' && isspace( res[ 0 ] ) ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
111 { |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
112 Str nextLine; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
113 const char *here; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
114 char *next; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
115 |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
116 here = res; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
117 res = Utl_getLn( nextLine, res ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
118 next = Utl_stripWhiteSpace( nextLine ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
119 |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
120 if ( next[ 0 ] != '\0' ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
121 { |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
122 Utl_catStr( result, " " ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
123 Utl_catStr( result, next ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
124 } |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
125 else |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
126 { |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
127 res = here; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
128 break; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
129 } |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
130 } |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
131 |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
132 return res; |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
133 } |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
134 |
0 | 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_cpyStr( Str dst, const char *src ) | |
166 { | |
167 dst[ 0 ] = '\0'; | |
168 strncat( dst, src, MAXCHAR ); | |
169 } | |
170 | |
171 void | |
172 Utl_cpyStrN( Str dst, const char *src, size_t n ) | |
173 { | |
26
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
174 if ( n > MAXCHAR ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
175 n = MAXCHAR; |
0 | 176 dst[ 0 ] = '\0'; |
177 strncat( dst, src, n ); | |
178 } | |
179 | |
180 void | |
26
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
181 Utl_catStr( Str dst, const char *src ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
182 { |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
183 strncat( dst, src, MAXCHAR - strlen( dst ) ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
184 } |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
185 |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
186 void |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
187 Utl_catStrN( Str dst, const char *src, size_t n ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
188 { |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
189 if ( n > MAXCHAR - strlen( dst ) ) |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
190 n = MAXCHAR - strlen( dst ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
191 strncat( dst, src, n ); |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
192 } |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
193 |
526a4c34ee2e
[svn] Applied patch from Jim Hague: support for local groups / new command
enz
parents:
0
diff
changeset
|
194 void |
0 | 195 Utl_stamp( Str file ) |
196 { | |
197 FILE *f; | |
198 time_t t; | |
199 | |
200 time( &t ); | |
201 if ( ! ( f = fopen( file, "w" ) ) ) | |
202 { | |
203 Log_err( "Could not open %s for writing (%s)", | |
204 file, strerror( errno ) ); | |
205 return; | |
206 } | |
207 fprintf( f, "%lu\n", t ); | |
208 fclose( f ); | |
209 } | |
210 | |
211 Bool | |
212 Utl_getStamp( time_t *result, Str file ) | |
213 { | |
214 FILE *f; | |
215 | |
216 if ( ! ( f = fopen( file, "r" ) ) ) | |
217 return FALSE; | |
218 if ( fscanf( f, "%lu", result ) != 1 ) | |
219 { | |
220 Log_err( "File %s corrupted", file ); | |
221 fclose( f ); | |
222 return FALSE; | |
223 } | |
224 fclose( f ); | |
225 return TRUE; | |
226 } | |
227 | |
228 void | |
229 Utl_allocAndCpy( char **dst, const char *src ) | |
230 { | |
231 int len = strlen( src ); | |
232 if ( ! ( *dst = (char *)malloc( len + 1 ) ) ) | |
233 { | |
234 Log_err( "Cannot allocate string with length %lu", strlen( src ) ); | |
235 exit( EXIT_FAILURE ); | |
236 } | |
237 memcpy( *dst, src, len + 1 ); | |
238 } | |
239 | |
240 Bool | |
241 Utl_matchPattern( const char *text, const char *pattern ) | |
242 { | |
243 if ( pattern[ 0 ] == '*' && pattern[ 1 ] == '\0' ) | |
244 return TRUE; | |
245 return ( fnmatch( pattern, text, 0 ) == 0 ); | |
246 } |