1 /* libguestfs - the guestfsd daemon
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.
26 #include "../src/guestfs_protocol.h"
31 grep (const char *prog, const char *flag, const char *regex, const char *path)
38 /* Make the path relative to /sysroot. */
39 buf = sysroot_path (path);
41 reply_with_perror ("malloc");
45 /* Note that grep returns an error if no match. We want to
46 * suppress this error and return an empty list.
48 r = commandr (&out, &err, prog, flag, regex, buf, NULL);
50 if (r == -1 || r > 1) {
51 reply_with_error ("%s %s %s: %s", prog, flag, regex, err);
59 lines = split_lines (out);
61 if (lines == NULL) return NULL;
67 do_grep (const char *regex, const char *path)
69 /* The "--" is not really needed, but it helps when we don't need a flag. */
70 return grep ("grep", "--", regex, path);
74 do_egrep (const char *regex, const char *path)
76 return grep ("egrep", "--", regex, path);
80 do_fgrep (const char *regex, const char *path)
82 return grep ("fgrep", "--", regex, path);
86 do_grepi (const char *regex, const char *path)
88 return grep ("grep", "-i", regex, path);
92 do_egrepi (const char *regex, const char *path)
94 return grep ("egrep", "-i", regex, path);
98 do_fgrepi (const char *regex, const char *path)
100 return grep ("fgrep", "-i", regex, path);
104 do_zgrep (const char *regex, const char *path)
106 return grep ("zgrep", "--", regex, path);
110 do_zegrep (const char *regex, const char *path)
112 return grep ("zegrep", "--", regex, path);
116 do_zfgrep (const char *regex, const char *path)
118 return grep ("zfgrep", "--", regex, path);
122 do_zgrepi (const char *regex, const char *path)
124 return grep ("zgrep", "-i", regex, path);
128 do_zegrepi (const char *regex, const char *path)
130 return grep ("zegrep", "-i", regex, path);
134 do_zfgrepi (const char *regex, const char *path)
136 return grep ("zfgrep", "-i", regex, path);