xref: /spdk/scripts/eofnl (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
1#!/usr/bin/env bash
2# Make sure file has a trailing newline
3
4f="$1"
5
6if [ -z "$f" ]; then
7	echo "usage: $0 <file>"
8	exit 1
9fi
10
11if [ ! -f "$f" ]; then
12	exit 0
13fi
14
15if [[ $(tail -c1 "$f") ]]; then
16	echo "$f: No newline at end of file"
17	echo '' >> "$f"
18	exit 1
19fi
20
21if [[ ! $(tail -c2 "$f") ]]; then
22	echo "$f: Extra trailing newline"
23	exit 1
24fi
25
26if grep -q $'\r' "$f"; then
27	echo "$f: DOS-style newlines"
28	dos2unix "$f" &> /dev/null
29	exit 1
30fi
31
32if grep -q $'[\t ]$' "$f"; then
33	echo "$f: Trailing whitespace"
34	sed -i $'s/[ \t]*$//' "$f"
35	exit 1
36fi
37
38exit 0
39