Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make sure nothing bad happens here
[simgrid.git] / src / smpi / smpitools.sh
1 SAVEIFS="$IFS"
2 LISTSEP="$(printf '\b')"
3
4 # Create a temporary file, with its name of the form $1_XXX$2, where XXX is
5 # replaced by an unique string.
6 # $1: prefix, $2: suffix
7 mymktemp () {
8     tmp=$(mktemp --suffix="$2" "$1_XXXXXXXXXX" 2> /dev/null)
9     if [ -z "$tmp" ]; then
10         # mktemp failed (unsupported --suffix ?), try unsafe mode
11         tmp=$(mktemp -u "$1_XXXXXXXXXX" 2> /dev/null)
12         if [ -z "$tmp" ]; then
13             # mktemp failed again (doesn't exist ?), try very unsafe mode
14             if [ -z "${mymktemp_seq}" ]; then
15                 mymktemp_seq=$(date +%d%H%M%S)
16             fi
17             tmp="$1_$$x${mymktemp_seq}"
18             mymktemp_seq=$((mymktemp_seq + 1))
19         fi
20         tmp="${tmp}$2"
21         # create temp file, and exit if it existed before
22         sh -C -c "true > \"${tmp}\"" || exit 1
23     fi
24     echo "${tmp}"
25 }
26
27 # Add a word to the end of a list (words separated by LISTSEP)
28 # $1: list, $2...: words to add
29 list_add () {
30     local list content
31     list="$1"
32     shift
33     eval content=\"\${$list}\"
34     IFS="$LISTSEP"
35     if [ -z "$content" ]; then
36         content="$*"
37     else
38         content="$content${LISTSEP}$*"
39     fi
40     IFS="$SAVEIFS"
41     eval $list=\"\${content}\"
42 }
43
44 # Set contents of a list (words separated by LISTSEP)
45 # $1: list, $2...: words to set
46 list_set () {
47     eval $1=""
48     list_add "$@"
49 }
50
51 # Get the content of a list: positional parameters ($1, $2, ...) are set to the
52 # content of the list
53 # $1: list
54 # usage:  eval $(list_get list)
55 list_get () {
56     printf 'IFS="$LISTSEP"; eval set -- \\$%s; IFS="$SAVEIFS"' "$1"
57 }