exec.c revision 22997
1#ifndef lint 2static const char *rcsid = "$Id$"; 3#endif 4 5/* 6 * FreeBSD install - a package for the installation and maintainance 7 * of non-core utilities. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Jordan K. Hubbard 19 * 18 July 1993 20 * 21 * Miscellaneous system routines. 22 * 23 */ 24 25#include "lib.h" 26 27/* 28 * Unusual system() substitute. Accepts format string and args, 29 * builds and executes command. Returns exit code. 30 */ 31 32int 33vsystem(const char *fmt, ...) 34{ 35 va_list args; 36 char *cmd; 37 int ret, maxargs; 38 39 maxargs = sysconf(_SC_ARG_MAX); 40 maxargs -= 32; /* some slop for the sh -c */ 41 cmd = malloc(maxargs); 42 if (!cmd) { 43 whinge("vsystem can't alloc arg space"); 44 return 1; 45 } 46 47 va_start(args, fmt); 48 if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) { 49 whinge("vsystem args are too long"); 50 return 1; 51 } 52#ifdef DEBUG 53printf("Executing %s\n", cmd); 54#endif 55 ret = system(cmd); 56 va_end(args); 57 free(cmd); 58 return ret; 59} 60 61