-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellmemory.c
52 lines (42 loc) · 1.09 KB
/
shellmemory.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shellmemory.h"
struct MEM shellMemory[1000];
int setVar(char *var, char *val);
int printVar(char *var);
int findVarIdx(char *var, int *retIdx);
int memFilled = 0;
int setVar(char *var, char *val){
int idx= 0;
if(findVarIdx(var, &idx) == 0) {
char *var_dup = strdup(var);
char *val_dup = strdup(val);
struct MEM newVar = {var_dup, val_dup};
shellMemory[memFilled++] = newVar;
} else {
shellMemory[idx].var = strdup(var);
shellMemory[idx].value = strdup(val);
}
return 0;
}
int printVar(char *var){
int idx = 0;
if(findVarIdx(var, &idx) == 0){
printf("Variable does not exist \n");
} else {
printf("%s \n", shellMemory[idx].value);
}
return 0;
}
//searches shellMemory for variable and returns 0 if doesn't exist. or else it returns 1 if exists as well as saves the idx in retIdx
int findVarIdx(char *var, int *retIdx){
if(memFilled == 0) return 0;
for(int i = 0; i<memFilled; i++){
if(strcmp(shellMemory[i].var, var) == 0){
*retIdx = i;
return 1;
}
}
return 0;
}