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);
141 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
143 gb->n_used = 0;/* nothing stored so far */
144 gb->size = num_initial_bytes;
145 gb->buf = (char *) safe_malloc(gb->size + 1);
151 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
153 if(!gb || !format) return 0;
159 va_copy(a_copy, args);
161 va_start(args, format);
162 len = va_list_size(format, args);
167 va_start(a_copy, format);
168 vsnprintf(buf, len - 1, format, a_copy);
171 return buffer_add(gb, buf);
175 int buffer_add(growing_buffer* gb, char* data) {
178 if( ! gb || ! data ) { return 0; }
179 int data_len = strlen( data );
181 if( data_len == 0 ) { return 0; }
182 int total_len = data_len + gb->n_used;
184 while( total_len >= gb->size ) {
188 if( gb->size > BUFFER_MAX_SIZE ) {
189 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
194 char* new_data = (char*) safe_malloc( gb->size );
196 strcpy( new_data, gb->buf );
200 strcat( gb->buf, data );
201 gb->n_used = total_len;
206 int buffer_reset( growing_buffer *gb){
207 if( gb == NULL ) { return 0; }
208 if( gb->buf == NULL ) { return 0; }
209 memset( gb->buf, 0, gb->size );
214 int buffer_free( growing_buffer* gb ) {
222 char* buffer_data( growing_buffer *gb) {
223 return strdup( gb->buf );
227 int buffer_add_char(growing_buffer* gb, char c) {
237 char* uescape( const char* string, int size, int full_escape ) {
239 growing_buffer* buf = buffer_init(size + 64);
241 long unsigned int c = 0;
243 while (string[idx]) {
247 if ((string[idx] & 0xF0) == 0xF0) {
250 if( size - idx < 4 ) return NULL;
253 c |= (string[idx] & 0x3F)<<12;
256 c |= (string[idx] & 0x3F)<<6;
259 c |= (string[idx] & 0x3F);
263 buffer_fadd(buf, "\\u%0.4x", c);
265 } else if ((string[idx] & 0xE0) == 0xE0) {
267 if( size - idx < 3 ) return NULL;
270 c |= (string[idx] & 0x3F)<<6;
273 c |= (string[idx] & 0x3F);
277 buffer_fadd(buf, "\\u%0.4x", c);
279 } else if ((string[idx] & 0xC0) == 0xC0) {
282 if( size - idx < 2 ) return NULL;
285 c |= (string[idx] & 0x3F);
289 buffer_fadd(buf, "\\u%0.4x", c);
294 /* escape the usual suspects */
298 buffer_add_char(buf, '\\');
299 buffer_add_char(buf, '"');
303 buffer_add_char(buf, '\\');
304 buffer_add_char(buf, 'b');
308 buffer_add_char(buf, '\\');
309 buffer_add_char(buf, 'f');
313 buffer_add_char(buf, '\\');
314 buffer_add_char(buf, 't');
318 buffer_add_char(buf, '\\');
319 buffer_add_char(buf, 'n');
323 buffer_add_char(buf, '\\');
324 buffer_add_char(buf, 'r');
328 buffer_add_char(buf, c);
332 buffer_add_char(buf, c);
339 char* d = buffer_data(buf);
345 // A function to turn a process into a daemon and set it's process name in ps/top
350 perror("Failed to fork!");
353 } else if (f == 0) { // We're in the child now...
357 } else { // We're in the parent...
362 int stringisnum(char* s) {
363 char* w = (char*) malloc(strlen(s) * sizeof(char*));
373 char* file_to_string(const char* filename) {
375 if(!filename) return NULL;
380 growing_buffer* gb = buffer_init(len);
382 FILE* file = fopen(filename, "r");
384 perror("Unable to open file in json_parse_file()");
388 while(fgets(buf, len - 1, file)) {
395 char* data = buffer_data(gb);
401 char* md5sum( char* text, ... ) {
404 unsigned char digest[16];
408 VA_LIST_TO_STRING(text);
411 for ( i=0 ; i != strlen(VA_BUF) ; i++ )
412 MD5_feed (&ctx, VA_BUF[i]);
414 MD5_stop (&ctx, digest);
422 for ( i=0 ; i<16 ; i++ ) {
423 sprintf(buf, "%02x", digest[i]);
424 strcat( final, buf );
427 return strdup(final);
431 int osrfUtilsCheckFileDescriptor( int fd ) {
441 if( select(fd + 1, &tmpset, NULL, NULL, &tv) == -1 ) {
442 if( errno == EBADF ) return -1;