Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / lib / exec.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * Miscellaneous system routines.
18  *
19  * $FreeBSD: src/usr.sbin/pkg_install/lib/exec.c,v 1.7.2.3 2002/08/20 06:35:08 obrien Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/exec.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include "lib.h"
24 #include <err.h>
25
26 /*
27  * Unusual system() substitute.  Accepts format string and args,
28  * builds and executes command.  Returns exit code.
29  */
30
31 int
32 vsystem(const char *fmt, ...)
33 {
34     va_list args;
35     char *cmd;
36     int ret, maxargs;
37
38     maxargs = sysconf(_SC_ARG_MAX);
39     maxargs -= 32;                      /* some slop for the sh -c */
40     cmd = malloc(maxargs);
41     if (!cmd) {
42         warnx("vsystem can't alloc arg space");
43         return 1;
44     }
45
46     va_start(args, fmt);
47     if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
48         warnx("vsystem args are too long");
49         return 1;
50     }
51 #ifdef DEBUG
52 printf("Executing %s\n", cmd);
53 #endif
54     ret = system(cmd);
55     va_end(args);
56     free(cmd);
57     return ret;
58 }
59
60 char *
61 vpipe(const char *fmt, ...)
62 {
63    FILE *fp;
64    char *cmd, *rp;
65    int maxargs;
66    va_list args;
67
68     rp = malloc(MAXPATHLEN);
69     if (!rp) {
70         warnx("vpipe can't alloc buffer space");
71         return NULL;
72     }
73     maxargs = sysconf(_SC_ARG_MAX);
74     maxargs -= 32;                          /* some slop for the sh -c */
75     cmd = alloca(maxargs);
76     if (!cmd) {
77         warnx("vpipe can't alloc arg space");
78         return NULL;
79     }
80
81     va_start(args, fmt);
82     if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) {
83         warnx("vsystem args are too long");
84         return NULL;
85     }
86 #ifdef DEBUG
87     fprintf(stderr, "Executing %s\n", cmd);
88 #endif
89     fflush(NULL);
90     fp = popen(cmd, "r");
91     if (fp == NULL) {
92         warnx("popen() failed");
93         return NULL;
94     }
95     get_string(rp, MAXPATHLEN, fp);
96 #ifdef DEBUG
97     fprintf(stderr, "Returned %s\n", rp);
98 #endif
99     va_end(args);
100     if (pclose(fp) || (strlen(rp) == 0)) {
101         free(rp);
102         return NULL;
103     }
104     return rp;
105 }