Google Groups Home
Help | Sign in
Why does redirection cause the program to terminate in the following case
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
K-mart Cashier  
View profile
 More options 26 Aug, 14:55
Newsgroups: comp.unix.programmer
From: K-mart Cashier <cdal...@gmail.com>
Date: Tue, 26 Aug 2008 06:55:33 -0700 (PDT)
Local: Tues 26 Aug 2008 14:55
Subject: Why does redirection cause the program to terminate in the following case
Given the following

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(void)
{
  char name[] = "enter name:";
  char line[BUFSIZ];
  ssize_t n;
  int fd;

  if((fd = open("/dev/tty", O_RDWR)) < 0) {
    fprintf(stderr, "open error\n");
    exit(1);
  }

  if ((write(1, name, sizeof(name)-1)) != sizeof(name)-1) {
    fprintf(stderr, "write error \n");
    exit(1);
  }

  while ((n = read(0, line, BUFSIZ)) > 0) {
    write(1, line, n);
  }

  close(fd);

  exit(0);

}

[cdalten@localhost oakland]$ gcc -Wall pass.c -o pass
[cdalten@localhost oakland]$ more input
molly

When the program reads from the file input, the program terminates
right after it reads the data,

[cdalten@localhost oakland]$ ./pass < input
enter name:molly
[cdalten@localhost oakland]$

Why does this happen?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pk  
View profile
 More options 26 Aug, 15:09
Newsgroups: comp.unix.programmer
From: pk <p...@pk.invalid>
Date: Tue, 26 Aug 2008 16:09:13 +0200
Local: Tues 26 Aug 2008 15:09
Subject: Re: Why does redirection cause the program to terminate in the following case
On Tuesday 26 August 2008 15:55, K-mart Cashier wrote:

> When the program reads from the file input, the program terminates
> right after it reads the data,

What else should it do? Your code does that: read from fd 0, and write to fd
1. You redirect fd 0, so it reads from the file instead.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Why does redirection cause the program to terminate in the following ?case" by Jens Thoms Toerring
Jens Thoms Toerring  
View profile
 More options 26 Aug, 16:15
Newsgroups: comp.unix.programmer
From: j...@toerring.de (Jens Thoms Toerring)
Date: 26 Aug 2008 15:15:57 GMT
Local: Tues 26 Aug 2008 16:15
Subject: Re: Why does redirection cause the program to terminate in the following ?case

Why open that file? You never use it.

What else should it do? The end of the file is reached, so
read() returns 0 and on that condition your program exits.
Or is the question why it doesn't stop after the first line
when you have the program read from the terminal? If it's
that then it's that there's no end-of-file for the terminal
(unless you enter Crtl-D) so read doesn't return 0 but waits
until there's something to read, i.e. you enter a new line.

                         Regards, Jens
--
  \   Jens Thoms Toerring  ___      j...@toerring.de
   \__________________________      http://toerring.de


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
K-mart Cashier  
View profile
 More options 26 Aug, 20:35
Newsgroups: comp.unix.programmer
From: K-mart Cashier <cdal...@gmail.com>
Date: Tue, 26 Aug 2008 12:35:25 -0700 (PDT)
Local: Tues 26 Aug 2008 20:35
Subject: Re: Why does redirection cause the program to terminate in the following ?case
On Aug 26, 8:15 am, j...@toerring.de (Jens Thoms Toerring) wrote:

I was stumped on a much larger programming problem. So I wrote a
stripped down version of the issue that I was facing.  However, it
appears that I forgot to remove the call to open().

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google