Professional Web Applications Themes

string concatenation - UNIX Programming

James Antill <org> writes:     >> >> Please cite the specific differences between the implementations; I don't >> know of any.[/ref][/ref]     For the bogus "size = 0" case; you can't have a string that takes 0 bytes of storage. I don't consider that a serious impediment to portable programming because the strlcat with a size == 0 argument is something that I do not expect to happen. Any call of "strlcat(dst, src, len)" where strlen(dst) >= len is suspect and I would call any results in that domain "unspecified" and certainly not any obstacle to using the ...

  1. #21

    Default Re: string concatenation

    James Antill <org> writes:
     
     
    >>
    >> Please cite the specific differences between the implementations; I don't
    >> know of any.[/ref][/ref]
     
     

    For the bogus "size = 0" case; you can't have a string that takes 0 bytes
    of storage. I don't consider that a serious impediment to portable
    programming because the strlcat with a size == 0 argument is something that
    I do not expect to happen.

    Any call of "strlcat(dst, src, len)" where strlen(dst) >= len is suspect
    and I would call any results in that domain "unspecified" and certainly not
    any obstacle to using the functions in portable programs. Certainly not
    sufficient to give this fact that much attention.

    Come to think of it, abort() is possibly the better way to handle this.

    Strange that the Solaris source does reference beyond the "len" in dest
    because I distinctly remember telling Theo and Todd about the OpenBSD source
    doing the same when we were about to implement it in Solaris.

    you can find this fact in the CVS commit history at:

    http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/strlcat.c

    rev 1.2

    A case of "do as I say, not do as I do", I guess; I see about
    getting us to follow suit.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    Casper Guest

  2. #22

    Default Re: string concatenation

    Richard Bos wrote:
     
    >
    > Not in ISO C, they haven't. Maybe in POSIX.
    >
    > Richard[/ref]

    I was introduced to them by someone who worked on *BSD platforms quite a
    bit, and ended up writing equivalents of my own. This being a boring
    afternoon, here's my implementation of strlcpy and strlcat. Comments most
    likely lifted from *BSD sources as I am always too lazy to write them
    myself. As everyone knows, my code *never* has any bugs :-)

    strlcpy.c:
    #include <stddef.h>

    /*
    * Copies src to string dst of size sz. sz is the full size of dst
    * including the NUL character. At most sz-1 characters will be
    * copied. Always NUL terminates dst, and doesn't fill dst with NULs
    * like strncpy when sz > strlen(src). src *MUST* be a valid NUL
    * terminated string.
    *
    * Returns strlen(src).
    *
    * If retval >= sz, truncation occurred.
    */
    size_t strlcpy(char *dst, const char *src, size_t sz) {
    register char *d = dst;
    register const char *s = src;
    register size_t n = sz;

    if ( n ) {
    --n;
    while ( n && *s ) *d++ = *s++, --n;
    *d = 0;
    }

    while ( *s++ );

    return s - src - 1;
    }

    strlcat.c:

    #include <stddef.h>

    /*
    * Appends src to string dst of size sz (unlike strncat, sz is the
    * full size of dst, not space left). At most sz-1 characters will be
    * copied. Always NUL terminates (unless sz <= strlen(dst)).
    *
    * Returns strlen(src) + MIN(sz, strlen(initial dst)).
    *
    * If retval >= sz, truncation occurred.
    */
    size_t strlcat(char *dst, const char *src, size_t sz) {
    register char *d = dst;
    register const char *s = src;
    register size_t n = sz;

    if ( n ) {
    --n;
    while ( n && *d ) ++d, --n;
    if ( n ) {
    while ( n && *s ) *d++ = *s++, --n;
    *d = 0;
    }
    n = d - dst + (*d != 0);
    }

    src = s;
    while ( *s++ ) ;

    return n + (s - src - 1);
    }

    -nrk.

    --
    Remove devnull for email
    nrk Guest

  3. #23

    Default Re: string concatenation


    James Antill <org> writes:
     
    >
    > Not only are they not in std. C, they aren't available at all on any
    > of the major Linux variants. They also are/were implemented subtly
    > differently on Solaris.
    >
    > You probably also want to read:
    >
    > http://www.and.org/vstr/security.html#alloc[/ref]

    An excellent summary of the reasons why strlcpy/cat are valuable.
    I don't think availability is much of a problem either, since the
    OpenBSD implementations are:

    - Free as in beer
    - Free as in speech
    - A whopping 30 lines of code apiece.

    -SEan












    Sean Guest

Page 2 of 2 FirstFirst 12

Similar Threads

  1. string concatenation
    By Rinoa in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 22nd, 01:05 PM
  2. String concatenation qn
    By Wolf Blaum in forum PERL Beginners
    Replies: 4
    Last Post: January 26th, 12:21 PM
  3. Replies: 5
    Last Post: September 7th, 11:02 PM
  4. String Concatenation Bug
    By ecow in forum PHP Development
    Replies: 1
    Last Post: August 18th, 02:14 AM
  5. String concatenation with .= <FH>
    By Jay Tilton in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 13th, 01:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139