guestfish: Make more strings translatable.
[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, char *regex, char *path)
32 {
33   char *buf;
34   char *out, *err;
35   int r;
36   char **lines;
37
38   NEED_ROOT (NULL);
39   ABS_PATH (path, NULL);
40
41   /* Make the path relative to /sysroot. */
42   buf = sysroot_path (path);
43   if (!buf) {
44     reply_with_perror ("malloc");
45     return NULL;
46   }
47
48   /* Note that grep returns an error if no match.  We want to
49    * suppress this error and return an empty list.
50    */
51   r = commandr (&out, &err, prog, flag, regex, buf, NULL);
52   free (buf);
53   if (r == -1 || r > 1) {
54     reply_with_error ("%s %s %s: %s", prog, flag, regex, err);
55     free (out);
56     free (err);
57     return NULL;
58   }
59
60   free (err);
61
62   lines = split_lines (out);
63   free (out);
64   if (lines == NULL) return NULL;
65
66   return lines;
67 }
68
69 char **
70 do_grep (char *regex, char *path)
71 {
72   /* The "--" is not really needed, but it helps when we don't need a flag. */
73   return grep ("grep", "--", regex, path);
74 }
75
76 char **
77 do_egrep (char *regex, char *path)
78 {
79   return grep ("egrep", "--", regex, path);
80 }
81
82 char **
83 do_fgrep (char *regex, char *path)
84 {
85   return grep ("fgrep", "--", regex, path);
86 }
87
88 char **
89 do_grepi (char *regex, char *path)
90 {
91   return grep ("grep", "-i", regex, path);
92 }
93
94 char **
95 do_egrepi (char *regex, char *path)
96 {
97   return grep ("egrep", "-i", regex, path);
98 }
99
100 char **
101 do_fgrepi (char *regex, char *path)
102 {
103   return grep ("fgrep", "-i", regex, path);
104 }
105
106 char **
107 do_zgrep (char *regex, char *path)
108 {
109   return grep ("zgrep", "--", regex, path);
110 }
111
112 char **
113 do_zegrep (char *regex, char *path)
114 {
115   return grep ("zegrep", "--", regex, path);
116 }
117
118 char **
119 do_zfgrep (char *regex, char *path)
120 {
121   return grep ("zfgrep", "--", regex, path);
122 }
123
124 char **
125 do_zgrepi (char *regex, char *path)
126 {
127   return grep ("zgrep", "-i", regex, path);
128 }
129
130 char **
131 do_zegrepi (char *regex, char *path)
132 {
133   return grep ("zegrep", "-i", regex, path);
134 }
135
136 char **
137 do_zfgrepi (char *regex, char *path)
138 {
139   return grep ("zfgrep", "-i", regex, path);
140 }