ref: 57cc52be5b9df84c0ffa06b21307577cbc10e32f
parent: 07a7f1fec37694de82495c433073c606ea3b483c
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Jan 19 08:16:59 EST 2017
[cc1] Fix relative inclusion "" is used to include a file from the current directory, but cc1 was not controlling that the current directory depends of the source being processed at the moment. This patch gets the current work directory (cwd) from the name of the current file.
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -444,10 +444,25 @@
return addinput(path, NULL, NULL);
}
+static char *
+cwd(char *buf)
+{
+ char *p, *s = input->fname;
+ size_t len;
+
+ if ((p = strrchr(s, '/')) == NULL)
+ return NULL;
+ if ((len = p - s) >= FILENAME_MAX)
+ die("current work directory too long");
+ memcpy(buf, s, len);
+ buf[len] = '\0';
+ return buf;
+}
+
static void
include(void)
{
- char file[FILENAME_MAX], *p, **bp;
+ char dir[FILENAME_MAX], file[FILENAME_MAX], *p, **bp;
size_t filelen;
static char *sysinclude[] = {
PREFIX "/include/scc/" ARCH "/",
@@ -490,7 +505,7 @@
if (next() != '\n')
goto trailing_characters;
- if (includefile(NULL, file, filelen))
+ if (includefile(cwd(dir), file, filelen))
goto its_done;
break;
default:
--- a/tests/execute/0062-include.c
+++ b/tests/execute/0062-include.c
@@ -1,4 +1,4 @@
#include \
-"0062-include.h"
- return 0;
+"include/0062-include.h"
+ return x;
}
--- a/tests/execute/0062-include.h
+++ /dev/null
@@ -1,3 +1,0 @@
-int
-main()
-{
--- a/tests/execute/0064-sysinclude.c
+++ b/tests/execute/0064-sysinclude.c
@@ -3,5 +3,5 @@
int
main()
{
- return x;
+ return x - y;
}
--- a/tests/execute/chktest.sh
+++ b/tests/execute/chktest.sh
@@ -8,5 +8,5 @@
do
printf "%s\t" $i
rm -f a.out
- (scc -Iinclude -m qbe "$i" && ./a.out) 2>/dev/null && echo [OK] || echo [FAILED]
+ (scc -Isysinclude -m qbe "$i" && ./a.out) 2>/dev/null && echo [OK] || echo [FAILED]
done
--- /dev/null
+++ b/tests/execute/include/0062-include.h
@@ -1,0 +1,5 @@
+#include "0062-include2.h"
+
+int
+main()
+{
--- /dev/null
+++ b/tests/execute/include/0062-include2.h
@@ -1,0 +1,2 @@
+int x;
+
--- a/tests/execute/include/0064-sysinclude.h
+++ /dev/null
@@ -1,2 +1,0 @@
-int x = 0;
-
--- /dev/null
+++ b/tests/execute/sysinclude/0064-sysinclude.h
@@ -1,0 +1,4 @@
+#include "0064-sysinclude2.h"
+
+int x = 2;
+
--- /dev/null
+++ b/tests/execute/sysinclude/0064-sysinclude2.h
@@ -1,0 +1,2 @@
+
+int y = 2;