-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alias.h
98 lines (86 loc) · 3 KB
/
Alias.h
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef UNTITLED6_ALIAS_H
#define UNTITLED6_ALIAS_H
#define MAX_ALIAS_LEN 1017//1025 - alias - (= )- (' )- (' )= 1017
int alias_count = 0;
// Structure for alias linked list
typedef struct Alias {
char name[MAX_ALIAS_LEN]; // Alias name
char command[MAX_ALIAS_LEN]; // Command associated with the alias
struct Alias *next; // Pointer to the next alias in the list
} Alias;
Alias *aliases = NULL; // Head of the alias linked list
// Function to add a new alias or update an existing alias
void add_alias(const char *name, const char *command) {
Alias *current = aliases;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
// Alias with the same name exists, update its command
strcpy(current->command, command);
return;
}
current = current->next;
}
// Alias does not exist, create a new alias
Alias *new_alias = (Alias *)malloc(sizeof(Alias)); // Allocate memory for the new alias
if (!new_alias) {
perror("malloc");
_exit(EXIT_FAILURE);
}
strcpy(new_alias->name, name); // Copy the alias name
strcpy(new_alias->command, command); // Copy the alias command
new_alias->next = aliases; // Insert the new alias at the head of the list
aliases = new_alias;
alias_count++; // Increment alias count
}
// Function to find an alias by name
Alias* find_alias(const char *name) {
Alias *current = aliases;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current; // Return the alias if found
}
current = current->next;
}
return NULL; // Return NULL if the alias is not found
}
// Function to print all defined aliases
void print_aliases() {
Alias *current = aliases;
while (current != NULL) {
printf("%s='%s'\n", current->name, current->command);
current = current->next;
}
}
// Function to remove an alias by name
void unalias(const char *name) {
Alias *prev = NULL;
Alias *current = aliases;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
if (prev == NULL) {
aliases = current->next; // Update head of the list if removing the first node
} else {
prev->next = current->next; // Update pointers if removing a node in the middle or end
}
free(current); // Free memory for the removed alias
alias_count--;// Decrement alias count
return;
}
prev = current;
current = current->next;
}
printf("ERR: Alias '%s' not found\n", name); // Print message if alias not found
}
// Function to free all allocated alias structures
void free_aliases() {
Alias *current = aliases;
while (current != NULL) {
Alias *temp = current;
current = current->next;
free(temp);
}
}
#endif //UNTITLED6_ALIAS_H