Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Why does write() to stdin work? (http://www.velocityreviews.com/forums/t960050-why-does-write-to-stdin-work.html)

ishwar.rattan@gmail.com 04-23-2013 01:59 PM

Why does write() to stdin work?
 
Tried the following code under gcc and it writes to stdin??

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

int main()
{
int k;
for(k= 0; k < 4; k++)
write(k, "Hello world!\n", 13);
}

Any poiners?
-ishwar

osmium 04-23-2013 03:52 PM

Re: Why does write() to stdin work?
 
<ishwar.rattan@gmail.com> wrote:

> Tried the following code under gcc and it writes to stdin??
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> int k;
> for(k= 0; k < 4; k++)
> write(k, "Hello world!\n", 13);
> }
>
> Any poiners?


There is an "fwrite()" in C but no "write()". so you must have stumbled
across a gcc extension. There is probably a command line way to force gcc
to do ANSI C if you don't like this result.



James Kuyper 04-23-2013 03:58 PM

Re: Why does write() to stdin work?
 
On 04/23/2013 09:59 AM, ishwar.rattan@gmail.com wrote:
> Tried the following code under gcc and it writes to stdin??
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> int k;
> for(k= 0; k < 4; k++)
> write(k, "Hello world!\n", 13);
> }
>
> Any poiners?


write() is not a C standard library function, so the reason why it did
or did not work depends upon the library that provides write(), so your
question should be directed to a forum appropriate to that library. If,
for instance, it's the Unix standard library function, then you should
try comp.unix.programmer.

How do you know that it worked? As far as I can tell, your program makes
no attempt to determine whether or not the call to write() was
successful. For example, if this is the Unix function, your code doesn't
check the return value from write(), which should be 13 for a successful
call, nor does it check the value of errno, which should be 0 after a
successful call, unless it was already set to a non-zero value before
the call.



ishwar.rattan@gmail.com 04-23-2013 04:39 PM

Re: Why does write() to stdin work?
 
On Tuesday, April 23, 2013 11:58:43 AM UTC-4, James Kuyper wrote:
>
> > Tried the following code under gcc and it writes to stdin??

>
> >

>
> > #include <stdio.h>

>
> > #include <stdlib.h>

>
> >

>
> > int main()

>
> > {

>
> > int k;

>
> > for(k= 0; k < 4; k++)

>
> > write(k, "Hello world!\n", 13);

>
> > }

>
> >

>
> > Any poiners?

>
>
>
> write() is not a C standard library function, so the reason why it did
>
> or did not work depends upon the library that provides write(), so your
>
> question should be directed to a forum appropriate to that library. If,
>
> for instance, it's the Unix standard library function, then you should
>
> try comp.unix.programmer.
>
>
>
> How do you know that it worked? As far as I can tell, your program makes
>
> no attempt to determine whether or not the call to write() was
>
> successful. For example, if this is the Unix function, your code doesn't
>
> check the return value from write(), which should be 13 for a successful
>
> call, nor does it check the value of errno, which should be 0 after a
>
> successful call, unless it was already set to a non-zero value before
>
> the call.


write() a is system call Unix systems, does not return failure and I will try comp.unix.programmer group.

Thanks for the effort.

-ishwar

Keith Thompson 04-23-2013 05:05 PM

Re: Why does write() to stdin work?
 
"osmium" <r124c4u102@comcast.net> writes:
> <ishwar.rattan@gmail.com> wrote:
>> Tried the following code under gcc and it writes to stdin??
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main()
>> {
>> int k;
>> for(k= 0; k < 4; k++)
>> write(k, "Hello world!\n", 13);
>> }
>>
>> Any poiners?

>
> There is an "fwrite()" in C but no "write()". so you must have stumbled
> across a gcc extension. There is probably a command line way to force gcc
> to do ANSI C if you don't like this result.


write() isn't a gcc extension; it's defined by POSIX.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Nobody 04-24-2013 01:14 AM

Re: Why does write() to stdin work?
 
On Tue, 23 Apr 2013 09:39:48 -0700, ishwar.rattan wrote:

> write() a is system call Unix systems, does not return failure


On success, it returns the number of bytes written. On failure, it returns
-1 and sets errno to indicate the nature of the failure. In your example,
it will return -1 and set errno to EBADF.


Edward A. Falk 04-24-2013 01:38 AM

Re: Why does write() to stdin work?
 
It doesn't work.

You just got very lucky the one time you tried it, probably because you
left stdin connected to your terminal which is also an output device
*and* you happened to be linked to a runtime library that let you get
away with it. Under different conditions, your code would probably fail.

You have encountered undefined behavior. From my quotes file:

What undefined means is:

undefined.
Do not rely on the results.
You have gone outside the domain of the function
You have broken the programming model of the C language.
Here there be dragons!
The implementors can do anything they want.
Be careful.
Use a different algorithm.
This is non-portable.
Don't do it.


Here's one of the rules about being a good programmer: Don't
just try things until they work. Read the documentation and
write code that adheres to the specifications. Just because
it worked today doesn't mean it will work tomorrow.

--
-Ed Falk, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/

glen herrmannsfeldt 04-24-2013 02:08 AM

Re: Why does write() to stdin work?
 
Nobody <nobody@nowhere.com> wrote:
> On Tue, 23 Apr 2013 09:39:48 -0700, ishwar.rattan wrote:


>> write() a is system call Unix systems, does not return failure


> On success, it returns the number of bytes written. On failure,
> it returns -1 and sets errno to indicate the nature of the failure.
> In your example, it will return -1 and set errno to EBADF.


There is no rule against writing to file descriptor zero.

The convention for inetd programs is that they read and write
of fd 0.

If fd 0 is a terminal, writing should be fine.

-- glen

Ian Collins 04-24-2013 02:55 AM

Re: Why does write() to stdin work?
 
ishwar.rattan@gmail.com wrote:
> Tried the following code under gcc and it writes to stdin??
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> int k;
> for(k= 0; k < 4; k++)
> write(k, "Hello world!\n", 13);
> }


Just one thing to add: check the return value of write (always good
practice) and you will see where your code fails.

--
Ian Collins

Keith Thompson 04-24-2013 03:58 AM

Re: Why does write() to stdin work?
 
falk@rahul.net (Edward A. Falk) writes:
> It doesn't work.
>
> You just got very lucky the one time you tried it, probably because you
> left stdin connected to your terminal which is also an output device
> *and* you happened to be linked to a runtime library that let you get
> away with it. Under different conditions, your code would probably fail.
>
> You have encountered undefined behavior. From my quotes file:
>
> What undefined means is:
>
> undefined.
> Do not rely on the results.
> You have gone outside the domain of the function
> You have broken the programming model of the C language.
> Here there be dragons!
> The implementors can do anything they want.
> Be careful.
> Use a different algorithm.
> This is non-portable.
> Don't do it.
>
>
> Here's one of the rules about being a good programmer: Don't
> just try things until they work. Read the documentation and
> write code that adheres to the specifications. Just because
> it worked today doesn't mean it will work tomorrow.


How is the behavior undefined?

If you mean that the C standard doesn't define the behavior of
write(), that's true but hardly useful; the POSIX standard does.

Somewhat off-topic:

As far as I can tell from a quick reading of the POSIX specification,
a call to write() can either succeed or fail. I'd naively expect
it to fail on an attempt to write to a file descriptor not opened
for writing, but I don't see anything that says it's *required*
to fail. And even if it were, failure is not undefined behavior;
write() would simply return -1 and set errno.

(I certainly think that writing to stdin is a silly thing to do.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 06:02 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.