9 #define BUFFERSIZE 4096
11 static char *_read_stream (FILE *sd
)
16 size
+= BUFFERSIZE
+ (size
== 0);
17 buffer
= (char *) realloc (buffer
, size
);
18 memset (buffer
+ size
- BUFFERSIZE
- 1, 0, BUFFERSIZE
+ 1);
19 fread (buffer
+ size
- BUFFERSIZE
- 1, 1, BUFFERSIZE
, sd
);
23 if (buffer
[0] == '\0') {
31 char *exec_cmd (char *cmd
)
36 VERBOSE (DEBUG
, fprintf (stdout
, "exec command: %s\n", cmd
));
37 FILE *fp
= popen (cmd
, "r");
39 buffer
= _read_stream (fp
);
43 if ((status
== -1) || (buffer
== NULL
)) {
51 char *load_file (char *name
)
56 VERBOSE (DEBUG
, fprintf (stdout
, "open file: %s\n", name
));
57 FILE *fd
= fopen (name
, "r");
59 buffer
= _read_stream (fd
);
71 char *read_stdin (void)
75 VERBOSE (DEBUG
, fprintf (stdout
, "read stdin\n"));
76 buffer
= _read_stream (stdin
);
81 char **split_lines (char *buffer
, int max
)
87 char *line
= (char *) calloc (max
+ 1, sizeof (char));
88 for (i
= j
= 0; buffer
[i
] != '\0'; i
++) {
89 if (buffer
[i
] >= ' ') {
90 line
[j
++] = buffer
[i
];
92 if ((j
== max
) || (buffer
[i
] == '\n')) {
93 lines
= (char **) realloc (lines
, (n
+ 2) * sizeof (char *));
96 line
= calloc (max
+ 1, sizeof (char));
101 if (line
[0] != '\0') {
102 lines
= (char **) realloc (lines
, (n
+ 2) * sizeof (char *));
107 VERBOSE (DEBUG
, fprintf (stdout
, "split lines: %d\n", n
));
112 void free_lines (char **lines
)
122 /* vim: set ts=4 sw=4 et: */