-
Notifications
You must be signed in to change notification settings - Fork 3
/
replace_tabs
executable file
·45 lines (38 loc) · 1015 Bytes
/
replace_tabs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh
# replace tabs with spaces in files in given as argument
#
# wraps expand so that students are unlikely to lose data
# assumes tabs are 4 spaces
#
# Repo: https://github.com/COMP1511UNSW/c_check
TAB_SIZE=4
PATH=/bin:/usr/bin:$PATH
for file in "$@"
do
backup_file="$file.original_with_tabs"
if test -e "$backup_file"
then
echo "$0: can not backup '$file' to '$backup_file' because it already exists" 1>&2
echo "rename or remove '$backup_file' and rerun" 1>&2
exit 1
fi
mv "$file" "$backup_file" || exit 1
if expand -it 4 "$backup_file" >"$file"
then
if diff "$backup_file" "$file" >/dev/null
then
mv "$backup_file" "$file"
echo "$0: no tabs found in $file"
else
echo "mv $file $backup_file"
echo "expand -it $TAB_SIZE $backup_file >$file"
chmod --reference="$backup_file" "$file"
echo "$0: tabs replaced in '$file', original contents left in '$backup_file'"
fi
else
mv "$backup_file" "$file"
echo "$0: tab expansion failed" 1>&2
exit 1
fi
done
exit 0