Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pintos_forked_resit
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor 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
s2-alsaloumi
Pintos_forked_resit
Commits
88e19c60
Commit
88e19c60
authored
1 year ago
by
h2-addad
Browse files
Options
Downloads
Patches
Plain Diff
Update
parent
f0b10ff6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/examples/shell.c
+104
-0
104 additions, 0 deletions
src/examples/shell.c
with
104 additions
and
0 deletions
src/examples/shell.c
0 → 100644
+
104
−
0
View file @
88e19c60
#include
<stdbool.h>
#include
<stdio.h>
#include
<string.h>
#include
<syscall.h>
static
void
read_line
(
char
line
[],
size_t
);
static
bool
backspace
(
char
**
pos
,
char
line
[]);
int
main
(
void
)
{
printf
(
"Shell starting...
\n
"
);
for
(;;)
{
char
command
[
80
];
/* Read command. */
printf
(
"--"
);
read_line
(
command
,
sizeof
command
);
/* Execute command. */
if
(
!
strcmp
(
command
,
"exit"
))
break
;
else
if
(
!
memcmp
(
command
,
"cd "
,
3
))
{
if
(
!
chdir
(
command
+
3
))
printf
(
"
\"
%s
\"
: chdir failed
\n
"
,
command
+
3
);
}
else
if
(
command
[
0
]
==
'\0'
)
{
/* Empty command. */
}
else
{
pid_t
pid
=
exec
(
command
);
if
(
pid
!=
PID_ERROR
)
printf
(
"
\"
%s
\"
: exit code %d
\n
"
,
command
,
wait
(
pid
));
else
printf
(
"exec failed
\n
"
);
}
}
printf
(
"Shell exiting."
);
return
EXIT_SUCCESS
;
}
/* Reads a line of input from the user into LINE, which has room
for SIZE bytes. Handles backspace and Ctrl+U in the ways
expected by Unix users. On return, LINE will always be
null-terminated and will not end in a new-line character. */
static
void
read_line
(
char
line
[],
size_t
size
)
{
char
*
pos
=
line
;
for
(;;)
{
char
c
;
read
(
STDIN_FILENO
,
&
c
,
1
);
switch
(
c
)
{
case
'\r'
:
*
pos
=
'\0'
;
putchar
(
'\n'
);
return
;
case
'\b'
:
backspace
(
&
pos
,
line
);
break
;
case
(
'U'
-
'A'
)
+
1
:
/* Ctrl+U. */
while
(
backspace
(
&
pos
,
line
))
continue
;
break
;
default:
/* Add character to line. */
if
(
pos
<
line
+
size
-
1
)
{
putchar
(
c
);
*
pos
++
=
c
;
}
break
;
}
}
}
/* If *POS is past the beginning of LINE, backs up one character
position. Returns true if successful, false if nothing was
done. */
static
bool
backspace
(
char
**
pos
,
char
line
[])
{
if
(
*
pos
>
line
)
{
/* Back up cursor, overwrite character, back up
again. */
printf
(
"
\b
\b
"
);
(
*
pos
)
--
;
return
true
;
}
else
return
false
;
}
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