File: utils.c

package info (click to toggle)
urlredir 2.01
  • links: PTS
  • area: main
  • in suites: potato
  • size: 204 kB
  • ctags: 86
  • sloc: ansic: 590; yacc: 369; sh: 152; makefile: 105; lex: 94
file content (218 lines) | stat: -rw-r--r-- 3,634 bytes parent folder | download | duplicates (2)
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
/* utils.c - Utility functions for redir. */

#ifndef lint
char RCS_utils_c[] =
"$Id: utils.c,v 2.5 1999/01/19 12:27:04 caleishm Exp caleishm $  Produced by Chris Leishman & Trevor Cohn.  Ormond College student IT department, 1998.\n";
#endif /* not lint */


#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <ctype.h>
#include "utils.h"

#define  ASCII_SIZE 256
#define  MAXTIMELEN 50
#define  TRUE       1
#define  NOMATCH    -1


/* safemalloc performs all space allocation (using the malloc library
   function) and ensures the space was correctly allocated.
*/
void *safemalloc(size_t size)
{
   void *memPtr;

   if (!(memPtr = malloc(size))) {
      fprintf(stderr, "\nERROR: Insufficient Memory.\n");
      exit(EXIT_FAILURE);
   }

   return memPtr;
}


#ifndef HAVE_STRDUP

/* strdup duplicates a string, allocating the minimum amount of memory
   required to store it.
*/
char *strdup(const char *s)
{
   char *new;

   new = (char *) safemalloc(strlen(s) + 1);
   strcpy(new, s);

   return new;
}

#endif


void flush_to_eol(FILE *stream)
{
	int c;

	do {
		c = fgetc(stream);
		if ((c == EOF) || (c == '\n'))
			return;
	} while(TRUE);
}



#ifdef NOUSE_REGEX

/* Boyer-Moore string search (p is pattern, a is the string). */

#ifdef NOSAVESKIP
int search(const char *p, const char *a) 
#else
int search(const char *p, const char *a, int *skip) 
#endif
{
	int M = strlen(p), N = strlen(a), i = M - 1, j = M - 1;
#ifdef NOSAVESKIP
	int *skip = buildskip(p); 
#endif
	int min_pos = 0;

#ifdef DEBUG
	printf("--search called, %s, %s\n", a, p);
#endif

	/* Check for ^ and $ characters */

	if (p[0] == '^') {
		if (M-1 > N)
			return NOMATCH;
		for (i=0; i < M-1; i++)
			if (p[i+1] != a[i])
				return NOMATCH;
		return 0;
	}

	if (p[M-1] == '$') {
		if (M-1 > N)
			return NOMATCH;
		for (i=0; i < M-1; i++)
			if (p[i] != a[i+N-M+1])
				return NOMATCH;
		return N-M+1;
	}

	do {
		if(a[i] == p[j]) {
			if(j == M - 1)		
				min_pos = i + 1;
			i--;		
			j--;
		}
		else {	
			i += skip[(int)a[i]];
			if(i < min_pos)	
				i = min_pos;
			j = M - 1;		
		}
	} while(j >= 0 && i < N);

	if(j < 0) {
#ifdef DEBUG
	printf("--search match found, %d\n", i + 1);
#endif
		return i + 1;
	}

#ifdef DEBUG
	printf("--search match not found\n");
#endif
	return NOMATCH;
}

/* Boyer-Moore skip array builder
*/
int *buildskip(const char *p) {
	int *skip;
	int i, M = strlen(p);

	skip = (int *) safemalloc(ASCII_SIZE * sizeof(int));

	for(i = 0; i < ASCII_SIZE; i++)
		skip[i] = M;

	for(i = 0; i < M; i++)
		skip[(int)p[i]] = M - i - 1;

	return skip;
}

#endif



/* Add a line to a log file */

void addlog(char *logfile, int i, ...)
{
	FILE *fp;
	time_t t;
	va_list ap;
	int j;
#if defined LONGDATE || !defined HAVE_STRFTIME
	char *currtime;
#else
	char currtime[MAXTIMELEN];
#endif

	if (logfile == NULL)
		return;

	va_start(ap, i);

	if ((fp = fopen(logfile, "a")) == NULL) {
		fprintf(stderr, "ERROR: Couldn't open file \"%s\"\n", logfile);
		return;
	}

#ifdef DEBUG
	printf("Adding log to %s\n", logfile);
#endif

	time(&t);
#if defined LONGDATE || !defined HAVE_STRFTIME
	currtime = ctime(&t);
	currtime[strlen(currtime)-1] = '\0';
#else
	strftime(currtime, MAXTIMELEN, "%b %d %H:%M:%S", localtime(&t));
#endif

	fprintf(fp, "%s ", currtime);

	for(j = 1; j <= i; j++) {
		fprintf(fp, "%s ", va_arg(ap, char *));
	}

	fprintf(fp, "\n");

	if (fclose(fp) == EOF) {
		fprintf(stderr, "ERROR: Couldn't close file \"%s\"\n", logfile);
		return;
	}
}


char *strlower(char *s, int length) {
	int i;

	for (i = 0; i <= length && s[i] != '\0'; i++)
		s[i] = tolower(s[i]);

	return s;
}