xref: /original-bsd/contrib/ed/add_line.c (revision 84651319)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)add_line.c	8.1 (Berkeley) 05/31/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <regex.h>
18 #include <setjmp.h>
19 #include <stdio.h>
20 
21 #ifdef DBI
22 #include <db.h>
23 #endif
24 
25 #include "ed.h"
26 #include "extern.h"
27 
28 /*
29  * This is where the lines actually are put into the buffer.
30  */
31 #ifdef STDIO
32 long
33 add_line(p, len)
34 	char *p;
35 	long len;
36 {
37 	extern int file_loc;
38 	long l_key;
39 
40 	sigspecial++;
41 	if (file_seek)  /* x-ref to get_line for what this does */ {
42 		file_seek = 0;
43 		fseek(fhtmp, 0L, 2); /* set to end-to-file */
44 	}
45 	l_key = ftell(fhtmp);
46 					/* keeps user time down 20% approx. */
47 	file_loc = l_key + fwrite(p, sizeof(char), len, fhtmp);
48 	sigspecial--;
49 	return (l_key);
50 }
51 #endif
52 
53 #ifdef DBI
54 recno_t
55 add_line(p, len)
56 	char *p;
57 	long len;
58 {
59 	DBT db_key, db_data;
60 	static recno_t l_key=0;
61 
62 	sigspecial++;
63 	l_key++;
64 	(db_key.data) = &l_key;
65 	(db_key.size) = sizeof(recno_t);
66 	(db_data.data) = p;
67 	(db_data.size) = len;
68 	(dbhtmp->put)(dbhtmp, &db_key, &db_data, (u_int)(R_NOOVERWRITE));
69 	sigspecial--;
70 	return (l_key);
71 }
72 #endif
73 
74 #ifdef MEMORY
75 char *
76 add_line(p, len)
77 	char *p;
78 	long len;
79 {
80 	char *tmp;
81 
82 	sigspecial++;
83 	tmp = (char *)calloc(len+1, sizeof(char));
84 	if (tmp) {
85 		memmove(tmp, p, len);
86 		tmp[len] = '\0';
87 	}
88 	sigspecial--;
89 	return (tmp);
90 }
91 #endif
92