English 中文(简体)
使用 unix 系统调用在 C 程序内安装类似 shell 特性
原标题:Implementation of shell like feature within C program using unix system calls
  • 时间:2012-05-25 18:56:44
  •  标签:
  • c
  • unix

是否有人可以建议此程序有什么问题 。 我试图在此创建子进程来执行像 shell 一样的特性 。 在给出一个单词如 < code> > 或 < code> < pwd 的命令时, 它有效, 但命令中却有多个单词, 如 < code> > - lrt 或 < code> 谁是 i < / code > 无效 。 我正在做一些愚蠢的错误, 但无法调试 。

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <iostream>
    #include <wait.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <cstdlib>
    #define BUFSIZE  200
    #define ARGVSIZE 40
    #define DELIM    "
	
"

    int main ()
    {
      int i,n;
      char buf[BUFSIZE + 1] ;
      char * str = "Shell > ";
      char * clargs[ARGVSIZE] ;
      int returnstatus;
      for(;;)
      {
          n = 1;
          write(STDOUT_FILENO,str,strlen(str));

          read(STDIN_FILENO,buf,BUFSIZE);
          if(!strcmp(buf,"exit
"))
          {
              perror("exit");
              exit(20);
          时 时

          clargs[0] = strtok(buf,DELIM);

          while((clargs[n] = strtok(NULL,DELIM)) != NULL)
              n++;

          clargs[n] = NULL;

          switch(fork())
          {
          case 0:
          if((execvp(clargs[0],&clargs[0])) < 0)
              exit(200);

          default:
          wait(&returnstatus);
          printf("Exit status of command : %d
",WEXITSTATUS(returnstatus));
          for(int i =0; i <= n;i++)
              clargs[i] = "";

          for(int i =0; i < BUFSIZE+1;i++)
              buf[i] =   ;
      时 时
  时 时

  return 0;

时 时

问题回答

You don t have a space in DELIM.
When trying to run ls -lrt, you want to run the ls executable, with two arguments - ls and -lrt.
But your strtok wouldn t break ls -lrt in two. So you re actually trying to run a program called ls -lrt, and there s no such program.

DELIM 添加一个空格应该解决它 。

不是说对某些案例来说不够好。例如,运行 echo "a b" 时,您希望 a b" 是一个参数,因为括号。 strtok 会把它分成两个。真正的外壳会做更复杂的解析 。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签