Hi Martijn,

Martijn van Duren wrote on Sun, Jan 09, 2022 at 08:09:20AM +0100:

> I fully agree with your reasoning and also prefer this one over the
> previous two diff.
> 
> OK martijn@

Thanks for checking; committed.
Also thanks to cheloha@ for his work on this issue
and to milllert@ for his feedback.
  Ingo


> On Sun, 2022-01-09 at 00:45 +0100, Ingo Schwarze wrote:

>> Index: rev.c
>> ===================================================================
>> RCS file: /cvs/src/usr.bin/rev/rev.c,v
>> retrieving revision 1.13
>> diff -u -p -r1.13 rev.c
>> --- rev.c    10 Apr 2016 17:06:52 -0000      1.13
>> +++ rev.c    8 Jan 2022 23:19:46 -0000
>> @@ -46,13 +46,14 @@ void usage(void);
>>  int
>>  main(int argc, char *argv[])
>>  {
>> -    char *filename, *p = NULL, *t, *u;
>> +    char *filename, *p = NULL, *t, *te, *u;
>>      FILE *fp;
>>      ssize_t len;
>>      size_t ps = 0;
>> -    int ch, rval;
>> +    int ch, multibyte, rval;
>>  
>>      setlocale(LC_CTYPE, "");
>> +    multibyte = MB_CUR_MAX > 1;
>>  
>>      if (pledge("stdio rpath", NULL) == -1)
>>              err(1, "pledge");
>> @@ -83,14 +84,16 @@ main(int argc, char *argv[])
>>                      if (p[len - 1] == '\n')
>>                              --len;
>>                      for (t = p + len - 1; t >= p; --t) {
>> -                            if (isu8cont(*t))
>> -                                    continue;
>> -                            u = t;
>> -                            do {
>> -                                    putchar(*u);
>> -                            } while (isu8cont(*(++u)));
>> +                            te = t;
>> +                            if (multibyte)
>> +                                    while (t > p && isu8cont(*t))
>> +                                            --t;
>> +                            for (u = t; u <= te; ++u)
>> +                                    if (putchar(*u) == EOF)
>> +                                            err(1, "stdout");
>>                      }
>> -                    putchar('\n');
>> +                    if (putchar('\n') == EOF)
>> +                            err(1, "stdout");
>>              }
>>              if (ferror(fp)) {
>>                      warn("%s", filename);
>> @@ -104,7 +107,7 @@ main(int argc, char *argv[])
>>  int
>>  isu8cont(unsigned char c)
>>  {
>> -    return MB_CUR_MAX > 1 && (c & (0x80 | 0x40)) == 0x80;
>> +    return (c & (0x80 | 0x40)) == 0x80;
>>  }
>>  
>>  void

Reply via email to