exec.c revision 93520
1193323Sed/* 2193323Sed * FreeBSD install - a package for the installation and maintainance 3193323Sed * of non-core utilities. 4193323Sed * 5193323Sed * Redistribution and use in source and binary forms, with or without 6193323Sed * modification, are permitted provided that the following conditions 7193323Sed * are met: 8193323Sed * 1. Redistributions of source code must retain the above copyright 9193323Sed * notice, this list of conditions and the following disclaimer. 10193323Sed * 2. Redistributions in binary form must reproduce the above copyright 11193323Sed * notice, this list of conditions and the following disclaimer in the 12193323Sed * documentation and/or other materials provided with the distribution. 13193323Sed * 14193323Sed * Jordan K. Hubbard 15193323Sed * 18 July 1993 16193323Sed * 17193323Sed * Miscellaneous system routines. 18193323Sed * 19193323Sed */ 20205218Srdivacky 21202375Srdivacky#include <sys/cdefs.h> 22198090Srdivacky__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/lib/exec.c 93520 2002-04-01 09:39:07Z obrien $"); 23198090Srdivacky 24193323Sed#include "lib.h" 25193323Sed#include <err.h> 26193323Sed 27193323Sed/* 28198892Srdivacky * Unusual system() substitute. Accepts format string and args, 29193323Sed * builds and executes command. Returns exit code. 30198090Srdivacky */ 31193323Sed 32193323Sedint 33212904Sdimvsystem(const char *fmt, ...) 34212904Sdim{ 35198090Srdivacky va_list args; 36193323Sed char *cmd; 37193323Sed int ret, maxargs; 38193323Sed 39193323Sed maxargs = sysconf(_SC_ARG_MAX); 40193323Sed maxargs -= 32; /* some slop for the sh -c */ 41193323Sed cmd = malloc(maxargs); 42193323Sed if (!cmd) { 43198892Srdivacky warnx("vsystem can't alloc arg space"); 44193323Sed return 1; 45193323Sed } 46193323Sed 47193323Sed va_start(args, fmt); 48193323Sed if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) { 49193323Sed warnx("vsystem args are too long"); 50193323Sed return 1; 51193323Sed } 52193323Sed#ifdef DEBUG 53193323Sedprintf("Executing %s\n", cmd); 54193323Sed#endif 55193323Sed ret = system(cmd); 56193323Sed va_end(args); 57193323Sed free(cmd); 58212904Sdim return ret; 59212904Sdim} 60193323Sed 61193323Sedchar * 62193323Sedvpipe(const char *fmt, ...) 63193323Sed{ 64193323Sed FILE *fp; 65193323Sed char *cmd, *rp; 66193323Sed int maxargs; 67193323Sed va_list args; 68193323Sed 69193323Sed rp = malloc(MAXPATHLEN); 70193323Sed if (!rp) { 71193323Sed warnx("vpipe can't alloc buffer space"); 72193323Sed return NULL; 73212904Sdim } 74193323Sed maxargs = sysconf(_SC_ARG_MAX); 75193323Sed maxargs -= 32; /* some slop for the sh -c */ 76193323Sed cmd = alloca(maxargs); 77193323Sed if (!cmd) { 78193323Sed warnx("vpipe can't alloc arg space"); 79193323Sed return NULL; 80193323Sed } 81198090Srdivacky 82193323Sed va_start(args, fmt); 83193323Sed if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) { 84193323Sed warnx("vsystem args are too long"); 85193323Sed return NULL; 86193323Sed } 87198090Srdivacky#ifdef DEBUG 88193323Sed fprintf(stderr, "Executing %s\n", cmd); 89193323Sed#endif 90193323Sed fflush(NULL); 91198090Srdivacky fp = popen(cmd, "r"); 92193323Sed if (fp == NULL) { 93193323Sed warnx("popen() failed"); 94193323Sed return NULL; 95193323Sed } 96198090Srdivacky get_string(rp, MAXPATHLEN, fp); 97202375Srdivacky#ifdef DEBUG 98198090Srdivacky fprintf(stderr, "Returned %s\n", rp); 99193323Sed#endif 100193323Sed va_end(args); 101193323Sed if (pclose(fp) || (strlen(rp) == 0)) { 102193323Sed free(rp); 103193323Sed return NULL; 104193323Sed } 105193323Sed return rp; 106193323Sed} 107193323Sed