xref: /csrg-svn/contrib/ed/add_line.c (revision 57675)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * 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	5.1 (Berkeley) 01/23/93";
13 #endif /* not lint */
14 
15 #include "ed.h"
16 
17 /*
18  * This is where the lines actually are put into the buffer. Notice
19  * all the #ifdef's for the various methods of buffering - POSIX said
20  * no "no attempt is made to imply a specific implementation". So,
21  * you get your choice: standard I/O, core memory, or a database.
22  */
23 
24 #ifdef STDIO
25 long
26 add_line(p, len)
27 #endif
28 #ifdef DBI
29 recno_t
30 add_line(p, len)
31 #endif
32 #ifdef MEMORY
33 char
34 *add_line(p, len)
35 #endif
36 
37 char *p;
38 long len;
39 
40 {
41 #ifdef STDIO
42   long l_key;
43   extern int file_loc;
44 
45   if (file_seek)  /* x-ref to get_line for what this does */
46     {
47       file_seek = 0;
48       fseek(fhtmp, 0L, 2); /* set to end-to-file */
49     }
50   l_key = ftell(fhtmp);
51   file_loc = l_key + fwrite(p, sizeof(char), len, fhtmp); /* keeps user time down 20%approx */
52   return(l_key);
53 #endif
54 
55 #ifdef DBI
56   DBT db_key, db_data;
57   static recno_t l_key=0;
58 
59   l_key++;
60   (db_key.data) = &l_key;
61   (db_key.size) = sizeof(recno_t);
62   (db_data.data) = p;
63   (db_data.size) = len;
64   (dbhtmp->put)(dbhtmp, &db_key, &db_data, (u_int)(R_NOOVERWRITE));
65   return(l_key);
66 #endif
67 
68 #ifdef MEMORY
69   char *tmp;
70   tmp = (char *)calloc(len+1, sizeof(char));
71   if (tmp)
72     {
73       bcopy(p, tmp, len);
74       tmp[len] = '\0';
75     }
76   return(tmp);
77 #endif
78 
79 } /* end-add_line */
80