1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009-2010 Red Hat Inc.
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
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 /* A bit tricky because in the case where there are multiple
29 * paths we have to perform a Cartesian product.
31 static void glob_issue (char *cmd, size_t argc, char ***globs, int *posn, int *count, int *r);
34 run_glob (const char *cmd, size_t argc, char *argv[])
36 /* For 'glob cmd foo /s* /usr/s*' this could be:
38 * (globs[0]) globs[1] globs[1] globs[2]
39 * (cmd) foo /sbin /usr/sbin
43 * and then we call every combination (ie. 1x3x3) of
53 fprintf (stderr, _("use 'glob command [args...]'\n"));
57 /* This array will record the current execution position
58 * in the Cartesian product.
59 * NB. globs[0], posn[0], count[0] are ignored.
61 for (i = 1; i < argc; ++i)
63 for (i = 1; i < argc; ++i)
66 for (i = 1; i < argc; ++i) {
69 /* Only if it begins with '/' can it possibly be a globbable path. */
70 if (argv[i][0] == '/') {
71 pp = guestfs_glob_expand (g, argv[i]);
72 if (pp == NULL) { /* real error in glob_expand */
73 fprintf (stderr, _("glob: guestfs_glob_expand call failed: %s\n"),
78 /* If there were no matches, then we add a single element list
79 * containing just the original argv[i] string.
84 pp2 = realloc (pp, sizeof (char *) * 2);
92 pp[0] = strdup (argv[i]);
101 /* Doesn't begin with '/' */
103 pp = malloc (sizeof (char *) * 2);
108 pp[0] = strdup (argv[i]);
118 count[i] = count_strings (pp);
121 /* Issue the commands. */
122 glob_issue (argv[0], argc, globs, posn, count, &r);
124 /* Free resources. */
126 for (i = 1; i < argc; ++i)
128 free_strings (globs[i]);
133 glob_issue (char *cmd, size_t argc,
134 char ***globs, int *posn, int *count,
144 for (i = 1; i < argc; ++i)
145 argv[i] = globs[i][posn[i]];
147 if (issue_command (argv[0], &argv[1], NULL, 0) == -1)
148 *r = -1; /* ... but don't exit */
150 for (i = argc-1; i >= 1; --i) {
152 if (posn[i] < count[i])
156 if (i == 0) /* All done. */