summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2021-01-04 13:55:46 (EST)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2021-01-04 13:55:46 (EST)
commit5f448e339d7491bb887fcf74022aec3087991376 (patch)
tree343eb2b8a609fa3242ec1d8483cc452ad4947b47 /src
parent89a79ad7217e1d2618a6c247402e2d7b562f3e9d (diff)
fdalloc(): New function
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
}