-
Notifications
You must be signed in to change notification settings - Fork 0
/
shlibs_array
executable file
·89 lines (75 loc) · 1.34 KB
/
shlibs_array
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
#! /bin/sh
# brings array API to SH
#
# Usage :
# ARRAY=$(array_create)
# array_push $ARRAY foo
# array_push $ARRAY bar
# ...
# array_destroy $ARRAY
array_check() {
[ -z "$1" ] && return 1
[ ! -e "$1/0_shlib_array.canary" ] && return 2
return 0
}
array_create() {
ARRAY_DIR=$(mktemp -d)
echo "Array for $0 : $$" > ${ARRAY_DIR}/0_shlib_array.canary
echo $ARRAY_DIR
}
array_destroy() {
array_check "$@" && ARRAY_DIR="$1" || return 1
shift
rm -Rf "$ARRAY_DIR"
}
array_len()
{
array_check "$@" && ARRAY_DIR="$1" || return 1
shift
(
cd "$ARRAY_DIR"
LAST_ELEM=$(ls -1r | head -n 1)
[ "$LAST_ELEM" = "0_shlib_array.canary" ] && echo 0 || echo $LAST_ELEM
)
}
array_push()
{
array_check "$@" && ARRAY_DIR="$1" || return 1
shift
(
cd "$ARRAY_DIR"
NB_ELEMS=$(ls -1 | wc -l)
echo "$@" > $NB_ELEMS
)
}
array_pop()
{
array_check "$@" && ARRAY_DIR="$1" || return 1
shift
(
cd "$ARRAY_DIR"
LAST_ELEM=$(array_len $ARRAY_DIR)
[ "$LAST_ELEM" = "0" ] && return 0
cat "$LAST_ELEM"
rm -f "$LAST_ELEM"
)
}
array_idx()
{
array_check "$@" && ARRAY_DIR="$1" || return 1
shift
(
cd "$ARRAY_DIR"
[ ! -r "$1" ] && return 0
cat "$1"
)
}
array_values()
{
array_check "$@" && ARRAY_DIR="$1" || return 1
shift
(
cd "$ARRAY_DIR"
ls -1 | grep -v 0_shlib_array.canary
)
}