-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshellmemory.c
More file actions
47 lines (38 loc) · 1.19 KB
/
Copy pathshellmemory.c
File metadata and controls
47 lines (38 loc) · 1.19 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
// Muhammad Huzaifa Elahi
// 260726386
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shellmemory.h"
// Constants
const int MAX_SHELL_MEMORY = 1000;
const int MAX_STRING_LENGTH = 1000;
struct MEM shellMemory[1000];
char* getValue(char* var){
for(int i = 0; i < MAX_SHELL_MEMORY; i++){
if ((shellMemory[i].var != NULL) && (strcmp(shellMemory[i].var, var) == 0)){
return shellMemory[i].value;
}
}
return NULL; // Did not find key
}
int addNode(char *var , char *value) {
for(int i = 0; i < MAX_SHELL_MEMORY; i++){
// Insert in first empty struct in array
if(shellMemory[i].var == NULL){
// Create and allocate new node
struct MEM newNode;
newNode.var = calloc(MAX_STRING_LENGTH, sizeof(char));
newNode.value = calloc(MAX_STRING_LENGTH, sizeof(char));
strcpy(newNode.var, var);
strcpy(newNode.value, value);
shellMemory[i] = newNode;
return 0;
// Overwrite
} else if (strcmp(shellMemory[i].var, var) == 0){
strcpy(shellMemory[i].value, value);
return 0;
}
}
return -1; // Full
}