[hackers] [scc] [cc1] Fix relative inclusion || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 19 Jan 2017 13:41:08 +0100 (CET)

commit 8776d6e2e93ea4a1d3be9ed3ac3a8c2f99d2a684
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Thu Jan 19 13:16:59 2017 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Thu Jan 19 13:40:41 2017 +0100

    [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.

diff --git a/cc1/cpp.c b/cc1/cpp.c
index 610bf8c..44c8dd6 100644
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
_AT_@ -444,10 +444,25 @@ includefile(char *dir, char *file, size_t filelen)
         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 "/",
_AT_@ -490,7 +505,7 @@ include(void)
                 if (next() != '\n')
                         goto trailing_characters;
 
- if (includefile(NULL, file, filelen))
+ if (includefile(cwd(dir), file, filelen))
                         goto its_done;
                 break;
         default:
diff --git a/tests/execute/0062-include.c b/tests/execute/0062-include.c
index 12877fd..8083d50 100644
--- a/tests/execute/0062-include.c
+++ b/tests/execute/0062-include.c
_AT_@ -1,4 +1,4 @@
 #include \
-"0062-include.h"
- return 0;
+"include/0062-include.h"
+ return x;
 }
diff --git a/tests/execute/0062-include.h b/tests/execute/0062-include.h
deleted file mode 100644
index a4d76de..0000000
--- a/tests/execute/0062-include.h
+++ /dev/null
_AT_@ -1,3 +0,0 @@
-int
-main()
-{
diff --git a/tests/execute/0064-sysinclude.c b/tests/execute/0064-sysinclude.c
index 51ab801..700e481 100644
--- a/tests/execute/0064-sysinclude.c
+++ b/tests/execute/0064-sysinclude.c
_AT_@ -3,5 +3,5 @@
 int
 main()
 {
- return x;
+ return x - y;
 }
diff --git a/tests/execute/chktest.sh b/tests/execute/chktest.sh
index 950e31d..0d36c53 100755
--- a/tests/execute/chktest.sh
+++ b/tests/execute/chktest.sh
_AT_@ -8,5 +8,5 @@ for i in $@
 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
diff --git a/tests/execute/include/0062-include.h b/tests/execute/include/0062-include.h
new file mode 100644
index 0000000..939c5ab
--- /dev/null
+++ b/tests/execute/include/0062-include.h
_AT_@ -0,0 +1,5 @@
+#include "0062-include2.h"
+
+int
+main()
+{
diff --git a/tests/execute/include/0062-include2.h b/tests/execute/include/0062-include2.h
new file mode 100644
index 0000000..9bd7378
--- /dev/null
+++ b/tests/execute/include/0062-include2.h
_AT_@ -0,0 +1,2 @@
+int x;
+
diff --git a/tests/execute/include/0064-sysinclude.h b/tests/execute/include/0064-sysinclude.h
deleted file mode 100644
index fffa928..0000000
--- a/tests/execute/include/0064-sysinclude.h
+++ /dev/null
_AT_@ -1,2 +0,0 @@
-int x = 0;
-
diff --git a/tests/execute/sysinclude/0064-sysinclude.h b/tests/execute/sysinclude/0064-sysinclude.h
new file mode 100644
index 0000000..f25bbd0
--- /dev/null
+++ b/tests/execute/sysinclude/0064-sysinclude.h
_AT_@ -0,0 +1,4 @@
+#include "0064-sysinclude2.h"
+
+int x = 2;
+
diff --git a/tests/execute/sysinclude/0064-sysinclude2.h b/tests/execute/sysinclude/0064-sysinclude2.h
new file mode 100644
index 0000000..5c25525
--- /dev/null
+++ b/tests/execute/sysinclude/0064-sysinclude2.h
_AT_@ -0,0 +1,2 @@
+
+int y = 2;
Received on Thu Jan 19 2017 - 13:41:08 CET

This archive was generated by hypermail 2.3.0 : Thu Jan 19 2017 - 13:48:15 CET