简体   繁体   中英

Issue with CTRL+D on Windows Subsystem for Linux

I was working on an assignment using Windows Subsystem for Linux. Below is the C Code that was used to write a mini-shell for this assignment. I ran into an interesting issue using WSL. On line 35, you can see that I call the read function to read in the buffer and it does a check for null. When pressing Ctrl + D , while using WSL, it will go into the if statement and prints the print message on line 36 infinitely and does not stop until I used Ctrl + C to exit. When running this program on a Linux machine, it behaves appropriately and prints once, and brings us to the top of the loop.
Any ideas as to what this bug could be?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <sys/types
#include <unistd.h>
#include <error.h>

char prompt[] = "$ ";

static int
Fork()
{
  pid_t pid;

  if ((pid = fork()) < 0)
    error(EXIT_FAILURE, errno, "fork error");
    return(pid);
}

int    
main(void)
{
  long MAX = sysconf(_SC_LINE_MAX);
  char buf[MAX];
  pid_t pid;
  int status, n;

  do {
    write(STDOUT_FILENO, prompt, strlen(prompt));
    fflush(NULL);
    memset(buf, 0, MAX);
    if((n = read(STDIN_FILENO, buf, MAX)) == 0) {
      printf("use exit to exit shell\n");
      continue;
    }
    buf[strlen(buf) - 1] = '\0'; // chomp '\n'

    if (strncmp(buf, "exit", MAX) == 0) { // match
      break;
    }
    pid = Fork();
    if (pid == 0) {  // child
      execlp(buf, buf, (char *)NULL);
      error(EXIT_FAILURE, errno, "exec failure");
    }
    // parent
    if ((pid = waitpid(pid, &status, 0)) < 0)
      error(EXIT_FAILURE, errno, "waitpid error");
  } while(1);
  exit(EXIT_SUCCESS);
}

The program is in C but there are no options available to insert C Code snippets.

不同的操作系统对EOF使用不同的击键。

Documentation on read() (Linux manpages v 3.54) does not specify that end of file (ctrl/D) causes read to return anything besides of 0. On the contrary, it says that return value zero indicates end of file. So you're relying upon undefined behavior. Somehow on your Linux ctrl/D causes error, thus read() returns -1. Your program in this case exits the loop. Or, ctrl/D is read literally, then read() returns 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM