Ever since an update a few months ago, command-line tools on CentOS that happen to invoke ghostscript appear broken. Sometimes they fail with an error message; sometimes they succeed partially, but an error message is still displayed. Bleeping annoying.

Here is one example, as I tried to convert a valid .ps (PostScript) file into an EPS file:

$ ps2epsi g0.ps g0.epsi
Error: /invalidaccess in --.locksafe--
Operand stack:
   true   --nostringval--   --dict:1169/1684(ro)(G)--   getenv   --nostringval--
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2
   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1
   %stopped_push   1884   1   4   %oparray_pop   1883   1   4
   %oparray_pop   1867   1   4   %oparray_pop   1755   1   4
   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2
   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push
   --nostringval--   --nostringval--   1892   0   5   %oparray_pop   1891   0   5
   %oparray_pop   --nostringval--
Dictionary stack:
   --dict:1169/1684(ro)(G)--   --dict:0/20(G)--   --dict:81/200(L)--
   --dict:5/30(L)--   --dict:20/30(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 8558
GPL Ghostscript 9.07: Unrecoverable error, exit code 1

Bleeping annoying, and with no fix in sight, I had to do something. A bit of online searching led me to a hint as to the right solution: an error in gs_init.ps. The correct solution, in CentOS 7.2, then, is given by this diff:

--- /usr/share/ghostscript/9.07/Resource/Init/gs_init-orig.ps 2017-03-03 03:07:30.000000000 -0500
+++ /usr/share/ghostscript/9.07/Resource/Init/gs_init.ps      2017-03-27 23:53:34.612075548 -0400
@@ -2019,7 +2019,7 @@
       /LockFilePermissions //true
     >> setuserparams
   }
-  systemdict /getenv {pop //false} put
+  systemdict /getenv {pop //false} .forceput
   if
   % setpagedevice has the side effect of clearing the page, but
   % we will just document that. Using setpagedevice keeps the device

So far, it seems to do the trick. Let's hope that eventually, the package gets fixed, but until then, I recorded the fix here for posterity.