summaryrefslogtreecommitdiffstats
path: root/tests/fileio.rc
blob: 58871b3b97a5fc3e588224ac5f0a02ab43fd49fd (plain)
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
#!/bin/bash

function fd_available() {
         for i in {1..65536}; do
	     if [ ! -e /proc/$$/fd/$i ]; then
		 echo $i;
		 return 0;
	     fi
	 done

	 return 1;
}

function fd_open() {
	 local fd=$1;
	 local mode=$2
	 local path=$3;

	 case $mode in
	     r)
		 eval "exec $fd<$path";;
	     w)
		 eval "exec $fd>$path";;
	     rw)
		 eval "exec $fd<>$path";;
	     *)
		 false;;
	 esac
}


function fd_cat() {
	 local fd=$1;

	 eval "cat <&$fd";
}


function fd_write() {
	 local fd=$1;
	 shift;
	 local msg="$@";

	 eval "echo $@ >&$fd";
}


function fd_close() {
	 local fd=$1;

	 eval "exec $fd>&-";
}


function fd_based_example() {
    TEST fd=`fd_available`;
    TEST fd_open $fd "rw" $M0/filename;
    TEST fd_cat $fd; # print existing stuff
    TEST fd_write $fd "new stuff"; # append
    TEST fd_close $fd;
}