1327Sjkh/*
2327Sjkh * FreeBSD install - a package for the installation and maintainance
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;
37252635Sobrien    long maxargs;
38252635Sobrien    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");
514996Sjkh	return 1;
524996Sjkh    }
53327Sjkh#ifdef DEBUG
54327Sjkhprintf("Executing %s\n", cmd);
55327Sjkh#endif
56327Sjkh    ret = system(cmd);
57327Sjkh    va_end(args);
584996Sjkh    free(cmd);
59327Sjkh    return ret;
60327Sjkh}
61327Sjkh
6274699Ssobomaxchar *
6374699Ssobomaxvpipe(const char *fmt, ...)
6474699Ssobomax{
6574699Ssobomax   FILE *fp;
6674699Ssobomax   char *cmd, *rp;
67252638Sobrien   long maxargs;
6874699Ssobomax   va_list args;
6974699Ssobomax
7074699Ssobomax    rp = malloc(MAXPATHLEN);
7174699Ssobomax    if (!rp) {
72167972Snjl	warnx("vpipe can't alloc buffer space");
73167972Snjl	return NULL;
7474699Ssobomax    }
7574699Ssobomax    maxargs = sysconf(_SC_ARG_MAX);
7674699Ssobomax    maxargs -= 32;			    /* some slop for the sh -c */
7774699Ssobomax    cmd = alloca(maxargs);
7874699Ssobomax    if (!cmd) {
7974699Ssobomax	warnx("vpipe can't alloc arg space");
8074699Ssobomax	return NULL;
8174699Ssobomax    }
8274699Ssobomax
8374699Ssobomax    va_start(args, fmt);
8474699Ssobomax    if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
8574699Ssobomax	warnx("vsystem args are too long");
8674699Ssobomax	return NULL;
8774699Ssobomax    }
8874699Ssobomax#ifdef DEBUG
8974699Ssobomax    fprintf(stderr, "Executing %s\n", cmd);
9074699Ssobomax#endif
9174699Ssobomax    fflush(NULL);
9274699Ssobomax    fp = popen(cmd, "r");
9374699Ssobomax    if (fp == NULL) {
9474699Ssobomax	warnx("popen() failed");
9574699Ssobomax	return NULL;
9674699Ssobomax    }
9774699Ssobomax    get_string(rp, MAXPATHLEN, fp);
9874699Ssobomax#ifdef DEBUG
9974699Ssobomax    fprintf(stderr, "Returned %s\n", rp);
10074699Ssobomax#endif
10174699Ssobomax    va_end(args);
10274699Ssobomax    if (pclose(fp) || (strlen(rp) == 0)) {
10374699Ssobomax	free(rp);
10474699Ssobomax	return NULL;
10574699Ssobomax    }
10674699Ssobomax    return rp;
10774699Ssobomax}
108