add more test (3)
[cmore.git] / cmd.c
CommitLineData
18e7a1ae
LM
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
ff9767eb 4#include <unistd.h>
ec215ba5 5
4bbdea75
LM
6#include "debug.h"
7
ec215ba5
LM
8#include "cmd.h"
9
10#define BUFFERSIZE 4096
11
18e7a1ae 12static char *_read_stream (FILE *sd)
ec215ba5
LM
13{
14 char *buffer = NULL;
15 size_t size = 0;
16 do {
17 size += BUFFERSIZE + (size == 0);
18 buffer = (char *) realloc (buffer, size);
19 memset (buffer + size - BUFFERSIZE - 1, 0, BUFFERSIZE + 1);
a8e8f058 20 } while (fread (buffer + size - BUFFERSIZE - 1, 1, BUFFERSIZE, sd) > 0);
ec215ba5 21
53cd5b25
LM
22 /* check size */
23 if (buffer[0] == '\0') {
24 free (buffer);
25 buffer = NULL;
26 }
27
ec215ba5
LM
28 return buffer;
29}
30
31char *exec_cmd (char *cmd)
32{
33 int status = -1;
34 char *buffer = NULL;
35
97efd1ff 36 VERBOSE (DEBUG, fprintf (stdout, "exec command: %s\n", cmd));
ff9767eb
LM
37 int saved_stderr = dup (STDERR_FILENO);
38 close (STDERR_FILENO);
ec215ba5
LM
39 FILE *fp = popen (cmd, "r");
40 if (fp != NULL) {
18e7a1ae 41 buffer = _read_stream (fp);
ec215ba5
LM
42 status = pclose(fp);
43 }
c22e49bd 44 dup2 (saved_stderr, STDERR_FILENO);
ec215ba5 45
53cd5b25 46 if ((status == -1) || (buffer == NULL)) {
ec215ba5
LM
47 free (buffer);
48 buffer = NULL;
49 }
50
51 return buffer;
52}
53
18e7a1ae 54char *load_file (char *name)
ec215ba5
LM
55{
56 int status = -1;
57 char *buffer = NULL;
58
97efd1ff 59 VERBOSE (DEBUG, fprintf (stdout, "open file: %s\n", name));
ec215ba5
LM
60 FILE *fd = fopen (name, "r");
61 if (fd != NULL) {
18e7a1ae 62 buffer = _read_stream (fd);
ec215ba5
LM
63 status = fclose (fd);
64 }
65
66 if (status == -1) {
67 free (buffer);
68 buffer = NULL;
69 }
70
71 return buffer;
72}
73
74char *read_stdin (void)
75{
ec215ba5
LM
76 char *buffer = NULL;
77
97efd1ff 78 VERBOSE (DEBUG, fprintf (stdout, "read stdin\n"));
18e7a1ae 79 buffer = _read_stream (stdin);
ec215ba5
LM
80
81 return buffer;
82}
83
84char **split_lines (char *buffer, int max)
85{
86 int n = 0;
87
88 char **lines = NULL;
89 int i, j;
90 char *line = (char *) calloc (max + 1, sizeof (char));
91 for (i = j = 0; buffer[i] != '\0'; i++) {
92 if (buffer[i] >= ' ') {
93 line[j++] = buffer[i];
94 }
95 if ((j == max) || (buffer[i] == '\n')) {
96 lines = (char **) realloc (lines, (n + 2) * sizeof (char *));
97 lines[n] = line;
98 lines[n + 1] = NULL;
99 line = calloc (max + 1, sizeof (char));
100 n++;
101 j = 0;
102 }
103 }
104 if (line[0] != '\0') {
105 lines = (char **) realloc (lines, (n + 2) * sizeof (char *));
97efd1ff 106 lines[n++] = line;
ec215ba5
LM
107 } else {
108 free (line);
109 }
97efd1ff 110 VERBOSE (DEBUG, fprintf (stdout, "split lines: %d\n", n));
ec215ba5
LM
111
112 return lines;
113}
114
115void free_lines (char **lines)
116{
117 int i = 0;
18e7a1ae 118
08f3bcd3
LM
119 if (!lines) {
120 return;
121 }
122
ec215ba5
LM
123 while (lines[i]) {
124 free (lines[i++]);
125 }
126 free (lines);
ec215ba5 127}
97efd1ff
LM
128
129/* vim: set ts=4 sw=4 et: */