blob: 20a89cca791a230b5a61d11c302f6d232a26b361 (
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
|
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* dirstack.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include "util.h"
#define STACK_DEPTH 128
static int dirstack[STACK_DEPTH];
static int stacktop = 0;
int pushd(const char *path)
{
int fd;
assert(stacktop < STACK_DEPTH);
fd = open(".", O_DIRECTORY | O_PATH | O_RDONLY | O_CLOEXEC);
if (fd < 0) {
perror("open ./");
return -1;
}
if (chdir(path)) {
perror(path);
close(fd);
return -1;
}
dirstack[stacktop++] = fd;
return 0;
}
int pushdn(const char *path, size_t len)
{
char *temp;
int ret;
temp = strndup(path, len);
if (temp == NULL) {
perror("pushd");
return -1;
}
ret = pushd(temp);
free(temp);
return ret;
}
int popd(void)
{
int fd;
assert(stacktop > 0);
fd = dirstack[stacktop - 1];
if (fchdir(fd)) {
perror("popd");
return -1;
}
--stacktop;
close(fd);
return 0;
}
|