1993-08-26 01:19:55 +00:00
|
|
|
#ifndef lint
|
1997-10-08 07:48:21 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1993-08-26 01:19:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FreeBSD install - a package for the installation and maintainance
|
|
|
|
* of non-core utilities.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Jordan K. Hubbard
|
|
|
|
* 18 July 1993
|
|
|
|
*
|
|
|
|
* Miscellaneous system routines.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib.h"
|
2001-03-23 18:45:24 +00:00
|
|
|
#include <err.h>
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unusual system() substitute. Accepts format string and args,
|
|
|
|
* builds and executes command. Returns exit code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
vsystem(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
1994-12-06 00:51:50 +00:00
|
|
|
char *cmd;
|
|
|
|
int ret, maxargs;
|
|
|
|
|
|
|
|
maxargs = sysconf(_SC_ARG_MAX);
|
|
|
|
maxargs -= 32; /* some slop for the sh -c */
|
|
|
|
cmd = malloc(maxargs);
|
|
|
|
if (!cmd) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("vsystem can't alloc arg space");
|
1994-12-06 00:51:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
1994-12-06 00:51:50 +00:00
|
|
|
if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("vsystem args are too long");
|
1994-12-06 00:51:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Executing %s\n", cmd);
|
|
|
|
#endif
|
|
|
|
ret = system(cmd);
|
|
|
|
va_end(args);
|
1994-12-06 00:51:50 +00:00
|
|
|
free(cmd);
|
1993-08-26 01:19:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-03-23 18:45:24 +00:00
|
|
|
char *
|
|
|
|
vpipe(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *cmd, *rp;
|
|
|
|
int maxargs;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
rp = malloc(MAXPATHLEN);
|
|
|
|
if (!rp) {
|
|
|
|
warnx("vpipe can't alloc buffer space");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
maxargs = sysconf(_SC_ARG_MAX);
|
|
|
|
maxargs -= 32; /* some slop for the sh -c */
|
|
|
|
cmd = alloca(maxargs);
|
|
|
|
if (!cmd) {
|
|
|
|
warnx("vpipe can't alloc arg space");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
|
|
|
|
warnx("vsystem args are too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Executing %s\n", cmd);
|
|
|
|
#endif
|
|
|
|
fflush(NULL);
|
|
|
|
fp = popen(cmd, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
warnx("popen() failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
get_string(rp, MAXPATHLEN, fp);
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Returned %s\n", rp);
|
|
|
|
#endif
|
|
|
|
va_end(args);
|
|
|
|
if (pclose(fp) || (strlen(rp) == 0)) {
|
|
|
|
free(rp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|