home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / hdiff / stripnl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  1.7 KB  |  69 lines

  1. /*
  2.  * f=stripnl.c
  3.  * author - dennis bednar 8 31 84
  4.  *
  5.  * this routine is designed to work in conjunction with fgets ().
  6.  * In this case, the L_BUFOVER error should never be returned, but
  7.  * the check is there anyway!
  8.  *
  9.  * strip new lines by converting the newline in the string to a null
  10.  * returns
  11.  *    L_BUFOVER = buffer is overfilled.  For example if bufsize = 10, then
  12.  *        the string len could be at most 9 (so the NULL fits)
  13.  *    L_TOOLONG = no newline was found (line too long) and buffer was filled.
  14.  *        For a bufsize of 10, buffer[9] is the last char, and it's
  15.  *        not the expected newline.
  16.  *    L_BADFORM = no newline was found and buffer not filled, so bad format
  17.  *    L_SUCCESS = success
  18.  */
  19.  
  20. #include "stripnl.h"
  21.  
  22. stripnl (buffer, bufsize)
  23.     char    *buffer;
  24.     int    bufsize;
  25. {
  26.     char    *cp;
  27.     int    len;    /* number of chars in string */
  28.  
  29.  
  30.     /* get string length */
  31.     len = strlen (buffer);
  32.  
  33.     /* make sure it's not already overrun */
  34.     if (len >= bufsize)
  35.         return (L_BUFOVER);
  36.  
  37.     /* if "" is passed, len is zero, and so 'last char' doesn't exist */
  38.     if (len <= 0)
  39.         return (L_BADFORM);
  40.  
  41.     /* now the string definitely fits in the buffer */
  42.  
  43.     /* get pointer to last char in string */
  44.     cp = &buffer [len - 1];
  45.  
  46.     /* if the last char of the string is a newline, change it to a NULL */
  47.     if (*cp == '\n')
  48.         {
  49.         *cp = '\0';
  50.         return L_SUCCESS;
  51.         }
  52.  
  53.     /* now the string fits in the buffer, and the last char != '\n' */
  54.  
  55.     /* line too long if the string length is the buffer size minus
  56.      * one for the null.
  57.      */
  58.     else if (len >= bufsize-1)
  59.         return L_TOOLONG;
  60.  
  61.     /* badly formatted if the line fits in the buffer, but
  62.      * contains no newline at the end.
  63.      * This happens on a file which is corrupted by, say,
  64.      * a transmission error.
  65.      */
  66.     else
  67.         return L_BADFORM;
  68. }
  69.