Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Variable exists only with SMPI_F2C.
[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 newcontent
31     list="$1"
32     shift
33     if [ $# -gt 0 ]; then
34         eval content=\"\${$list}\"
35         IFS="$LISTSEP"
36         newcontent="$*"
37         IFS="$SAVEIFS"
38         if [ -z "$content" ]; then
39             content="$newcontent"
40         else
41             content="$content${LISTSEP}$newcontent"
42         fi
43         eval $list=\"\${content}\"
44     fi
45 }
46
47 # Like list_add, but only if first word to add ($2) is not empty
48 list_add_not_empty () {
49     if [ -n "$2" ]; then
50         list_add "$@"
51     fi
52 }
53
54 # Set contents of a list (words separated by LISTSEP)
55 # $1: list, $2...: words to set
56 list_set () {
57     eval $1=""
58     list_add "$@"
59 }
60
61 # Get the content of a list: positional parameters ($1, $2, ...) are set to the
62 # content of the list
63 # $1: list
64 # usage:  eval $(list_get list)
65 list_get () {
66     printf 'IFS="$LISTSEP"; eval set -- \\$%s; IFS="$SAVEIFS"' "$1"
67 }