File: util.c

package info (click to toggle)
cvsps 2.1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 352 kB
  • ctags: 402
  • sloc: ansic: 4,687; sh: 189; makefile: 77
file content (291 lines) | stat: -rw-r--r-- 5,282 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
 * See COPYING file for license information 
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
#include <search.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <regex.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <cbtcommon/debug.h>

#include "util.h"

typedef int (*compare_func)(const void *, const void *);

static void * string_tree;
char *readfile(char const *filename, char *buf, size_t size)
{
    FILE *fp;
    char *ptr;
    size_t len;

    fp = fopen(filename, "r");
    if (!fp)
	return NULL;

    ptr = fgets(buf, size, fp);
    fclose(fp);

    if (!ptr)
	return NULL;

    len = strlen(buf);
    if (buf[len-1] == '\n')
	buf[len-1] = '\0';
    
    return buf;
}

char *strrep(char *s, char find, char replace)
{
    char * p = s;
    while (*p)
    {
	if (*p == find)
	    *p = replace;
	p++;
    }

    return s;
}

char *get_cvsps_dir()
{
    struct stat sbuf;
    static char prefix[PATH_MAX];
    const char * home;

    if (prefix[0])
	return prefix;

    if (!(home = getenv("HOME")))
    {
	debug(DEBUG_APPERROR, "HOME environment variable not set");
	exit(1);
    }

    if (snprintf(prefix, PATH_MAX, "%s/%s", home, CVSPS_PREFIX) >= PATH_MAX)
    {
	debug(DEBUG_APPERROR, "prefix buffer overflow");
	exit(1);
    }

    /* Make sure the prefix directory exists */
    if (stat(prefix, &sbuf) < 0)
    {
	int ret;
	ret = mkdir(prefix, 0777);
	if (ret < 0)
	{
	    debug(DEBUG_SYSERROR, "Cannot create the cvsps directory '%s'", CVSPS_PREFIX);
	    exit(1);
	}
    }
    else
    {
	if (!(S_ISDIR(sbuf.st_mode)))
	    debug(DEBUG_APPERROR, "cvsps directory '%s' is not a directory!", CVSPS_PREFIX);
    }

    return prefix;
}

char *xstrdup(char const *str)
{
    char *ret;
    assert(str);
    ret = strdup(str);
    if (!ret)
    {
	debug(DEBUG_ERROR, "strdup failed");
	exit(1);
    }

    return ret;
}

void strzncpy(char * dst, const char * src, int n)
{
    strncpy(dst, src, n);
    dst[n - 1] = 0;
}

char *get_string(char const *str)
{
    char ** res;

    if (!str)
	return NULL;
    
    res = (char **)tfind(str, &string_tree, (compare_func)strcmp);
    if (!res)
    {
	char *key = xstrdup(str);
	res = (char **)tsearch(key, &string_tree, (compare_func)strcmp);
	*res = key;
    }

    return *res;
}

static int get_int_substr(const char * str, const regmatch_t * p)
{
    char buff[256];
    memcpy(buff, str + p->rm_so, p->rm_eo - p->rm_so);
    buff[p->rm_eo - p->rm_so] = 0;
    return atoi(buff);
}

static time_t mktime_utc(struct tm * tm)
{
    char * old_tz = getenv("TZ");
    time_t ret;

    setenv("TZ", "UTC", 1);

    tzset();
	    
    ret = mktime(tm);

    if (old_tz)
	setenv("TZ", old_tz, 1);
    else 
	unsetenv("TZ");

    tzset();

    return ret;
}

void convert_date(time_t * t, const char * dte)
{
    static regex_t date_re;
    static int init_re;

#define MAX_MATCH 16
    size_t nmatch = MAX_MATCH;
    regmatch_t match[MAX_MATCH];

    if (!init_re) 
    {
	if (regcomp(&date_re, "([0-9]{4})[-/]([0-9]{2})[-/]([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", REG_EXTENDED)) 
	{
	    fprintf(stderr, "FATAL: date regex compilation error\n");
	    exit(1);
	}
	init_re = 1;
    }
    
    if (regexec(&date_re, dte, nmatch, match, 0) == 0)
    {
	regmatch_t * pm = match;
	struct tm tm = {0};

	/* first regmatch_t is match location of entire re */
	pm++;
	
	tm.tm_year = get_int_substr(dte, pm++);
	tm.tm_mon  = get_int_substr(dte, pm++);
	tm.tm_mday = get_int_substr(dte, pm++);
	tm.tm_hour = get_int_substr(dte, pm++);
	tm.tm_min  = get_int_substr(dte, pm++);
	tm.tm_sec  = get_int_substr(dte, pm++);

	tm.tm_year -= 1900;
	tm.tm_mon--;

	*t = mktime_utc(&tm);
    }
    else
    {
	*t = atoi(dte);
    }
}

static struct timeval start_time;

void timing_start()
{
    gettimeofday(&start_time, NULL);
}

void timing_stop(const char * msg)
{
    struct timeval stop_time;
    gettimeofday(&stop_time, NULL);
    stop_time.tv_sec -= start_time.tv_sec;
    stop_time.tv_usec -= start_time.tv_usec;
    if (stop_time.tv_usec < 0)
	stop_time.tv_sec--,stop_time.tv_usec += 1000000;

    printf("Elapsed time for %s: %d.%06d\n", msg, (int)stop_time.tv_sec, (int)stop_time.tv_usec);
}

extern char ** environ;

/* taken from the linux manual page for system */
int my_system (const char *command) 
{
    int pid, status;
    
    if (command == 0)
	return 1;
    pid = fork();
    if (pid == -1)
	return -1;
    if (pid == 0) {
	char *argv[4];
	argv[0] = "sh";
	argv[1] = "-c";
	argv[2] = (char*)command; /* discard const */
	argv[3] = 0;
	execve("/bin/sh", argv, environ);
	exit(127);
    }
    do {
	if (waitpid(pid, &status, 0) == -1) {
	    if (errno != EINTR)
		return -1;
	} else
	    return status;
    } while(1);
}

int escape_filename(char * dst, int len, const char * src)
{
    static char * naughty_chars = " \\\"'@<>=;|&()#$`?*[!:{";

    if (len > 0)
    {
	while (len > 1 && *src)
	{
	    if (strchr(naughty_chars, *src))
	    {
		if (len == 2)
		    break;
		*dst++ = '\\';
		len--;
	    }
	    
	    *dst++ = *src++;
	    len--;
	}

	*dst = 0;
    }

    return (*src == 0) ? 0 : -1;
}