Move make_names() and related variables to its own source file.
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "logger.h"
24 #include "xalloc.h"
25
26 char *netname = NULL;
27 char *confdir = NULL;           /* base configuration directory */
28 char *confbase = NULL;          /* base configuration directory for this instance of tinc */
29 char *identname = NULL;         /* program name for syslog */
30 char *logfilename = NULL;       /* log file location */
31 char *pidfilename = NULL;
32 char *program_name = NULL;
33
34 /*
35   Set all files and paths according to netname
36 */
37 void make_names(void) {
38 #ifdef HAVE_MINGW
39         HKEY key;
40         char installdir[1024] = "";
41         long len = sizeof installdir;
42 #endif
43
44         if(netname)
45                 xasprintf(&identname, "tinc.%s", netname);
46         else
47                 identname = xstrdup("tinc");
48
49 #ifdef HAVE_MINGW
50         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
51                 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
52                         confdir = xstrdup(installdir);
53                         if(!logfilename)
54                                 xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", installdir, identname);
55                         if(!confbase) {
56                                 if(netname)
57                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
58                                 else
59                                         xasprintf(&confbase, "%s", installdir);
60                         }
61                         if(!pidfilename)
62                                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
63                 }
64                 RegCloseKey(key);
65         }
66 #endif
67         if(!confdir)
68                 confdir = xstrdup(CONFDIR);
69
70         if(!logfilename)
71                 xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
72
73         if(!pidfilename)
74                 xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
75
76         if(netname) {
77                 if(!confbase)
78                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
79                 else
80                         logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
81         } else {
82                 if(!confbase)
83                         xasprintf(&confbase, CONFDIR SLASH "tinc");
84         }
85 }
86
87 void free_names(void) {
88         free(identname);
89         free(netname);
90         free(pidfilename);
91         free(logfilename);
92         free(confbase);
93         free(confdir);
94 }