2 Copyright (C) 2005 Georgia Public Library Service
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
18 inline void* safe_malloc( int size ) {
19 void* ptr = (void*) malloc( size );
21 perror("safe_malloc(): Out of Memory" );
24 memset( ptr, 0, size );
29 char** __global_argv = NULL;
30 int __global_argv_size = 0;
32 int init_proc_title( int argc, char* argv[] ) {
38 int len = strlen( __global_argv[i]);
39 bzero( __global_argv[i++], len );
40 __global_argv_size += len;
43 __global_argv_size -= 2;
47 int set_proc_title( char* format, ... ) {
48 VA_LIST_TO_STRING(format);
49 bzero( *(__global_argv), __global_argv_size );
50 return snprintf( *(__global_argv), __global_argv_size, VA_BUF );
54 /* utility method for profiling */
55 double get_timestamp_millis() {
57 gettimeofday(&tv, NULL);
58 double time = (int)tv.tv_sec + ( ((double)tv.tv_usec / 1000000) );
63 /* setting/clearing file flags */
64 int set_fl( int fd, int flags ) {
68 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
69 fprintf(stderr, "fcntl F_GETFL error");
75 if( fcntl( fd, F_SETFL, val ) < 0 ) {
76 fprintf(stderr, "fcntl F_SETFL error");
82 int clr_fl( int fd, int flags ) {
86 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
87 fprintf(stderr, "fcntl F_GETFL error" );
93 if( fcntl( fd, F_SETFL, val ) < 0 ) {
94 fprintf( stderr, "fcntl F_SETFL error" );
100 long va_list_size(const char* format, va_list args) {
102 len = vsnprintf(NULL, 0, format, args);
109 char* va_list_to_string(const char* format, ...) {
115 va_copy(a_copy, args);
117 va_start(args, format);
118 len = va_list_size(format, args);
123 va_start(a_copy, format);
124 vsnprintf(buf, len - 1, format, a_copy);
129 // ---------------------------------------------------------------------------------
130 // Flesh out a ubiqitous growing string buffer
131 // ---------------------------------------------------------------------------------
133 growing_buffer* buffer_init(int num_initial_bytes) {
135 if( num_initial_bytes > BUFFER_MAX_SIZE ) return NULL;
137 size_t len = sizeof(growing_buffer);
140 OSRF_MALLOC(gb, len);
142 gb->n_used = 0;/* nothing stored so far */
143 gb->size = num_initial_bytes;
144 OSRF_MALLOC(gb->buf, gb->size + 1);
150 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
152 if(!gb || !format) return 0;
158 va_copy(a_copy, args);
160 va_start(args, format);
161 len = va_list_size(format, args);
166 va_start(a_copy, format);
167 vsnprintf(buf, len - 1, format, a_copy);
170 return buffer_add(gb, buf);
175 int buffer_add(growing_buffer* gb, char* data) {
176 if(!(gb && data)) return 0;
178 int data_len = strlen( data );
180 if(data_len == 0) return 0;
182 int total_len = data_len + gb->n_used;
184 if( total_len >= gb->size ) {
185 while( total_len >= gb->size ) {
189 if( gb->size > BUFFER_MAX_SIZE ) {
190 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
196 OSRF_MALLOC(new_data, gb->size );
198 strcpy( new_data, gb->buf );
203 strcat( gb->buf, data );
204 gb->n_used = total_len;
209 int buffer_reset( growing_buffer *gb){
210 if( gb == NULL ) { return 0; }
211 if( gb->buf == NULL ) { return 0; }
212 memset( gb->buf, 0, gb->size );
217 int buffer_free( growing_buffer* gb ) {
225 char* buffer_data( growing_buffer *gb) {
226 return strdup( gb->buf );
231 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
234 if(gb->n_used < gb->size - 1)\
235 gb->buf[gb->n_used++] = c;\
237 buffer_add_char(gb, c);\
242 int buffer_add_char(growing_buffer* gb, char c) {
252 char* uescape( const char* string, int size, int full_escape ) {
254 growing_buffer* buf = buffer_init(size + 64);
257 unsigned long int c = 0x0;
259 while (string[idx]) {
263 if ((unsigned char)string[idx] >= 0x80) { // not ASCII
265 if ((unsigned char)string[idx] >= 0xC0 && (unsigned char)string[idx] <= 0xF4) { // starts a UTF8 string
268 if (((unsigned char)string[idx] & 0xF0) == 0xF0) {
270 c = (unsigned char)string[idx] ^ 0xF0;
272 } else if (((unsigned char)string[idx] & 0xE0) == 0xE0) {
274 c = (unsigned char)string[idx] ^ 0xE0;
276 } else if (((unsigned char)string[idx] & 0xC0) == 0xC0) {
278 c = (unsigned char)string[idx] ^ 0xC0;
283 idx++; // look at the next byte
284 c = (c << 6) | ((unsigned char)string[idx] & 0x3F); // add this byte worth
288 buffer_fadd(buf, "\\u%04x", c);
298 /* escape the usual suspects */
302 OSRF_BUFFER_ADD_CHAR(buf, '\\');
303 OSRF_BUFFER_ADD_CHAR(buf, '"');
307 OSRF_BUFFER_ADD_CHAR(buf, '\\');
308 OSRF_BUFFER_ADD_CHAR(buf, 'b');
312 OSRF_BUFFER_ADD_CHAR(buf, '\\');
313 OSRF_BUFFER_ADD_CHAR(buf, 'f');
317 OSRF_BUFFER_ADD_CHAR(buf, '\\');
318 OSRF_BUFFER_ADD_CHAR(buf, 't');
322 OSRF_BUFFER_ADD_CHAR(buf, '\\');
323 OSRF_BUFFER_ADD_CHAR(buf, 'n');
327 OSRF_BUFFER_ADD_CHAR(buf, '\\');
328 OSRF_BUFFER_ADD_CHAR(buf, 'r');
332 OSRF_BUFFER_ADD_CHAR(buf, '\\');
333 OSRF_BUFFER_ADD_CHAR(buf, '\\');
337 if( c < 32 ) buffer_fadd(buf, "\\u%04x", c);
338 else OSRF_BUFFER_ADD_CHAR(buf, c);
342 OSRF_BUFFER_ADD_CHAR(buf, c);
349 char* d = buffer_data(buf);
355 // A function to turn a process into a daemon and set it's process name in ps/top
360 perror("Failed to fork!");
363 } else if (f == 0) { // We're in the child now...
367 } else { // We're in the parent...
373 /* Return 1 if the string represents an integer, */
374 /* as recognized by strtol(); Otherwise return 0. */
376 int stringisnum(char* s) {
384 char* file_to_string(const char* filename) {
386 if(!filename) return NULL;
391 growing_buffer* gb = buffer_init(len);
393 FILE* file = fopen(filename, "r");
395 int l = strlen(filename) + 64;
397 snprintf(b,l,"Unable to open file [%s] in file_to_string()", filename);
402 while(fgets(buf, len - 1, file)) {
409 char* data = buffer_data(gb);
415 char* md5sum( char* text, ... ) {
418 unsigned char digest[16];
422 VA_LIST_TO_STRING(text);
425 for ( i=0 ; i != strlen(VA_BUF) ; i++ )
426 MD5_feed (&ctx, VA_BUF[i]);
428 MD5_stop (&ctx, digest);
436 for ( i=0 ; i<16 ; i++ ) {
437 sprintf(buf, "%02x", digest[i]);
438 strcat( final, buf );
441 return strdup(final);
445 int osrfUtilsCheckFileDescriptor( int fd ) {
455 if( select(fd + 1, &tmpset, NULL, NULL, &tv) == -1 ) {
456 if( errno == EBADF ) return -1;