forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.c
118 lines (102 loc) · 3 KB
/
configuration.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
118
/**
* @file configuration.c
* @author Michal Vasko <[email protected]>
* @brief yanglint configuration
*
* Copyright (c) 2017 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include "configuration.h"
#include "../../linenoise/linenoise.h"
/* Yanglint home (appended to ~/) */
#define YL_DIR ".yanglint"
char *
get_yanglint_dir(void)
{
int ret;
struct passwd *pw;
char *user_home, *yl_dir;
if (!(pw = getpwuid(getuid()))) {
fprintf(stderr, "Determining home directory failed (%s).\n", strerror(errno));
return NULL;
}
user_home = pw->pw_dir;
yl_dir = malloc(strlen(user_home) + 1 + strlen(YL_DIR) + 1);
if (!yl_dir) {
fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
return NULL;
}
sprintf(yl_dir, "%s/%s", user_home, YL_DIR);
ret = access(yl_dir, R_OK | X_OK);
if (ret == -1) {
if (errno == ENOENT) {
/* directory does not exist */
fprintf(stdout, "Configuration directory \"%s\" does not exist, creating it.\n", yl_dir);
if (mkdir(yl_dir, 00700)) {
fprintf(stderr, "Configuration directory \"%s\" cannot be created (%s).\n", yl_dir, strerror(errno));
free(yl_dir);
return NULL;
}
} else {
fprintf(stderr, "Configuration directory \"%s\" exists but cannot be accessed (%s).\n", yl_dir, strerror(errno));
free(yl_dir);
return NULL;
}
}
return yl_dir;
}
void
load_config(void)
{
char *yl_dir, *history_file;
if ((yl_dir = get_yanglint_dir()) == NULL) {
return;
}
history_file = malloc(strlen(yl_dir) + 9);
if (!history_file) {
fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
free(yl_dir);
return;
}
sprintf(history_file, "%s/history", yl_dir);
if (access(history_file, F_OK) && (errno == ENOENT)) {
fprintf(stdout, "No saved history.\n");
} else if (linenoiseHistoryLoad(history_file)) {
fprintf(stderr, "Failed to load history.\n");
}
free(history_file);
free(yl_dir);
}
void
store_config(void)
{
char *yl_dir, *history_file;
if ((yl_dir = get_yanglint_dir()) == NULL) {
return;
}
history_file = malloc(strlen(yl_dir) + 9);
if (!history_file) {
fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
free(yl_dir);
return;
}
sprintf(history_file, "%s/history", yl_dir);
if (linenoiseHistorySave(history_file)) {
fprintf(stderr, "Failed to save history.\n");
}
free(history_file);
free(yl_dir);
}