Skip to content

EOF not handled in getline() #14

@harishankarv

Description

@harishankarv

I did not know where else to raise an issue regarding the blog post, as getline is not there in the code on github.

The code on the blog post does not handle the case where the user inputs an EOF, wanting to exit the shell. I believe it should be:

char *lsh_read_line(void)
{
  char *line = NULL;
  ssize_t bufsize = 0;  // have getline allocate a buffer for us
  if (getline(&line, &bufsize, stdin) == -1)
    exit(EXIT_SUCCESS);
  return line;
}

Or, to be even more pedantic:

char *lsh_read_line(void)
{
  char *line = NULL;
  ssize_t bufsize = 0;  // have getline allocate a buffer for us

  if (getline(&line, &bufsize, stdin) == -1){
    if (feof(stdin)) {  
      exit(EXIT_SUCCESS);  // We recieved an EOF
    } else  {
      perror("readline");
      exit(EXIT_FAILURE);
    }
  }
  return line;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions