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