-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsPath.c
More file actions
118 lines (104 loc) · 2.8 KB
/
fsPath.c
File metadata and controls
118 lines (104 loc) · 2.8 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
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
/**************************************************************
* Class:: CSC-415-02 Summer 2025
* Name::Phillip Davis Igor Tello Jared Aung Preet Vithani
* Student IDs:: 923980431, 923043807, 922772159, 923575806
* GitHub-Name::R3plug
* Group-Name::Team Kentucky Kernels
* Project:: Basic File System
*
* File:: fsPath.c
*
* Description:: This file includes functions to manage the stack, clean, and
* convert the stack to a string representation of the path. The implementation
* ensures memory is properly allocated and freed, and paths are correctly
* managed.
*
**************************************************************/
#include "fsPath.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dirLow.h"
#include "mfs.h"
#define MAXSTACKSIZE 50 // Max Path Component
// We need an internal stack of the path component
static char *stack[MAXSTACKSIZE];
static char *strPath = NULL;
static int size = 0;
void push(const char *value) {
if (size >= MAXSTACKSIZE) return;
stack[size] = malloc(strlen(value) + 1);
if (!stack[size]) return;
strcpy(stack[size], value);
size++;
}
void pop(void) {
if (size <= 0) return;
free(stack[--size]);
stack[size] = NULL;
}
void pathCleaner(char *path) {
// Lets reset the stack to root, if the path is absolute
if (path[0] == '/') {
while (size > 0) pop();
}
// We need to tokenize on /
char *tok = strtok(path, "/");
while (tok) {
if (strcmp(tok, ".") == 0) {
// pass
}
else if (strcmp(tok, "..") == 0) {
if (size > 0) pop();
}
else {
push(tok);
}
tok = strtok(NULL, "/");
}
}
char *toString(void) {
// We need to build
// one / per component
// plus trailing / and null
int needed = 1;
for (int i = 0; i < size; i++) {
needed += strlen(stack[i]) + 1;
}
needed += 1;
char *buf = realloc(strPath, needed);
if (!buf) return NULL;
strPath = buf;
// we need the build output
char *p = strPath;
*p++ = '/';
for (int i = 0; i < size; i++) {
size_t len = strlen(stack[i]);
memcpy(p, stack[i], len);
p += len;
*p++ = '/';
}
*p = '\0';
return strPath;
}
char *getCWDStr(void) {
return toString();
}
void freeSTRCWD(void) {
while (size > 0) pop();
free(strPath);
strPath = NULL;
}
void freePPI(ppInfo *ppi) {
if (!ppi) return;
// if this parent buffer isn’t the global cwd or root, free it
if (ppi->parent != getCwd() && ppi->parent != getRootDir()) {
free(ppi->parent);
}
free(ppi);
}
int entryIsDir(ppInfo *ppi) {
if (!ppi || ppi->index < 0) return 0;
printf("is directory = %d\n", ppi->parent[ppi->index].isDir);
return (ppi->parent[ppi->index].isDir == 1);
}