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() {
42 (int)t.time + ( ((double)t.millitm) / 1000 ) );
47 /* setting/clearing file flags */
48 int set_fl( int fd, int flags ) {
52 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
53 fprintf(stderr, "fcntl F_GETFL error");
59 if( fcntl( fd, F_SETFL, val ) < 0 ) {
60 fprintf(stderr, "fcntl F_SETFL error");
66 int clr_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 // ---------------------------------------------------------------------------------
85 // Flesh out a ubiqitous growing string buffer
86 // ---------------------------------------------------------------------------------
88 growing_buffer* buffer_init(int num_initial_bytes) {
90 if( num_initial_bytes > BUFFER_MAX_SIZE ) {
95 size_t len = sizeof(growing_buffer);
97 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
99 gb->n_used = 0;/* nothing stored so far */
100 gb->size = num_initial_bytes;
101 gb->buf = (char *) safe_malloc(gb->size + 1);
106 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
108 if(!gb || !format) return 0;
114 char* f_copy = strdup(format);
116 va_copy(a_copy,args);
118 va_start(a_copy, f_copy);
119 len = vsnprintf(NULL, 0, f_copy, a_copy);
127 va_start(args, format);
128 vsnprintf(buf, len - 1, format, args);
133 return buffer_add(gb, buf);
137 int buffer_add(growing_buffer* gb, char* data) {
140 if( ! gb || ! data ) { return 0; }
141 int data_len = strlen( data );
143 if( data_len == 0 ) { return 0; }
144 int total_len = data_len + gb->n_used;
146 while( total_len >= gb->size ) {
150 if( gb->size > BUFFER_MAX_SIZE ) {
151 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
156 char* new_data = (char*) safe_malloc( gb->size );
158 strcpy( new_data, gb->buf );
162 strcat( gb->buf, data );
163 gb->n_used = total_len;
168 int buffer_reset( growing_buffer *gb){
169 if( gb == NULL ) { return 0; }
170 if( gb->buf == NULL ) { return 0; }
171 memset( gb->buf, 0, gb->size );
176 int buffer_free( growing_buffer* gb ) {
184 char* buffer_data( growing_buffer *gb) {
185 return strdup( gb->buf );
189 int buffer_add_char(growing_buffer* gb, char c) {
199 char* uescape( const char* string, int size, int full_escape ) {
201 growing_buffer* buf = buffer_init(size + 64);
203 long unsigned int c = 0;
205 while (string[idx]) {
209 if ((string[idx] & 0xF0) == 0xF0) {
212 if( size - idx < 4 ) return NULL;
215 c |= (string[idx] & 0x3F)<<12;
218 c |= (string[idx] & 0x3F)<<6;
221 c |= (string[idx] & 0x3F);
225 buffer_fadd(buf, "\\u%0.4x", c);
227 } else if ((string[idx] & 0xE0) == 0xE0) {
229 if( size - idx < 3 ) return NULL;
232 c |= (string[idx] & 0x3F)<<6;
235 c |= (string[idx] & 0x3F);
239 buffer_fadd(buf, "\\u%0.4x", c);
241 } else if ((string[idx] & 0xC0) == 0xC0) {
244 if( size - idx < 2 ) return NULL;
247 c |= (string[idx] & 0x3F);
251 buffer_fadd(buf, "\\u%0.4x", c);
256 /* escape the usual suspects */
260 buffer_add_char(buf, '\\');
261 buffer_add_char(buf, '"');
265 buffer_add_char(buf, '\\');
266 buffer_add_char(buf, 'b');
270 buffer_add_char(buf, '\\');
271 buffer_add_char(buf, 'f');
275 buffer_add_char(buf, '\\');
276 buffer_add_char(buf, 't');
280 buffer_add_char(buf, '\\');
281 buffer_add_char(buf, 'n');
285 buffer_add_char(buf, '\\');
286 buffer_add_char(buf, 'r');
290 buffer_add_char(buf, c);
294 buffer_add_char(buf, c);
301 char* d = buffer_data(buf);
307 // A function to turn a process into a daemon and set it's process name in ps/top
311 perror("Failed to fork!");
314 // We're in the child now...
318 // We're in the parent...
323 int stringisnum(char* s) {
324 char* w = (char*) malloc(strlen(s) * sizeof(char*));
326 long blah = strtol(s, &w, 10);