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, char *regex, char *path)
39 ABS_PATH (path, NULL);
41 /* Make the path relative to /sysroot. */
42 buf = sysroot_path (path);
44 reply_with_perror ("malloc");
48 /* Note that grep returns an error if no match. We want to
49 * suppress this error and return an empty list.
51 r = commandr (&out, &err, prog, flag, regex, buf, NULL);
53 if (r == -1 || r > 1) {
54 reply_with_error ("%s %s %s: %s", prog, flag, regex, err);
62 lines = split_lines (out);
64 if (lines == NULL) return NULL;
70 do_grep (char *regex, char *path)
72 /* The "--" is not really needed, but it helps when we don't need a flag. */
73 return grep ("grep", "--", regex, path);
77 do_egrep (char *regex, char *path)
79 return grep ("egrep", "--", regex, path);
83 do_fgrep (char *regex, char *path)
85 return grep ("fgrep", "--", regex, path);
89 do_grepi (char *regex, char *path)
91 return grep ("grep", "-i", regex, path);
95 do_egrepi (char *regex, char *path)
97 return grep ("egrep", "-i", regex, path);
101 do_fgrepi (char *regex, char *path)
103 return grep ("fgrep", "-i", regex, path);
107 do_zgrep (char *regex, char *path)
109 return grep ("zgrep", "--", regex, path);
113 do_zegrep (char *regex, char *path)
115 return grep ("zegrep", "--", regex, path);
119 do_zfgrep (char *regex, char *path)
121 return grep ("zfgrep", "--", regex, path);
125 do_zgrepi (char *regex, char *path)
127 return grep ("zgrep", "-i", regex, path);
131 do_zegrepi (char *regex, char *path)
133 return grep ("zegrep", "-i", regex, path);
137 do_zfgrepi (char *regex, char *path)
139 return grep ("zfgrep", "-i", regex, path);