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 #include <sys/types.h>
24 #include <sys/timeb.h>
27 inline void* safe_malloc( int size ) {
28 void* ptr = (void*) malloc( size );
30 perror("safe_malloc(): Out of Memory" );
33 memset( ptr, 0, size );
37 /* utility method for profiling */
38 double get_timestamp_millis() {
41 gettimeofday(&tv, NULL);
43 double time = (int)t.time + ( ((double)t.millitm) / 1000 ) + ( ((double)tv.tv_usec / 1000000) );
48 /* setting/clearing file flags */
49 int set_fl( int fd, int flags ) {
53 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
54 fprintf(stderr, "fcntl F_GETFL error");
60 if( fcntl( fd, F_SETFL, val ) < 0 ) {
61 fprintf(stderr, "fcntl F_SETFL error");
67 int clr_fl( int fd, int flags ) {
71 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
72 fprintf(stderr, "fcntl F_GETFL error" );
78 if( fcntl( fd, F_SETFL, val ) < 0 ) {
79 fprintf( stderr, "fcntl F_SETFL error" );
85 // ---------------------------------------------------------------------------------
86 // Flesh out a ubiqitous growing string buffer
87 // ---------------------------------------------------------------------------------
89 growing_buffer* buffer_init(int num_initial_bytes) {
91 if( num_initial_bytes > BUFFER_MAX_SIZE ) {
96 size_t len = sizeof(growing_buffer);
98 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
100 gb->n_used = 0;/* nothing stored so far */
101 gb->size = num_initial_bytes;
102 gb->buf = (char *) safe_malloc(gb->size + 1);
107 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
109 if(!gb || !format) return 0;
115 char* f_copy = strdup(format);
117 va_copy(a_copy,args);
119 va_start(a_copy, f_copy);
120 len = vsnprintf(NULL, 0, f_copy, a_copy);
128 va_start(args, format);
129 vsnprintf(buf, len - 1, format, args);
134 return buffer_add(gb, buf);
138 int buffer_add(growing_buffer* gb, char* data) {
141 if( ! gb || ! data ) { return 0; }
142 int data_len = strlen( data );
144 if( data_len == 0 ) { return 0; }
145 int total_len = data_len + gb->n_used;
147 while( total_len >= gb->size ) {
151 if( gb->size > BUFFER_MAX_SIZE ) {
152 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
157 char* new_data = (char*) safe_malloc( gb->size );
159 strcpy( new_data, gb->buf );
163 strcat( gb->buf, data );
164 gb->n_used = total_len;
169 int buffer_reset( growing_buffer *gb){
170 if( gb == NULL ) { return 0; }
171 if( gb->buf == NULL ) { return 0; }
172 memset( gb->buf, 0, gb->size );
177 int buffer_free( growing_buffer* gb ) {
185 char* buffer_data( growing_buffer *gb) {
186 return strdup( gb->buf );
190 int buffer_add_char(growing_buffer* gb, char c) {
200 char* uescape( const char* string, int size, int full_escape ) {
202 growing_buffer* buf = buffer_init(size + 64);
204 long unsigned int c = 0;
206 while (string[idx]) {
210 if ((string[idx] & 0xF0) == 0xF0) {
213 if( size - idx < 4 ) return NULL;
216 c |= (string[idx] & 0x3F)<<12;
219 c |= (string[idx] & 0x3F)<<6;
222 c |= (string[idx] & 0x3F);
226 buffer_fadd(buf, "\\u%0.4x", c);
228 } else if ((string[idx] & 0xE0) == 0xE0) {
230 if( size - idx < 3 ) return NULL;
233 c |= (string[idx] & 0x3F)<<6;
236 c |= (string[idx] & 0x3F);
240 buffer_fadd(buf, "\\u%0.4x", c);
242 } else if ((string[idx] & 0xC0) == 0xC0) {
245 if( size - idx < 2 ) return NULL;
248 c |= (string[idx] & 0x3F);
252 buffer_fadd(buf, "\\u%0.4x", c);
257 /* escape the usual suspects */
261 buffer_add_char(buf, '\\');
262 buffer_add_char(buf, '"');
266 buffer_add_char(buf, '\\');
267 buffer_add_char(buf, 'b');
271 buffer_add_char(buf, '\\');
272 buffer_add_char(buf, 'f');
276 buffer_add_char(buf, '\\');
277 buffer_add_char(buf, 't');
281 buffer_add_char(buf, '\\');
282 buffer_add_char(buf, 'n');
286 buffer_add_char(buf, '\\');
287 buffer_add_char(buf, 'r');
291 buffer_add_char(buf, c);
295 buffer_add_char(buf, c);
302 char* d = buffer_data(buf);
308 // A function to turn a process into a daemon and set it's process name in ps/top
312 perror("Failed to fork!");
315 // We're in the child now...
319 // We're in the parent...
324 int stringisnum(char* s) {
325 char* w = (char*) malloc(strlen(s) * sizeof(char*));