home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / hint / part01 / stov.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  2.4 KB  |  97 lines

  1. /* stov.c -- convert string to vector
  2.    Copyright (C) 1989 David MacKenzie
  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. #include "hint.h"
  19.  
  20. /* Number of arguments between realloc's. */
  21. #define GRANULARITY 10
  22.  
  23. /* Make an argv-like vector (string array) from string `s' (such as an
  24.    environment variable).  The last element of the vector is null.
  25.    Return a static structure with a malloc'd array of pointers into `s'.
  26.    Scatters nulls throughout `s'.  */
  27.  
  28. struct args *
  29. stov (s)
  30.      char *s;
  31. {
  32.   static struct args args;
  33.   int argc;
  34.   char **argv;
  35.  
  36.   argc = 0;
  37.   argv = (char **) xmalloc ((unsigned) (sizeof (char *) * GRANULARITY));
  38.  
  39.   for (s = strtok (s, " \t\n\r"); s; s = strtok ((char *) NULL, " \t\n\r"))
  40.     {
  41.       if (argc > 0 && argc % GRANULARITY == 0)
  42.     argv = (char **) xrealloc ((char *) argv,
  43.                (unsigned) (sizeof (char *) * (argc + GRANULARITY)));
  44.       argv[argc++] = s;
  45.     }
  46.   argv = (char **) xrealloc ((char *) argv,
  47.                  (unsigned) (sizeof (char *) * (argc + 1)));
  48.   argv[argc] = NULL;
  49.   args.argc = argc;
  50.   args.argv = argv;
  51.   return &args;
  52. }
  53.  
  54. static void
  55. memory_out ()
  56. {
  57.   fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  58.   exit (1);
  59. }
  60.  
  61. /* Allocate `n' bytes of memory dynamically, with error checking.  */
  62.  
  63. char *
  64. xmalloc (n)
  65.      unsigned n;
  66. {
  67.   char *p;
  68.  
  69.   p = malloc (n);
  70.   if (p == 0)
  71.     memory_out ();
  72.   return p;
  73. }
  74.  
  75. /* Change the size of an allocated block of memory `p' to `n' bytes,
  76.    with error checking.
  77.    If `p' is NULL, run xmalloc.
  78.    If `n' is 0, run free and return NULL.  */
  79.  
  80. char *
  81. xrealloc (p, n)
  82.      char *p;
  83.      unsigned n;
  84. {
  85.   if (p == 0)
  86.     return xmalloc (n);
  87.   if (n == 0)
  88.     {
  89.       free (p);
  90.       return 0;
  91.     }
  92.   p = realloc (p, n);
  93.   if (p == 0)
  94.     memory_out ();
  95.   return p;
  96. }
  97.