-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellmemory.c
More file actions
84 lines (65 loc) · 1.52 KB
/
Copy pathshellmemory.c
File metadata and controls
84 lines (65 loc) · 1.52 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
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct memory_struct{
char *var;
char *value;
};
struct memory_struct shellmemory[1000];
// Helper functions
int match(char *model, char *var) {
int i, len=strlen(var), matchCount=0;
for(i=0;i<len;i++)
if (*(model+i) == *(var+i)) matchCount++;
if (matchCount == len)
return 1;
else
return 0;
}
char *extract(char *model) {
char token='='; // look for this to find value
char value[1000]; // stores the extract value
int i,j, len=strlen(model);
for(i=0;i<len && *(model+i)!=token;i++); // loop till we get there
// extract the value
for(i=i+1,j=0;i<len;i++,j++) value[j]=*(model+i);
value[j]='\0';
return strdup(value);
}
// Shell memory functions
void mem_init(){
int i;
for (i=0; i<1000; i++){
shellmemory[i].var = "none";
shellmemory[i].value = "none";
}
}
// Set key value pair
void mem_set_value(char *var_in, char *value_in) {
int i;
for (i=0; i<1000; i++){
if (strcmp(shellmemory[i].var, var_in) == 0){
shellmemory[i].value = strdup(value_in);
return;
}
}
//Value does not exist, need to find a free spot.
for (i=0; i<1000; i++){
if (strcmp(shellmemory[i].var, "none") == 0){
shellmemory[i].var = strdup(var_in);
shellmemory[i].value = strdup(value_in);
return;
}
}
return;
}
//get value based on input key
char *mem_get_value(char *var_in) {
int i;
for (i=0; i<1000; i++){
if (strcmp(shellmemory[i].var, var_in) == 0){
return strdup(shellmemory[i].value);
}
}
return "Variable does not exist";
}