| 
									
										
										
										
											2004-08-23 20:38:10 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-02-13 10:55:45 +00:00
										 |  |  |  *  Copyright (C) 2007-2009 Sourcefire, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Authors: Tomasz Kojm | 
					
						
							| 
									
										
										
										
											2004-08-23 20:38:10 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  This program is free software; you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2007-03-31 20:31:04 +00:00
										 |  |  |  *  it under the terms of the GNU General Public License version 2 as | 
					
						
							|  |  |  |  *  published by the Free Software Foundation. | 
					
						
							| 
									
										
										
										
											2004-08-23 20:38:10 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  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 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 | 
					
						
							| 
									
										
										
										
											2006-04-09 19:59:28 +00:00
										 |  |  |  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | 
					
						
							|  |  |  |  *  MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2004-08-23 20:38:10 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include "clamav-config.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2006-09-27 10:49:26 +00:00
										 |  |  | #ifdef	HAVE_UNISTD_H
 | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2006-09-27 10:49:26 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-08-23 20:38:10 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2004-09-04 15:48:30 +00:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2009-09-24 19:23:21 +02:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2009-02-17 17:35:31 +00:00
										 |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2009-09-24 19:23:21 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2006-06-08 20:00:50 +00:00
										 |  |  | #include <dirent.h>
 | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2006-04-10 09:59:51 +00:00
										 |  |  | #include <ctype.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-08 20:00:50 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2004-08-23 20:38:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-02 21:45:46 +00:00
										 |  |  | #include "shared/optparser.h"
 | 
					
						
							| 
									
										
										
										
											2006-09-27 10:49:26 +00:00
										 |  |  | #include "shared/output.h"
 | 
					
						
							| 
									
										
										
										
											2004-09-07 21:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-27 10:49:26 +00:00
										 |  |  | #include "libclamav/clamav.h"
 | 
					
						
							|  |  |  | #include "libclamav/cvd.h"
 | 
					
						
							| 
									
										
										
										
											2007-03-05 21:05:22 +00:00
										 |  |  | #include "libclamav/others.h" /* for cli_rmdirs() */
 | 
					
						
							| 
									
										
										
										
											2008-07-18 16:31:02 +00:00
										 |  |  | #include "libclamav/regex/regex.h"
 | 
					
						
							| 
									
										
										
										
											2008-08-04 10:38:24 +00:00
										 |  |  | #include "libclamav/version.h"
 | 
					
						
							| 
									
										
										
										
											2007-02-11 00:41:13 +00:00
										 |  |  | #include "shared/misc.h"
 | 
					
						
							| 
									
										
										
										
											2006-09-27 10:49:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-04 10:38:24 +00:00
										 |  |  | #ifndef REPO_VERSION
 | 
					
						
							|  |  |  | #define REPO_VERSION "exported"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char *get_version(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(!strncmp("devel-",VERSION,6) && strcmp("exported",REPO_VERSION)) { | 
					
						
							| 
									
										
										
										
											2008-10-09 09:00:26 +00:00
										 |  |  | 		return REPO_VERSION""VERSION_SUFFIX; | 
					
						
							| 
									
										
										
										
											2008-08-04 10:38:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	/* it is a release, or we have nothing better */ | 
					
						
							| 
									
										
										
										
											2008-10-09 09:00:26 +00:00
										 |  |  | 	return VERSION""VERSION_SUFFIX; | 
					
						
							| 
									
										
										
										
											2008-08-04 10:38:24 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-16 17:41:57 +03:00
										 |  |  | /* CL_NOLIBCLAMAV means to omit functions that depends on libclamav */ | 
					
						
							|  |  |  | #ifndef CL_NOLIBCLAMAV
 | 
					
						
							|  |  |  | char *freshdbdir(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct cl_cvd *d1, *d2; | 
					
						
							|  |  |  | 	struct optstruct *opts; | 
					
						
							|  |  |  | 	const struct optstruct *opt; | 
					
						
							|  |  |  | 	const char *dbdir; | 
					
						
							|  |  |  | 	char *retdir; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* try to find the most up-to-date db directory */ | 
					
						
							|  |  |  |     dbdir = cl_retdbdir(); | 
					
						
							|  |  |  |     if((opts = optparse(CONFDIR_FRESHCLAM, 0, NULL, 0, OPT_FRESHCLAM, 0, NULL))) { | 
					
						
							|  |  |  | 	if((opt = optget(opts, "DatabaseDirectory"))->enabled) { | 
					
						
							|  |  |  | 	    if(strcmp(dbdir, opt->strarg)) { | 
					
						
							|  |  |  | 		    char *daily = (char *) malloc(strlen(opt->strarg) + strlen(dbdir) + 30); | 
					
						
							|  |  |  | 		sprintf(daily, "%s"PATHSEP"daily.cvd", opt->strarg); | 
					
						
							|  |  |  | 		if(access(daily, R_OK)) | 
					
						
							|  |  |  | 		    sprintf(daily, "%s"PATHSEP"daily.cld", opt->strarg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!access(daily, R_OK) && (d1 = cl_cvdhead(daily))) { | 
					
						
							|  |  |  | 		    sprintf(daily, "%s"PATHSEP"daily.cvd", dbdir); | 
					
						
							|  |  |  | 		    if(access(daily, R_OK)) | 
					
						
							|  |  |  | 			sprintf(daily, "%s"PATHSEP"daily.cld", dbdir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		    if(!access(daily, R_OK) && (d2 = cl_cvdhead(daily))) { | 
					
						
							|  |  |  | 			free(daily); | 
					
						
							|  |  |  | 			if(d1->version > d2->version) | 
					
						
							|  |  |  | 			    dbdir = opt->strarg; | 
					
						
							|  |  |  | 			cl_cvdfree(d2); | 
					
						
							|  |  |  | 		    } else { | 
					
						
							|  |  |  | 			free(daily); | 
					
						
							|  |  |  | 			dbdir = opt->strarg; | 
					
						
							|  |  |  | 		    } | 
					
						
							|  |  |  | 		    cl_cvdfree(d1); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 		    free(daily); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     retdir = strdup(dbdir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(opts) | 
					
						
							|  |  |  | 	optfree(opts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return retdir; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void print_version(const char *dbdir) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char *fdbdir = NULL, *path; | 
					
						
							|  |  |  | 	const char *pt; | 
					
						
							|  |  |  | 	struct cl_cvd *daily; | 
					
						
							|  |  |  | 	time_t db_time; | 
					
						
							|  |  |  | 	unsigned int db_version = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(dbdir) | 
					
						
							|  |  |  | 	pt = dbdir; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  | 	pt = fdbdir = freshdbdir(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(!pt) { | 
					
						
							|  |  |  | 	printf("ClamAV %s\n",get_version()); | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(!(path = malloc(strlen(pt) + 11))) { | 
					
						
							|  |  |  | 	if(!dbdir) | 
					
						
							|  |  |  | 	    free(fdbdir); | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sprintf(path, "%s"PATHSEP"daily.cvd", pt); | 
					
						
							|  |  |  |     if(!access(path, R_OK)) { | 
					
						
							|  |  |  | 	daily = cl_cvdhead(path); | 
					
						
							|  |  |  | 	if(daily) { | 
					
						
							|  |  |  | 	    db_version = daily->version; | 
					
						
							|  |  |  | 	    db_time = daily->stime; | 
					
						
							|  |  |  | 	    cl_cvdfree(daily); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sprintf(path, "%s"PATHSEP"daily.cld", pt); | 
					
						
							|  |  |  |     if(!access(path, R_OK)) { | 
					
						
							|  |  |  | 	daily = cl_cvdhead(path); | 
					
						
							|  |  |  | 	if(daily) { | 
					
						
							|  |  |  | 	    if(daily->version > db_version) { | 
					
						
							|  |  |  | 		db_version = daily->version; | 
					
						
							|  |  |  | 		db_time = daily->stime; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	    cl_cvdfree(daily); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(!dbdir) | 
					
						
							|  |  |  | 	free(fdbdir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(db_version) { | 
					
						
							|  |  |  | 	printf("ClamAV %s/%u/%s", get_version(), db_version, ctime(&db_time)); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  | 	printf("ClamAV %s\n",get_version()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     free(path); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int check_flevel(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if(cl_retflevel() < CL_FLEVEL) { | 
					
						
							|  |  |  | 	fprintf(stderr, "ERROR: This tool requires libclamav with functionality level %u or higher (current f-level: %u)\n", CL_FLEVEL, cl_retflevel()); | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-21 13:43:05 +00:00
										 |  |  | const char *filelist(const struct optstruct *opts, int *err) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static char buff[1025]; | 
					
						
							|  |  |  | 	static unsigned int cnt = 0; | 
					
						
							|  |  |  | 	const struct optstruct *opt; | 
					
						
							|  |  |  | 	static FILE *fs = NULL; | 
					
						
							|  |  |  | 	size_t len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(!cnt && (opt = optget(opts, "file-list"))->enabled) { | 
					
						
							|  |  |  | 	if(!fs) { | 
					
						
							|  |  |  | 	    fs = fopen(opt->strarg, "r"); | 
					
						
							|  |  |  | 	    if(!fs) { | 
					
						
							|  |  |  | 		fprintf(stderr, "ERROR: --file-list: Can't open file %s\n", opt->strarg); | 
					
						
							|  |  |  | 		if(err) | 
					
						
							|  |  |  | 		    *err = 54; | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(fgets(buff, 1024, fs)) { | 
					
						
							|  |  |  | 	    buff[1024] = 0; | 
					
						
							|  |  |  | 	    len = strlen(buff); | 
					
						
							|  |  |  | 	    if(!len) { | 
					
						
							|  |  |  | 		fclose(fs); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	    len--; | 
					
						
							|  |  |  | 	    while(len && ((buff[len] == '\n') || (buff[len] == '\r'))) | 
					
						
							|  |  |  | 		buff[len--] = '\0'; | 
					
						
							|  |  |  | 	    return buff; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 	    fclose(fs); | 
					
						
							|  |  |  | 	    return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return opts->filename ? opts->filename[cnt++] : NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | int filecopy(const char *src, const char *dest) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-24 16:21:51 +02:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  |     return (!CopyFileA(src, dest, 0)); | 
					
						
							|  |  |  | #elif defined(C_DARWIN)
 | 
					
						
							| 
									
										
										
										
											2005-04-28 22:58:31 +00:00
										 |  |  | 	pid_t pid; | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-28 22:58:31 +00:00
										 |  |  |     /* On Mac OS X use ditto and copy resource fork, too. */ | 
					
						
							|  |  |  |     switch(pid = fork()) { | 
					
						
							|  |  |  | 	case -1: | 
					
						
							|  |  |  | 	    return -1; | 
					
						
							|  |  |  | 	case 0: | 
					
						
							| 
									
										
										
										
											2008-01-10 18:01:16 +00:00
										 |  |  | 	    execl("/usr/bin/ditto", "ditto", src, dest, NULL); | 
					
						
							| 
									
										
										
										
											2006-06-09 10:46:12 +00:00
										 |  |  | 	    perror("execl(ditto)"); | 
					
						
							| 
									
										
										
										
											2005-04-28 22:58:31 +00:00
										 |  |  | 	    break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 	    wait(NULL); | 
					
						
							|  |  |  | 	    return 0; | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-28 22:58:31 +00:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-03 18:47:18 +00:00
										 |  |  | #else /* C_DARWIN */
 | 
					
						
							|  |  |  |     return cli_filecopy(src, dest); | 
					
						
							| 
									
										
										
										
											2004-11-28 23:26:29 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2005-04-29 01:21:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  | int daemonize(void) | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-24 19:10:27 +02:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  |     fputs("Background mode is not supported on your operating system\n", stderr); | 
					
						
							| 
									
										
										
										
											2008-02-11 10:27:51 +00:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  | 	int fds[3], i; | 
					
						
							|  |  |  | 	pid_t pid; | 
					
						
							| 
									
										
										
										
											2006-09-27 10:49:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  |     fds[0] = open("/dev/null", O_RDONLY); | 
					
						
							|  |  |  |     fds[1] = open("/dev/null", O_WRONLY); | 
					
						
							|  |  |  |     fds[2] = open("/dev/null", O_WRONLY); | 
					
						
							|  |  |  |     if(fds[0] == -1 || fds[1] == -1 || fds[2] == -1) { | 
					
						
							|  |  |  | 	fputs("Can't open /dev/null\n", stderr); | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | 	for(i = 0; i <= 2; i++) | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  | 	    if(fds[i] != -1) | 
					
						
							|  |  |  | 		close(fds[i]); | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  |     for(i = 0; i <= 2; i++) { | 
					
						
							|  |  |  | 	if(dup2(fds[i], i) == -1) { | 
					
						
							|  |  |  | 	    fprintf(stderr, "dup2(%d, %d) failed\n", fds[i], i); /* may not be printed */ | 
					
						
							|  |  |  | 	    for(i = 0; i <= 2; i++) | 
					
						
							|  |  |  | 		if(fds[i] != -1) | 
					
						
							|  |  |  | 		    close(fds[i]); | 
					
						
							|  |  |  | 	    return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  |     for(i = 0; i <= 2; i++) | 
					
						
							|  |  |  | 	if(fds[i] > 2) | 
					
						
							|  |  |  | 	    close(fds[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pid = fork(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(pid == -1) | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(pid) | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | 	exit(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setsid(); | 
					
						
							| 
									
										
										
										
											2008-01-22 17:47:15 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2006-08-30 22:41:21 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-07-18 16:31:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int match_regex(const char *filename, const char *pattern) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	regex_t reg; | 
					
						
							| 
									
										
										
										
											2008-07-18 18:48:10 +00:00
										 |  |  | 	int match, flags = REG_EXTENDED | REG_NOSUB; | 
					
						
							| 
									
										
										
										
											2008-07-18 16:31:02 +00:00
										 |  |  | 	char fname[513]; | 
					
						
							| 
									
										
										
										
											2009-09-24 19:10:27 +02:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2008-07-18 18:48:10 +00:00
										 |  |  | 	flags |= REG_ICASE; /* case insensitive on Windows */ | 
					
						
							| 
									
										
										
										
											2008-07-18 16:31:02 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 	if(cli_regcomp(®, pattern, flags) != 0) | 
					
						
							|  |  |  | 	    return 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 19:07:39 +02:00
										 |  |  | 	if(pattern[strlen(pattern) - 1] == *PATHSEP) { | 
					
						
							|  |  |  | 	    snprintf(fname, 511, "%s"PATHSEP, filename); | 
					
						
							| 
									
										
										
										
											2008-07-18 16:31:02 +00:00
										 |  |  | 	    fname[512] = 0; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 	    strncpy(fname, filename, 513); | 
					
						
							|  |  |  | 	    fname[512]='\0'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	match = (cli_regexec(®, fname, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1; | 
					
						
							|  |  |  | 	cli_regfree(®); | 
					
						
							|  |  |  | 	return match; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-02-12 21:14:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-25 00:09:13 +02:00
										 |  |  | int cli_is_abspath(const char *path) { | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  |     int len = strlen(path); | 
					
						
							| 
									
										
										
										
											2009-10-21 04:31:11 +02:00
										 |  |  |     return (len > 2 && path[0] == '\\' && path[1] == '\\') || (len >= 2 && ((*path >= 'a' && *path <= 'z') || (*path >= 'A' && *path <= 'Z')) && path[1] == ':'); | 
					
						
							| 
									
										
										
										
											2009-09-25 00:09:13 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     return *path == '/'; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-09-16 14:37:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | unsigned int countlines(const char *filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	FILE *fh; | 
					
						
							|  |  |  | 	char buff[1024]; | 
					
						
							|  |  |  | 	unsigned int lines = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if((fh = fopen(filename, "r")) == NULL) | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while(fgets(buff, sizeof(buff), fh)) { | 
					
						
							|  |  |  | 	if(buff[0] == '#') continue; | 
					
						
							|  |  |  | 	lines++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fclose(fh); | 
					
						
							|  |  |  |     return lines; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |