summaryrefslogtreecommitdiffstats
path: root/libsh/stdio.sh
blob: 0d00fa5db8cd9182ed73b8fb697802f131c1b4f8 (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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright (C) 2013, 2016  Patrick "P. J." McDermott
#
# This file is part of libsh.
#
# libsh is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libsh is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with libsh.  If not, see
# <http://www.gnu.org/licenses/>.

# Shells and the file descriptors they reserve for the user:
#   * Debian Almquist Shell
#     - The shell doesn't understand any file descriptor greater than 9.
#     - File descriptors starting at 10 are used for I/O redirection.
#   * BusyBox ash
#     - File descriptors starting at 10 are used for job control and I/O
#       redirection.
#   * GNU Bash:
#     - GNU Readline uses file descriptor 255 in interactive shells.
#     - File descriptors starting at 10 are used for I/O redirection.
#   * MirOS Korn Shell
#     - File descriptors starting at either 10 or 24 are used for the shell's
#       tty_fd and shl_dbg_fd.
#   * Solaris ksh
#     - The shell doesn't understand any file descriptor greater than 9.
static fd_min=3
static fd_max=9

static fd_3_set=false
static fd_4_set=false
static fd_5_set=false
static fd_6_set=false
static fd_7_set=false
static fd_8_set=false
static fd_9_set=false

fd=0

fopen(string path, string mode)
{
	local mode_r=
	local mode_w=
	local i=
	local f=

	case "${mode}" in
		'r')  mode_r=true  mode_w=false mode='<' ;;
		'r+') mode_r=true  mode_w=true  mode='<>';;
		'w')  mode_r=false mode_w=true  mode='>' ;;
		'a')  mode_r=false mode_w=true  mode='>>';;
		*) return ${EINVAL};;
	esac

	# Find first available file descriptor.
	i=${fd_min}
	while [ ${i} -le ${fd_max} ]; do
		if ! $(eval "printf '%s' \${fd_${i}_set}"); then
			f=${i}
			break
		fi
		i=$(($i + 1))
	done
	if [ "x${f}" = 'x' ]; then
		return ${EMFILE}
	fi

	# Check file mode and existence.
	if ${mode_r} && ! [ -r "${path}" ]; then
		return ${EACCES}
	fi
	if ${mode_w} && ! [ -w "${path}" ]; then
		return ${EACCES}
	fi
	if ! [ -e "${path}" ]; then
		return ${ENOENT}
	fi
	if [ -d "${path}" ]; then
		return ${EISDIR}
	fi

	if ! eval "exec ${f}${mode}'${path}'" 2>/dev/null; then
		return ${EUNK}
	fi

	eval "fd_${f}_set=true"
	fd="${f}"
	return 0
}

fclose(int f)
{
	local f=${1}

	# Make sure the file descriptor is valid and open.
	if [ ${f} -lt ${fd_min} ] || [ ${f} -gt ${fd_max} ]; then
		return ${EINVAL}
	fi
	if $(eval "printf '%s' \${fd_${i}_set}"); then
		return ${EBADF}
	fi

	eval "exec ${f}>&-"
	unset "fd_${f}"

	return 0
}