aboutsummaryrefslogtreecommitdiffhomepage
path: root/share/functions/umask.fish
blob: a03c39b7e4cd13113d5c9d232de2f253290ef364 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

function __fish_umask_parse -d "Internal umask function"
	# Test if already a valid octal mask, and pad it with zeros
	if echo $argv | __fish_sgrep -E '^0?[0-7]{1,3}$' >/dev/null
		set -l char_count (echo $argv| wc -c)
		for i in (seq (math 5 - $char_count)); set argv 0$argv; end
		echo $argv
	else
		# Test if argument really is a valid symbolic mask
		if not echo $argv | __fish_sgrep -E '^(((u|g|o|a|)(=|\+|-)|)(r|w|x)*)(,(((u|g|o|a|)(=|\+|-)|)(r|w|x)*))*$' >/dev/null
			printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
			return 1
		end

		set -l implicit_all

		# Insert inverted umask into res variable

		set -l mode
		set -l val
		set -l tmp $umask
		set -l res

		for i in 1 2 3
			set tmp (echo $tmp|cut -c 2-)
			set -l char_count (echo $tmp|cut -c 1)
			set res[$i] (math 7 - $char_count)
		end

		set -l el (echo $argv|tr , \n)
		for i in $el
			switch $i
				case 'u*'
					set idx 1
					set i (echo $i| cut -c 2-)

				case 'g*'
					set idx 2
					set i (echo $i| cut -c 2-)

				case 'o*'
					set idx 3
					set i (echo $i| cut -c 2-)

				case 'a*'
					set idx 1 2 3
					set i (echo $i| cut -c 2-)

				case '*'
					set implicit_all 1
					set idx 1 2 3
			end

			switch $i
				case '=*'
					set mode set
					set i (echo $i| cut -c 2-)

				case '+*'
					set mode add
					set i (echo $i| cut -c 2-)

				case '-*'
					set mode remove
					set i (echo $i| cut -c 2-)

				case '*'
					if not count $implicit_all >/dev/null
						printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
						return
					end
					set mode set
			end

			if not echo $perm| __fish_sgrep -E '^(r|w|x)*$' >/dev/null
				printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
				return
			end

			set val 0
			if echo $i | __fish_sgrep 'r' >/dev/null
				set val 4
			end
			if echo $i | __fish_sgrep 'w' >/dev/null
				set val (math $val + 2)
			end
			if echo $i | __fish_sgrep 'x' >/dev/null
				set val (math $val + 1)
			end

			for j in $idx
				switch $mode
					case set
						 set res[$j] $val

					case add
						set res[$j] (perl -e 'print( ( '$res[$j]'|'$val[$j]' )."\n" )')

					case remove
						set res[$j] (perl -e 'print( ( (7-'$res[$j]')&'$val[$j]' )."\n" )')
				end
			end
		end

		for i in 1 2 3
			set res[$i] (math 7 - $res[$i])
		end
		echo 0$res[1]$res[2]$res[3]
	end
end

function __fish_umask_print_symbolic
	set -l res ""
	set -l letter a u g o

	for i in 2 3 4
		set res $res,$letter[$i]=
		set val (echo $umask|cut -c $i)

		if contains $val 0 1 2 3
			set res {$res}r
		end

		if contains $val 0 1 4 5
			set res {$res}w
		end

		if contains $val 0 2 4 6
			set res {$res}x
		end

	end

	echo $res|cut -c 2-
end

function umask --description "Set default file permission mask"

	set -l as_command 0
	set -l symbolic 0
	
	set -l options
	set -l shortopt pSh
	if not getopt -T >/dev/null
		# GNU getopt
		set longopt -l as-command,symbolic,help
		set options -o $shortopt $longopt --
		# Verify options
		if not getopt -n umask $options $argv >/dev/null
			return 1
		end
	else
		# Old getopt, used on OS X
		set options $shortopt
		# Verify options
		if not getopt $options $argv >/dev/null
			return 1
		end
	end

	set -l tmp (getopt $options $argv)
	eval set opt $tmp

	while count $opt >/dev/null

		switch $opt[1]
			case -h --help
				__fish_print_help umask
				return 0

			case -p --as-command
				set as_command 1

			case -S --symbolic
				set symbolic 1

			case --
				set -e opt[1]
				break

		end

		set -e opt[1]

	end

	switch (count $opt)

		case 0
			if not set -q umask
				set -g umask 113
			end
			if test $as_command -eq 1
				echo umask $umask
			else
				if test $symbolic -eq 1
					__fish_umask_print_symbolic $umask
				else
					echo $umask
				end
			end

		case 1
			set -l parsed (__fish_umask_parse $opt)
			if test (count $parsed) -eq 1
				set -g umask $parsed
				return 0
			end
			return 1

		case '*'
			printf (_ '%s: Too many arguments\n') umask >&2

	end

end