home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / GETLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  911 b   |  51 lines

  1. /*
  2.  * getline.c
  3.  * contains: getline()
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "gfuncts.h"
  9.  
  10. /*
  11.  *  int
  12.  * getline(str,max)
  13.  *
  14.  * ARGUMENT
  15.  *  (char *) str    =    Destination string
  16.  *  (int)    max    =    Maximum number of characters to transfer
  17.  *
  18.  * DESCRIPTION
  19.  *   Get a line of text from stdin.
  20.  *
  21.  *   Text is copied from stdin to the destination string until:
  22.  *    1.  a newline is encountered, or
  23.  *    2.  a NULL character is input.
  24.  *   In any event the output string is terminated with '\0'.
  25.  *
  26.  * RETURNS
  27.  *  Integer length of string.
  28.  *
  29.  * AUTHOR
  30.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  31.  */
  32. int GF_CONV getline(str,imax)
  33. char *str;
  34. int imax;
  35. {
  36.     int i;
  37.     char c;
  38.  
  39.     i=0;
  40.     while(--imax&&(c=(char)getchar())!=EOF&&c!='\n'&&c!='\0') {
  41.         *str++=c;
  42.         i++;
  43.     }
  44.     if(c=='\n'||c=='\0') {
  45.         *str++ = '\n';
  46.         *str= '\0';
  47.         return i;
  48.     }
  49.     return i;
  50. }
  51.