0

I am recreating a basic bash like shell and I use getcwd for determining the path I am currently in to print it in a nice way.

I have this in my header file:

#define BLUE "\e[1;36m"
#define WHITE "\e[0;00m"
#define PWD getcwd((NULL), 0)
#define PROMPT BLUE PWD WHITE

Then, I try to print PROMPT using a putstr but when I compile I get this error:

cc -g3   -c -o src/minishell.o src/minishell.c
In file included from src/minishell.c:9:
src/minishell.c: In function ‘minishell’:
src/../include/minishell2.h:14:13: error: expected ‘)’ before ‘getcwd’
   14 | #define PWD getcwd((NULL), 0)
      |             ^~~~~~
src/../include/minishell2.h:15:21: note: in expansion of macro ‘PWD’
   15 | #define PROMPT BLUE PWD WHITE
      |                     ^~~
src/minishell.c:36:15: note: in expansion of macro ‘PROMPT’
   36 |     my_putstr(PROMPT);
      |               ^~~~~~
src/minishell.c:36:14: note: to match this ‘(’
   36 |     my_putstr(PROMPT);
      |              ^
make: *** [<builtin>: src/minishell.o] Error 1

I would love some help on how I can define a string and print it the same way I call any other string but use the color variables I have set with BLUE and WHITE Thanks!

Blifix
  • 1

1 Answers1

2

It looks like you expect BLUE PWD WHITE to concatenate the strings. That will not work. Adjacent string literals are concatenated during compilation; "abc" "def" will become "abcdef". But PWD is not a string literal; it is getcwd((NULL), 0). The getcwd routine returns a string at run-time. You cannot concatenate it that way. The easiest solution may be to write three separate calls to my_putstr, one for BLUE, one for PWD, and one for WHITE. Alternatively, you need to write additional code to concatenate strings.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I thought there would be a way to add them all together during the header file process but I guess I will go the route you have given, thanks! – Blifix Apr 18 '22 at 15:52