-
Notifications
You must be signed in to change notification settings - Fork 117
/
pathconfig.c
118 lines (102 loc) · 3.03 KB
/
pathconfig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2014 by Alain Pannetrat <[email protected]>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek 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 Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "pathconfig.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include "misc.h"
const char *PATH_CONFIG_OPTIONS[NUM_PATH_CONFIG_OPTIONS]=
{
NULL,
NULL,
NULL,
"",
"scripts",
"replay",
"logs",
"config.lua",
"cardpeekrc.lua",
"version",
"scripts/etc/smartcard_list.txt",
"scripts/etc/smartcard_list.download",
"cardpeek.update"
};
char *PATH_CONFIG_STRING[NUM_PATH_CONFIG_OPTIONS];
int path_config_init(void)
{
char path_config_string[PATH_MAX];
char current_working_dir[PATH_MAX];
char cardpeek_dir[PATH_MAX];
const char *home_dir;
unsigned i;
#ifndef _WIN32
home_dir = getenv("HOME");
#else
home_dir = getenv("USERDATA");
if (home_dir==NULL)
home_dir = getenv("USERPROFILE");
#endif
if (home_dir==NULL)
return 0;
if (getenv("CARDPEEK_DIR"))
strncpy(cardpeek_dir,getenv("CARDPEEK_DIR"),PATH_MAX);
else
snprintf(cardpeek_dir,PATH_MAX,"%s/.cardpeek",home_dir);
if (getcwd(current_working_dir,PATH_MAX))
PATH_CONFIG_STRING[0]=strdup(current_working_dir);
else
PATH_CONFIG_STRING[0]=strdup(home_dir);
snprintf(path_config_string,PATH_MAX,"%s/.cardpeek.log",home_dir);
PATH_CONFIG_STRING[1]=strdup(path_config_string);
PATH_CONFIG_STRING[2]=strdup(home_dir);
for (i=PATH_CONFIG_FOLDER_CARDPEEK; i<NUM_PATH_CONFIG_OPTIONS; i++)
{
snprintf(path_config_string,PATH_MAX,"%s/%s",cardpeek_dir,PATH_CONFIG_OPTIONS[i]);
PATH_CONFIG_STRING[i]=strdup(path_config_string);
}
return 1;
}
const char *path_config_get_string(unsigned c_index)
{
if (c_index>NUM_PATH_CONFIG_OPTIONS)
return NULL;
return PATH_CONFIG_STRING[c_index];
}
int path_config_set_string(unsigned c_index, const char *path)
{
if (c_index>NUM_PATH_CONFIG_OPTIONS)
return 0;
if (PATH_CONFIG_STRING[c_index])
free(PATH_CONFIG_STRING[c_index]);
if (path)
PATH_CONFIG_STRING[c_index]=strdup(path);
else
PATH_CONFIG_STRING[c_index]=strdup("");
return 1;
}
void path_config_release(void)
{
unsigned i;
for (i=0; i<NUM_PATH_CONFIG_OPTIONS; i++)
free(PATH_CONFIG_STRING[i]);
}