d1f5a3f773f837bbadee955f9416f3091b6568dc
[libguestfs.git] / daemon / grep.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 static char **
31 grep (const char *prog, const char *flag, const char *regex, const char *path)
32 {
33   char *buf;
34   char *out, *err;
35   int r;
36   char **lines;
37
38   /* Make the path relative to /sysroot. */
39   buf = sysroot_path (path);
40   if (!buf) {
41     reply_with_perror ("malloc");
42     return NULL;
43   }
44
45   /* Note that grep returns an error if no match.  We want to
46    * suppress this error and return an empty list.
47    */
48   r = commandr (&out, &err, prog, flag, regex, buf, NULL);
49   free (buf);
50   if (r == -1 || r > 1) {
51     reply_with_error ("%s %s %s: %s", prog, flag, regex, err);
52     free (out);
53     free (err);
54     return NULL;
55   }
56
57   free (err);
58
59   lines = split_lines (out);
60   free (out);
61   if (lines == NULL) return NULL;
62
63   return lines;
64 }
65
66 char **
67 do_grep (const char *regex, const char *path)
68 {
69   /* The "--" is not really needed, but it helps when we don't need a flag. */
70   return grep ("grep", "--", regex, path);
71 }
72
73 char **
74 do_egrep (const char *regex, const char *path)
75 {
76   return grep ("egrep", "--", regex, path);
77 }
78
79 char **
80 do_fgrep (const char *regex, const char *path)
81 {
82   return grep ("fgrep", "--", regex, path);
83 }
84
85 char **
86 do_grepi (const char *regex, const char *path)
87 {
88   return grep ("grep", "-i", regex, path);
89 }
90
91 char **
92 do_egrepi (const char *regex, const char *path)
93 {
94   return grep ("egrep", "-i", regex, path);
95 }
96
97 char **
98 do_fgrepi (const char *regex, const char *path)
99 {
100   return grep ("fgrep", "-i", regex, path);
101 }
102
103 char **
104 do_zgrep (const char *regex, const char *path)
105 {
106   return grep ("zgrep", "--", regex, path);
107 }
108
109 char **
110 do_zegrep (const char *regex, const char *path)
111 {
112   return grep ("zegrep", "--", regex, path);
113 }
114
115 char **
116 do_zfgrep (const char *regex, const char *path)
117 {
118   return grep ("zfgrep", "--", regex, path);
119 }
120
121 char **
122 do_zgrepi (const char *regex, const char *path)
123 {
124   return grep ("zgrep", "-i", regex, path);
125 }
126
127 char **
128 do_zegrepi (const char *regex, const char *path)
129 {
130   return grep ("zegrep", "-i", regex, path);
131 }
132
133 char **
134 do_zfgrepi (const char *regex, const char *path)
135 {
136   return grep ("zfgrep", "-i", regex, path);
137 }