1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, 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, int argc, char ***globs, int *posn, int *count, int *r);
34 do_glob (const char *cmd, int 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
52 fprintf (stderr, _("use 'glob command [args...]'\n"));
56 /* This array will record the current execution position
57 * in the Cartesian product.
58 * NB. globs[0], posn[0], count[0] are ignored.
60 for (i = 1; i < argc; ++i)
62 for (i = 1; i < argc; ++i)
65 for (i = 1; i < argc; ++i) {
68 /* Only if it begins with '/' can it possibly be a globbable path. */
69 if (argv[i][0] == '/') {
70 pp = guestfs_glob_expand (g, argv[i]);
71 if (pp == NULL) { /* real error in glob_expand */
72 fprintf (stderr, _("glob: guestfs_glob_expand call failed: %s\n"),
77 /* If there were no matches, then we add a single element list
78 * containing just the original argv[i] string.
83 pp2 = realloc (pp, sizeof (char *) * 2);
91 pp[0] = strdup (argv[i]);
100 /* Doesn't begin with '/' */
102 pp = malloc (sizeof (char *) * 2);
107 pp[0] = strdup (argv[i]);
117 count[i] = count_strings (pp);
120 /* Issue the commands. */
121 glob_issue (argv[0], argc, globs, posn, count, &r);
123 /* Free resources. */
125 for (i = 1; i < argc; ++i)
127 free_strings (globs[i]);
132 glob_issue (char *cmd, int argc,
133 char ***globs, int *posn, int *count,
143 printf ("%s", argv[0]);
144 for (i = 1; i < argc; ++i) {
145 argv[i] = globs[i][posn[i]];
146 printf (" %s", argv[i]);
150 if (issue_command (argv[0], &argv[1], NULL) == -1)
151 *r = -1; /* ... but don't exit */
153 for (i = argc-1; i >= 1; --i) {
155 if (posn[i] < count[i])
159 if (i == 0) /* All done. */