Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hello, I have an odd problem with using the open system call: it seems whenever I try to open an existing file in a certain program I've written, the file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52

    open(2) returns 0


    Hello,

    I have an odd problem with using the open system call: it seems whenever I try to open an existing file in a certain program I've written, the file descriptor is always 0.

    I haven't put in a close for stdin anywhere in my code . . . is there any other reason why open would return 0 as the file descriptor

    and no the program doesn't work, i think all subsequent reads are being done to stdin instead of the file :drown:

    thanks in advance for any help

  2. #2
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401

    Code, please...

    It seems strange... can post your code (only the section interested), please?
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    According to the man page, open(2) returns the lowest FD not currently open for the process. Has your program actually got stdin open?

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52

    Re: Code, please...

    Quote Originally Posted by burnit
    It seems strange... can post your code (only the section interested), please?
    Code:
    redir_file *open_redirect_file(redirection direction, 
                                   redir_file *rf)
    {
    	char *filename, *arg[MAXARG];
    	int narg;
    	gettok(&filename);
    	int fd = -1;
    
    	/* Ignore rest of the line */
    	for (narg=0; 
    	    &#40;narg<MAXARG&#41; && &#40;gettok&#40;&arg&#91;narg&#93;&#41; != EOL&#41;;
    	    narg++&#41;;
    	
    	/* If file exists then open it */
    	if &#40;fd = open&#40;filename, 
    		&#40;&#40;direction == LEFT&#41; ? 
    		 &#40;O_RDONLY | O_RSYNC&#41; &#58; 
    		 &#40;O_WRONLY | O_SYNC&#41;&#41;&#41; == -1&#41;
    	&#123;
    		/* Else create it */
    		if &#40;direction == LEFT&#41;
    		&#123;
    			fprintf&#40;stderr, "smallsh redirect&#58; %s does not exists!\n", filename&#41;;
    			return NULL;
    		&#125;
    		fd = open&#40;filename, O_CREAT | O_WRONLY | O_TRUNC, 
    			S_IRUSR | S_IWUSR&#41;;
    	&#125;
    	
    	rf->direction = direction;
    	rf->fd = fd;
    	if &#40;rf->fd == -1&#41;
    	&#123;
    		perror&#40;"smallsh redirect"&#41;;
    		return NULL;
    	&#125;
    
    	return rf;
    &#125;
    code is fairly simple.. filename is correct.. no errors are returned .. i could change the open command to
    Code:
    open&#40;filename, O_RDWR&#41;
    but the same problem persists

  6. #5
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52
    Quote Originally Posted by scm
    According to the man page, open(2) returns the lowest FD not currently open for the process. Has your program actually got stdin open?
    there haven't been any previous close calls on stdin, the only time stdin is played with is after the function exists when i dup2(2) the file's file descriptor with 0

  7. #6
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    Code:
    /* If file exists then open it */
    if &#40;fd = open&#40;filename, &#40;&#40;direction == LEFT&#41; ? &#40;O_RDONLY | O_RSYNC&#41; &#58; &#40;O_WRONLY | O_SYNC&#41;&#41;&#41; == -1&#41;
    &#123;
    Try to turn into brackets the operation fd =...

    Code:
     if &#40;&#40;fd = open&#40;filename, mode&#41;&#41; == -1&#41;
    Probably the open call fails (and so returns -1), and the compiler traduce:

    fd = (open(filename, mode) == -1)

    Because the open is supposed to fail, the true condition returns 0, wich will be the value of fd.
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  8. #7
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52
    Quote Originally Posted by burnit
    Code:
    /* If file exists then open it */
    if &#40;fd = open&#40;filename, &#40;&#40;direction == LEFT&#41; ? &#40;O_RDONLY | O_RSYNC&#41; &#58; &#40;O_WRONLY | O_SYNC&#41;&#41;&#41; == -1&#41;
    &#123;
    Try to turn into brackets the operation fd =...

    Code:
     if &#40;&#40;fd = open&#40;filename, mode&#41;&#41; == -1&#41;
    Probably the open call fails (and so returns -1), and the compiler traduce:

    fd = (open(filename, mode) == -1)

    Because the open is supposed to fail, the true condition returns 0, wich will be the value of fd.
    *kicks myself*
    yeah i let mode = the mode code and put parenthesis in fd=open() and that worked... such a newbie mistake.. i guess that why coding conventions are so important!!!!

    thanks a lot for you're help.. i appreciate it =D

  9. #8
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Assuming you want to redirect stdout or stdin - freopen is the best choice.
    Code:
    freopen&#40;"OUTPUT.dat","w",stdout&#41;;
    freopen&#40;"INPUT.dat", "r", stdin&#41;;
    stdout now writes to the file OUTPUT.dat
    stdin now reads from the INPUT.dat file

  10. #9
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52
    Quote Originally Posted by jim mcnamara
    Assuming you want to redirect stdout or stdin - freopen is the best choice.
    Code:
    freopen&#40;"OUTPUT.dat","w",stdout&#41;;
    freopen&#40;"INPUT.dat", "r", stdin&#41;;
    stdout now writes to the file OUTPUT.dat
    stdin now reads from the INPUT.dat file
    exactly right, and i was tempted to do it that way too except that the assignment called for only system calls... the sadist

    do you think bash does it that way also? i looked at the 3.0 code and saw only dup2 calls

  11. #10
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    You do realize that 2 = stderr

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •