GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/vi/build/../ex/ex_mkexrc.c Lines: 0 32 0.0 %
Date: 2017-11-13 Branches: 0 45 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ex_mkexrc.c,v 1.7 2016/01/06 22:28:52 millert Exp $	*/
2
3
/*-
4
 * Copyright (c) 1992, 1993, 1994
5
 *	The Regents of the University of California.  All rights reserved.
6
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7
 *	Keith Bostic.  All rights reserved.
8
 *
9
 * See the LICENSE file for redistribution information.
10
 */
11
12
#include "config.h"
13
14
#include <sys/types.h>
15
#include <sys/queue.h>
16
#include <sys/stat.h>
17
18
#include <bitstring.h>
19
#include <errno.h>
20
#include <fcntl.h>
21
#include <limits.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <unistd.h>
26
27
#include "../common/common.h"
28
#include "pathnames.h"
29
30
/*
31
 * ex_mkexrc -- :mkexrc[!] [file]
32
 *
33
 * Create (or overwrite) a .exrc file with the current info.
34
 *
35
 * PUBLIC: int ex_mkexrc(SCR *, EXCMD *);
36
 */
37
int
38
ex_mkexrc(SCR *sp, EXCMD *cmdp)
39
{
40
	struct stat sb;
41
	FILE *fp;
42
	int fd, sverrno;
43
	char *fname;
44
45
	switch (cmdp->argc) {
46
	case 0:
47
		fname = _PATH_EXRC;
48
		break;
49
	case 1:
50
		fname = cmdp->argv[0]->bp;
51
		set_alt_name(sp, fname);
52
		break;
53
	default:
54
		abort();
55
	}
56
57
	if (!FL_ISSET(cmdp->iflags, E_C_FORCE) && !stat(fname, &sb)) {
58
		msgq_str(sp, M_ERR, fname,
59
		    "%s exists, not written; use ! to override");
60
		return (1);
61
	}
62
63
	/* Create with max permissions of rw-r--r--. */
64
	if ((fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY,
65
	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
66
		msgq_str(sp, M_SYSERR, fname, "%s");
67
		return (1);
68
	}
69
70
	if ((fp = fdopen(fd, "w")) == NULL) {
71
		sverrno = errno;
72
		(void)close(fd);
73
		goto e2;
74
	}
75
76
	if (seq_save(sp, fp, "abbreviate ", SEQ_ABBREV) || ferror(fp))
77
		goto e1;
78
	if (seq_save(sp, fp, "map ", SEQ_COMMAND) || ferror(fp))
79
		goto e1;
80
	if (seq_save(sp, fp, "map! ", SEQ_INPUT) || ferror(fp))
81
		goto e1;
82
	if (opts_save(sp, fp) || ferror(fp))
83
		goto e1;
84
	if (fclose(fp)) {
85
		sverrno = errno;
86
		goto e2;
87
	}
88
89
	msgq_str(sp, M_INFO, fname, "New exrc file: %s");
90
	return (0);
91
92
e1:	sverrno = errno;
93
	(void)fclose(fp);
94
e2:	errno = sverrno;
95
	msgq_str(sp, M_SYSERR, fname, "%s");
96
	return (1);
97
}