Skip to content

Commit

Permalink
Fix that writing tags to stdout didn't work with MSYS2
Browse files Browse the repository at this point in the history
MinGW's mkstemp() deletes a tempfile automatically when the fd is closed.
So we should not close the fd before sorting is finished. Use
redirection when external sort is used.
Additionally, tempdir was set to '/tmp', but it's not prefer for
Windows. Use TMP environment for the temporary directory.
  • Loading branch information
k-takata committed Aug 17, 2015
1 parent c62294a commit 5351779
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ fi
# Checks for header files
# -----------------------

AC_CHECK_HEADERS_ONCE([dirent.h fcntl.h stat.h stdlib.h string.h])
AC_CHECK_HEADERS_ONCE([dirent.h fcntl.h io.h stat.h stdlib.h string.h])
AC_CHECK_HEADERS_ONCE([time.h types.h unistd.h])
AC_CHECK_HEADERS_ONCE([sys/dir.h sys/stat.h sys/times.h sys/types.h sys/wait.h])

Expand Down
1 change: 1 addition & 0 deletions main/e_msoft.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define HAVE_TEMPNAM 1
#define HAVE_FNMATCH 1
#define HAVE_FNMATCH_H 1
#define HAVE_PUTENV 1
#define tempnam(dir,pfx) _tempnam(dir,pfx)
#define TMPDIR "\\"

Expand Down
16 changes: 11 additions & 5 deletions main/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ extern void openTagFile (void)
/* Open the tags file.
*/
if (TagsToStdout)
TagFile.fp = tempFile ("w", &TagFile.name);
TagFile.fp = tempFile ("w+", &TagFile.name);
else
{
boolean fileExists;
Expand Down Expand Up @@ -496,8 +496,6 @@ static void sortTagFile (void)
else if (TagsToStdout)
catFile (tagFileName ());
}
if (TagsToStdout)
remove (tagFileName ()); /* remove temporary file */
}

static void resizeTagFile (const long newSize)
Expand Down Expand Up @@ -551,11 +549,13 @@ extern void closeTagFile (const boolean resize)
if (Option.etags)
writeEtagsIncludes (TagFile.fp);
abort_if_ferror (TagFile.fp);
fflush (TagFile.fp);
desiredSize = ftell (TagFile.fp);
fseek (TagFile.fp, 0L, SEEK_END);
size = ftell (TagFile.fp);
if (fclose (TagFile.fp) != 0)
error (FATAL | PERROR, "cannot close tag file");
if (! TagsToStdout)
if (fclose (TagFile.fp) != 0)
error (FATAL | PERROR, "cannot close tag file");

