From 5f448e339d7491bb887fcf74022aec3087991376 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Mon, 04 Jan 2021 13:55:46 -0500 Subject: fdalloc(): New function --- 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 } -- cgit v0.9.1