mirror of
https://github.com/ipxe/ipxe
synced 2025-12-21 12:30:20 +03:00
[cmdline] Add generic concat_args() function
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -267,6 +267,42 @@ int system ( const char *command ) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate arguments
|
||||
*
|
||||
* @v args Argument list (NULL-terminated)
|
||||
* @ret string Concatenated arguments
|
||||
*
|
||||
* The returned string is allocated with malloc(). The caller is
|
||||
* responsible for eventually free()ing this string.
|
||||
*/
|
||||
char * concat_args ( char **args ) {
|
||||
char **arg;
|
||||
size_t len;
|
||||
char *string;
|
||||
char *ptr;
|
||||
|
||||
/* Calculate total string length */
|
||||
len = 1 /* NUL */;
|
||||
for ( arg = args ; *arg ; arg++ )
|
||||
len += ( 1 /* possible space */ + strlen ( *arg ) );
|
||||
|
||||
/* Allocate string */
|
||||
string = zalloc ( len );
|
||||
if ( ! string )
|
||||
return NULL;
|
||||
|
||||
/* Populate string */
|
||||
ptr = string;
|
||||
for ( arg = args ; *arg ; arg++ ) {
|
||||
ptr += sprintf ( ptr, "%s%s",
|
||||
( ( ptr == string ) ? "" : " " ), *arg );
|
||||
}
|
||||
assert ( ptr < ( string + len ) );
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* "echo" command
|
||||
*
|
||||
@@ -274,13 +310,14 @@ int system ( const char *command ) {
|
||||
* @v argv Argument list
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int echo_exec ( int argc, char **argv ) {
|
||||
int i;
|
||||
static int echo_exec ( int argc __unused, char **argv ) {
|
||||
char *text;
|
||||
|
||||
for ( i = 1 ; i < argc ; i++ ) {
|
||||
printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] );
|
||||
}
|
||||
printf ( "\n" );
|
||||
text = concat_args ( &argv[1] );
|
||||
if ( ! text )
|
||||
return -ENOMEM;
|
||||
printf ( "%s\n", text );
|
||||
free ( text );
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user