summaryrefslogtreecommitdiffstats
path: root/src/core/exec.c
diff options
context:
space:
mode:
authorMichael Brown2011-03-07 19:31:38 +0100
committerMichael Brown2011-03-07 20:21:43 +0100
commita281c4080bf268fa08d0b6001ab081c53a225035 (patch)
treeabc49ad178090fd248a27d79ceba40139d032b78 /src/core/exec.c
parent[script] Add an iPXE error URI to the "not in a script" message (diff)
downloadipxe-a281c4080bf268fa08d0b6001ab081c53a225035.tar.gz
ipxe-a281c4080bf268fa08d0b6001ab081c53a225035.tar.xz
ipxe-a281c4080bf268fa08d0b6001ab081c53a225035.zip
[cmdline] Add generic concat_args() function
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/exec.c')
-rw-r--r--src/core/exec.c49
1 files changed, 43 insertions, 6 deletions
diff --git a/src/core/exec.c b/src/core/exec.c
index e6f20487..df722304 100644
--- a/src/core/exec.c
+++ b/src/core/exec.c
@@ -268,19 +268,56 @@ int system ( const char *command ) {
}
/**
+ * 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
*
* @v argc Argument count
* @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;
}