亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1372 | 回復(fù): 0
打印 上一主題 下一主題

[C] Linux終端間通信 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2014-03-17 16:21 |只看該作者 |倒序瀏覽
    自己編寫了一個小程序,想實現(xiàn)將一個程序的從一個終端的標準輸入獲取數(shù)據(jù),并將標準輸出在同一終端上輸出。
    現(xiàn)在遇到的問題是:在新打開的終端中輸入的數(shù)據(jù)時并不會完全被程序接受,而且所輸入的內(nèi)容還有可能被shell解釋執(zhí)行,導(dǎo)致程序無法從新打開的終端上正確獲取數(shù)據(jù)。
以下是程序代碼:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/un.h>
  7. #include <sys/socket.h>
  8. #include <stddef.h>
  9. #include <fcntl.h>

  10. #define MAXLINE 1024

  11. int main(void)
  12. {
  13.     pid_t pid;
  14.     int ptsfd;
  15.     int rc, a;
  16.     char result_buf[MAXLINE], command[MAXLINE];
  17.     FILE *fp;

  18.     sprintf(command, "gnome-terminal -x bash -c \"tty > a\";gnome-terminal;cat a; rm a");
  19.     fp = popen(command, "r");
  20.     if (fp == NULL) {
  21.         fprintf(stderr, "popen error! \n");
  22.         exit (errno);
  23.     }
  24.     while (fgets(result_buf, sizeof(result_buf), fp) != NULL) {
  25.         if (result_buf[strlen(result_buf) - 1] == '\n')
  26.             result_buf[strlen(result_buf) - 1] = '\0';
  27.         printf("command: %s \noutput: %s\n", command, result_buf);
  28.     }
  29.     rc = pclose(fp);
  30.     if (rc == -1) {
  31.         fprintf(stderr, "pclose error\n");
  32.         exit (-1);
  33.     }
  34.     else
  35.         printf("command %s's child terminal code is %d %d\n", command, rc, WEXITSTATUS(rc));

  36.     ptsfd = open(result_buf, O_RDWR);
  37.     int len;
  38.     char tmp[10] = {0};
  39.     bzero(command, MAXLINE);
  40.     while ((len = read(ptsfd, tmp, 10)) > 0) {
  41. //      printf("read %d successfully, totally %d bytes\n", tmp, len);
  42.         strcat(command, tmp);
  43.         if (tmp[0] != 13) {
  44.             continue;
  45.         }
  46.         if((len = write(ptsfd, "----test----", 13)) == -1)
  47.             break;
  48.         printf("write command is %s, totally %d bytes\n", command, strlen(command));
  49.         fflush(NULL);
  50.         bzero(command, MAXLINE);
  51.     }
  52.     close(ptsfd);
  53.     return 0;
  54. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP