Browse Source

[cmdline] Store exit status of failed command in errno

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
5b4958388d
1 changed files with 22 additions and 6 deletions
  1. 22
    6
      src/core/exec.c

+ 22
- 6
src/core/exec.c View File

59
 int execv ( const char *command, char * const argv[] ) {
59
 int execv ( const char *command, char * const argv[] ) {
60
 	struct command *cmd;
60
 	struct command *cmd;
61
 	int argc;
61
 	int argc;
62
+	int rc;
62
 
63
 
63
 	/* Count number of arguments */
64
 	/* Count number of arguments */
64
 	for ( argc = 0 ; argv[argc] ; argc++ ) {}
65
 	for ( argc = 0 ; argv[argc] ; argc++ ) {}
65
 
66
 
66
 	/* An empty command is deemed to do nothing, successfully */
67
 	/* An empty command is deemed to do nothing, successfully */
67
-	if ( command == NULL )
68
-		return 0;
68
+	if ( command == NULL ) {
69
+		rc = 0;
70
+		goto done;
71
+	}
69
 
72
 
70
 	/* Sanity checks */
73
 	/* Sanity checks */
71
 	if ( argc == 0 ) {
74
 	if ( argc == 0 ) {
72
 		DBG ( "%s: empty argument list\n", command );
75
 		DBG ( "%s: empty argument list\n", command );
73
-		return -EINVAL;
76
+		rc = -EINVAL;
77
+		goto done;
74
 	}
78
 	}
75
 
79
 
76
 	/* Reset getopt() library ready for use by the command.  This
80
 	/* Reset getopt() library ready for use by the command.  This
82
 
86
 
83
 	/* Hand off to command implementation */
87
 	/* Hand off to command implementation */
84
 	for_each_table_entry ( cmd, COMMANDS ) {
88
 	for_each_table_entry ( cmd, COMMANDS ) {
85
-		if ( strcmp ( command, cmd->name ) == 0 )
86
-			return cmd->exec ( argc, ( char ** ) argv );
89
+		if ( strcmp ( command, cmd->name ) == 0 ) {
90
+			rc = cmd->exec ( argc, ( char ** ) argv );
91
+			goto done;
92
+		}
87
 	}
93
 	}
88
 
94
 
89
 	printf ( "%s: command not found\n", command );
95
 	printf ( "%s: command not found\n", command );
90
-	return -ENOEXEC;
96
+	rc = -ENOEXEC;
97
+
98
+ done:
99
+	/* Store error number, if an error occurred */
100
+	if ( rc ) {
101
+		errno = rc;
102
+		if ( errno < 0 )
103
+			errno = -errno;
104
+	}
105
+
106
+	return rc;
91
 }
107
 }
92
 
108
 
93
 /**
109
 /**

Loading…
Cancel
Save