Re: Why can't lseek the STDIN_FILENO?

From: wen-yi <wen-yi(at)qq(dot)com>
To:
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Why can't lseek the STDIN_FILENO?
Date: 2023-06-24 13:36:07
Message-ID: tencent_8D647B9C8A5BB3749FCB6E1DD22A96A90406@qq.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

------------------&nbsp;Original&nbsp;------------------
From: "John McKown" <john(dot)archie(dot)mckown(at)gmail(dot)com&gt;;
Date:&nbsp;Sat, Jun 24, 2023 11:02 AM
To:&nbsp;
Cc:&nbsp;"pgsql-general"<pgsql-general(at)lists(dot)postgresql(dot)org&gt;;
Subject:&nbsp;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.&nbsp;

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.&nbsp;

This is probably a better explanation:&nbsp;https://unix.stackexchange.com/questions/502518/problems-when-test-whether-standard-input-is-capable-of-seeking

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(at)qq(dot)com&gt; wrote:

Hi community,
I am testing the lseek &amp; write &amp; read, and I write the code like this:

/*
&nbsp;&nbsp; &nbsp;lseek_test.c
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Test the lseek
&nbsp;&nbsp; &nbsp;Wen Yi
*/
#include <unistd.h&gt;
#include <fcntl.h&gt;
int main()
{
&nbsp;&nbsp; &nbsp;int fd = 0;
&nbsp;&nbsp; &nbsp;char buffer[16] = {};
&nbsp;&nbsp; &nbsp;write(STDIN_FILENO, "Hello world\n", sizeof("Hello world\n"));
&nbsp;&nbsp; &nbsp;lseek(STDIN_FILENO, 0, SEEK_SET);
&nbsp;&nbsp; &nbsp;read(STDIN_FILENO, buffer, sizeof(buffer));
&nbsp;&nbsp; &nbsp;write(STDIN_FILENO, buffer, sizeof(buffer));
&nbsp;&nbsp; &nbsp;return 0;
}

And I run the program ("Something Input" is my input content)

[beginnerc(at)bogon 学习 C语言]$ gcc lseek_test.c
[beginnerc(at)bogon 学习 C语言]$ ./a.out
Hello world
Something Input
Something Input
[beginnerc(at)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message wen-yi 2023-06-24 13:36:13 A question about the postgres's website
Previous Message Christoph Moench-Tegeder 2023-06-24 12:56:39 Re: Accessing referential constraint information with minimal permissions