Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS210-shell
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nikolay Tsanov CS2018
CS210-shell
Commits
6e69590a
Commit
6e69590a
authored
5 years ago
by
Nikolay Tsanov
Browse files
Options
Downloads
Patches
Plain Diff
Alias and unalias
parent
c04161ba
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
shell.c
+67
-58
67 additions, 58 deletions
shell.c
with
67 additions
and
58 deletions
shell.c
+
67
−
58
View file @
6e69590a
...
...
@@ -132,18 +132,50 @@ int run_command(String arguments[]) {
}
if
(
strcmp
(
arguments
[
0
],
"alias"
)
==
0
)
{
if
(
arguments
[
1
]
!=
NULL
&&
arguments
[
2
]
!=
NULL
)
{
int
status
=
-
1
;
// Check if there is a place in the aliases array and store a new alias.
for
(
int
i
=
0
;
i
<
MAX_ALIASES
;
i
++
)
{
if
(
aliases
[
i
].
key
!=
NULL
&&
strcmp
(
aliases
[
i
].
key
,
arguments
[
1
])
==
0
)
{
status
=
0
;
}
else
if
(
aliases
[
i
].
key
==
NULL
)
{
status
=
1
;
// Count the arguments after the "alias" one.
int
argumentsCount
=
0
;
while
(
arguments
[
argumentsCount
+
1
]
!=
NULL
)
{
argumentsCount
++
;
}
// For each case, check if there is a numbering.
switch
(
argumentsCount
)
{
// Case 0: If there are no arguments, we want to print the list of the existing ones.
case
0
:
{
int
i
=
0
;
// Print alises if they exist.
while
(
aliases
[
i
].
key
!=
NULL
)
{
printf
(
"Alias `%s` set to `%s`
\n
"
,
aliases
[
i
].
key
,
aliases
[
i
].
command
);
i
++
;
}
if
(
status
!=
-
1
)
{
// If no aliases exist, print a message.
if
(
i
<
1
)
{
printf
(
"ERROR: No aliases found
\n
"
);
}
break
;
}
// Case 1: Only one argument which is not sufficient for the `alias` command.
case
1
:
printf
(
"ERROR: Not enough arguments supplied for `alias` command"
);
break
;
// Case 2: More than one argument, we need to overwrite or add another alias.
default:
{
int
status
=
-
1
;
// Check if there is a place in the aliases array and store a new alias.
for
(
int
i
=
0
;
i
<
MAX_ALIASES
;
i
++
)
{
if
(
aliases
[
i
].
key
!=
NULL
&&
strcmp
(
aliases
[
i
].
key
,
arguments
[
1
])
==
0
)
{
status
=
0
;
}
else
if
(
aliases
[
i
].
key
==
NULL
)
{
status
=
1
;
}
if
(
status
==
-
1
)
{
continue
;
}
aliases
[
i
].
key
=
malloc
(
sizeof
(
char
)
*
MAX_INPUT_LENGTH
);
strcpy
(
aliases
[
i
].
key
,
arguments
[
1
]);
...
...
@@ -167,61 +199,38 @@ int run_command(String arguments[]) {
free
(
response
);
return
SUCCESS_STATUS
;
}
}
printf
(
"ERROR: No more aliases can be set"
);
return
SUCCESS_STATUS
;
printf
(
"ERROR: No more aliases can be set"
);
}
break
;
}
int
i
=
0
;
// Print alises if they exist.
while
(
aliases
[
i
].
key
!=
NULL
)
{
printf
(
"Alias `%s` set to `%s`
\n
"
,
aliases
[
i
].
key
,
aliases
[
i
].
command
);
i
++
;
}
// If no aliases exist, print a message.
if
(
i
<
1
)
{
printf
(
"ERROR: No aliases found
\n
"
);
}
return
SUCCESS_STATUS
;
}
if
(
strcmp
(
arguments
[
0
],
"unalias"
)
==
0
)
{
// if(arguments[1] == NULL) {
// printf("ERROR: Invalid arguments. Command unalias requires one argument\n");
// return SUCCESS_STATUS;
// }
// //Check if we have at least one alias set in the allias array
// Boolean hasElements = false;
// for(int i = 0; i < MAX_ALIASES; i++) {
// if(aliases[i].key != NULL) {
// hasElements = true;
// }
// }
// //If we have not set any aliases and try to unalias then display an error message and return to I/O input
// if(!hasElements) {
// printf("ERROR: No alias set\n");
// return SUCCESS_STATUS;
// }
// for(int i = 0; i < MAX_ALIASES; i++) {
// if(strcmp(aliases[i].key, arguments[1]) == 0) {
// aliases[i].key = NULL;
// int j = 0;
// while(aliases[i].command[j] != NULL) {
// aliases[i].command[j] = NULL;
// free(aliases[i].command[j]);
// }
// printf("The alias %s is removed", arguments[1]);
// return SUCCESS_STATUS;
// }
// }
printf
(
"ERROR: Invalid arguments. Command alias requires more argument
\n
"
);
if
(
arguments
[
1
]
!=
NULL
)
{
Boolean
isFound
=
false
;
for
(
int
i
=
0
;
i
<
MAX_ALIASES
;
i
++
)
{
if
(
aliases
[
i
].
key
!=
NULL
&&
strcmp
(
aliases
[
i
].
key
,
arguments
[
1
])
==
0
)
{
for
(
int
j
=
i
;
j
<
MAX_ALIASES
;
j
++
)
{
aliases
[
j
]
=
aliases
[
j
+
1
];
}
isFound
=
true
;
break
;
}
}
if
(
isFound
)
{
printf
(
"Alias `%s` has been removed
\n
"
,
arguments
[
1
]);
}
else
{
printf
(
"Alias: No alias with key `%s` has been found
\n
"
,
arguments
[
1
]);
}
}
else
{
printf
(
"ERROR: Invalid arguments. Command `unalias` requires one argument
\n
"
);
}
return
SUCCESS_STATUS
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment