home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libc / awksplit.c < prev    next >
C/C++ Source or Header  |  1992-10-21  |  899b  |  36 lines

  1. /*
  2.  * awksplit - awk-like split(3) that handles memory allocation automatically
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. int                /* number of fields, including overflow */
  10. awksplit(string, fieldsp, nfields, sep)
  11. char *string;
  12. register char ***fieldsp;    /* list is not NULL-terminated */
  13. register int nfields;        /* number of entries available in fields[] */
  14. char *sep;            /* "" white, "c" single char, "ab" [ab]+ */
  15. {
  16.     register int nf;
  17.  
  18.     nf = split(string, *fieldsp, nfields, sep);
  19.     if (nf > nfields) {    /* too many fields to fieldsp? */
  20.         register char **array =
  21.             (char **)malloc((unsigned)(nf * sizeof(char *)));
  22.         register int i;
  23.  
  24.         if (array == NULL)
  25.             *fieldsp = NULL;    /* you lose */
  26.         else {
  27.             for (i = 0; i < nfields; i++)
  28.                 array[i] = (*fieldsp)[i];
  29.             *fieldsp = array;
  30.             (void) split(array[nfields-1], &array[nfields],
  31.                 nf - nfields, sep);
  32.         }
  33.     }
  34.     return nf;
  35. }
  36.