1 | /*␊ |
2 | * shell_process.cpp␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/2010.␊ |
5 | *␊ |
6 | */␊ |
7 | ␊ |
8 | #include "ShellProcess.h"␊ |
9 | #include <string.h>␊ |
10 | #include <sys/stat.h>␊ |
11 | #include "string_util.h"␊ |
12 | ␊ |
13 | //----------------------------------------------------------------␊ |
14 | // portable open process:␊ |
15 | FILE * ShellProcess::open(const char *cmd, const char *mode) {␊ |
16 | ␉_fpt=::popen(cmd,mode);␊ |
17 | ␉return _fpt;␊ |
18 | }␊ |
19 | ␊ |
20 | //----------------------------------------------------------------␊ |
21 | int ShellProcess::close() {␊ |
22 | ␉int ret = ::pclose(_fpt);␊ |
23 | ␉_fpt=NULL;␊ |
24 | ␉return ret;␊ |
25 | }␊ |
26 | ␊ |
27 | //----------------------------------------------------------------␊ |
28 | char * ShellProcess::get_line(char * line, size_t s, const char *matching)␊ |
29 | {␊ |
30 | ␉if (_fpt==NULL || line==NULL) return NULL;␊ |
31 | ␉char * l = fgets(line, s, _fpt);␊ |
32 | ␉␊ |
33 | ␉if (matching==NULL || (*matching)=='\0') ␊ |
34 | ␉␉return l; // we're done␊ |
35 | ␉␊ |
36 | ␉// search first line matching substring 'matching'␊ |
37 | ␉size_t len = strlen(line);␊ |
38 | ␉␊ |
39 | ␉for (; l!=NULL && (len >0) ;)␊ |
40 | ␉{␊ |
41 | ␉␉if (strstr(line, matching))␊ |
42 | ␉␉{␊ |
43 | ␉␉␉return l;␊ |
44 | ␉␉}␊ |
45 | ␉␉␊ |
46 | ␉␉*line = '\0';␊ |
47 | ␉␉l = fgets(line, s, _fpt);␊ |
48 | ␉␉len = strlen(line);␊ |
49 | ␉}␊ |
50 | ␉␊ |
51 | ␉return l;␊ |
52 | }␊ |
53 | |