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.
19 inline void* safe_malloc( int size ) {
20 void* ptr = (void*) malloc( size );
22 perror("safe_malloc(): Out of Memory" );
25 memset( ptr, 0, size );
30 char** __global_argv = NULL;
31 int __global_argv_size = 0;
33 int init_proc_title( int argc, char* argv[] ) {
39 int len = strlen( __global_argv[i]);
40 bzero( __global_argv[i++], len );
41 __global_argv_size += len;
44 __global_argv_size -= 2;
48 int set_proc_title( char* format, ... ) {
49 VA_LIST_TO_STRING(format);
50 bzero( *(__global_argv), __global_argv_size );
51 return snprintf( *(__global_argv), __global_argv_size, VA_BUF );
55 /* utility method for profiling */
56 double get_timestamp_millis() {
58 gettimeofday(&tv, NULL);
59 double time = (int)tv.tv_sec + ( ((double)tv.tv_usec / 1000000) );
64 /* setting/clearing file flags */
65 int set_fl( int fd, int flags ) {
69 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
70 fprintf(stderr, "fcntl F_GETFL error");
76 if( fcntl( fd, F_SETFL, val ) < 0 ) {
77 fprintf(stderr, "fcntl F_SETFL error");
83 int clr_fl( int fd, int flags ) {
87 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
88 fprintf(stderr, "fcntl F_GETFL error" );
94 if( fcntl( fd, F_SETFL, val ) < 0 ) {
95 fprintf( stderr, "fcntl F_SETFL error" );
101 long va_list_size(const char* format, va_list args) {
103 len = vsnprintf(NULL, 0, format, args);
110 char* va_list_to_string(const char* format, ...) {
116 va_copy(a_copy, args);
118 va_start(args, format);
119 len = va_list_size(format, args);
124 va_start(a_copy, format);
125 vsnprintf(buf, len - 1, format, a_copy);
130 // ---------------------------------------------------------------------------------
131 // Flesh out a ubiqitous growing string buffer
132 // ---------------------------------------------------------------------------------
134 growing_buffer* buffer_init(int num_initial_bytes) {
136 if( num_initial_bytes > BUFFER_MAX_SIZE ) return NULL;
138 size_t len = sizeof(growing_buffer);
140 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
142 gb->n_used = 0;/* nothing stored so far */
143 gb->size = num_initial_bytes;
144 gb->buf = (char *) safe_malloc(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);
174 int buffer_add(growing_buffer* gb, char* data) {
177 if( ! gb || ! data ) { return 0; }
178 int data_len = strlen( data );
180 if( data_len == 0 ) { return 0; }
181 int total_len = data_len + gb->n_used;
183 while( total_len >= gb->size ) {
187 if( gb->size > BUFFER_MAX_SIZE ) {
188 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
193 char* new_data = (char*) safe_malloc( gb->size );
195 strcpy( new_data, gb->buf );
199 strcat( gb->buf, data );
200 gb->n_used = total_len;
205 int buffer_reset( growing_buffer *gb){
206 if( gb == NULL ) { return 0; }
207 if( gb->buf == NULL ) { return 0; }
208 memset( gb->buf, 0, gb->size );
213 int buffer_free( growing_buffer* gb ) {
221 char* buffer_data( growing_buffer *gb) {
222 return strdup( gb->buf );
226 int buffer_add_char(growing_buffer* gb, char c) {
236 char* uescape( const char* string, int size, int full_escape ) {
238 growing_buffer* buf = buffer_init(size + 64);
240 long unsigned int c = 0;
242 while (string[idx]) {
246 if ((string[idx] & 0xF0) == 0xF0) {
249 if( size - idx < 4 ) return NULL;
252 c |= (string[idx] & 0x3F)<<12;
255 c |= (string[idx] & 0x3F)<<6;
258 c |= (string[idx] & 0x3F);
262 buffer_fadd(buf, "\\u%0.4x", c);
264 } else if ((string[idx] & 0xE0) == 0xE0) {
266 if( size - idx < 3 ) return NULL;
269 c |= (string[idx] & 0x3F)<<6;
272 c |= (string[idx] & 0x3F);
276 buffer_fadd(buf, "\\u%0.4x", c);
278 } else if ((string[idx] & 0xC0) == 0xC0) {
281 if( size - idx < 2 ) return NULL;
284 c |= (string[idx] & 0x3F);
288 buffer_fadd(buf, "\\u%0.4x", c);
293 /* escape the usual suspects */
297 buffer_add_char(buf, '\\');
298 buffer_add_char(buf, '"');
302 buffer_add_char(buf, '\\');
303 buffer_add_char(buf, 'b');
307 buffer_add_char(buf, '\\');
308 buffer_add_char(buf, 'f');
312 buffer_add_char(buf, '\\');
313 buffer_add_char(buf, 't');
317 buffer_add_char(buf, '\\');
318 buffer_add_char(buf, 'n');
322 buffer_add_char(buf, '\\');
323 buffer_add_char(buf, 'r');
327 buffer_add_char(buf, c);
331 buffer_add_char(buf, c);
338 char* d = buffer_data(buf);
344 // A function to turn a process into a daemon and set it's process name in ps/top
349 perror("Failed to fork!");
352 } else if (f == 0) { // We're in the child now...
356 } else { // We're in the parent...
361 int stringisnum(char* s) {
362 char* w = (char*) malloc(strlen(s) * sizeof(char*));
372 char* file_to_string(const char* filename) {
374 if(!filename) return NULL;
379 growing_buffer* gb = buffer_init(len);
381 FILE* file = fopen(filename, "r");
383 perror("Unable to open file in json_parse_file()");
387 while(fgets(buf, len - 1, file)) {
394 char* data = buffer_data(gb);
400 char* md5sum( char* text ) {
403 unsigned char digest[16];
408 for ( i=0 ; i != strlen(text) ; i++ )
409 MD5_feed (&ctx, text[i]);
411 MD5_stop (&ctx, digest);
419 for ( i=0 ; i<16 ; i++ ) {
420 sprintf(buf, "%02x", digest[i]);
421 strcat( final, buf );
424 return strdup(final);