ambiguity in program output

1 #include <fcntl.h>
2
3 main(int argc, char argv[])
4 {
5 char buf[1];
6 int fd, count = 0;
7
8 if (argc > 1)
9 fd = open(argv[1], O_RDONLY);
10 else
11 fd = 0; /
Use standard input */
12
13 while (read(fd, buf, 1) > 0) {
14 if (count < 5) write(1, buf, 1);
15 if (buf[0] == '\n') count++;
16 }
17 }

Here buf can store only one character.
But if i run it as:-

"prog_name /etc/passwd" how does it give first five lines.

Sorry for bothering anyone if read it.
Got the answer.

read() function reads an entire line from input, i was thinking it of as a character.

Thanks
bishweshwar

Please use CODE tags :slight_smile: ,

its quite difficult to read through the code without them.

It seems that the code is pasted from editor with line numbers set

man fgets.