blob: 03e9f584932febbe1fdcd811c46fcdbcee8b75c2 (
plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/* wydawca - automatic release submission daemon
Copyright (C) 2011, 2013, 2017 Sergey Poznyakoff
Wydawca 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.
Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */
#include "wydawca.h"
struct saved_dir {
struct saved_dir *prev;
int fd;
char *name;
};
struct saved_dir *dir_stack;
#ifndef O_SEARCH
# define O_SEARCH 0
#endif
char *
getcwd_alloc(void)
{
size_t size = 0;
char *buf = NULL;
char *p;
do {
if (!buf) {
size = 64;
buf = malloc(size);
} else {
char *np;
size_t ns = 2 * size;
if (ns < size) {
free(buf);
errno = ENOMEM;
return NULL;
}
size = ns;
np = realloc(buf, size);
if (!np)
free(buf);
buf = np;
}
if (!buf)
return NULL;
}
while ((p = getcwd(buf, size)) == NULL && errno == ERANGE);
if (!p) {
int ec = errno;
free(buf);
buf = NULL;
errno = ec;
}
return buf;
}
static int
_save_dir(struct saved_dir *sd)
{
int fd;
#ifdef HAVE_FCHDIR
fd = open(".", O_SEARCH);
#else
fd = -1;
#endif
if (fd == -1) {
sd->name = getcwd_alloc();
if (!sd->name)
return errno;
} else
sd->name = NULL;
sd->fd = fd;
return 0;
}
static void
_drop_dir(struct saved_dir *sd)
{
if (sd->fd != -1)
close(sd->fd);
free(sd->name);
free(sd);
}
static int
_push_dir(struct saved_dir *sd, const char *dirname)
{
int rc = _save_dir(sd);
if (rc)
return rc;
if (dirname && chdir(dirname)) {
rc = errno;
_drop_dir(sd);
return rc;
}
return 0;
}
int
push_dir(const char *dirname)
{
int rc;
struct saved_dir *sd = malloc(sizeof(*sd));
if (!sd)
return errno;
rc = _push_dir(sd, dirname);
if (rc) {
errno = rc;
return -1;
}
sd->prev = dir_stack;
dir_stack = sd;
return 0;
}
int
pop_dir()
{
struct saved_dir *sd = dir_stack;
int rc;
if (!sd) {
errno = ENOENT;
return -1;
}
dir_stack = sd->prev;
if (sd->fd != -1)
rc = fchdir(sd->fd);
else
rc = chdir(sd->name);
if (rc)
rc = errno;
_drop_dir(sd);
errno = rc;
return rc;
}
|