调用函数时候,传入脚本路径名称或者具体命令。

int shell_call(std::string &cmdstr) {
  enum { maxline=100 };
  char line[maxline];
  FILE *fpin;
  int ret;
  if((fpin = popen(cmdstr.c_str(), "r")) == NULL) {
    printf("popen error\n");
    exit(-1);
  }
  for(;;) {
    fputs("prompt> ", stdout);
    fflush(stdout);
    if(fgets(line, maxline, fpin) == NULL) /*read from pipe*/
      break;
    if(fputs(line, stdout) == EOF) {
      printf("fputs error\n");
      exit(-1);
    }
  }
  if((ret = pclose(fpin)) == -1) {
    printf("pclose error\n");
    exit(-1);
  }
  return ret;
}

相关文章:

  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-09-13
  • 2022-01-25
猜你喜欢
  • 2021-10-02
  • 2021-07-30
  • 2022-12-23
  • 2021-06-14
  • 2021-07-22
  • 2021-10-05
  • 2021-11-05
相关资源
相似解决方案