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