Blame support/support_run_diff.c

Packit 6c4009
/* Invoke the system diff tool to compare two strings.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <support/run_diff.h>
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
write_to_temp_file (const char *prefix, const char *str)
Packit 6c4009
{
Packit 6c4009
  char *template = xasprintf ("run_diff-%s", prefix);
Packit 6c4009
  char *name = NULL;
Packit 6c4009
  int fd = create_temp_file (template, &name);
Packit 6c4009
  TEST_VERIFY_EXIT (fd >= 0);
Packit 6c4009
  free (template);
Packit 6c4009
  xwrite (fd, str, strlen (str));
Packit 6c4009
  xclose (fd);
Packit 6c4009
  return name;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
support_run_diff (const char *left_label, const char *left,
Packit 6c4009
                  const char *right_label, const char *right)
Packit 6c4009
{
Packit 6c4009
  /* Ensure that the diff command output is ordered properly with
Packit 6c4009
     standard output.  */
Packit 6c4009
  TEST_VERIFY_EXIT (fflush (stdout) == 0);
Packit 6c4009
Packit 6c4009
  char *left_path = write_to_temp_file ("left-diff", left);
Packit 6c4009
  char *right_path = write_to_temp_file ("right-diff", right);
Packit 6c4009
Packit 6c4009
  pid_t pid = xfork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      execlp ("diff", "diff", "-u",
Packit 6c4009
              "--label", left_label, "--label", right_label,
Packit 6c4009
              "--", left_path, right_path,
Packit 6c4009
              NULL);
Packit 6c4009
      _exit (17);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      int status;
Packit 6c4009
      xwaitpid (pid, &status, 0);
Packit 6c4009
      if (!WIFEXITED (status) || WEXITSTATUS (status) != 1)
Packit 6c4009
        printf ("warning: could not run diff, exit status: %d\n"
Packit 6c4009
                "*** %s ***\n%s\n"
Packit 6c4009
                "*** %s ***\n%s\n",
Packit 6c4009
                status, left_label, left, right_label, right);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (right_path);
Packit 6c4009
  free (left_path);
Packit 6c4009
}