view lib/readtokens0.c @ 6259:96c32553b4c6

Use a consistent style for including <config.h>. * __fpending.c, acl.c, argmatch.c, argp-help.c, argp-parse.c, argp-pvh.c, backupfile.c, basename.c, c-stack.c, calloc.c, check-version.c, cloexec.c, closeout.c, copy-file.c, creat-safer.c, cycle-check.c, dirfd.c, dirname.c, dup-safer.c, dup2.c, euidaccess.c, exclude.c, exitfail.c, fatal-signal.c, fd-safer.c, file-type.c, fileblocks.c, filemode.c, filenamecat.c, findprog.c, fnmatch.c, fopen-safer.c, free.c, fsusage.c, ftruncate.c, full-write.c, fwriteerror.c, getaddrinfo.c, getcwd.c, getdelim.c, getline.c, getlogin_r.c, getndelim2.c, getnline.c, getopt1.c, getpass.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c, human.c, idcache.c, inet_ntop.c, isdir.c, long-options.c, malloc.c, memcasecmp.c, memcmp.c, memcoll.c, memcpy.c, memmove.c, mkdir-p.c, modechange.c, mountlist.c, open-safer.c, physmem.c, pipe-safer.c, pipe.c, poll.c, posixver.c, progname.c, progreloc.c, putenv.c, quote.c, quotearg.c, readline.c, readlink.c, realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c, same.c, save-cwd.c, savedir.c, sig2str.c, strcspn.c, strerror.c, stripslash.c, strncasecmp.c, strndup.c, strnlen.c, strnlen1.c, strsep.c, strstr.c, strtod.c, strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c, userspec.c, utimecmp.c, version-etc-fsf.c, version-etc.c, wait-process.c, xalloc-die.c, xgetcwd.c, xmalloc.c, xmemcoll.c, xnanosleep.c, xreadlink.c, xsetenv.c, xstrndup.c, xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Standardize inclusion of config.h. * __fpending.h, dirfd.h, getdate.h, human.h, inttostr.h: Removed inclusion of config.h from header files. * inttostr.c: Adjusted in-tree users. * timespec.h: Remove superfluous warning to include config.h. * atexit.c, chdir-long.c chown.c, fchown-stub.c, getgroups.c, gettimeofday.c, lchown.c, lstat.c, mkdir.c, mkstemp.c, nanosleep.c, openat.c, raise.c, readtokens0.c, readutmp.c, unlinkdir.c: Guard inclusion of config.h with HAVE_CONFIG_H.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 19 Sep 2005 17:28:14 +0000
parents a48fb0e98c8c
children 8a1a9361108c
line wrap: on
line source

/* readtokens0.c -- Read NUL-separated tokens from an input stream.
   Copyright (C) 2004 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

   Written by Jim Meyering. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>

#include "readtokens0.h"

#define obstack_chunk_alloc malloc
#define obstack_chunk_free free

void
readtokens0_init (struct Tokens *t)
{
  t->n_tok = 0;
  t->tok = NULL;
  t->tok_len = NULL;
  obstack_init (&t->o_data);
  obstack_init (&t->o_tok);
  obstack_init (&t->o_tok_len);
}

void
readtokens0_free (struct Tokens *t)
{
  obstack_free (&t->o_data, NULL);
  obstack_free (&t->o_tok, NULL);
  obstack_free (&t->o_tok_len, NULL);
}

/* Finalize (in the obstack_finish sense) the current token
   and record its pointer and length.  */
static void
save_token (struct Tokens *t)
{
  /* Don't count the trailing NUL byte in the length.  */
  size_t len = obstack_object_size (&t->o_data) - 1;
  char const *s = obstack_finish (&t->o_data);
  obstack_ptr_grow (&t->o_tok, s);
  obstack_grow (&t->o_tok_len, &len, sizeof len);
  t->n_tok++;
}

/* Read NUL-separated tokens from stream IN into T until EOF or error.
   The final NUL is optional.  Always append a NULL pointer to the
   resulting list of token pointers, but that pointer isn't counted
   via t->n_tok.  Return true if successful.  */
bool
readtokens0 (FILE *in, struct Tokens *t)
{

  while (1)
    {
      int c = fgetc (in);
      if (c == EOF)
	{
	  size_t len = obstack_object_size (&t->o_data);
	  /* If the current object has nonzero length, then there
	     was no NUL byte at EOF -- or maybe there was an error,
	     in which case, we need to append a NUL byte to our buffer.  */
	  if (len)
	    {
	      obstack_1grow (&t->o_data, '\0');
	      save_token (t);
	    }

	  break;
	}

      obstack_1grow (&t->o_data, c);
      if (c == '\0')
	save_token (t);
    }

  /* Add a NULL pointer at the end, in case the caller (like du)
     requires an argv-style array of strings.  */
  obstack_ptr_grow (&t->o_tok, NULL);

  t->tok = obstack_finish (&t->o_tok);
  t->tok_len = obstack_finish (&t->o_tok_len);
  return ! ferror (in);
}