pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedWhy can't lseek the STDIN_FILENO?
4+ messages / 4 participants
[nested] [flat]
* Why can't lseek the STDIN_FILENO?
@ 2023-06-24 02:16 Wen Yi <896634148@qq.com>
2023-06-24 02:40 ` Re: Why can't lseek the STDIN_FILENO? Tom Lane <tgl@sss.pgh.pa.us>
2023-06-24 03:02 ` Re: Why can't lseek the STDIN_FILENO? John McKown <john.archie.mckown@gmail.com>
0 siblings, 2 replies; 4+ messages in thread
From: Wen Yi @ 2023-06-24 02:16 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
Hi community,
I am testing the lseek & write & read, and I write the code like this:
/*
lseek_test.c
Test the lseek
Wen Yi
*/
#include <unistd.h>
#include <fcntl.h>
int main()
{
int fd = 0;
char buffer[16] = {};
write(STDIN_FILENO, "Hello world\n", sizeof("Hello world\n"));
lseek(STDIN_FILENO, 0, SEEK_SET);
read(STDIN_FILENO, buffer, sizeof(buffer));
write(STDIN_FILENO, buffer, sizeof(buffer));
return 0;
}
And I run the program ("Something Input" is my input content)
[beginnerc@bogon 学习 C语言]$ gcc lseek_test.c
[beginnerc@bogon 学习 C语言]$ ./a.out
Hello world
Something Input
Something Input
[beginnerc@bogon 学习 C语言]$
I really don't know, why the buffer's content not be "Hello world\n"? (I use the lseek to move the cursor to the beginning region)
Can someone give me some advice?
Thanks in advance!
Yours,
Wen Yi
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Why can't lseek the STDIN_FILENO?
2023-06-24 02:16 Why can't lseek the STDIN_FILENO? Wen Yi <896634148@qq.com>
@ 2023-06-24 02:40 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 4+ messages in thread
From: Tom Lane @ 2023-06-24 02:40 UTC (permalink / raw)
To: Wen Yi <896634148@qq.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
"=?gb18030?B?V2VuIFlp?=" <896634148@qq.com> writes:
> lseek(STDIN_FILENO, 0, SEEK_SET);
If you are not checking for failure return from a system call,
you are in a state of sin.
> I really don't know, why the buffer's content not be "Hello world\n"?
Probably because a tty input device is not seekable.
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Why can't lseek the STDIN_FILENO?
2023-06-24 02:16 Why can't lseek the STDIN_FILENO? Wen Yi <896634148@qq.com>
@ 2023-06-24 03:02 ` John McKown <john.archie.mckown@gmail.com>
2023-06-24 13:36 ` Re: Why can't lseek the STDIN_FILENO? wen-yi <wen-yi@qq.com>
1 sibling, 1 reply; 4+ messages in thread
From: John McKown @ 2023-06-24 03:02 UTC (permalink / raw)
To: ; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
My best advice would be to ask a C language question on a C language forum.
This forum is really only for questions about the SQL language for the
PostgreSQL database. I.e. no MariaDB, MySQL, MS SQL questions.
First, you didn't say what OS and she'll you're using. I an guessing BASH
and Linux.
Second, you did NO error checking. I would purely guess that the lseek() is
getting a return value of -1, probably with an error of ESPIPE.
This is probably a better explanation:
https://unix.stackexchange.com/questions/502518/problems-when-test-whether-standard-input-is-capable...
The bottom line from the above post is that STDIN is not seekable when it
is a terminal.
On Fri, Jun 23, 2023, 21:17 Wen Yi <896634148@qq.com> wrote:
> Hi community,
> I am testing the lseek & write & read, and I write the code like this:
>
> /*
> lseek_test.c
> Test the lseek
> Wen Yi
> */
> #include <unistd.h>
> #include <fcntl.h>
> int main()
> {
> int fd = 0;
> char buffer[16] = {};
> write(STDIN_FILENO, "Hello world\n", sizeof("Hello world\n"));
> lseek(STDIN_FILENO, 0, SEEK_SET);
> read(STDIN_FILENO, buffer, sizeof(buffer));
> write(STDIN_FILENO, buffer, sizeof(buffer));
> return 0;
> }
>
> And I run the program ("Something Input" is my input content)
>
> [beginnerc@bogon 学习 C语言]$ gcc lseek_test.c
> [beginnerc@bogon 学习 C语言]$ ./a.out
> Hello world
> Something Input
> Something Input
> [beginnerc@bogon 学习 C语言]$
>
> I really don't know, why the buffer's content not be "Hello world\n"? (I
> use the lseek to move the cursor to the beginning region)
>
> Can someone give me some advice?
> Thanks in advance!
>
> Yours,
> Wen Yi
>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Why can't lseek the STDIN_FILENO?
2023-06-24 02:16 Why can't lseek the STDIN_FILENO? Wen Yi <896634148@qq.com>
2023-06-24 03:02 ` Re: Why can't lseek the STDIN_FILENO? John McKown <john.archie.mckown@gmail.com>
@ 2023-06-24 13:36 ` wen-yi <wen-yi@qq.com>
0 siblings, 0 replies; 4+ messages in thread
From: wen-yi @ 2023-06-24 13:36 UTC (permalink / raw)
To: ; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
OK, I see, I'm so sorry for my action.
I will ask in cpplang slack community in the future.
And really thanks for your advice.
Your,
Wen Yi
------------------ Original ------------------
From: "John McKown" <john.archie.mckown@gmail.com>;
Date: Sat, Jun 24, 2023 11:02 AM
To:
Cc: "pgsql-general"<pgsql-general@lists.postgresql.org>;
Subject: Re: Why can't lseek the STDIN_FILENO?
My best advice would be to ask a C language question on a C language forum. This forum is really only for questions about the SQL language for the PostgreSQL database. I.e. no MariaDB, MySQL, MS SQL questions.
First, you didn't say what OS and she'll you're using. I an guessing BASH and Linux.
Second, you did NO error checking. I would purely guess that the lseek() is getting a return value of -1, probably with an error of ESPIPE.
This is probably a better explanation: https://unix.stackexchange.com/questions/502518/problems-when-test-whether-standard-input-is-capable...
The bottom line from the above post is that STDIN is not seekable when it is a terminal.
On Fri, Jun 23, 2023, 21:17 Wen Yi <896634148@qq.com> wrote:
Hi community,
I am testing the lseek & write & read, and I write the code like this:
/*
lseek_test.c
Test the lseek
Wen Yi
*/
#include <unistd.h>
#include <fcntl.h>
int main()
{
int fd = 0;
char buffer[16] = {};
write(STDIN_FILENO, "Hello world\n", sizeof("Hello world\n"));
lseek(STDIN_FILENO, 0, SEEK_SET);
read(STDIN_FILENO, buffer, sizeof(buffer));
write(STDIN_FILENO, buffer, sizeof(buffer));
return 0;
}
And I run the program ("Something Input" is my input content)
[beginnerc@bogon 学习 C语言]$ gcc lseek_test.c
[beginnerc@bogon 学习 C语言]$ ./a.out
Hello world
Something Input
Something Input
[beginnerc@bogon 学习 C语言]$
I really don't know, why the buffer's content not be "Hello world\n"? (I use the lseek to move the cursor to the beginning region)
Can someone give me some advice?
Thanks in advance!
Yours,
Wen Yi
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2023-06-24 13:36 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-06-24 02:16 Why can't lseek the STDIN_FILENO? Wen Yi <896634148@qq.com>
2023-06-24 02:40 ` Tom Lane <tgl@sss.pgh.pa.us>
2023-06-24 03:02 ` John McKown <john.archie.mckown@gmail.com>
2023-06-24 13:36 ` wen-yi <wen-yi@qq.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox