summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fd.sh39
1 files changed, 24 insertions, 15 deletions
diff --git a/src/fd.sh b/src/fd.sh
index 45b1423..b929dfa 100644
--- a/src/fd.sh
+++ b/src/fd.sh
@@ -38,22 +38,11 @@ _FD_MAX=9
FD=
-fopen()
+fdalloc()
{
- local path="${1}"
- local mode="${2}"
- shift 2
local i=
local fd=
- case "${mode}" in
- 'r') mode='<' ;;
- 'w') mode='>' ;;
- 'a') mode='>>';;
- 'rw') mode='<>';;
- *) return 125;;
- esac
-
# Find first available file descriptor.
i=${_FD_MIN}
while [ ${i} -le ${_FD_MAX} ]; do
@@ -69,12 +58,32 @@ fopen()
return 1
fi
- if eval "exec ${fd}${mode}\"\${path}\""; then
- eval "_fd_${fd}=\"\${mode}\${path}\""
- FD="${fd}"
+ FD="${fd}"
+ return 0
+}
+
+fopen()
+{
+ local path="${1}"
+ local mode="${2}"
+ shift 2
+
+ case "${mode}" in
+ 'r') mode='<' ;;
+ 'w') mode='>' ;;
+ 'a') mode='>>';;
+ 'rw') mode='<>';;
+ *) return 125;;
+ esac
+
+ fdalloc || return 1
+
+ if eval "exec ${FD}${mode}\"\${path}\""; then
+ eval "_fd_${FD}=\"\${mode}\${path}\""
return 0
else
error "$(get_msg 'cant_fopen')"
+ FD=
return 1
fi
}