Imported Upstream version 2.5.1
[ossec-hids.git] / src / win32 / setup-shared.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11        
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <dirent.h>
19 #include <time.h>
20 #include <windows.h>
21 #include "os_regex/os_regex.h"
22
23 #define OSSECCONF   "ossec.conf"
24 #define OS_MAXSTR   1024
25
26
27 /* Checks if a file exist. */
28 int fileexist(char *file)
29 {
30     FILE *fp;
31
32     /* Opening file */
33     fp = fopen(file, "r");
34     if(!fp)
35         return(0);
36
37     fclose(fp);
38     return(1);
39 }
40
41
42 /* Grep for a string in a file. */
43 int dogrep(char *file, char *str)
44 {
45     char line[OS_MAXSTR +1];
46     FILE *fp;
47
48     /* Opening file */
49     fp = fopen(file, "r");
50     if(!fp)
51         return(0);
52
53     /* Clearing memory */
54     memset(line, '\0', OS_MAXSTR +1);
55
56     /* Reading file and looking for str */ 
57     while(fgets(line, OS_MAXSTR, fp) != NULL)
58     {
59         if(OS_Match(str, line))
60         {
61             fclose(fp);
62             return(1);
63         }
64     }
65
66     fclose(fp);
67     return(0);
68 }
69
70
71 /* Check if dir exists */
72 int direxist(char *dir)
73 {
74     DIR *dp;
75
76     /* Opening dir */
77     dp = opendir(dir);
78     if(dp == NULL)
79         return(0);
80
81     closedir(dp);
82     return(1);
83 }
84
85
86 /* Get Windows main directory */
87 void get_win_dir(char *file, int f_size)
88 {
89     ExpandEnvironmentStrings("%WINDIR%", file, f_size);
90
91     if(!direxist(file))
92     {
93         strncpy(file, "C:\\WINDOWS", f_size);
94     }
95 }
96
97
98 /* EOF */