2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
4 Mike Rylander <mrylander@gmail.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
20 inline void* safe_malloc( int size ) {
21 void* ptr = (void*) malloc( size );
23 perror("safe_malloc(): Out of Memory" );
26 memset( ptr, 0, size );
31 char** __global_argv = NULL;
32 int __global_argv_size = 0;
34 int init_proc_title( int argc, char* argv[] ) {
40 int len = strlen( __global_argv[i]);
41 bzero( __global_argv[i++], len );
42 __global_argv_size += len;
45 __global_argv_size -= 2;
49 int set_proc_title( char* format, ... ) {
50 VA_LIST_TO_STRING(format);
51 bzero( *(__global_argv), __global_argv_size );
52 return snprintf( *(__global_argv), __global_argv_size, VA_BUF );
56 /* utility method for profiling */
57 double get_timestamp_millis() {
59 gettimeofday(&tv, NULL);
60 double time = (int)tv.tv_sec + ( ((double)tv.tv_usec / 1000000) );
65 /* setting/clearing file flags */
66 int set_fl( int fd, int flags ) {
70 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
71 fprintf(stderr, "fcntl F_GETFL error");
77 if( fcntl( fd, F_SETFL, val ) < 0 ) {
78 fprintf(stderr, "fcntl F_SETFL error");
84 int clr_fl( int fd, int flags ) {
88 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
89 fprintf(stderr, "fcntl F_GETFL error" );
95 if( fcntl( fd, F_SETFL, val ) < 0 ) {
96 fprintf( stderr, "fcntl F_SETFL error" );
102 long va_list_size(const char* format, va_list args) {
104 len = vsnprintf(NULL, 0, format, args);
111 char* va_list_to_string(const char* format, ...) {
117 va_copy(a_copy, args);
119 va_start(args, format);
120 len = va_list_size(format, args);
125 va_start(a_copy, format);
126 vsnprintf(buf, len - 1, format, a_copy);
131 // ---------------------------------------------------------------------------------
132 // Flesh out a ubiqitous growing string buffer
133 // ---------------------------------------------------------------------------------
135 growing_buffer* buffer_init(int num_initial_bytes) {
137 if( num_initial_bytes > BUFFER_MAX_SIZE ) return NULL;
139 size_t len = sizeof(growing_buffer);
142 OSRF_MALLOC(gb, len);
144 gb->n_used = 0;/* nothing stored so far */
145 gb->size = num_initial_bytes;
146 OSRF_MALLOC(gb->buf, gb->size + 1);
152 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
154 if(!gb || !format) return 0;
160 va_copy(a_copy, args);
162 va_start(args, format);
163 len = va_list_size(format, args);
168 va_start(a_copy, format);
169 vsnprintf(buf, len - 1, format, a_copy);
172 return buffer_add(gb, buf);
177 int buffer_add(growing_buffer* gb, char* data) {
178 if(!(gb && data)) return 0;
180 int data_len = strlen( data );
182 if(data_len == 0) return 0;
184 int total_len = data_len + gb->n_used;
186 if( total_len >= gb->size ) {
187 while( total_len >= gb->size ) {
191 if( gb->size > BUFFER_MAX_SIZE ) {
192 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
198 OSRF_MALLOC(new_data, gb->size );
200 strcpy( new_data, gb->buf );
205 strcat( gb->buf, data );
206 gb->n_used = total_len;
211 int buffer_reset( growing_buffer *gb){
212 if( gb == NULL ) { return 0; }
213 if( gb->buf == NULL ) { return 0; }
214 memset( gb->buf, 0, gb->size );
219 int buffer_free( growing_buffer* gb ) {
227 char* buffer_data( growing_buffer *gb) {
228 return strdup( gb->buf );
233 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
236 if(gb->n_used < gb->size - 1)\
237 gb->buf[gb->n_used++] = c;\
239 buffer_add_char(gb, c);\
244 int buffer_add_char(growing_buffer* gb, char c) {
254 char* uescape( const char* string, int size, int full_escape ) {
256 growing_buffer* buf = buffer_init(size + 64);
259 unsigned long int c = 0x0;
261 while (string[idx]) {
265 if ((unsigned char)string[idx] >= 0x80) { // not ASCII
267 if ((unsigned char)string[idx] >= 0xC0 && (unsigned char)string[idx] <= 0xF4) { // starts a UTF8 string
270 if (((unsigned char)string[idx] & 0xF0) == 0xF0) {
272 c = (unsigned char)string[idx] ^ 0xF0;
274 } else if (((unsigned char)string[idx] & 0xE0) == 0xE0) {
276 c = (unsigned char)string[idx] ^ 0xE0;
278 } else if (((unsigned char)string[idx] & 0xC0) == 0xC0) {
280 c = (unsigned char)string[idx] ^ 0xC0;
285 idx++; // look at the next byte
286 c = (c << 6) | ((unsigned char)string[idx] & 0x3F); // add this byte worth
290 buffer_fadd(buf, "\\u%04x", c);
299 /* escape the usual suspects */
303 OSRF_BUFFER_ADD_CHAR(buf, '\\');
304 OSRF_BUFFER_ADD_CHAR(buf, '"');
308 OSRF_BUFFER_ADD_CHAR(buf, '\\');
309 OSRF_BUFFER_ADD_CHAR(buf, 'b');
313 OSRF_BUFFER_ADD_CHAR(buf, '\\');
314 OSRF_BUFFER_ADD_CHAR(buf, 'f');
318 OSRF_BUFFER_ADD_CHAR(buf, '\\');
319 OSRF_BUFFER_ADD_CHAR(buf, 't');
323 OSRF_BUFFER_ADD_CHAR(buf, '\\');
324 OSRF_BUFFER_ADD_CHAR(buf, 'n');
328 OSRF_BUFFER_ADD_CHAR(buf, '\\');
329 OSRF_BUFFER_ADD_CHAR(buf, 'r');
333 OSRF_BUFFER_ADD_CHAR(buf, '\\');
334 OSRF_BUFFER_ADD_CHAR(buf, '\\');
338 if( c < 32 ) buffer_fadd(buf, "\\u%0.4x", c);
339 else OSRF_BUFFER_ADD_CHAR(buf, c);
343 OSRF_BUFFER_ADD_CHAR(buf, c);
350 char* d = buffer_data(buf);
356 // A function to turn a process into a daemon and set it's process name in ps/top
361 perror("Failed to fork!");
364 } else if (f == 0) { // We're in the child now...
368 } else { // We're in the parent...
373 int stringisnum(char* s) {
374 char* w = (char*) malloc(strlen(s) * sizeof(char*));
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;