Skip to content

Commit

Permalink
main: reserve errnor when system(3) fails
Browse files Browse the repository at this point in the history
This change fixes that ctags reports the following strange error
message:

  $ /usr/bin/ctags --quiet --options=NONE -o - main/*.c | no-such-command
  zsh: no-such-command: command not found...
  ctags: cannot sort tag file : Success

With this change, ctags reports:

  ./ctags --quiet --options=NONE -o - main/*.c | no-such-command
  zsh: no-such-command: command not found...
  ctags: cannot sort tag file

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Jul 22, 2023
1 parent f412cbf commit 2af6f05
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion main/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
#include "general.h" /* must always come first */

#include <errno.h>
#if defined (HAVE_IO_H)
# include <io.h>
#endif
Expand Down Expand Up @@ -93,6 +94,7 @@ extern void externalSortTags (const bool toStdout, MIO *tagFile)
PE_CONST char *const sortOrder2 = "LC_ALL=C";
# endif
int ret = -1;
int system_errno = 0;

{
vString *cmd = vStringNew ();
Expand Down Expand Up @@ -143,16 +145,28 @@ extern void externalSortTags (const bool toStdout, MIO *tagFile)
if (lseek (fdstdin, 0, SEEK_SET) != 0)
error (FATAL | PERROR, "cannot rewind tag file");
ret = system (vStringValue (cmd));
system_errno = errno;
if (dup2 (fdsave, fdstdin) < 0)
error (FATAL | PERROR, "cannot restore stdin fd");
close (fdsave);
}
else
{
ret = system (vStringValue (cmd));
system_errno = errno;
}
vStringDelete (cmd);
}
if (ret != 0)
error (FATAL | PERROR, "cannot sort tag file");
{
errorSelection selection = FATAL;
if (ret == -1)
{
selection |= PERROR;
errno = system_errno;
}
error (selection, "cannot sort tag file");
}
}

#else
Expand Down

0 comments on commit 2af6f05

Please sign in to comment.