This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PR/15448: add a compiler timestamp to PCH files (take 2)


Paolo Bonzini wrote:
Gawd, no.  make_relative_prefix in libiberty already knows how to find
a binary given argv[0]; let's use that algorithm, instead of messing
with /proc.


Very much fine with me. See the attached patch, ok for mainline? (Bootstrapped/regtested on powerpc-darwin).

And here is the new file, path-search.c.


Paolo

/* Path search support
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is part of libiberty.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

/*

@deftypefn Extension {const char*} path_search (const char *@var{progname}, const char *@var{search_path}, int @var{executable})

Given a filename and a search path, if the filename has a path, return a
copy of the filename resolving .. directories and symbolic links.  Otherwise,
find the full path to a file with that name, also resolve .. directories
and symbolic links, and return malloc-ed storage from it.  If the file no
path can be found, return NULL.

If @var{executable} is non-zero, only look for an executable file.
Otherwise look for any file.
@end deftypefn

*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <string.h>

#include "ansidecl.h"
#include "libiberty.h"

#ifndef R_OK
#define R_OK 4
#define W_OK 2
#define X_OK 1
#endif

#ifndef DIR_SEPARATOR
#  define DIR_SEPARATOR '/'
#endif

#if defined (_WIN32) || defined (__MSDOS__) \
    || defined (__DJGPP__) || defined (__OS2__)
#  define HAVE_HOST_EXECUTABLE_SUFFIX
#  define HOST_EXECUTABLE_SUFFIX ".exe"
#  ifndef DIR_SEPARATOR_2 
#    define DIR_SEPARATOR_2 '\\'
#  endif
#  define PATH_SEPARATOR ';'
#else
#  define PATH_SEPARATOR ':'
#endif

#ifndef DIR_SEPARATOR_2
#  define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
#else
#  define IS_DIR_SEPARATOR(ch) \
	(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
#endif

/* Given a filename and a search path, if the filename has a path, return a
   copy of the filename resolving .. directories and symbolic links.  Otherwise,
   find the full path to a file with that name, also resolve .. directories
   and symbolic links, and return malloc-ed storage from it.  If the file no
   path can be found, return NULL.

   If EXECUTABLE is non-zero, only look for an executable file.  Otherwise look
   for any file.  */

char *
path_search (progname, search_path, executable)
     const char *progname;
     const char *search_path;
     int executable;
{
  const char *startp, *endp;
  char *nstore;
  size_t prefixlen;

  if (lbasename (progname) != progname)
    return lrealpath (progname);

  if (search_path == NULL)
    return NULL;

  prefixlen = strlen (search_path) + 1;
  if (prefixlen < 2)
    prefixlen = 2;

  nstore = (char *) alloca (prefixlen + strlen (progname) + 1);

  for (startp = endp = search_path; ; endp = startp = endp + 1)
    {
      while (*endp != PATH_SEPARATOR && *endp != 0)
	endp++;

      if (endp == startp)
	{
	  nstore[0] = '.';
	  nstore[1] = DIR_SEPARATOR;
	  nstore[2] = '\0';
	}
      else
	{
	  memcpy (nstore, startp, endp - startp);
	  if (! IS_DIR_SEPARATOR (endp[-1]))
	    {
	      nstore[endp - startp] = DIR_SEPARATOR;
	      nstore[endp - startp + 1] = 0;
	    }
	  else
	    nstore[endp - startp] = 0;
	}
      strcat (nstore, progname);
      if (executable
          ? !access (nstore, X_OK)
#ifdef HAVE_HOST_EXECUTABLE_SUFFIX
            || !access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK)
#endif
	  : !access (nstore, R_OK))
	return lrealpath (nstore);

      if (*endp == 0)
	return NULL;
    }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]