dotfiles

Alpine Linux dotfiles

git clone git://git.lin.moe/dotfiles.git

 1#!/bin/sh -e
 2
 3help() {
 4	cat - >&2 <<EOF
 5doasedit - like sudoedit, but for doas
 6
 7doasedit file...
 8
 9Every argument will be treated as a file to edit. There's no support for
10passing arguments to doas, so you can only doas root.
11
12This script is SECURITY SENSITIVE! Special care has been taken to correctly
13preserve file attributes. Please exercise CAUTION when modifying AND using
14this script.
15EOF
16}
17
18case "$1" in --help|-h) help; exit 0;; esac
19
20export TMPDIR=/dev/shm/
21trap 'trap - EXIT HUP QUIT TERM INT ABRT; rm -f "$tmp" "$tmpcopy"' EXIT HUP QUIT TERM INT ABRT
22
23for file; do
24	case "$file" in -*) file=./"$file" ;; esac
25
26	tmp="$(mktemp)"
27	if [ -f "$file" ] && [ ! -r "$file" ]; then
28		doas cat "$file" > "$tmp"
29	elif [ -r "$file" ]; then
30		cat "$file" > "$tmp"
31	fi
32
33	tmpcopy="$(mktemp)"
34	cat "$tmp" > "$tmpcopy"
35
36	${EDITOR:-vi} "$tmp"
37
38	if cmp -s "$tmp" "$tmpcopy"; then
39		echo 'File unchanged, exiting...'
40	else
41		doas dd if="$tmp" of="$file"
42		echo 'Done, changes written'
43	fi
44
45	rm "$tmp" "$tmpcopy"
46done