Thread: Function name to wrap the standard strncpy function.

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    Function name to wrap the standard strncpy function.

    I need a function like strncpy; but, want to be sure the destination ends with a ASCII nul char.

    So, I wonder what name is used by others in this case.

    I have found strlcpy and it looks like a good name choice.
    http://en.wikibooks.org/wiki/C_Progr...andard/strlcpy

    Does anyone agree or disagree with another suggested function name?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As long as the name doesn't conflict with a standard name or a name in common use, use what ever you like. Assuming you intend to distribute this function the documentation will be more critical than whatever name you chose, IMO. Although, if distributing I would probably stay away from stlcpy() since I would consider it a name in common use, because of being used by several operating systems.

    Jim
    Last edited by jimblumberg; 11-05-2014 at 08:32 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Decided it is safest to use strncpy2; but, everything I search gets at least some Google results.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Most programmers will create a library of their own functions. Some of these are wrappers to Standard library functions. The preferred method is to choose two or three characters as a prefix to all your functions. This could be your initials, an organizations initials, etc...

    For example, if someone chose to use "xy" as the prefix, they would call their version of strncpy(), xystrncpy(), xy_strncpy(), or some form of Camel Case. By using this method, you avoid any conflicts with third party libraries you might use in the future. If they are smart, the third part library has used the same method of naming. It then becomes a simple form of Namespaces for C Programming.

    It is possible that strlcpy(), strlcat(), etc... might become part of the Standard Library in some future version of the C Standard.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not being an advocate of numbered versions of functions, I'd use something descriptive, like "terminated_strncpy()", nulled_strncpy(), or (for true lovers of verbosity) "null_terminated_strncpy()".

    The compiler is unlikely to really care. For those who resent typing, most modern IDE's will give you a selection of names to pick from that match what you have typed so far. So use the tools available to you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I too don't care for numbered functions in general. I've used strlcpy in the past, when I've had it available, and also defined my own strlcpy when it wasn't available. That was, of course, before I was familiar with the standard. You should be careful of your naming:
    Quote Originally Posted by C11 7.30.11
    7.30.11 String handling <string.h>
    1
    Function names that begin with str, mem, or wcs and a lowercase letter may be added
    to the declarations in the <string.h> header.
    Thus, it's safest to avoid creating any function that starts with str[a-z]. They could add strncpy2 or strlcpy to the standard library and if these function differ in behavior from your own implementation, it could be a giant pain in the rear. I think strncpy2 and strlcpy are unlikely candidates to be added, and if strlcpy is, it will likely behave as existing implementations already do. Still,

    A prefix would work great, as would some #define and #ifdef statements that only build your version if the implementation doesn't already provide strlcpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Simple Word Wrap Function
    By kabuki in forum C Programming
    Replies: 9
    Last Post: 10-22-2013, 07:57 PM
  2. Help with Word Wrap function
    By kabuki in forum C Programming
    Replies: 2
    Last Post: 10-15-2013, 09:51 AM
  3. Replies: 9
    Last Post: 06-11-2012, 03:45 AM
  4. Is there a standard function?
    By frktons in forum C Programming
    Replies: 223
    Last Post: 07-16-2010, 03:31 PM
  5. Is there a standard C function to do this?
    By lucidrave in forum C Programming
    Replies: 9
    Last Post: 08-14-2009, 11:21 PM