2001-03-31 15:37:44 +04:00
|
|
|
/* $NetBSD: ex_source.c,v 1.8 2001/03/31 11:37:50 aymeric Exp $ */
|
1998-01-09 11:03:16 +03:00
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*-
|
1994-03-28 08:28:20 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994
|
1994-01-24 08:52:58 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-05-20 07:47:00 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-01-24 08:52:58 +03:00
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
#ifndef lint
|
2001-03-31 15:37:44 +04:00
|
|
|
static const char sccsid[] = "@(#)ex_source.c 10.12 (Berkeley) 8/10/96";
|
1994-01-24 08:52:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <sys/queue.h>
|
1996-05-20 07:47:00 +04:00
|
|
|
#include <sys/stat.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
|
|
|
|
#include <bitstring.h>
|
1996-05-20 07:47:00 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
1996-05-20 07:47:00 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "../common/common.h"
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_source -- :source file
|
|
|
|
* Execute ex commands from a file.
|
1996-05-20 07:47:00 +04:00
|
|
|
*
|
|
|
|
* PUBLIC: int ex_source __P((SCR *, EXCMD *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_source(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
1996-05-20 07:47:00 +04:00
|
|
|
struct stat sb;
|
|
|
|
int fd, len;
|
|
|
|
char *bp, *name;
|
|
|
|
|
|
|
|
name = cmdp->argv[0]->bp;
|
|
|
|
if ((fd = open(name, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* I'd like to test to see if the file is too large to malloc. Since
|
|
|
|
* we don't know what size or type off_t's or size_t's are, what the
|
|
|
|
* largest unsigned integral type is, or what random insanity the local
|
|
|
|
* C compiler will perpetrate, doing the comparison in a portable way
|
|
|
|
* is flatly impossible. So, put an fairly unreasonable limit on it,
|
|
|
|
* I don't want to be dropping core here.
|
|
|
|
*/
|
|
|
|
#define MEGABYTE 1048576
|
|
|
|
if (sb.st_size > MEGABYTE) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
|
1996-05-20 07:47:00 +04:00
|
|
|
if (bp == NULL) {
|
|
|
|
(void)close(fd);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
bp[sb.st_size] = '\0';
|
|
|
|
|
|
|
|
/* Read the file into memory. */
|
|
|
|
len = read(fd, bp, (int)sb.st_size);
|
|
|
|
(void)close(fd);
|
|
|
|
if (len == -1 || len != sb.st_size) {
|
|
|
|
if (len != sb.st_size)
|
|
|
|
errno = EIO;
|
|
|
|
free(bp);
|
|
|
|
err: msgq_str(sp, M_SYSERR, name, "%s");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put it on the ex queue. */
|
|
|
|
return (ex_run_str(sp, name, bp, (size_t)sb.st_size, 1, 1));
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|