2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: ex_source.c,v 10.14 2000/07/16 15:37:24 skimo Exp $ (Berkeley) $Date: 2000/07/16 15:37:24 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 #include "../common/common.h"
32 * ex_source -- :source file
33 * Execute ex commands from a file.
35 * PUBLIC: int ex_source __P((SCR *, EXCMD *));
50 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, name
, nlen
);
51 if ((fd
= open(name
, O_RDONLY
, 0)) < 0 || fstat(fd
, &sb
))
56 * I'd like to test to see if the file is too large to malloc. Since
57 * we don't know what size or type off_t's or size_t's are, what the
58 * largest unsigned integral type is, or what random insanity the local
59 * C compiler will perpetrate, doing the comparison in a portable way
60 * is flatly impossible. So, put an fairly unreasonable limit on it,
61 * I don't want to be dropping core here.
63 #define MEGABYTE 1048576
64 if (sb
.st_size
> MEGABYTE
) {
69 MALLOC(sp
, bp
, char *, (size_t)sb
.st_size
+ 1);
74 bp
[sb
.st_size
] = '\0';
76 /* Read the file into memory. */
77 len
= read(fd
, bp
, (int)sb
.st_size
);
79 if (len
== -1 || len
!= sb
.st_size
) {
80 if (len
!= sb
.st_size
)
83 err
: msgq_str(sp
, M_SYSERR
, name
, "%s");
87 CHAR2INT(sp
, bp
, (size_t)sb
.st_size
+ 1, wp
, wlen
);
88 dp
= v_wstrdup(sp
, wp
, wlen
- 1);
90 /* Put it on the ex queue. */
91 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, name
, nlen
);
92 return (ex_run_str(sp
, name
, dp
, (size_t)sb
.st_size
, 1, 1));