Sponsored Content
Top Forums Programming what would happen if a process wrote to its own stdin? Post 302302522 by c_d on Tuesday 31st of March 2009 07:42:18 AM
Old 03-31-2009
Code:
#include <stdio.h>

int main(void) {
    if ( fprintf(stdin, "A") < 1 )
        perror("fprintf");
    return 0;
}

OP:
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ ./a.out 
fprintf: Bad file descriptor

so...how come now stdin becomes an invalid fd?

now when i do this
Code:
#include <stdio.h>
#include <unistd.h>

int main(void) {
    FILE *fakestdin;
    fakestdin = fdopen(STDIN_FILENO, "a+");
    if(fakestdin!=NULL)
    {
        fprintf(fakestdin, "A\n");
         perror("fakestdin");
    }
    else
          printf("fakestdin is NULL");
    return 0;    
}

OP:
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ ./a.out <temp.c
fakestdin is NULL[c_d@localhost C scratchpad]$ ./a.out 
A
fakestdin: Success
[c_d@localhost C scratchpad]$

it opens and writes to /dev/tty...

so what does this mean?

does it mean that (STDIN_FILENO) points to /dev/tty under normal conditions, but stdin does not? then where does it point to?

why did i have to open "stdin"'s fd to write to it? because i never have to open "stdout"'s fd to write to it...

Code:
#include <stdio.h>

int main(void) {
     fprintf(stdout, "HELLO!");
        perror("fprintf");
    return 0;
}

Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ ./a.out 
fprintf: Success
HELLO![c_d@localhost C scratchpad]$

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

what happen when changing Hostname?

I 'm using RH 7.2 Genome in the Network Configuration I change therer are two places one for static hostname for my machine and in DNS hostname I don't know what happen when restarting my PC when connecting using dialer I can't browse the Internet also I can't use sendmail .......Server timeout... (2 Replies)
Discussion started by: atiato
2 Replies

2. Programming

Any one can tell me how this happen?

The #1 Online Store for Louis Vuitton Replicas is: http://www.opichina.com.cn. We offer Louis Vuitton Replicas and more! Whatever you call it: LV Bags, LV Replicas, Louis Vuitton Fake, Louis Vuitton Knockoffs, Louis Vuitton Bag, Louis Vuitton Purse, Louis Vuitton Wallet, Louis Vuitton Shoes,... (10 Replies)
Discussion started by: jiangyanna
10 Replies

3. UNIX for Dummies Questions & Answers

What would happen if. . .

