Added a description for my extensions (README-my-extensions.html).
[parsecvs/imz-RCS2git-use-cases.git] / gitutil.c
blobf3b43ae0344edda8769242d1de368daacde2f374
1 /*
2 * Copyright © 2006 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include "cvs.h"
21 int
22 git_system (char *command)
24 int ret = 0;
26 /* printf ("\t%s\n", command); */
27 #if 1
28 ret = system (command);
29 if (ret != 0) {
30 fprintf (stderr, "%s failed\n", command);
32 #endif
33 return ret;
36 char *
37 git_system_to_string (char *command)
39 FILE *f;
40 char buf[1024];
41 char *nl;
43 f = popen (command, "r");
44 if (!f) {
45 fprintf (stderr, "%s: %s\n", command, strerror (errno));
46 return NULL;
48 if (fgets (buf, sizeof (buf), f) == NULL) {
49 fprintf (stderr, "%s: %s\n", command, strerror (errno));
50 pclose (f);
51 return NULL;
53 while (getc (f) != EOF)
55 pclose (f);
56 nl = strchr (buf, '\n');
57 if (nl)
58 *nl = '\0';
59 return atom (buf);
62 int
63 git_string_to_system (char *command, char *string)
65 FILE *f;
67 f = popen (command, "w");
68 if (!f) {
69 fprintf (stderr, "%s: %s\n", command, strerror (errno));
70 return -1;
72 if (fputs (string, f) == EOF) {
73 fprintf (stderr, "%s: %s\n", command, strerror (errno));
74 pclose (f);
75 return -1;
77 pclose (f);
78 return 0;
81 char *
82 git_format_command (const char *fmt, ...)
84 /* Guess we need no more than 100 bytes. */
85 int n, size = 100;
86 char *p, *np;
87 va_list ap;
89 if ((p = malloc (size)) == NULL)
90 return NULL;
92 while (1) {
93 /* Try to print in the allocated space. */
94 va_start(ap, fmt);
95 n = vsnprintf (p, size, fmt, ap);
96 va_end(ap);
97 /* If that worked, return the string. */
98 if (n > -1 && n < size)
99 return p;
100 /* Else try again with more space. */
101 if (n > -1) /* glibc 2.1 */
102 size = n+1; /* precisely what is needed */
103 else /* glibc 2.0 */
104 size *= 2; /* twice the old size */
105 if ((np = realloc (p, size)) == NULL) {
106 free(p);
107 return NULL;
108 } else {
109 p = np;