home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / STRINGS.ZIP / LEFT.C < prev    next >
Text File  |  1993-01-14  |  939b  |  38 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4.  
  5. /***
  6.  *  Function    :  strleft
  7.  *
  8.  *  Description :  Copy the first ... characters of a string.
  9.  *           Like strncpy but add a '\0' at the end of the output string.
  10.  *
  11.  *  Decisions   :  If given length > string length : normal strcpy
  12.  *           If given length <= 0 returns an empty string.
  13.  *
  14.  *  Parameters  :  out  char  *out_str     result
  15.  *                 in   char  *in_str      in string
  16.  *                 in   int   length       length to be copied
  17.  *
  18.  *  Return code :   pointer to result.
  19.  *
  20.  *  OS/Compiler :   All
  21.  */
  22.  
  23. char *strleft( char *out_str, const char *in_str, int length )
  24.  
  25. { char *ptr = out_str;
  26.  
  27.   if ( length <= 0 ) { *out_str = '\0';
  28.                    return out_str;
  29.              }
  30.  
  31.   while ( (*out_str++ = *in_str++) && -- length );
  32.  
  33.   if ( ! length ) *out_str = '\0';
  34.  
  35.   return ptr;
  36. }
  37.  
  38.