home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cpio11as.zip / DSTRING.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  3KB  |  109 lines

  1. /* dstring.c - The dynamic string handling routines used by cpio.
  2.    Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.
  26.  
  27.    $Header: e:/gnu/cpio/RCS/dstring.c 1.1.0.2 90/09/23 23:11:06 tho Exp $
  28.  */
  29.  
  30. #include <stdio.h>
  31. #ifdef USG
  32. #include <string.h>
  33. #else
  34. #include <strings.h>
  35. #endif
  36. #include "dstring.h"
  37.  
  38. #ifdef MSDOS
  39. extern char *xmalloc (unsigned int size);
  40. extern char *xrealloc (char *ptr, unsigned int size);
  41. #else /* not MSDOS */
  42. char *xmalloc ();
  43. char *xrealloc ();
  44. #endif /* not MSDOS */
  45.  
  46. /* Initialiaze dynamic string STRING with space for SIZE characters.  */
  47.  
  48. void
  49. ds_init (string, size)
  50.      dynamic_string *string;
  51.      int size;
  52. {
  53.   string->ds_length = size;
  54.   string->ds_string = (char *) xmalloc (size);
  55. }
  56.  
  57. /* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
  58.  
  59. void
  60. ds_resize (string, size)
  61.      dynamic_string *string;
  62.      int size;
  63. {
  64.   if (size > string->ds_length)
  65.     {
  66.       string->ds_length = size;
  67.       string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
  68.     }
  69. }
  70.  
  71. /* Dynamic string S gets a string from file F.  S will increase
  72.    in size during the function if the string from F is longer than
  73.    the current size of S.
  74.    Return NULL if end of file is detected.  Otherwise,
  75.    Return a pointer to the null-terminated string in S.  */
  76.  
  77. char *
  78. ds_fgets (f, s)
  79.      FILE *f;
  80.      dynamic_string *s;
  81. {
  82.   int insize;            /* Amount needed for line. */
  83.   int strsize;            /* Amount allocated for S. */
  84.   int next_ch;
  85.  
  86.   /* Initialize.  */
  87.   insize = 0;
  88.   strsize = s->ds_length;
  89.  
  90.   /* Read the input string.  */
  91.   next_ch = getc (f);
  92.   while (next_ch != '\n' && next_ch != EOF)
  93.     {
  94.       if (insize >= strsize - 1)
  95.     {
  96.       ds_resize (s, strsize * 2 + 2);
  97.       strsize = s->ds_length;
  98.     }
  99.       s->ds_string[insize++] = next_ch;
  100.       next_ch = getc (f);
  101.     }
  102.   s->ds_string[insize++] = '\0';
  103.  
  104.   if (insize == 1 && next_ch == EOF)
  105.     return NULL;
  106.   else
  107.     return s->ds_string;
  108. }
  109.