Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
M
MiniShell
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamadbashar.disoki
MiniShell
Commits
23a7f629
Commit
23a7f629
authored
Jan 09, 2024
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mini shell code
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
0 deletions
+88
-0
myshell.c
myshell.c
+88
-0
No files found.
myshell.c
0 → 100644
View file @
23a7f629
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
// Read a line from stdin
char
*
readcommand
(){
char
*
line
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
MAX
);
printf
(
"$> "
);
fgets
(
line
,
MAX
,
stdin
);
size_t
ln
=
strlen
(
line
)
-
1
;
if
(
line
[
ln
]
==
'\n'
)
line
[
ln
]
=
'\0'
;
return
line
;
}
// Split the line into tokens
char
**
splitcommand
(
char
*
line
){
char
delim
[]
=
" "
;
char
*
ptr
=
strtok
(
line
,
delim
);
char
**
tokens
=
(
char
**
)
malloc
(
sizeof
(
char
)
*
MAX
);
int
index
=
0
;
while
(
ptr
!=
NULL
)
{
tokens
[
index
]
=
ptr
;
index
++
;
// printf("'%s'\n", ptr);
ptr
=
strtok
(
NULL
,
delim
);
}
return
tokens
;
}
// write to file
void
writeBye
(
char
*
filename
){
FILE
*
fptr
;
// opening file in writing mode
fptr
=
fopen
(
filename
,
"w"
);
// exiting program
if
(
fptr
==
NULL
)
{
printf
(
"Error!"
);
exit
(
1
);
}
fprintf
(
fptr
,
"%s"
,
"Bye!
\n
"
);
fclose
(
fptr
);
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
2
){
printf
(
"Please insert exitfile as an argument!
\n
"
);
return
;
}
while
(
1
){
char
*
line
;
char
**
tokens
;
line
=
readcommand
();
tokens
=
splitcommand
(
line
);
if
(
strcmp
(
tokens
[
0
],
"exit"
)
==
0
){
writeBye
(
argv
[
1
]);
exit
(
0
);
}
if
(
fork
()
==
0
){
if
(
execvp
(
tokens
[
0
],
tokens
)
==
-
1
){
perror
(
"The following error occured: "
);
}
exit
(
1
);
}
else
wait
(
0
);
}
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment