Patchwork [libiberty,include] : Add additional helper functions for directory-separator searching

login
register
mail settings
Submitter Kai Tietz
Date March 8, 2011, 3:29 p.m.
Message ID <AANLkTiksa6=7MWxeyDvgzBs89McAEgG12o+x41K02_OH@mail.gmail.com>
Download mbox | patch
Permalink /patch/86019/
State New
Headers show

Comments

Kai Tietz - March 8, 2011, 3:29 p.m.
Umm, sorry. I found a wrong copy & paste. So I re-sent the corrected
patch. Additionally I adjuste the changes in Makefile.in so, that
alphabetic order remains.

Kai
Eli Zaretskii - March 8, 2011, 6:35 p.m.
> Date: Tue, 8 Mar 2011 16:29:51 +0100
> From: Kai Tietz <ktietz70@googlemail.com>
> 
> Umm, sorry. I found a wrong copy & paste. So I re-sent the corrected
> patch. Additionally I adjuste the changes in Makefile.in so, that
> alphabetic order remains.

I think we don't need filename_dirchr, only filename_dirrchr.

And you didn't change anything in the documentation to address my
comments earlier today.
DJ Delorie - March 8, 2011, 7:41 p.m.
> I think we don't need filename_dirchr, only filename_dirrchr.

I see no harm in having both, for completeness, though.  One could
argue they should be in separate files, but they're trivial functions
on non-dos-fs systems.

What bothers me about this patch is that we're adding yet another set
of functions that don't discriminate between the host filesystem, the
target filesystem, and the build filesystem.
Eli Zaretskii - March 8, 2011, 7:58 p.m.
> Date: Tue, 8 Mar 2011 14:41:00 -0500
> From: DJ Delorie <dj@redhat.com>
> CC: ktietz70@googlemail.com, binutils@sourceware.org,        gdb-patches@sourceware.org, gcc-patches@gcc.gnu.org
> 
> 
> I see no harm in having both, for completeness, though.

I don't see any need for completeness in this case.  But it's your
call.  I still think that the documentation should be fixed, though.
Pedro Alves - March 8, 2011, 10:37 p.m.
Actually, is there any case where lbasename wouldn't
work instead of filename_dirrchr?

(gdb is already making use of 
unix_lbasename / dos_lbasename at places where it
needs to handle host vs target paths).
Eli Zaretskii - March 9, 2011, 5:29 a.m.
> From: Pedro Alves <pedro@codesourcery.com>
> Date: Tue, 8 Mar 2011 22:37:59 +0000
> Cc: DJ Delorie <dj@redhat.com>,
>  Eli Zaretskii <eliz@gnu.org>,
>  ktietz70@googlemail.com,
>  binutils@sourceware.org,
>  gcc-patches@gcc.gnu.org
> 
> Actually, is there any case where lbasename wouldn't
> work instead of filename_dirrchr?

Almost: lbasename returns a pointer one character _after_ the last
slash.  It also skips the drive letter on DOS/Windows (which might be
TRT, actually).

It would be reasonable to rewrite filename_dirrchr in terms of
lbasename, though, by backing up the pointer returned by lbasename if
it points to a slash, and otherwise returning NULL.  The case of
"d:foo" should also be resolved (probably, return a pointer to the
colon).

Patch

Index: gcc/include/filenames.h
===================================================================
--- gcc.orig/include/filenames.h	2011-02-28 19:16:35.000000000 +0100
+++ gcc/include/filenames.h	2011-03-08 11:11:10.909109700 +0100
@@ -75,6 +75,8 @@  extern int filename_cmp (const char *s1,
 
 extern int filename_ncmp (const char *s1, const char *s2,
 			  size_t n);
+extern char *filename_dirchr (const char *p);
+extern char *filename_dirrchr (const char *p);
 
 #ifdef __cplusplus
 }
Index: gcc/libiberty/functions.texi
===================================================================
--- gcc.orig/libiberty/functions.texi	2011-02-28 19:16:35.000000000 +0100
+++ gcc/libiberty/functions.texi	2011-03-08 16:26:29.547971700 +0100
@@ -296,6 +296,30 @@  and backward slashes are equal.
 
 @end deftypefn
 