Hi, Could someone please tell me what would happen if the following were entered into the command line: rm -i /books/*.* rm /books/* Many thanks! (3 Replies)
Discussion started by: crispy
3 Replies

4. UNIX for Advanced & Expert Users

Unix ID deleted - What happen to process

I have an unix id (AIX system) which is used to run a couple of processes. They also write some log files into a file system (that is not in the home directory of the user id, but in different location). One bad day, the id was deleted accidentally. But the home directory, files and everything... (1 Reply)
Discussion started by: cmgreat
1 Replies

5. UNIX for Dummies Questions & Answers

whats happen when we create new user

hi frndz I wanna knw exatly what happen when we create new user... which directories are created ?? which files are modified ?? thanx.... (2 Replies)
Discussion started by: luckypower
2 Replies

6. AIX

How does ITIL processing happen in AIX?

How does ITIL process is implemened in AIX? (6 Replies)
Discussion started by: AIXlearner
6 Replies

7. UNIX for Dummies Questions & Answers

What Does Happen During Boot Process? - BIOS and MBR

I'm talking about boot process in multi-boot Linux perceptive. Please tell me whether my explanation is right or wrong? If wrong, please explain. "The BIOS checks the system and loads this initial bootstrapping code into memory. This initial bootstrap code searches for an active partition... (0 Replies)
Discussion started by: f.ben.isaac
0 Replies

8. UNIX for Advanced & Expert Users

Who actually wrote Professional Linux Programming ?

Hello, Who actually wrote Professional Linux Programming of Wrox publication as there are two different sets of writers, one set consists of Jon Masters and Richard Blum and another set of writers is Neil Mathew with lots of other writers. Plz resolve it. I'm really confused. Regards.. (0 Replies)
Discussion started by: vectrum
0 Replies

9. Shell Programming and Scripting

Crontab - wrote Simple Script but i cant work out how to play it at a certain time.

Hi everyone. Silly might be silly be I'm still new to bash. I'm trying to make an Alarm Clock for in the morning using my laptop i have wrote this Simple Script but i cant work out how to play it at a certain time. #!/bin/bash cd /home/josh/Music/Bruno_Mars/Hooligans/ cvlc... (8 Replies)
Discussion started by: jtsmith90
8 Replies

10. What is on Your Mind?

Ah, the AMIGA, (another poem I wrote in 2005).

Well I wrote this in 2005 and uploaded to AMINET.as a commemoration of a machine that is still in use today. It is now 29 years since this machne came into being. Phenominal and it is still being supported- WOW! My A1200 is on 24/7 and I use it to test code developed on AMIGA emulators... ... (0 Replies)
Discussion started by: wisecracker
0 Replies
explain_fdopen(3)					     Library Functions Manual						 explain_fdopen(3)

NAME
explain_fdopen - explain fdopen(3) errors SYNOPSIS
#include <libexplain/fdopen.h> const char *explain_fdopen(int fildes, const char *flags); const char *explain_errno_fdopen(int errnum, int fildes, const char *flags); void explain_message_fdopen(char *message, int message_size, int fildes, const char *flags); void explain_message_errno_fdopen(char *message, int message_size, int errnum, int fildes, const char *flags); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the fdopen(3) system call. explain_fdopen const char *explain_fdopen(int fildes, const char *flags); The explain_fdopen function is used to obtain an explanation of an error returned by the fdopen(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: FILE *fp = fdopen(fildes, flags); if (!fp) { fprintf(stderr, "%s ", explain_fdopen(fildes, flags)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_fdopen_or_die(3) function. fildes The original fildes, exactly as passed to the fdopen(3) system call. flags The original flags, exactly as passed to the fdopen(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_errno_fdopen const char *explain_errno_fdopen(int errnum, int fildes, const char *flags); The explain_errno_fdopen function is used to obtain an explanation of an error returned by the fdopen(3) system call. The least the mes- sage will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: FILE *fp = fdopen(fildes, flags); if (!fp) { int err = errno; fprintf(stderr, "%s ", explain_errno_fdopen(err, fildes, flags)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_fdopen_or_die(3) function. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. fildes The original fildes, exactly as passed to the fdopen(3) system call. flags The original flags, exactly as passed to the fdopen(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_message_fdopen void explain_message_fdopen(char *message, int message_size, int fildes, const char *flags); The explain_message_fdopen function may be used to obtain an explanation of an error returned by the fdopen(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: FILE *fp = fdopen(fildes, flags); if (!fp) { char message[3000]; explain_message_fdopen(message, sizeof(message), fildes, flags); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_fdopen_or_die(3) function. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. fildes The original fildes, exactly as passed to the fdopen(3) system call. flags The original flags, exactly as passed to the fdopen(3) system call. explain_message_errno_fdopen void explain_message_errno_fdopen(char *message, int message_size, int errnum, int fildes, const char *flags); The explain_message_errno_fdopen function may be used to obtain an explanation of an error returned by the fdopen(3) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: FILE *fp = fdopen(fildes, flags); if (!fp) { int err = errno; char message[3000]; explain_message_errno_fdopen(message, sizeof(message), err, fildes, flags); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_fdopen_or_die(3) function. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. fildes The original fildes, exactly as passed to the fdopen(3) system call. flags The original flags, exactly as passed to the fdopen(3) system call. SEE ALSO
fdopen(3) stream open functions explain_fdopen_or_die(3) stream open functions and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_fdopen(3)
All times are GMT -4. The time now is 04:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy