Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp 1 - shell
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
sebastie.gendre
tp 1 - shell
Commits
569b022a
Commit
569b022a
authored
4 years ago
by
Sébastien Gendre
Browse files
Options
Downloads
Patches
Plain Diff
Modify the command struct to have an argv close to what execve ask
parent
0022fd02
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/command.c
+19
-20
19 additions, 20 deletions
src/command.c
src/command.h
+0
-1
0 additions, 1 deletion
src/command.h
src/interfaces.c
+4
-4
4 additions, 4 deletions
src/interfaces.c
with
23 additions
and
25 deletions
src/command.c
+
19
−
20
View file @
569b022a
...
...
@@ -9,33 +9,34 @@ int command_from_str(char *str, cmd_t *cmd) {
char
*
next_arg
;
//Initialize a simple command (empty, simple, foreground)
cmd
->
command
=
NULL
;
cmd
->
argv
=
NULL
;
cmd
->
argc
=
0
;
cmd
->
foreground
=
true
;
// Extract the commmand name
cmd
->
command
=
strtok
(
str
,
DELIMITERS
);
// Extract the command arguments
do
{
// Get the next argument
next_arg
=
strtok
(
NULL
,
DELIMITERS
);
if
(
cmd
->
argc
==
0
)
{
// Extract the commmand name
next_arg
=
strtok
(
str
,
DELIMITERS
);
}
else
{
// Get the next argument
next_arg
=
strtok
(
NULL
,
DELIMITERS
);
}
// Extend the cmd->argv array witha new char pointer
cmd
->
argv
=
realloc
(
cmd
->
argv
,
(
cmd
->
argc
+
1
)
*
sizeof
(
char
*
));
if
(
cmd
->
argv
==
NULL
)
{
// In case the reallocation didn't work
die_errno
(
"parse_command::realloc"
);
}
// Store the argument inside cmd->argv
cmd
->
argv
[
cmd
->
argc
]
=
next_arg
;
if
(
next_arg
!=
NULL
)
{
// A new element will be added to cmd->argv
// A new
non null
element will be added to cmd->argv
cmd
->
argc
+=
1
;
// Extend the cmd->argv array witha new char pointer
cmd
->
argv
=
realloc
(
cmd
->
argv
,
(
cmd
->
argc
)
*
sizeof
(
char
*
));
if
(
cmd
->
argv
==
NULL
)
{
// In case the reallocation didn't work
die_errno
(
"parse_command::realloc"
);
}
// Store the argument inside cmd->argv
cmd
->
argv
[
cmd
->
argc
-
1
]
=
next_arg
;
}
}
while
(
next_arg
!=
NULL
);
...
...
@@ -44,8 +45,6 @@ int command_from_str(char *str, cmd_t *cmd) {
// Dispose the commande
void
command_dispose
(
cmd_t
*
cmd
)
{
// Free the command name str
free
(
cmd
->
command
);
// Free all the command arguments str inside argv
for
(
int
i
=
0
;
i
<
cmd
->
argc
;
++
i
)
{
free
(
cmd
->
argv
[
i
]);
...
...
This diff is collapsed.
Click to expand it.
src/command.h
+
0
−
1
View file @
569b022a
...
...
@@ -16,7 +16,6 @@
// A full user command
typedef
struct
cmd
cmd_t
;
struct
cmd
{
char
*
command
;
// The command name
char
**
argv
;
// The command arguments
int
argc
;
// Number of command arguments
bool
foreground
;
// Should the command be run in foreground or background (might not be relevant for your TP depending on TP version)
...
...
This diff is collapsed.
Click to expand it.
src/interfaces.c
+
4
−
4
View file @
569b022a
...
...
@@ -25,13 +25,13 @@ int interface_ask_user_input(char* user_input) {
//Print command for debugging
void
interface_print_command
(
cmd_t
*
cmd
)
{
printf
(
"Command:
\n
"
);
printf
(
"
\t
- Name: %s
\n
"
,
cmd
->
command
);
printf
(
"
\t
- Arguments count: %d
\n
"
,
cmd
->
argc
);
if
(
cmd
->
argc
==
0
)
printf
(
"
\t
- Name: %s
\n
"
,
cmd
->
argv
[
0
]
);
printf
(
"
\t
- Arguments count: %d
\n
"
,
cmd
->
argc
-
1
);
if
(
cmd
->
argc
==
1
)
printf
(
"
\t
- No arguments
\n
"
);
else
{
printf
(
"
\t
- Arguments:
\n
"
);
for
(
int
i
=
0
;
i
<
cmd
->
argc
;
++
i
)
{
for
(
int
i
=
1
;
i
<
cmd
->
argc
;
++
i
)
{
printf
(
"
\t\t
- %s
\n
"
,
cmd
->
argv
[
i
]);
}
}
...
...
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