-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcowsayfs.sh
executable file
·81 lines (68 loc) · 1.31 KB
/
cowsayfs.sh
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
#!/bin/bash
. ./booze.sh
basestat()
{
local now=`date +%s`
local times="$now $now $now"
local ids="`id -u` `id -g`"
echo "0 $1 2 $ids 0 102400000 0 $times"
}
dirstat() { basestat "$(printf '%o' $((S_IFDIR | ${1:-0755})))"; }
regstat() { basestat "$(printf '%o' $((S_IFREG | ${1:-0644})))"; }
cows=($(cd /usr/share/cowsay/cows/ && for c in *.cow; do echo "${c%.cow}"; done))
cowpat="$(echo ${cows[@]} | tr ' ' '|')"
cs_getattr()
{
if [ "$1" == "/" ] || [[ "$1" =~ ^/($cowpat)$ ]]; then
booze_out=`dirstat`
return 0
elif [[ "$1" =~ ^/($cowpat)/[^/]+$ ]]; then
booze_out=`regstat`
return 0
else
booze_err=-$ENOENT
return 1
fi
}
cs_readdir()
{
if [ "$1" == "/" ]; then
booze_out="./../${cowpat//|//}"
return 0
elif [[ "$1" =~ ^/($cowpat)$ ]]; then
booze_out="./.."
return 0
else
booze_err=-$ENOENT
return 1
fi
}
cs_open()
{
if [[ "$1" =~ ^/($cowpat)/[^/]+$ ]]; then
return 0
else
booze_err=-$EINVAL
return 1
fi
}
cs_read()
{
if ! [[ "$1" =~ ^/($cowpat)/[^/]+$ ]]; then
booze_err=-$EINVAL
return 1
elif [ "$3" != 0 ]; then
return 0
fi
local msg="${1#/*/}"
local cow="${1#/}"
cow="${cow%%/*}"
cowsay -f "$cow" "$msg"
}
declare -A cs_ops
for name in ${BOOZE_CALL_NAMES[@]}; do
if [ "`type -t cs_$name`" == "function" ]; then
cs_ops[$name]=cs_$name
fi
done
booze cs_ops "$1"