xref: /csrg-svn/contrib/ed/add_line.c (revision 60663)
157675Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457675Sbostic  *
557675Sbostic  * This code is derived from software contributed to Berkeley by
657675Sbostic  * Rodney Ruddock of the University of Guelph.
757675Sbostic  *
857675Sbostic  * %sccs.include.redist.c%
957675Sbostic  */
1057675Sbostic 
1157675Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)add_line.c	8.1 (Berkeley) 05/31/93";
1357675Sbostic #endif /* not lint */
1457675Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1757710Sbostic #include <regex.h>
1857710Sbostic #include <setjmp.h>
1957710Sbostic #include <stdio.h>
2057710Sbostic 
2158315Sbostic #ifdef DBI
2258315Sbostic #include <db.h>
2358315Sbostic #endif
2458315Sbostic 
2557675Sbostic #include "ed.h"
2657710Sbostic #include "extern.h"
2757675Sbostic 
2857675Sbostic /*
2957710Sbostic  * This is where the lines actually are put into the buffer.
3057675Sbostic  */
3158315Sbostic #ifdef STDIO
3258315Sbostic long
add_line(p,len)3358315Sbostic add_line(p, len)
3458315Sbostic 	char *p;
3558315Sbostic 	long len;
3658315Sbostic {
3758315Sbostic 	extern int file_loc;
3858315Sbostic 	long l_key;
3958315Sbostic 
4058710Sbostic 	sigspecial++;
4158315Sbostic 	if (file_seek)  /* x-ref to get_line for what this does */ {
4258315Sbostic 		file_seek = 0;
4358315Sbostic 		fseek(fhtmp, 0L, 2); /* set to end-to-file */
4458315Sbostic 	}
4558315Sbostic 	l_key = ftell(fhtmp);
4658356Sbostic 					/* keeps user time down 20% approx. */
4758315Sbostic 	file_loc = l_key + fwrite(p, sizeof(char), len, fhtmp);
4858710Sbostic 	sigspecial--;
4958315Sbostic 	return (l_key);
5058315Sbostic }
5158315Sbostic #endif
5258315Sbostic 
5358315Sbostic #ifdef DBI
5457675Sbostic recno_t
add_line(p,len)5557675Sbostic add_line(p, len)
5657710Sbostic 	char *p;
5757710Sbostic 	long len;
5857675Sbostic {
5957710Sbostic 	DBT db_key, db_data;
6058356Sbostic 	static recno_t l_key=0;
6157675Sbostic 
6258710Sbostic 	sigspecial++;
6357710Sbostic 	l_key++;
6457710Sbostic 	(db_key.data) = &l_key;
6557710Sbostic 	(db_key.size) = sizeof(recno_t);
6657710Sbostic 	(db_data.data) = p;
6757710Sbostic 	(db_data.size) = len;
6857710Sbostic 	(dbhtmp->put)(dbhtmp, &db_key, &db_data, (u_int)(R_NOOVERWRITE));
6958710Sbostic 	sigspecial--;
7058315Sbostic 	return (l_key);
7157710Sbostic }
7258315Sbostic #endif
7358315Sbostic 
7458315Sbostic #ifdef MEMORY
7558315Sbostic char *
add_line(p,len)7658315Sbostic add_line(p, len)
7758315Sbostic 	char *p;
7858315Sbostic 	long len;
7958315Sbostic {
8058315Sbostic 	char *tmp;
8158315Sbostic 
8258710Sbostic 	sigspecial++;
8358315Sbostic 	tmp = (char *)calloc(len+1, sizeof(char));
8458315Sbostic 	if (tmp) {
8559480Sbostic 		memmove(tmp, p, len);
8658315Sbostic 		tmp[len] = '\0';
8758315Sbostic 	}
8858710Sbostic 	sigspecial--;
8958315Sbostic 	return (tmp);
9058315Sbostic }
9158315Sbostic #endif
92