1327Sjkh/*
2228990Suqs * FreeBSD install - a package for the installation and maintenance
3327Sjkh * of non-core utilities.
4327Sjkh *
5327Sjkh * Redistribution and use in source and binary forms, with or without
6327Sjkh * modification, are permitted provided that the following conditions
7327Sjkh * are met:
8327Sjkh * 1. Redistributions of source code must retain the above copyright
9327Sjkh *    notice, this list of conditions and the following disclaimer.
10327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
11327Sjkh *    notice, this list of conditions and the following disclaimer in the
12327Sjkh *    documentation and/or other materials provided with the distribution.
13327Sjkh *
14327Sjkh * Jordan K. Hubbard
15327Sjkh * 18 July 1993
16327Sjkh *
17327Sjkh * Miscellaneous system routines.
18327Sjkh *
19327Sjkh */
20327Sjkh
2193520Sobrien#include <sys/cdefs.h>
2293520Sobrien__FBSDID("$FreeBSD$");
2393520Sobrien
2474699Ssobomax#include "lib.h"
2530221Scharnier#include <err.h>
26327Sjkh
27327Sjkh/*
28327Sjkh * Unusual system() substitute.  Accepts format string and args,
29327Sjkh * builds and executes command.  Returns exit code.
30327Sjkh */
31327Sjkh
32327Sjkhint
33327Sjkhvsystem(const char *fmt, ...)
34327Sjkh{
35327Sjkh    va_list args;
364996Sjkh    char *cmd;
37252348Sobrien    long maxargs;
38252348Sobrien    int ret;
39327Sjkh
404996Sjkh    maxargs = sysconf(_SC_ARG_MAX);
414996Sjkh    maxargs -= 32;			/* some slop for the sh -c */
424996Sjkh    cmd = malloc(maxargs);
434996Sjkh    if (!cmd) {
4430221Scharnier	warnx("vsystem can't alloc arg space");
454996Sjkh	return 1;
464996Sjkh    }
474996Sjkh
48327Sjkh    va_start(args, fmt);
494996Sjkh    if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
5030221Scharnier	warnx("vsystem args are too long");
51236213Skevlo	va_end(args);
524996Sjkh	return 1;
534996Sjkh    }
54327Sjkh#ifdef DEBUG
55327Sjkhprintf("Executing %s\n", cmd);
56327Sjkh#endif
57327Sjkh    ret = system(cmd);
58327Sjkh    va_end(args);
594996Sjkh    free(cmd);
60327Sjkh    return ret;
61327Sjkh}
62327Sjkh
6374699Ssobomaxchar *
6474699Ssobomaxvpipe(const char *fmt, ...)
6574699Ssobomax{
6674699Ssobomax   FILE *fp;
6774699Ssobomax   char *cmd, *rp;
68252363Sobrien   long maxargs;
6974699Ssobomax   va_list args;
7074699Ssobomax
7174699Ssobomax    rp = malloc(MAXPATHLEN);
7274699Ssobomax    if (!rp) {
73167972Snjl	warnx("vpipe can't alloc buffer space");
74167972Snjl	return NULL;
7574699Ssobomax    }
7674699Ssobomax    maxargs = sysconf(_SC_ARG_MAX);
7774699Ssobomax    maxargs -= 32;			    /* some slop for the sh -c */
7874699Ssobomax    cmd = alloca(maxargs);
7974699Ssobomax    if (!cmd) {
8074699Ssobomax	warnx("vpipe can't alloc arg space");
8174699Ssobomax	return NULL;
8274699Ssobomax    }
8374699Ssobomax
8474699Ssobomax    va_start(args, fmt);
8574699Ssobomax    if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
8674699Ssobomax	warnx("vsystem args are too long");
87236213Skevlo	va_end(args);
8874699Ssobomax	return NULL;
8974699Ssobomax    }
9074699Ssobomax#ifdef DEBUG
9174699Ssobomax    fprintf(stderr, "Executing %s\n", cmd);
9274699Ssobomax#endif
9374699Ssobomax    fflush(NULL);
9474699Ssobomax    fp = popen(cmd, "r");
9574699Ssobomax    if (fp == NULL) {
9674699Ssobomax	warnx("popen() failed");
97241021Skevlo	va_end(args);
9874699Ssobomax	return NULL;
9974699Ssobomax    }
10074699Ssobomax    get_string(rp, MAXPATHLEN, fp);
10174699Ssobomax#ifdef DEBUG
10274699Ssobomax    fprintf(stderr, "Returned %s\n", rp);
10374699Ssobomax#endif
10474699Ssobomax    va_end(args);
10574699Ssobomax    if (pclose(fp) || (strlen(rp) == 0)) {
10674699Ssobomax	free(rp);
10774699Ssobomax	return NULL;
10874699Ssobomax    }
10974699Ssobomax    return rp;
11074699Ssobomax}
111