/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
*/

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: copylist.c 1.3 89/10/23 $@(#)libgen:copylist.c	1.1"
/*
	copylist copies a file into a block of memory, replacing newlines
	with null characters, and returns a pointer to the copy.
*/

#include	<sys/types.h>
#include	<sys/stat.h>
#include	<stdio.h>

static long	linecount;

char *
copylist(filenm, szptr)
char	*filenm;
long	*szptr;
{
	FILE		*strm;
	struct	stat	stbuf;
	register int	c;
	char		*malloc();
	register char	*ptr, *p;
	int		stat();

	/* get size of file */
	if (stat(filenm, &stbuf) == -1) {
		fprintf(stderr, "copylist: can't find %s\n", filenm);
		return(NULL);
	}
	*szptr = stbuf.st_size;

	/* get block of memory */
	if((ptr = malloc((unsigned) *szptr)) == NULL) {
		fprintf(stderr, "copylist: malloc call unsuccessful\n");
		return(NULL);
	}

	/* copy contents of file into memory block, replacing newlines
	with null characters */
	if ((strm = fopen(filenm, "r")) == NULL) {
		fprintf(stderr, "copylist: can't open %s\n", filenm);
		return(NULL);
	}
	linecount = 0;
	for (p = ptr; p < ptr + *szptr  &&  (c = getc(strm)) != EOF; p++) {
		if (c == '\n') {
			*p = '\0';
			linecount++;
		}
		else
			*p = c;
	}
	fclose(strm);

	return(ptr);
}


long
clistcnt()
{
	return  linecount;
}