if (resize && desiredSize < size)
{
Expand All @@ -565,6 +565,12 @@ extern void closeTagFile (const boolean resize)
resizeTagFile (desiredSize);
}
sortTagFile ();
if (TagsToStdout)
{
if (fclose (TagFile.fp) != 0)
error (FATAL | PERROR, "cannot close tag file");
remove (tagFileName ()); /* remove temporary file */
}
eFree (TagFile.name);
TagFile.name = NULL;
}
Expand Down
12 changes: 11 additions & 1 deletion main/routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,16 +829,26 @@ extern FILE *tempFile (const char *const mode, char **const pName)
const char *const pattern = "tags.XXXXXX";
const char *tmpdir = NULL;
fileStatus *file = eStat (ExecutableProgram);
# ifdef WIN32
tmpdir = getenv ("TMP");
# else
if (! file->isSetuid)
tmpdir = getenv ("TMPDIR");
# endif
if (tmpdir == NULL)
tmpdir = TMPDIR;
name = xMalloc (strlen (tmpdir) + 1 + strlen (pattern) + 1, char);
sprintf (name, "%s%c%s", tmpdir, OUTPUT_PATH_SEPARATOR, pattern);
fd = mkstemp (name);
eStatFree (file);
#elif defined(HAVE_TEMPNAM)
name = tempnam (TMPDIR, "tags");
const char *tmpdir = NULL;
# ifdef WIN32
tmpdir = getenv ("TMP");
# endif
if (tmpdir == NULL)
tmpdir = TMPDIR;
name = tempnam (tmpdir, "tags");
if (name == NULL)
error (FATAL | PERROR, "cannot allocate temporary file name");
fd = open (name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
Expand Down
64 changes: 47 additions & 17 deletions main/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
*/
#include "general.h" /* must always come first */

#if defined (HAVE_IO_H)
# include <io.h>
#endif
#if defined (HAVE_STDLIB_H)
# include <stdlib.h> /* to declare malloc () */
#endif
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <string.h>
#include <stdio.h>

Expand Down Expand Up @@ -53,8 +59,8 @@ extern void catFile (const char *const name)

extern void externalSortTags (const boolean toStdout)
{
const char *const sortNormalCommand = "sort -u -o";
const char *const sortFoldedCommand = "sort -u -f -o";
const char *const sortNormalCommand = "sort -u";
const char *const sortFoldedCommand = "sort -u -f";
const char *sortCommand =
Option.sorted == SO_FOLDSORTED ? sortFoldedCommand : sortNormalCommand;
PE_CONST char *const sortOrder1 = "LC_COLLATE=C";
Expand All @@ -68,29 +74,44 @@ extern void externalSortTags (const boolean toStdout)
{
/* Ensure ASCII value sort order.
*/
#ifdef HAVE_SETENV
#if defined (HAVE_SETENV) || defined (HAVE_PUTENV)
# ifdef HAVE_SETENV
setenv ("LC_COLLATE", "C", 1);
setenv ("LC_ALL", "C", 1);
sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ());
#else
# ifdef HAVE_PUTENV
# else
putenv (sortOrder1);
putenv (sortOrder2);
sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ());
# else
sprintf (cmd, "%s %s %s %s %s", sortOrder1, sortOrder2, sortCommand,
tagFileName (), tagFileName ());
# endif
if (toStdout)
sprintf (cmd, "%s", sortCommand);
else
sprintf (cmd, "%s -o %s %s", sortCommand,
tagFileName (), tagFileName ());
#else
if (toStdout)
sprintf (cmd, "%s %s %s", sortOrder1, sortOrder2, sortCommand);
else
sprintf (cmd, "%s %s %s -o %s %s", sortOrder1, sortOrder2,
sortCommand, tagFileName (), tagFileName ());
#endif
verbose ("system (\"%s\")\n", cmd);
ret = system (cmd);
if (toStdout)
{
int oldfd0;

oldfd0 = dup (0);
dup2 (fileno (TagFile.fp), 0);
lseek (0, 0, SEEK_SET);
ret = system (cmd);
dup2 (oldfd0, 0);
}
else
ret = system (cmd);
free (cmd);

}
if (ret != 0)
error (FATAL | PERROR, "cannot sort tag file");
else if (toStdout)
catFile (tagFileName ());
}

#else
Expand Down Expand Up @@ -181,9 +202,17 @@ extern void internalSortTags (const boolean toStdout)

/* Open the tag file and place its lines into allocated buffers.
*/
fp = fopen (tagFileName (), "r");
if (fp == NULL)
failedSort (fp, NULL);
if (toStdout)
{
fp = TagFile.fp;
fseek (fp, 0, SEEK_SET);
}
else
{
fp = fopen (tagFileName (), "r");
if (fp == NULL)
failedSort (fp, NULL);
}
for (i = 0 ; i < numTags && ! feof (fp) ; )
{
line = readLine (vLine, fp);
Expand All @@ -208,7 +237,8 @@ extern void internalSortTags (const boolean toStdout)
}
}
numTags = i;
fclose (fp);
if (! toStdout)
fclose (fp);
vStringDelete (vLine);

/* Sort the lines.
Expand Down

0 comments on commit 5351779

Please sign in to comment.