-
Notifications
You must be signed in to change notification settings - Fork 1
/
popen_test.c
55 lines (41 loc) · 1.14 KB
/
popen_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <limits.h>
#include <string.h>
void *func_thrd();
int main(int argc, char *argv[])
{
pthread_t thread1;
pthread_create( &thread1, NULL, &func_thrd, NULL);
pthread_join( thread1, NULL);
return EXIT_SUCCESS;
}
void *func_thrd()
{
char buf[LINE_MAX], cmdstr[PATH_MAX], cmd_out_buf[LINE_MAX];
FILE *fp;
while(fgets(buf, LINE_MAX-1, stdin))
{
printf("%s:[%c] %s:[%d]\n","*(buf+strlen(buf)-1)",*(buf+strlen(buf)-1),"ASCII code",*(buf+strlen(buf)-1));
if('\n' == buf[strlen(buf)-1])
{
fputs("fgets got newline at the end!\n",stdout);
*(buf+strlen(buf)-1)='\0'; // trim the newline at the end
}
*cmdstr='\0';
strcat(cmdstr,"./fnd.sh ");
strcat(cmdstr,buf);
printf("%s:[%s]\n","command to execute",cmdstr);
if(!(fp = popen(cmdstr,"r")))
{
fputs("fp = popen(cmdstr,\"r\") -> ERR!",stdout); //output detailed error message, how to?
break;
}
#how to implement Arrow key Up/Down history toggle like in shell or linux terminal in C/C++?
while(NULL != fgets(cmd_out_buf,LINE_MAX,fp))
fputs(cmd_out_buf,stdout);
pclose(fp);
}
return NULL;
}