Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Weirdness in comm(1)



    Date:        Sat, 28 Nov 2009 19:52:47 +0100
    From:        Joerg Sonnenberger <joerg%britannica.bec.de@localhost>
    Message-ID:  <20091128185247.GC16437%britannica.bec.de@localhost>

  | getline behaves that way. The problem here is that comm needs to compare
  | the two lines and the newline is not part of the logical line.

I understand the issue...

And no, getline() doesn't behave the way I described (which is not a
fault of getline() - just not doing the same thing).

What I meant was to remove the \n but return the length that included
the \n (if it was there) - that is, assuming it used getline() as the
lower level function, I'd do (with error/EOF checking omitted for simmplicity):

        size_t
        next_line(buf (and the rest))
        {
                size_t n = getline(buf,...);
                if (buf[[n-1] == '\n')
                        buf[n-1] = 0;
                return n;
        }

Whether it uses getline(), fgets(), or while(getc()) as the
buffer filling function doesn't really matter for this

And then

        l1 = next_line(line1);
        l2 = next_line(line2);

        comp = compare(line1, line2);
        if (comp == 0)
                comp = l2 - l1;

        /* now comp < 0 ==> line1 < line2, comp > 0 ==> line2 > line2
           and comp == 0 ==> line1 == line2 */

That is, if it really is important that dealing with files containing
lines that don't contain the line terminating \n really is something worth
doing - I'm not sure it is, as I said last time, I'd just define the
behaviour in that case (and for files with embedded \0's, etc) as being
undefined - anything goes.

kre



Home | Main Index | Thread Index | Old Index