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
amro.abulshamat
minishell
Commits
3892a061
Commit
3892a061
authored
Jan 20, 2024
by
CentOS
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new file
parents
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
370 additions
and
0 deletions
+370
-0
2.c
2.c
+111
-0
3.c
3.c
+121
-0
4.c
4.c
+138
-0
No files found.
2.c
0 → 100755
View file @
3892a061
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
#define MAX_HISTORY_SIZE 10
// History struct to store command history
char
*
history
[
MAX_HISTORY_SIZE
];
int
history_count
=
0
;
// 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
++
;
ptr
=
strtok
(
NULL
,
delim
);
}
tokens
[
index
]
=
NULL
;
// Set the last element to NULL for execvp
return
tokens
;
}
// Add command to history
void
add_to_history
(
const
char
*
command
)
{
if
(
history_count
<
MAX_HISTORY_SIZE
)
{
history
[
history_count
]
=
strdup
(
command
);
history_count
++
;
}
else
{
free
(
history
[
0
]);
int
i
;
for
(
i
=
0
;
i
<
MAX_HISTORY_SIZE
-
1
;
i
++
)
{
history
[
i
]
=
history
[
i
+
1
];
}
history
[
MAX_HISTORY_SIZE
-
1
]
=
strdup
(
command
);
}
}
// Write to file
void
writeBye
(
const
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
1
;
}
while
(
1
)
{
char
*
line
;
char
**
tokens
;
line
=
readcommand
();
char
*
line_copy
=
strdup
(
line
);
tokens
=
splitcommand
(
line_copy
);
if
(
strcmp
(
tokens
[
0
],
"exit"
)
==
0
)
{
writeBye
(
argv
[
1
]);
exit
(
0
);
}
else
if
(
strcmp
(
tokens
[
0
],
"history"
)
==
0
)
{
add_to_history
(
line_copy
);
int
i
;
for
(
i
=
0
;
i
<
history_count
;
i
++
)
{
printf
(
"%d: %s
\n
"
,
i
+
1
,
history
[
i
]);
}
continue
;
}
else
{
add_to_history
(
line
);
// Add the command to history
}
if
(
fork
()
==
0
)
{
if
(
execvp
(
tokens
[
0
],
tokens
)
==
-
1
)
{
perror
(
"The following error occurred: "
);
}
exit
(
1
);
}
else
{
wait
(
0
);
}
}
return
0
;
}
3.c
0 → 100644
View file @
3892a061
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
#define MAX_HISTORY_SIZE 10
// History struct to store command history
char
*
history
[
MAX_HISTORY_SIZE
];
int
history_count
=
0
;
// 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
++
;
ptr
=
strtok
(
NULL
,
delim
);
}
tokens
[
index
]
=
NULL
;
// Set the last element to NULL for execvp
return
tokens
;
}
// Add command to history
void
add_to_history
(
const
char
*
command
)
{
if
(
history_count
<
MAX_HISTORY_SIZE
)
{
history
[
history_count
]
=
strdup
(
command
);
history_count
++
;
}
else
{
free
(
history
[
0
]);
int
i
;
for
(
i
=
0
;
i
<
MAX_HISTORY_SIZE
-
1
;
i
++
)
{
history
[
i
]
=
history
[
i
+
1
];
}
history
[
MAX_HISTORY_SIZE
-
1
]
=
strdup
(
command
);
}
}
// Write to file
void
writeBye
(
const
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
1
;
}
while
(
1
)
{
char
*
line
;
char
**
tokens
;
line
=
readcommand
();
char
*
line_copy
=
strdup
(
line
);
tokens
=
splitcommand
(
line_copy
);
if
(
strcmp
(
tokens
[
0
],
"exit"
)
==
0
)
{
writeBye
(
argv
[
1
]);
exit
(
0
);
}
else
if
(
strcmp
(
tokens
[
0
],
"history"
)
==
0
)
{
add_to_history
(
line_copy
);
int
i
;
for
(
i
=
0
;
i
<
history_count
;
i
++
)
{
printf
(
"%d: %s
\n
"
,
i
+
1
,
history
[
i
]);
}
continue
;
}
else
if
(
tokens
[
0
][
0
]
==
'!'
)
{
int
index
=
atoi
(
tokens
[
0
]
+
1
);
if
(
index
>
0
&&
index
<=
history_count
)
{
add_to_history
(
history
[
index
-
1
]);
// Add the command to history
tokens
=
splitcommand
(
history
[
index
-
1
]);
// Split the command into tokens
}
else
{
printf
(
"Invalid history index
\n
"
);
continue
;
}
}
else
{
add_to_history
(
line
);
// Add the command to history
}
if
(
fork
()
==
0
)
{
if
(
execvp
(
tokens
[
0
],
tokens
)
==
-
1
)
{
perror
(
"The following error occurred: "
);
}
exit
(
1
);
}
else
{
wait
(
0
);
}
}
return
0
;
}
4.c
0 → 100644
View file @
3892a061
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
#define MAX_HISTORY_SIZE 10
// History struct to store command history
char
*
history
[
MAX_HISTORY_SIZE
];
int
history_count
=
0
;
// 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
++
;
ptr
=
strtok
(
NULL
,
delim
);
}
tokens
[
index
]
=
NULL
;
// Set the last element to NULL for execvp
return
tokens
;
}
// Add command to history
void
add_to_history
(
const
char
*
command
)
{
if
(
history_count
<
MAX_HISTORY_SIZE
)
{
history
[
history_count
]
=
strdup
(
command
);
history_count
++
;
}
else
{
free
(
history
[
0
]);
int
i
;
for
(
i
=
0
;
i
<
MAX_HISTORY_SIZE
-
1
;
i
++
)
{
history
[
i
]
=
history
[
i
+
1
];
}
history
[
MAX_HISTORY_SIZE
-
1
]
=
strdup
(
command
);
}
}
// Write to file
void
writeBye
(
const
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
1
;
}
while
(
1
)
{
char
*
line
;
char
**
tokens
;
line
=
readcommand
();
char
*
line_copy
=
strdup
(
line
);
tokens
=
splitcommand
(
line_copy
);
if
(
strcmp
(
tokens
[
0
],
"exit"
)
==
0
)
{
writeBye
(
argv
[
1
]);
exit
(
0
);
}
else
if
(
strcmp
(
tokens
[
0
],
"history"
)
==
0
)
{
if
(
tokens
[
1
]
!=
NULL
&&
strcmp
(
tokens
[
1
],
"-del"
)
==
0
)
{
int
index
=
atoi
(
tokens
[
2
]);
if
(
index
>
0
&&
index
<=
history_count
)
{
free
(
history
[
index
-
1
]);
// Free the memory allocated for the command
int
i
;
for
(
i
=
index
-
1
;
i
<
history_count
-
1
;
i
++
)
{
history
[
i
]
=
history
[
i
+
1
];
// Shift the remaining commands in the array
}
history_count
--
;
continue
;
}
else
{
printf
(
"Invalid history index
\n
"
);
continue
;
}
}
else
{
add_to_history
(
line_copy
);
int
i
;
for
(
i
=
0
;
i
<
history_count
;
i
++
)
{
printf
(
"%d: %s
\n
"
,
i
+
1
,
history
[
i
]);
}
continue
;
}
}
else
if
(
tokens
[
0
][
0
]
==
'!'
)
{
int
index
=
atoi
(
tokens
[
0
]
+
1
);
if
(
index
>
0
&&
index
<=
history_count
)
{
add_to_history
(
history
[
index
-
1
]);
// Add the command to history
tokens
=
splitcommand
(
history
[
index
-
1
]);
// Split the command into tokens
}
else
{
printf
(
"Invalid history index
\n
"
);
continue
;
}
}
else
{
add_to_history
(
line
);
// Add the command to history
}
if
(
fork
()
==
0
)
{
if
(
execvp
(
tokens
[
0
],
tokens
)
==
-
1
)
{
perror
(
"The following error occurred: "
);
}
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