Remove building with NOCRYPTO option
[minix.git] / external / bsd / nvi / dist / ex / ex_source.c
blobb44719679f6dd1a03834b53e0a18b7760728853b
1 /* $NetBSD: ex_source.c,v 1.3 2014/01/26 21:43:45 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 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 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 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_source.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
26 #include <bitstring.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 #include "../common/common.h"
38 * ex_source -- :source file
39 * Execute ex commands from a file.
41 * PUBLIC: int ex_source __P((SCR *, EXCMD *));
43 int
44 ex_source(SCR *sp, EXCMD *cmdp)
46 struct stat sb;
47 int fd, len;
48 char *bp;
49 const char *name;
50 size_t nlen;
51 const CHAR_T *wp;
52 CHAR_T *dp;
53 size_t wlen;
55 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
56 if ((fd = open(name, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
57 goto err;
60 * XXX
61 * I'd like to test to see if the file is too large to malloc. Since
62 * we don't know what size or type off_t's or size_t's are, what the
63 * largest unsigned integral type is, or what random insanity the local
64 * C compiler will perpetrate, doing the comparison in a portable way
65 * is flatly impossible. So, put an fairly unreasonable limit on it,
66 * I don't want to be dropping core here.
68 #define MEGABYTE 1048576
69 if (sb.st_size > MEGABYTE) {
70 errno = ENOMEM;
71 goto err;
74 MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
75 if (bp == NULL) {
76 (void)close(fd);
77 return (1);
79 bp[sb.st_size] = '\0';
81 /* Read the file into memory. */
82 len = read(fd, bp, (int)sb.st_size);
83 (void)close(fd);
84 if (len == -1 || len != sb.st_size) {
85 if (len != sb.st_size)
86 errno = EIO;
87 free(bp);
88 err: msgq_str(sp, M_SYSERR, name, "%s");
89 return (1);
92 if (CHAR2INT(sp, bp, (size_t)sb.st_size + 1, wp, wlen))
93 msgq(sp, M_ERR, "323|Invalid input. Truncated.");
94 dp = v_wstrdup(sp, wp, wlen - 1);
95 free(bp);
96 /* Put it on the ex queue. */
97 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
98 return (ex_run_str(sp, name, dp, wlen - 1, 1, 1));