Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs283
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Luis Hernandez
cs283
Commits
67161e1b
There was an error fetching the commit references. Please try again later.
Commit
67161e1b
authored
4 months ago
by
luishernandez
Browse files
Options
Downloads
Patches
Plain Diff
3-shellp1
parent
f275573f
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
dshlib.c
+93
-0
93 additions, 0 deletions
dshlib.c
with
93 additions
and
0 deletions
dshlib.c
0 → 100644
+
93
−
0
View file @
67161e1b
#include
<stdlib.h>
#include
<stdio.h>
#include
<string.h>
#include
<ctype.h>
#include
"dshlib.h"
/*
* trim: Removes leading and trailing whitespace from a string in-place.
* Returns a pointer to the first non-space character.
*/
static
char
*
trim
(
char
*
str
)
{
/* Trim leading spaces */
while
(
isspace
((
unsigned
char
)
*
str
))
str
++
;
if
(
*
str
==
'\0'
)
/* All spaces? */
return
str
;
/* Trim trailing spaces */
char
*
end
=
str
+
strlen
(
str
)
-
1
;
while
(
end
>
str
&&
isspace
((
unsigned
char
)
*
end
))
{
*
end
=
'\0'
;
end
--
;
}
return
str
;
}
/*
* build_cmd_list:
* - Splits the provided cmd_line into commands separated by '|'
* - For each command it trims leading/trailing spaces and then splits the
* command into tokens (using whitespace as the delimiter).
* - The first token is stored in the exe field (if it is not too long).
* - Any subsequent tokens are concatenated (with no intervening spaces)
* and stored in the args field.
*
* Returns:
* OK if the command was parsed successfully.
* ERR_TOO_MANY_COMMANDS if more than CMD_MAX commands are specified.
* ERR_CMD_OR_ARGS_TOO_BIG if the executable or argument string exceeds its size.
*/
int
build_cmd_list
(
char
*
cmd_line
,
command_list_t
*
clist
)
{
clist
->
num
=
0
;
char
*
saveptr1
;
/* Use strtok_r to split the command line by the pipe '|' character */
char
*
token
=
strtok_r
(
cmd_line
,
PIPE_STRING
,
&
saveptr1
);
while
(
token
!=
NULL
)
{
char
*
trimmed
=
trim
(
token
);
if
(
strlen
(
trimmed
)
==
0
)
{
token
=
strtok_r
(
NULL
,
PIPE_STRING
,
&
saveptr1
);
continue
;
}
if
(
clist
->
num
>=
CMD_MAX
)
{
return
ERR_TOO_MANY_COMMANDS
;
}
char
command_buffer
[
SH_CMD_MAX
];
strncpy
(
command_buffer
,
trimmed
,
sizeof
(
command_buffer
)
-
1
);
command_buffer
[
sizeof
(
command_buffer
)
-
1
]
=
'\0'
;
char
*
saveptr2
;
/* Use strtok_r to split the command_buffer by spaces */
char
*
arg
=
strtok_r
(
command_buffer
,
" "
,
&
saveptr2
);
if
(
arg
==
NULL
)
{
token
=
strtok_r
(
NULL
,
PIPE_STRING
,
&
saveptr1
);
continue
;
}
if
(
strlen
(
arg
)
>=
EXE_MAX
)
{
return
ERR_CMD_OR_ARGS_TOO_BIG
;
}
strcpy
(
clist
->
commands
[
clist
->
num
].
exe
,
arg
);
/* Initialize the args string as empty */
clist
->
commands
[
clist
->
num
].
args
[
0
]
=
'\0'
;
while
((
arg
=
strtok_r
(
NULL
,
" "
,
&
saveptr2
))
!=
NULL
)
{
if
(
strlen
(
clist
->
commands
[
clist
->
num
].
args
)
+
strlen
(
arg
)
>=
ARG_MAX
)
{
return
ERR_CMD_OR_ARGS_TOO_BIG
;
}
strcat
(
clist
->
commands
[
clist
->
num
].
args
,
arg
);
}
clist
->
num
++
;
token
=
strtok_r
(
NULL
,
PIPE_STRING
,
&
saveptr1
);
}
return
OK
;
}
\ No newline at end of file
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