git.lucas.co / fontpreview
git clone https://git.lucas.co/fontpreview.git

commit347c8f3cc95c0ce0f62d813b12db40ab1a761be3
parent34ecfe4bdc
authorMateusz Piotrowski <[email protected]>
date2020-02-11 11:04
Create temporary files in a more portable way

Also:
- Do not put random strings into files inside the fontpreview directory.
  They are already unique there.
- Exit with an error code if the program fails to create the temporary
  files.

 fontpreview | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/fontpreview b/fontpreview
index 862675a..3d2ce80 100755
--- a/fontpreview
+++ b/fontpreview
@@ -4,12 +4,6 @@
 #
 # Dependencies: sxiv, imagemagick, xdotool, fzf
 
-# Use mktemp to create temporary files that won't
-# collide with any other application's tmp files.
-FONTPREVIEW_DIR="$(mktemp -d --tmpdir fontpreview_dir_XXXXXXXX)"
-PIDFILE="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.pid)"
-FONT_PREVIEW="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.png)"
-TERMWIN_IDFILE="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.termpid)"
 VERSION=1.0.3
 
 # Default values
@@ -153,6 +147,16 @@ trap "" SIGTSTP
 
 trap pre_exit EXIT
 
+# Use mktemp to create a temporary directory that won't
+# collide with temporary files of other application.
+FONTPREVIEW_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fontpreview_dir.XXXXXXXX")" || exit
+PIDFILE="$FONTPREVIEW_DIR/fontpreview.pid"
+touch "$PIDFILE" || exit
+FONT_PREVIEW="$FONTPREVIEW_DIR/fontpreview.png"
+touch "$FONT_PREVIEW" || exit
+TERMWIN_IDFILE="$FONTPREVIEW_DIR/fontpreview.termpid"
+touch "$TERMWIN_IDFILE" || exit
+
 # Parse the arguments
 options=$(getopt -o h --long position:,size:,version,search-prompt:,font-size:,bg-color:,fg-color:,preview-text:,help -- "$@")
 eval set -- "$options"