aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
blob: 5de61ed058ab7df1f865a9131e63d89c737f66a7 (plain)
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
/*
 * Copyright (C) 2003,2004,2005 Jason Woodward <woodwardj at jaos dot org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "main.h"


FILE *open_file(const char *file_name,const char *mode){
	FILE *fh = NULL;
	if( (fh = fopen(file_name,mode)) == NULL ){
		fprintf(stderr,_("Failed to open %s\n"),file_name);
		if( errno ){
			perror(file_name);
		}
		return NULL;
	}
	return fh;
}

/* initialize regex structure and compilie the regular expression */
void init_regex(sg_regex *regex_t, const char *regex_string){

	regex_t->nmatch = MAX_REGEX_PARTS;

	/* compile our regex */
	regex_t->reg_return = regcomp(&regex_t->regex, regex_string, REG_EXTENDED|REG_NEWLINE|REG_ICASE);
	if( regex_t->reg_return != 0 ){
		size_t regerror_size;
		char errbuf[1024];
		size_t errbuf_size = 1024;
		fprintf(stderr, _("Failed to compile regex\n"));

		if( (regerror_size = regerror(regex_t->reg_return, &regex_t->regex,errbuf,errbuf_size)) ){
			printf(_("Regex Error: %s\n"),errbuf);
		}
		exit(1);
	}

}

/*
	execute the regular expression and set the return code
	in the passed in structure
 */
void execute_regex(sg_regex *regex_t,const char *string){
	regex_t->reg_return = regexec(&regex_t->regex, string, regex_t->nmatch,regex_t->pmatch,0);
}

void free_regex(sg_regex *regex_t){
	regfree(&regex_t->regex);
}

void gen_md5_sum_of_file(FILE *f,char *result_sum){
	EVP_MD_CTX mdctx;
	const EVP_MD *md;
	unsigned char md_value[EVP_MAX_MD_SIZE];
	unsigned int md_len = 0, i;
	ssize_t getline_read;
	size_t getline_size;
	char *result_sum_tmp = NULL;
	char *getline_buffer = NULL;

	md = EVP_md5();

	EVP_MD_CTX_init(&mdctx);
	EVP_DigestInit_ex(&mdctx, md, NULL);

	rewind(f);

	while( (getline_read = getline(&getline_buffer, &getline_size, f)) != EOF )
		EVP_DigestUpdate(&mdctx, getline_buffer, getline_read);

	free(getline_buffer);

	EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
	EVP_MD_CTX_cleanup(&mdctx);

	result_sum[0] = '\0';

	for(i = 0; i < md_len; i++){
		char *p = malloc( sizeof *p * 3 );

		if( snprintf(p,3,"%02x",md_value[i]) > 0 ){

			if( (result_sum_tmp = strncat(result_sum,p,3)) != NULL )
				result_sum = result_sum_tmp;

		}

		free(p);
	}

}

/* recursively create dirs */
void create_dir_structure(const char *dir_name){
	char *cwd = NULL;
	int position = 0,len = 0;

	cwd = getcwd(NULL,0);
	if( cwd == NULL ){
		fprintf(stderr,_("Failed to get cwd\n"));
		return;
	}else{
		#if DEBUG == 1
		fprintf(stderr,_("\tCurrent working directory: %s\n"),cwd);
		#endif
	}

	len = strlen(dir_name);
	while( position < len ){

		char *pointer = NULL;
		char *dir_name_buffer = NULL;

		/* if no more directory delim, then this must be last dir */
		if( strstr(dir_name + position,"/" ) == NULL ){

			dir_name_buffer = strndup(
				dir_name + position,
				strlen(dir_name + position)
			);

			if( strcmp(dir_name_buffer,".") != 0 ){
				if( (mkdir(dir_name_buffer,0755)) == -1){
					#if DEBUG == 1
					fprintf(stderr,_("Failed to mkdir: %s\n"),dir_name_buffer);
					#endif
				}else{
					#if DEBUG == 1
					fprintf(stderr,_("\tCreated directory: %s\n"),dir_name_buffer);
					#endif
				}
				if( (chdir(dir_name_buffer)) == -1 ){
					fprintf(stderr,_("Failed to chdir to %s\n"),dir_name_buffer);
					return;
				}else{
					#if DEBUG == 1
					fprintf(stderr,_("\tchdir into %s\n"),dir_name_buffer);
					#endif
				}
			}/* don't create . */

			free(dir_name_buffer);
			break;
		}else{
			if( dir_name[position] == '/' ){
				/* move on ahead */
				++position;
			}else{

				/* figure our dir name and mk it */
				pointer = strchr(dir_name + position,'/');
				dir_name_buffer = strndup(
					dir_name + position,
					strlen(dir_name + position) - strlen(pointer)
				);

				if( strcmp(dir_name_buffer,".") != 0 ){
					if( (mkdir(dir_name_buffer,0755)) == -1 ){
						#if DEBUG == 1
						fprintf(stderr,_("Failed to mkdir: %s\n"),dir_name_buffer);
						#endif
					}
					if( (chdir(dir_name_buffer)) == -1 ){
						fprintf(stderr,_("Failed to chdir to %s\n"),dir_name_buffer);
						free(dir_name_buffer);
						return;
					}
				} /* don't create . */

				free(dir_name_buffer);
				position += (strlen(dir_name + position) - strlen(pointer));
			}
		}
	}/* end while */

	if( (chdir(cwd)) == -1 ){
		fprintf(stderr,_("Failed to chdir to %s\n"),cwd);
		return;
	}

	free(cwd);
}

int ask_yes_no(const char *format, ...)
{
	char prompt_answer[10];
	va_list arg_list;

	va_start(arg_list, format);
	vprintf(format, arg_list);
	va_end(arg_list);

	fgets(prompt_answer,10,stdin);
	if( tolower(prompt_answer[0]) == 'y' )
		return 1;
	if( tolower(prompt_answer[0]) == 'n' )
		return 0;
	return -1;
}

char *str_replace_chr(const char *string,const char find, const char replace){
	unsigned int i,len = 0;
	char *clean = calloc( strlen(string) + 1, sizeof *clean);;

	len = strlen(string);
	for(i = 0;i < len; ++i){
		if(string[i] == find ){
			clean[i] = replace;
		}else{
			clean[i] = string[i];
		}
	}
	clean[ strlen(string) ] = '\0';

	return clean;
}