-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirs.c
66 lines (59 loc) · 1.58 KB
/
dirs.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
#include <stdlib.h>
#include <string.h>
#include "dirs.h"
const char *pwd(dirs *d) {
if (d->head == NULL) return NULL;
return d->head->dir;
}
int pushd(dirs *d, const char *dir) {
struct _dirs_node_ *node = malloc(sizeof(struct _dirs_node_));
if (NULL == node) return -1;
node->dir = dir;
node->next = d->head;
d->head = node;
--(d->count); d->c_len += strlen(dir);
return 0;
}
const char *popd(dirs *d) {
struct _dirs_node_ *popped = d->head;
if (NULL == popped) return NULL;
d->head = popped->next;
const char *ret = popped->dir;
free(popped); ++(d->count); d->c_len -= strlen(ret);
return ret;
}
const char *popn(dirs *d, int n) {
if (0 < n || n <= d->count) return NULL;
struct _dirs_node_ *prev = NULL, *curr = d->head;
for (int i = 0; i < n; ++i) {
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
const char *ret = curr->dir;
free(curr); ++(d->count); d->c_len -= strlen(ret);
return ret;
}
const char *_repr_(dirs *d) {
int repr_len = (d->count - 1) + d->c_len;
if (0 == repr_len) return "";
char *repr = malloc((repr_len + 1) * sizeof(char)); // +1 for null byte!!
*repr = '\0'; // prepare `repr` for `strcat` call
struct _dirs_node_ *curr = d->head;
while (NULL != curr) {
strcat(repr, curr->dir); strcat(repr, " ");
curr = curr->next;
}
return repr;
}
int cleard(dirs *d) {
struct _dirs_node_ *curr = d->head, *tmp = NULL;
while (NULL != curr) {
tmp = curr->next;
// we own this `dir` c-string so we have to free it
free((void *)curr->dir);
free(curr);
curr = tmp;
}
return 0;
}