[MacPorts] #69571: Add stpncpy to legacy support

MacPorts noreply at macports.org
Thu Apr 4 19:21:08 UTC 2024


#69571: Add stpncpy to legacy support
-----------------------------+----------------------
  Reporter:  ryandesign      |      Owner:  mascguy
      Type:  enhancement     |     Status:  assigned
  Priority:  Normal          |  Milestone:
 Component:  ports           |    Version:
Resolution:                  |   Keywords:
      Port:  legacy-support  |
-----------------------------+----------------------

Comment (by mascguy):

 Given the relatively simple logic involved, I was expecting
 implementations of `stpncpy` to be reasonably similar. But just two
 variations alone - one originating from `gcc`, the other from `libc` -
 tell a different tale:

 GCC - via
 [https://opensource.apple.com/source/gcc/gcc-5465/libiberty/stpncpy.c.auto.html
 opensource.apple.com/source/gcc/gcc-5465/libiberty/stpncpy.c.auto.html]:

 {{{
 char *stpncpy(char *dst, const char *src, size_t len) {
   size_t n = strlen(src);
   if (n > len)
     n = len;
   return strncpy(dst, src, len) + n;
 }
 }}}

 LIBC - via
 [https://opensource.apple.com/source/Libc/Libc-825.26/string/stpncpy.c.auto.html
 opensource.apple.com/source/Libc/Libc-825.26/string/stpncpy.c.auto.html]:

 {{{
 char *stpncpy(char * restrict dst, const char * restrict src, size_t
 maxlen) {
     const size_t srclen = strnlen(src, maxlen);
     if (srclen < maxlen) {
         //  The stpncpy() and strncpy() functions copy at most maxlen
         //  characters from src into dst.
         memcpy(dst, src, srclen);
         //  If src is less than maxlen characters long, the remainder
         //  of dst is filled with '\0' characters.
         memset(dst+srclen, 0, maxlen-srclen);
         //  The stpcpy() and stpncpy() functions return a pointer to the
         //  terminating '\0' character of dst.
         return dst+srclen;
     } else {
         //  The stpncpy() and strncpy() functions copy at most maxlen
         //  characters from src into dst.
         memcpy(dst, src, maxlen);
         //  If stpncpy() does not terminate dst with a NUL character, it
         //  instead returns a pointer to src[maxlen] (which does not
         //  necessarily refer to a valid memory location.)
         return dst+maxlen;
     }
 }
 }}}

 And a 3rd, from [https://lore.kernel.org/dash/6cacf608-326e-211a-
 7d37-164041710980 at knaff.lu/]:

 {{{
 char *stpncpy(char *dst, const char *src, int len) {
     int i,nullSeen=0;
     char *ret=dst+len;
     for(i=0; i<len; i++) {
         if(nullSeen)
             dst[i] = '\0';
         else {
             dst[i] = src[i];
             if(dst[i] == '\0') {
                 nullSeen = 1;
                 ret = dst+i;
             }
         }
     }
     return ret;
 }
 }}}

 So we apparently have an embarrassment of riches...

-- 
Ticket URL: <https://trac.macports.org/ticket/69571#comment:2>
MacPorts <https://www.macports.org/>
Ports system for macOS


More information about the macports-tickets mailing list