version 1.11, 2002/02/16 21:27:43 |
version 1.12, 2003/04/04 00:21:20 |
|
|
{ |
{ |
int ch, clen, debug, i, l, magic, n, nargs, rval; |
int ch, clen, debug, i, l, magic, n, nargs, rval; |
char *c, *cmd, *p, *q; |
char *c, *cmd, *p, *q; |
|
size_t len; |
|
|
debug = 0; |
debug = 0; |
magic = '%'; /* Default magic char is `%'. */ |
magic = '%'; /* Default magic char is `%'. */ |
|
|
* the end to consume (nargs) arguments each time round the loop. |
* the end to consume (nargs) arguments each time round the loop. |
* Allocate enough space to hold the maximum command. |
* Allocate enough space to hold the maximum command. |
*/ |
*/ |
if ((cmd = malloc(sizeof("exec ") - 1 + |
len = sizeof("exec ") - 1 + |
strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1)) == NULL) |
strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1; |
|
if ((cmd = malloc(len)) == NULL) |
err(1, NULL); |
err(1, NULL); |
|
|
if (n == 0) { |
if (n == 0) { |
|
size_t l; |
|
|
/* If nargs not set, default to a single argument. */ |
/* If nargs not set, default to a single argument. */ |
if (nargs == -1) |
if (nargs == -1) |
nargs = 1; |
nargs = 1; |
|
|
p = cmd; |
l = snprintf(cmd, len, "exec %s", argv[0]); |
p += sprintf(cmd, "exec %s", argv[0]); |
if (l >= len) |
for (i = 1; i <= nargs; i++) |
err(1, "snprintf"); |
p += sprintf(p, " %c%d", magic, i); |
len -= l; |
|
p = cmd + l; |
|
|
|
for (i = 1; i <= nargs; i++) { |
|
l = snprintf(p, len, " %c%d", magic, i); |
|
if (l >= len) |
|
err(1, "snprintf"); |
|
len -= l; |
|
p += l; |
|
} |
|
|
/* |
/* |
* If nargs set to the special value 0, eat a single |
* If nargs set to the special value 0, eat a single |
|
|
if (nargs == 0) |
if (nargs == 0) |
nargs = 1; |
nargs = 1; |
} else { |
} else { |
(void)sprintf(cmd, "exec %s", argv[0]); |
(void)snprintf(cmd, len, "exec %s", argv[0]); |
nargs = n; |
nargs = n; |
} |
} |
|
|
|
|
|
|
/* Expand command argv references. */ |
/* Expand command argv references. */ |
for (p = cmd, q = c; *p != '\0'; ++p) |
for (p = cmd, q = c; *p != '\0'; ++p) |
if (p[0] == magic && isdigit(p[1]) && p[1] != '0') |
if (p[0] == magic && isdigit(p[1]) && p[1] != '0') { |
q += sprintf(q, "%s", argv[(++p)[0] - '0']); |
sprintf(q, "%s", argv[(++p)[0] - '0']); |
else |
q += strlen(q); |
|
} else |
*q++ = *p; |
*q++ = *p; |
|
|
/* Terminate the command string. */ |
/* Terminate the command string. */ |