News for hackers. Stuff that matters.
Re:The Sad State of C Strings by anon4 on Wednesday February 10, 2016 @04:29AM (#11071600)
Eh...

  // This code is licensed under CC0.
  // A copy of the license can be obtained at https://creativecommons.org/publicdomain/zero/1.0/
  // May you forever catenate in peace.
  #define strcatm(...) strcat_multi(__VA_ARGS__, NULL);
  char* strcat_multi(char *dest, ...) {
    va_list srcs;
    char *dest_end;
    const char *src;
    size_t src_sz;
    va_start(srcs, dest);
    for (dest_end = dest; *dest_end; dest_end++);
    for (src = va_arg(srcs, const char *); src; src = va_arg(srcs, const char *)) {
      src_sz = strlen(src);
      memmove(dest_end, src, src_sz);
      dest_end += src_sz;
      *dest_end = '\0';
    }
    va_end(srcs);
    return dest_end;
  }
There. Now go and catenate, children.
Re:The Sad State of C Strings by efaref on Wednesday February 10, 2016 @06:23AM (#11071975)

  // This code is licensed under CC0.
  // A copy of the license can be obtained at https://creativecommons.org/publicdomain/zero/1.0/
  // May you forever catenate in peace.
  #define strncatm(dest, sz, ...) strncat_multi((dest), (sz), __VA_ARGS__, NULL);
  char* strncat_multi(char *dest, size_t dest_sz, ...) {
    va_list srcs;
    char *dest_end;
    const char *src;
    size_t src_sz;
    size_t dest_off;
    size_t copy_sz;
    va_start(srcs, dest_sz);
    for (dest_end = dest, dest_off = 0; *dest_end && dest_off < dest_sz; dest_end++, dest_sz++);
    for (src = va_arg(srcs, const char *); src && dest_off < dest_sz; src = va_arg(srcs, const char *)) {
      src_sz = strlen(src);
      copy_sz = src_sz < (dest_sz - dest_off) ? src_sz : (dest_sz - dest_off);
      memmove(dest_end, src, copy_sz);
      dest_end += copy_sz;
      dest_off += copy_sz;
      if (dest_off < dest_sz)
      {
        *dest_end = '\0';
      }
    }
    va_end(srcs);
    return dest_end;
  }
Re:The Sad State of C Strings by anon4 on Wednesday February 10, 2016 @06:58AM (#11072151)
Good, but I'd do one modification -- always write a null byte at the end. Otherwise you end up with a non-null-terminated string and that's bad.

So,

  copy_sz = src_sz < (dest_sz - dest_off) ? src_sz : (dest_sz - dest_off - 1);
  ...
  *dest_end = '\0';
Re:The Sad State of C Strings by unwind on Wednesday February 10, 2016 @07:39AM (#11072313)
But remember to rename the function, since all function names starting with "str" are reserved in C.

To keep with the spirit of the article, calling strlen() just to allow convenient use of memmove() seems a bit counter-intuitive, I'd roll the two together into a copying loop instead.

Re:The Sad State of C Strings by anon4 on Wednesday February 10, 2016 @08:28AM (#11072545)
And then you do infinite copying if you're catenating a string with itself.
Re:The Sad State of C Strings by Kristine1975 on Wednesday February 10, 2016 @04:40AM (#11071623)
I'll concatenate the hell out of the buffer and whatever comes after it.
Re:The Sad State of C Strings by anon4 on Wednesday February 10, 2016 @04:32AM (#11071605)
As a bonus, this works with e.g.

  char buf[256]; buf[0] = 'x'; buf[1] = '\0';
  strcatm(buf, buf, buf, buf);
producing a string of 16 'x's
Re:The Sad State of C Strings by huhtenberg on Wednesday February 10, 2016 @05:05AM (#11071689)

  strcatm(..., NULL);
and it's 8 x/s, not 16. Also fix , to ; in the second for loop.
Re:The Sad State of C Strings by anon4 on Wednesday February 10, 2016 @05:16AM (#11071721)
strcatm is the convenience macro that adds a last NULL arg. And it's 16, because it doubles each time. Already fixed.
Re:The Sad State of C Strings by huhtenberg on Wednesday February 10, 2016 @07:13AM (#11072203)
strcatm - aye, missed that, but I must insist on 8.
Re:The Sad State of C Strings by anon4 on Wednesday February 10, 2016 @08:28AM (#11072548)
Right, 8. I miscounted it as 4 loops :)