-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpwd.c
More file actions
65 lines (55 loc) · 1.29 KB
/
pwd.c
File metadata and controls
65 lines (55 loc) · 1.29 KB
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
#include "header.h"
#include "pwd.h"
#include "tree_io.h"
#include <stdbool.h> // bool 타입 사용을 위해
int init_stack(Stack* s) {
if (s == NULL) {
fprintf(stderr, "Stack Buffer doesn't exist.\n");
return -1;
}
s->top = -1;
return 0;
}
bool IsFull(Stack* s) {
return s->top == MAXB - 1;
}
bool IsEmpty(Stack* s) {
return s->top == -1;
}
int push(Stack* s, const char* dir) {
if (IsFull(s)) {
fprintf(stderr, "Stack is Full.\n");
return -1;
}
s->path[++(s->top)] = strdup(dir);
return 0;
}
char* pop(Stack* s) {
if (IsEmpty(s)) {
return NULL;
}
return s->path[(s->top)--];
}
int get_pwd(DirectoryTree *dTree) {
Stack buff;
init_stack(&buff);
TreeNode *current = dTree->current;
// 루트 디렉터리까지 올라갈 때까지 반복
while (current->parent != NULL) {
push(&buff, current->name);
current = current->parent;
}
// 루트 경로인 "/" 출력
if (IsEmpty(&buff)) {
printf("/\n");
} else {
// 스택에 저장된 경로를 출력 (루트부터 현재까지)
char* name;
while ((name = pop(&buff)) != NULL) {
printf("/%s", name);
free(name);
}
printf("\n");
}
return 0;
}