(Disclamer: This is homework)
I am creating a shell program, lets call it fancysh. I am trying to add PATH (and other env vars) functionality to my shell, so far all is good. My naive approach was to store all these variables as static variables in fancysh.c. Now however I am trying to implement the environment variable SHLVL which holds the current "depth" of the shell. For example I can be running in the first instance of fancysh and the SHLVL should read 1, upon calling fancysh again the SHLVL should increment (and decrement when a shell is exited).
What I have tried...
fancysh.h
#ifndef FANCYSH_H
#define FANCYSH_H
extern int SHLVL;
#endif
fancysh.c
#include "fancysh.h"
int SHLVL;
int main(){
/* some fancy code to determine if SHLVL is initalized */
/* if not init to 0 */
SHLVL ++;
printf("%d\n", SHLVL);
/* Test Code Only */
int pid = fork();
if(pid == 0 && SHLVL < 10)
exec("fancysh");
wait();
/* Test Code Only */
/* shell code */
SHLVL--;
printf("%d\n", SHLVL);
exit(0);
}
I used the answers here and here as part of this solution.
So how would I go about implementing the fancy code to determine if SHLVL is initialized? I had some ideas about using a combination of #ifdef
and #define
but I'm not 100% sure how to do this.