Re: [dwm] input status text bug?

From: Ritesh Kumar <ritesh_AT_cs.unc.edu>
Date: Wed, 31 Oct 2007 14:48:19 -0400

On 10/31/07, Ritesh Kumar <ritesh_AT_cs.unc.edu> wrote:
>
> On 10/31/07, Anselm R. Garbe <arg_AT_suckless.org> wrote:
> >
> > Hi Ritesh,
>
>
> <snip>
>
>
> > Well let's have a look at the relevant bits in the code:
> >
> > (a) switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
> > [...]
> > default:
> > (b) for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >=
> > stext && *p == '\n'; *p-- = '\0');
> > (c) for(; p >= stext && *p != '\n'; --p);
> > (d) if(p > stext)
> > (e) strncpy(stext, p + 1, sizeof stext);
> > [...]
> >
> > (a) Reads the next bunch of stdin, beginning at stext[0] and
> > ending at stext[r].
> >
> > (b) 0-terminates stext, and trims rightmost '\n' from right to left
> > only.
> >
> > (c) If p still does not point to stext, it now seeks for a '\n'
> > character leftwards.
> >
> > (d) If during (c) such a '\n' character was found, it is the
> > begin of the last line which has been read.
> >
> > (e) Hence, we copy the last line read only to stext, beginning
> > at the '\n' character + 1 to stext. It is 0-terminated already.
> >
> > So in my eyes, assumed the complete output of the input script
> > is read, the code is correct and does what it is supposed
> > to do: it only displays the last line read from stdin (this might
> > be a bunch of data which is not '\n'-terminated yet during this read
> > round of course).
>
>
> The problem is that select will notify dwm the moment *anything* is
> available on stdin... and that could be partial data.
> There is one semantic difference between the code snippets. While the
> original one retains the last line of given multiline input, the suggested
> snippet shows the first line. However, its hard to think of a situation
> where somebody would pour in multiline text (all in a small amount of time)
> for the status as dwm would not be able to show all of it anyways regardless
> of the semantics.
>
> If you like to debug, you could add an
> >
> > fprintf(stderr, "'stext == %s'\n", stext);
> >
> > in there between (c) and (d). Then we can make sure that your
> > guess is correct.
> >
> > If the input is not read during one read call as you believe,
> > then at least there must be some flashing, because dwm will
> > actually render the <some text> at the status bar, but maybe
> > your system is really fast and you don't notice this.
>
>
> I will do this and post the results.
> You are right about my system... its really fast :). Actually, I did see a
> flash but it was merely a flicker and happened only sometimes.
>
> _r
>
> Okay, I tried the debug: the output with the previous code is
inputtext == <sometext>
inputtext == Wed Oct 31 14:46:32 EDT 2007
inputtext == <sometext>
inputtext == Wed Oct 31 14:46:33 EDT 2007
inputtext == <sometext>
inputtext == Wed Oct 31 14:46:34 EDT 2007
inputtext == <sometext>
inputtext == Wed Oct 31 14:46:35 EDT 2007
inputtext == <sometext>
inputtext == Wed Oct 31 14:46:36 EDT 2007

So, read() is really not reading all of the 'intended text to be read' in
one go.

_r
Received on Wed Oct 31 2007 - 19:48:22 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:03:56 UTC