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.
27 #include "guestfs_protocol.h"
32 grep (const char *prog, const char *flag, const char *regex, const char *path)
39 fd = open (path, O_RDONLY);
43 reply_with_perror ("%s", path);
47 /* Note that grep returns an error if no match. We want to
48 * suppress this error and return an empty list.
50 flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd;
51 r = commandrf (&out, &err, flags, prog, flag, regex, NULL);
52 if (r == -1 || r > 1) {
53 reply_with_error ("%s %s %s: %s", prog, flag, regex, err);
61 lines = split_lines (out);
63 if (lines == NULL) return NULL;
69 do_grep (const char *regex, const char *path)
71 /* The "--" is not really needed, but it helps when we don't need a flag. */
72 return grep ("grep", "--", regex, path);
76 do_egrep (const char *regex, const char *path)
78 return grep ("egrep", "--", regex, path);
82 do_fgrep (const char *regex, const char *path)
84 return grep ("fgrep", "--", regex, path);
88 do_grepi (const char *regex, const char *path)
90 return grep ("grep", "-i", regex, path);
94 do_egrepi (const char *regex, const char *path)
96 return grep ("egrep", "-i", regex, path);
100 do_fgrepi (const char *regex, const char *path)
102 return grep ("fgrep", "-i", regex, path);
106 do_zgrep (const char *regex, const char *path)
108 return grep ("zgrep", "--", regex, path);
112 do_zegrep (const char *regex, const char *path)
114 return grep ("zegrep", "--", regex, path);
118 do_zfgrep (const char *regex, const char *path)
120 return grep ("zfgrep", "--", regex, path);
124 do_zgrepi (const char *regex, const char *path)
126 return grep ("zgrep", "-i", regex, path);
130 do_zegrepi (const char *regex, const char *path)
132 return grep ("zegrep", "-i", regex, path);
136 do_zfgrepi (const char *regex, const char *path)
138 return grep ("zfgrep", "-i", regex, path);