0
|
1 /*
|
|
2 util.c
|
|
3
|
|
4 $Id: util.c 3 2000-01-04 11:35:42Z enz $
|
|
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
|
|
103 void
|
|
104 Utl_toLower( Str line )
|
|
105 {
|
|
106 char *p;
|
|
107
|
|
108 p = line;
|
|
109 while ( *p )
|
|
110 {
|
|
111 *p = tolower( *p );
|
|
112 ++p;
|
|
113 }
|
|
114 }
|
|
115
|
|
116 char *
|
|
117 Utl_stripWhiteSpace( char *line )
|
|
118 {
|
|
119 char *p;
|
|
120
|
|
121 while ( isspace( *line ) )
|
|
122 ++line;
|
|
123 p = line + strlen( line ) - 1;
|
|
124 while ( isspace( *p ) )
|
|
125 {
|
|
126 *p = '\0';
|
|
127 --p;
|
|
128 }
|
|
129 return line;
|
|
130 }
|
|
131
|
|
132 void
|
|
133 Utl_cpyStr( Str dst, const char *src )
|
|
134 {
|
|
135 dst[ 0 ] = '\0';
|
|
136 strncat( dst, src, MAXCHAR );
|
|
137 }
|
|
138
|
|
139 void
|
|
140 Utl_cpyStrN( Str dst, const char *src, size_t n )
|
|
141 {
|
|
142 dst[ 0 ] = '\0';
|
|
143 strncat( dst, src, n );
|
|
144 }
|
|
145
|
|
146 void
|
|
147 Utl_stamp( Str file )
|
|
148 {
|
|
149 FILE *f;
|
|
150 time_t t;
|
|
151
|
|
152 time( &t );
|
|
153 if ( ! ( f = fopen( file, "w" ) ) )
|
|
154 {
|
|
155 Log_err( "Could not open %s for writing (%s)",
|
|
156 file, strerror( errno ) );
|
|
157 return;
|
|
158 }
|
|
159 fprintf( f, "%lu\n", t );
|
|
160 fclose( f );
|
|
161 }
|
|
162
|
|
163 Bool
|
|
164 Utl_getStamp( time_t *result, Str file )
|
|
165 {
|
|
166 FILE *f;
|
|
167
|
|
168 if ( ! ( f = fopen( file, "r" ) ) )
|
|
169 return FALSE;
|
|
170 if ( fscanf( f, "%lu", result ) != 1 )
|
|
171 {
|
|
172 Log_err( "File %s corrupted", file );
|
|
173 fclose( f );
|
|
174 return FALSE;
|
|
175 }
|
|
176 fclose( f );
|
|
177 return TRUE;
|
|
178 }
|
|
179
|
|
180 void
|
|
181 Utl_allocAndCpy( char **dst, const char *src )
|
|
182 {
|
|
183 int len = strlen( src );
|
|
184 if ( ! ( *dst = (char *)malloc( len + 1 ) ) )
|
|
185 {
|
|
186 Log_err( "Cannot allocate string with length %lu", strlen( src ) );
|
|
187 exit( EXIT_FAILURE );
|
|
188 }
|
|
189 memcpy( *dst, src, len + 1 );
|
|
190 }
|
|
191
|
|
192 Bool
|
|
193 Utl_matchPattern( const char *text, const char *pattern )
|
|
194 {
|
|
195 if ( pattern[ 0 ] == '*' && pattern[ 1 ] == '\0' )
|
|
196 return TRUE;
|
|
197 return ( fnmatch( pattern, text, 0 ) == 0 );
|
|
198 }
|