c - Creating a Wrapper for strncpy to Insert Terminating null -


I decided to create a cover for strncpy because I have a lot in my source code Need to do string copies and I want to make sure the string is finished if the source is equal to or equal to the destination.

This code will be used in production, so I want to see if there is any potential danger in using it.

I have never used a wrapper before, so I am trying to make it right.

Many thanks for any advice,

  / * null * / char * strncpy_wrapper (char * dest, const char * source, const size_t dest_size, const size_t source_size ) {Finish strings after fighting strncpy (dest, source, dest_size); / * * Compare different lengths, if the source is big * or end up with a zero equal to the destination. * / If (source_size> = dest_size) {dest [dest_size - 1] = '\ 0'; } Return destination; }  

==== Updated updates ====

  / * faucet * / char * strncpy_wrapper (Four * Destination, Const char * Source, const size_t dest_size) {strncpy (dest, source, dest_size); / * * If more than zero space ends with zero then * / If (dest_size> 0) {dest [dest_size - 1] = '\ 0'; } And {dest [0] = '\ 0'; / * Return to the empty string, if the destination is zero length, then return / return; Check  dest_size  before reaching the array  

'Will come in trouble:

  if (dest_size> 0) {dest [dest_size - 1] =' \ 0 '; }  

Actually, now I think about it, it's better to just die:

  if (dest_size == 0 ) {Fputs (stadder, "stencopy (x, y, 0)"); Exit (1); }  

Otherwise, you have a problem similar to the original strncpy () , that dest can not be terminated Dest_size 0.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -