version 1.31, 2024/04/14 18:11:54 |
version 1.32, 2024/04/16 10:19:00 |
|
|
} |
} |
|
|
/* |
/* |
* Read a string from a file. |
|
* Return a pointer to the string in memory. |
|
*/ |
|
static char * |
|
readfd(FILE *fd) |
|
{ |
|
int len; |
|
int ch; |
|
char *buf; |
|
char *p; |
|
|
|
/* |
|
* Make a guess about how many chars in the string |
|
* and allocate a buffer to hold it. |
|
*/ |
|
len = 100; |
|
buf = ecalloc(len, sizeof (char)); |
|
for (p = buf; ; p++) { |
|
if ((ch = getc(fd)) == '\n' || ch == EOF) |
|
break; |
|
if (p >= buf + len-1) { |
|
/* |
|
* The string is too big to fit in the buffer we have. |
|
* Allocate a new buffer, twice as big. |
|
*/ |
|
len *= 2; |
|
*p = '\0'; |
|
p = ecalloc(len, sizeof (char)); |
|
strlcpy(p, buf, len); |
|
free(buf); |
|
buf = p; |
|
p = buf + strlen(buf); |
|
} |
|
*p = (char)ch; |
|
} |
|
*p = '\0'; |
|
return (buf); |
|
} |
|
|
|
/* |
|
* Execute a shell command. |
|
* Return a pointer to a pipe connected to the shell command's standard output. |
|
*/ |
|
static FILE * |
|
shellcmd(char *cmd) |
|
{ |
|
FILE *fd; |
|
|
|
char *shell; |
|
|
|
shell = lgetenv("SHELL"); |
|
if (shell != NULL && *shell != '\0') { |
|
char *scmd; |
|
char *esccmd; |
|
|
|
/* |
|
* Read the output of <$SHELL -c cmd>. |
|
* Escape any metacharacters in the command. |
|
*/ |
|
esccmd = shell_quote(cmd); |
|
if (esccmd == NULL) { |
|
fd = popen(cmd, "r"); |
|
} else { |
|
scmd = easprintf("%s -c %s", shell, esccmd); |
|
free(esccmd); |
|
fd = popen(scmd, "r"); |
|
free(scmd); |
|
} |
|
} else { |
|
fd = popen(cmd, "r"); |
|
} |
|
/* |
|
* Redirection in `popen' might have messed with the |
|
* standard devices. Restore binary input mode. |
|
*/ |
|
return (fd); |
|
} |
|
|
|
/* |
|
* Expand a filename, doing any system-specific metacharacter substitutions. |
* Expand a filename, doing any system-specific metacharacter substitutions. |
*/ |
*/ |
char * |
char * |