tags:

views:

436

answers:

8

What are some alternatives to the Microsoft security enhanced functions such as strncpy_s or _itoa_s? Although developing in MS environment the goal is to write code that could be ported easily to other platforms.

+1  A: 

Just use strncpy and disable the warning in VC++.

Nemanja Trifunovic
+3  A: 
std::string s(Cstring, n);
Mykola Golubyev
A: 

As far as I understand, strncpy_s has no complex logic: just takes minimum of string length, source and destination data sizes and copies resulting amount of chars into destination. If you need it so much, why not to implement it yourself?

Rageous
+1  A: 

Rageous is correct, there is no complex logic behind it.

I would just use Microsoft's version for now and if you decide to port to another OS later, THEN implement it yourself for the target platform and use preprocessor commands to specify your implementation on the non-Windows platform(s).

17 of 26
A: 

You could either provide an own implementation for platforms that don't have this function. Or use a macro to reduce the arguments by one and map to the standard string function.

Joey
+2  A: 

Try strlcpy. Though not standard C, it exists on a number of platforms and you can download implementations as the source code is available free of charge. See http://www.gratisoft.us/todd/papers/strlcpy.html

MeThinks
A: 

You may be interested in the Apache Portable Runtime project:

The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations.

autoconf can also take care of some of these platform-specific differences (by setting #defines, etc), although my experience with it under Windows leaves something to be desired.

leander
+1  A: 

If you really want to program in C:

Use the plain old standard strncpy.

** If you're programming in C++: **

Use the plain old standard string class std::string.

(hint: You probably want the latter. C strings are just bugs waiting to happen, even if you use the "secure" *_s functions. C++ added a string class for a reason)

jalf