CPP filename translation: problem with . or .. at end of pathname

Graham Shaw gdshaw at sagitta.demon.co.uk
Tue Jun 1 06:37:08 PDT 2004


CPP does not appear able to find files when given an include path such as
"../..".

The problem appears to lie in the function get_directory_name() (in
gcc/gcc/config/arm/rname.c) and affects any path ending in "." or "..".

(This function correctly handles "." and ".." when they appear in the
middle of a pathname, followed by "/", but not at the end.  Quite possibly
it was written with files in mind rather than directories.)

Attached is a patch against GCC 2.95.  I've had a quick look at the head
revision and it should work there too.  It returns ".\0" and "..\0"
without conversion, while continuing to handle "./" and "../" in the
existing manner.

-- 
Graham Shaw (http://www.sagitta.demon.co.uk/graham/)
The RISC OS Packaging Project (http://www.riscpkg.org/)
The RISC OS Toolkit (http://rtk.riscos.org.uk/)
-------------- next part --------------
diff -ru upstream/gcc/gcc/config/arm/rname.c patched/gcc/gcc/config/arm/rname.c
--- upstream/gcc/gcc/config/arm/rname.c	Mon May 31 20:06:31 2004
+++ patched/gcc/gcc/config/arm/rname.c	Mon May 31 20:42:57 2004
@@ -34,7 +34,7 @@
 static int
 get_directory_name (const char *input, char *output)
 {
-  const char *t;
+  const char *t = 0;
 
   if (*input == '\0')
     return 0;
@@ -47,24 +47,39 @@
     {
       if (input[0] == '.')
 	{
-	  if (input[1] == '/')	/* current directory */
+	  if (input[1] == '\0')        /* current directory, end of path */
+	    t = 0;
+	  else if (input[1] == '/')    /* current directory, not end of path */
 	    t = input + 1;
-	  else if (input[1] == '.' && input[2] == '/')	/* parent directory */
-	    t = input + 2;
+	  else if (input[1] == '.')
+	   {
+	     if (input[2] == '\0')     /* parent directory, end of path */
+	       t = 0;
+	     else if (input[2] == '/') /* parent directory, not end of path */
+	       t = input + 2;
+	   }
 	}
-      strncpy (output, input, t - input);
-      output[t - input] = '\0';
-      return 1;
     }
 #endif
-  /* If we reach here, we have two possibilities:
+
+  if (t)
+  {
+    strncpy (output, input, t - input);
+    output[t - input] = '\0';
+    return 1;
+  }
+  else
+  {
+  /* If we reach here, we have four possibilities:
      1. fname
      2. directory/fname
-
-     both don't need any conversion. No.1 has both compatible with RISC OS
-     and Unix, and no. 2 is already in Unix form.  */
-  strcpy (output, input);
-  return 0;
+     3. .
+     4. ..
+     none of which need any conversion. No.1 has both compatible with
+     RISC OS and Unix, and nos. 2-4 are already in Unix form.  */
+    strcpy (output, input);
+    return 0;
+  }
 }
 
 static char *


More information about the gcc mailing list