#1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    close && reopen stdout


    any reason i shouldn't do this? or is there a better more portable way?
    Code:
    main()
    {
            puts("hi");
            close(STDOUT_FILENO);
            puts("can u see me?");
            open("/dev/pts/0", O_RDWR);
            puts("now can u see me?");
    }
    its works as expected:
    <:oo7:> ./a.out
    hi
    now can u see me?
    i actually have no idea what '/dev/pts/0' is, but the symbolic link from '/dev/fd/1' leads there. when i try to open '/dev/fd/1' i get a ENOENT error, so i tried this and it worked.
    Last edited by infamous41md; September 28th, 2003 at 04:19 PM.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Other than wasted CPU cycles I don't think there is any danger. Since you have closed the handle to stdin, I presume the OS just dumps the puts() into the bit bucket. Obviously it doesn't crash since you got it to run properly. Is there any reason why you want to do it this way or just hacking? As for being portable, I think you can do an fclose() on stdin (though I haven't tried it). What you would do to open stdin I have no idea. Lemme know if you find an ANSI way of doing this.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    heh yea its just laziness basically. i have this fairly large program that i would like to add a "quiet" mode to. after further thinking i realized a perl script that does a s///g on all of the printf's and substitute tests for all of them should do the trick, but the idea of just closing stdin was more fun(hackish) i guess :D and yes when it calls write() it just returns EBADF which isnt fatal.

IMN logo majestic logo threadwatch logo seochat tools logo