-
Notifications
You must be signed in to change notification settings - Fork 0
/
snail_fd.sh
executable file
·48 lines (43 loc) · 1.23 KB
/
snail_fd.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
#!/usr/bin/env bash
# Copyright (c) 2017 Jean-Raphaël Gaglione
# openfd FD
function openfd {
{ [ "$1" -ge "0" ] && [ "$1" -lt "1024" ]; } 2>/dev/null || {
echo "${FUNCNAME[0]} : invalid file descriptor ‘$1’"; return 1
} >&2
eval "command >&$1" 2>/dev/null && {
echo "${FUNCNAME[0]} : file descriptor ‘$1’ already opened"; return 2
} >&2
local PIPE
PIPE=$(mktemp -u)
mkfifo "$PIPE"
eval "exec $1<>$PIPE"
rm "$PIPE"
}
# closefd FD
function closefd {
{ [ "$1" -ge "0" ] && [ "$1" -lt "1024" ]; } 2>/dev/null || {
echo "${FUNCNAME[0]} : invalid file descriptor ‘$1’"; return 1
} >&2
eval "exec $1>&-"
}
# seekfd FD [OFFSET] [WHENCE]
function seekfd {
local -a ARGS=("$@")
if [ $# -ge 3 ]; then
case "${ARGS[3]}" in
0|SET|SEEK_SET|START) ARGS[3]=0;;
1|CUR|SEEK_CUR|CURSOR) ARGS[3]=1;;
2|END|SEEK_END) ARGS[3]=2;;
*) echo "${FUNCNAME[0]} : ‘${ARGS[3]}’ : bad whence" >&2; return 1;;
esac
fi
# set -- "${ARGS[@]}"
"$SNAIL_PATH/util/seekfd" "${ARGS[@]}"
}
# rewindfd FD [OFFSET]
function rewindfd {
local -a ARGS=("$@")
# set -- "${ARGS[@]}"
"$SNAIL_PATH/util/rewindfd" "${ARGS[@]}"
}