+@c filename_chr.c:32
+@deftypefn Extension int filename_dirchr (const char *@var{p})
+
+The returned value is similar to what @code{strchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name.  However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
+@c filename_chr.c:65
+@deftypefn Extension int filename_dirrchr (const char *@var{p})
+
+The returned value is similar to what @code{strrchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name.  However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
 @c filename_cmp.c:81
 @deftypefn Extension int filename_ncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n})
 
Index: gcc/libiberty/Makefile.in
===================================================================
--- gcc.orig/libiberty/Makefile.in	2010-11-21 14:28:05.000000000 +0100
+++ gcc/libiberty/Makefile.in	2011-03-08 16:24:17.703229600 +0100
@@ -127,8 +127,8 @@  CFILES = alloca.c argv.c asprintf.c atex
 	calloc.c choose-temp.c clock.c concat.c cp-demangle.c		\
 	 cp-demint.c cplus-dem.c crc32.c				\
 	dyn-string.c							\
-	fdmatch.c ffs.c fibheap.c filename_cmp.c floatformat.c		\
-	fnmatch.c fopen_unlocked.c					\
+	fdmatch.c ffs.c fibheap.c filename_chr.c filename_cmp.c		\
+	floatformat.c fnmatch.c fopen_unlocked.c			\
 	getcwd.c getopt.c getopt1.c getpagesize.c getpwd.c getruntime.c	\
          gettimeofday.c                                                 \
 	hashtab.c hex.c							\
@@ -168,7 +168,8 @@  REQUIRED_OFILES =							\
 	./choose-temp.$(objext) ./concat.$(objext)			\
 	./cp-demint.$(objext) ./crc32.$(objext) ./dyn-string.$(objext)	\
 	./fdmatch.$(objext) ./fibheap.$(objext)				\
-	./filename_cmp.$(objext) ./floatformat.$(objext)		\
+	./filename_chr.$(objext) ./filename_cmp.$(objext)		\
+	./floatformat.$(objext)						\
 	./fnmatch.$(objext) ./fopen_unlocked.$(objext)			\
 	./getopt.$(objext) ./getopt1.$(objext) ./getpwd.$(objext)	\
 	./getruntime.$(objext) ./hashtab.$(objext) ./hex.$(objext)	\
@@ -646,6 +647,13 @@  $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/fibheap.c $(OUTPUT_OPTION)
 
+./filename_chr.$(objext): $(srcdir)/filename_chr.c config.h $(INCDIR)/filenames.h \
+	$(INCDIR)/safe-ctype.h
+	if [ x"$(PICFLAG)" != x ]; then \
+	  $(COMPILE.c) $(PICFLAG) $(srcdir)/filename_chr.c -o pic/$@; \
+	else true; fi
+	$(COMPILE.c) $(srcdir)/filename_chr.c $(OUTPUT_OPTION)
+
 ./filename_cmp.$(objext): $(srcdir)/filename_cmp.c config.h $(INCDIR)/filenames.h \
 	$(INCDIR)/safe-ctype.h
 	if [ x"$(PICFLAG)" != x ]; then \
Index: gcc/libiberty/filename_chr.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gcc/libiberty/filename_chr.c	2011-03-08 16:22:51.303258200 +0100
@@ -0,0 +1,95 @@ 
+/* File name character searching routines.
+
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This program 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.
+
+   This program 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 this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include "filenames.h"
+#include "safe-ctype.h"
+
+/*
+
+@deftypefn Extension int filename_dirchr (const char *@var{p})
+
+The returned value is similar to what @code{strchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name.  However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
+*/
+
+char *
+filename_dirchr (const char *p)
+{
+  char *r;
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+  char *r2;
+#endif
+  if (!p)
+    return NULL;
+  r = strchr (p, '/');
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+  r2 = strchr (p, '\\');
+  if (!r || (r2 && r2 < r))
+    r = r2;
+#endif
+  return r;
+}
+
+/*
+
+@deftypefn Extension int filename_dirrchr (const char *@var{p})
+
+The returned value is similar to what @code{strrchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name.  However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
+*/
+
+char *
+filename_dirrchr (const char *p)
+{
+  char *r;
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+  char *r2;
+#endif
+
+  if (!p)
+    return NULL;
+  r = strrchr (p, '/');
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+  r2 = strrchr (p, '\\');
+  if (!r || (r2 && r2 > r))
+    r = r2;
+#endif
+  return r;
+}