tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / ex / ex_source.c
bloba103e7a92f34705cf12a91834e102ebf31440008
1 /* $NetBSD: ex_source.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 static const char sccsid[] = "Id: ex_source.c,v 10.16 2001/08/18 21:49:58 skimo Exp (Berkeley) Date: 2001/08/18 21:49:58 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "../common/common.h"
33 * ex_source -- :source file
34 * Execute ex commands from a file.
36 * PUBLIC: int ex_source __P((SCR *, EXCMD *));
38 int
39 ex_source(SCR *sp, EXCMD *cmdp)
41 struct stat sb;
42 int fd, len;
43 char *bp;
44 const char *name;
45 size_t nlen;
46 const CHAR_T *wp;
47 CHAR_T *dp;
48 size_t wlen;
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))
52 goto err;
55 * XXX
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) {
65 errno = ENOMEM;
66 goto err;
69 MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
70 if (bp == NULL) {
71 (void)close(fd);
72 return (1);
74 bp[sb.st_size] = '\0';
76 /* Read the file into memory. */
77 len = read(fd, bp, (int)sb.st_size);
78 (void)close(fd);
79 if (len == -1 || len != sb.st_size) {
80 if (len != sb.st_size)
81 errno = EIO;
82 free(bp);
83 err: msgq_str(sp, M_SYSERR, name, "%s");
84 return (1);
87 if (CHAR2INT(sp, bp, (size_t)sb.st_size + 1, wp, wlen))
88 msgq(sp, M_ERR, "323|Invalid input. Truncated.");
89 dp = v_wstrdup(sp, wp, wlen - 1);
90 free(bp);
91 /* Put it on the ex queue. */
92 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
93 return (ex_run_str(sp, name, dp, wlen - 1, 1, 1));