From: Barry Margolin on
In article
<c820d5c3-6875-4ca0-825a-c93b96481b0f(a)z16g2000prn.googlegroups.com>,
Chad <cdalten(a)gmail.com> wrote:

> On Jun 13, 8:12�pm, Barry Margolin <bar...(a)alum.mit.edu> wrote:
> > In article
> > <91c0237b-9c76-47ca-ba83-0d7d391fd...(a)s21g2000prm.googlegroups.com>,
> >
> >
> >
> >
> >
> > �Chad <cdal...(a)gmail.com> wrote:
> > > Given the following
> >
> > > m-net% more step.c
> > > #include <sys/types.h>
> > > #include <sys/uio.h>
> > > #include <unistd.h>
> >
> > > #include <stdio.h>
> >
> > > int main(void)
> > > {
> > > int n;
> >
> > > while ((n = read(0, buf, BUFSIZ)) > 0 )
> > > write(1, buf, n);
> > > return 0;
> > > }
> > > m-net% gcc -g -Wall step.c -o step
> > > m-net% ./step
> > > a string
> > > a string
> >
> > > How does the zsh shell know that 'a string' is in fact a string?
> >
> > What makes you think it does? �Your program is simply echoing back what
> > you typed, and the shell isn't involved at all, except to start up your
> > program.
>
> Well if something wouldn't have terminated 'a string', wouldn't I just
> have gotten a bunch of garbage after the letter 'g'?

No. You told it to write only n characters, where n is the number of
characters you read from stdin. Had you written:

write(1, buf, BUFSIZ);

then you probably would have gotten lots of garbage.

BTW, how did you get this to compile without a declaration of buf?

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Chad on
On Jun 14, 8:30 am, Barry Margolin <bar...(a)alum.mit.edu> wrote:
> In article
> <c820d5c3-6875-4ca0-825a-c93b96481...(a)z16g2000prn.googlegroups.com>,
>
>
>
> Chad <cdal...(a)gmail.com> wrote:
> > On Jun 13, 8:12 pm, Barry Margolin <bar...(a)alum.mit.edu> wrote:
> > > In article
> > > <91c0237b-9c76-47ca-ba83-0d7d391fd...(a)s21g2000prm.googlegroups.com>,
>
> > > Chad <cdal...(a)gmail.com> wrote:
> > > > Given the following
>
> > > > m-net% more step.c
> > > > #include <sys/types.h>
> > > > #include <sys/uio.h>
> > > > #include <unistd.h>
>
> > > > #include <stdio.h>
>
> > > > int main(void)
> > > > {
> > > > int n;
>
> > > > while ((n = read(0, buf, BUFSIZ)) > 0 )
> > > > write(1, buf, n);
> > > > return 0;
> > > > }
> > > > m-net% gcc -g -Wall step.c -o step
> > > > m-net% ./step
> > > > a string
> > > > a string
>
> > > > How does the zsh shell know that 'a string' is in fact a string?
>
> > > What makes you think it does? Your program is simply echoing back what
> > > you typed, and the shell isn't involved at all, except to start up your
> > > program.
>
> > Well if something wouldn't have terminated 'a string', wouldn't I just
> > have gotten a bunch of garbage after the letter 'g'?
>
> No. You told it to write only n characters, where n is the number of
> characters you read from stdin. Had you written:
>
> write(1, buf, BUFSIZ);
>
> then you probably would have gotten lots of garbage.
>
> BTW, how did you get this to compile without a declaration of buf?
>
> --
> Barry Margolin, bar...(a)alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

I don't know how, but I somehow manage to screw up the copy and paste.
Here is the original code

m-net% more steph.c
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

#include <stdio.h>

int main(void)
{
char buf[BUFSIZ];
int n;

while ((n = read(0, buf, BUFSIZ)) > 0 )
write(1, buf, n);
return 0;
}