xref: /csrg-svn/contrib/ed/add_line.c (revision 58356)
157675Sbostic /*-
257675Sbostic  * Copyright (c) 1992 The Regents of the University of California.
357675Sbostic  * 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*58356Sbostic static char sccsid[] = "@(#)add_line.c	5.4 (Berkeley) 03/02/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
3358315Sbostic add_line(p, len)
3458315Sbostic 	char *p;
3558315Sbostic 	long len;
3658315Sbostic {
3758315Sbostic 	extern int file_loc;
3858315Sbostic 	long l_key;
3958315Sbostic 	int l_jmp_flag;
4058315Sbostic 
4158315Sbostic 	if (l_jmp_flag = setjmp(ctrl_position2))
4258315Sbostic 		return (0);
4358315Sbostic 	sigspecial2 = 1;
4458315Sbostic 	if (file_seek)  /* x-ref to get_line for what this does */ {
4558315Sbostic 		file_seek = 0;
4658315Sbostic 		fseek(fhtmp, 0L, 2); /* set to end-to-file */
4758315Sbostic 	}
4858315Sbostic 	l_key = ftell(fhtmp);
49*58356Sbostic 					/* keeps user time down 20% approx. */
5058315Sbostic 	file_loc = l_key + fwrite(p, sizeof(char), len, fhtmp);
51*58356Sbostic 	sigspecial2 = 0;
5258315Sbostic 	return (l_key);
5358315Sbostic }
5458315Sbostic #endif
5558315Sbostic 
5658315Sbostic #ifdef DBI
5757675Sbostic recno_t
5857675Sbostic add_line(p, len)
5957710Sbostic 	char *p;
6057710Sbostic 	long len;
6157675Sbostic {
6257710Sbostic 	DBT db_key, db_data;
63*58356Sbostic 	static recno_t l_key=0;
6458315Sbostic 	int l_jmp_flag;
6557675Sbostic 
6658315Sbostic 	if (l_jmp_flag = setjmp(ctrl_position2))
6758315Sbostic 		return ((recno_t)0);
6858315Sbostic 	sigspecial2 = 1;
6957710Sbostic 	l_key++;
7057710Sbostic 	(db_key.data) = &l_key;
7157710Sbostic 	(db_key.size) = sizeof(recno_t);
7257710Sbostic 	(db_data.data) = p;
7357710Sbostic 	(db_data.size) = len;
7457710Sbostic 	(dbhtmp->put)(dbhtmp, &db_key, &db_data, (u_int)(R_NOOVERWRITE));
75*58356Sbostic 	sigspecial2 = 0;
7658315Sbostic 	return (l_key);
7757710Sbostic }
7858315Sbostic #endif
7958315Sbostic 
8058315Sbostic #ifdef MEMORY
8158315Sbostic char *
8258315Sbostic add_line(p, len)
8358315Sbostic 	char *p;
8458315Sbostic 	long len;
8558315Sbostic {
8658315Sbostic 	char *tmp;
8758315Sbostic 	int l_jmp_flag;
8858315Sbostic 
8958315Sbostic 	if (l_jmp_flag = setjmp(ctrl_position2))
9058315Sbostic 		return (NULL);
9158315Sbostic 	sigspecial2 = 1;
9258315Sbostic 	tmp = (char *)calloc(len+1, sizeof(char));
9358315Sbostic 	if (tmp) {
9458315Sbostic 		bcopy(p, tmp, len);
9558315Sbostic 		tmp[len] = '\0';
9658315Sbostic 	}
97*58356Sbostic 	sigspecial2 = 0;
9858315Sbostic 	return (tmp);
9958315Sbostic }
10058315Sbostic #endif
101