command.c: avoid shadowing a global function
[libguestfs.git] / daemon / headtail.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 headtail (const char *prog, const char *flag, const char *n, 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   r = command (&out, &err, prog, flag, n, buf, NULL);
46   free (buf);
47   if (r == -1) {
48     reply_with_error ("%s %s %s: %s", prog, flag, n, err);
49     free (out);
50     free (err);
51     return NULL;
52   }
53
54   free (err);
55
56 #if 0
57   /* Split it at the first whitespace. */
58   len = strcspn (out, " \t\n");
59   out[len] = '\0';
60 #endif
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_head (const char *path)
71 {
72   return headtail ("head", "-n", "10", path);
73 }
74
75 char **
76 do_tail (const char *path)
77 {
78   return headtail ("tail", "-n", "10", path);
79 }
80
81 char **
82 do_head_n (int n, const char *path)
83 {
84   char nbuf[16];
85
86   snprintf (nbuf, sizeof nbuf, "%d", n);
87
88   return headtail ("head", "-n", nbuf, path);
89 }
90
91 char **
92 do_tail_n (int n, const char *path)
93 {
94   char nbuf[16];
95
96   if (n >= 0)
97     snprintf (nbuf, sizeof nbuf, "%d", n);
98   else
99     snprintf (nbuf, sizeof nbuf, "+%d", -n);
100
101   return headtail ("tail", "-n", nbuf, path);
102 }