Hi everyone. I've been dusting off some old projects lately and have found that one of my programs no longer works as it should. I've done internet and forum searches and can't find an answer.
The program is one I shared here some time back. It is a basic Linux command line interpreter. It has the ability run external programs as well as five(so far) internal functions. The problem is that three of those internal commands no longer seem to work.
Here are the relevant sections of code:
Code:
/*built-in change directory command*/
int cd()
{
if(strchr(intern_argv[1],'~'))
chdir(getenv("HOME"));
else
chdir(intern_argv[1]);
return(0);
}
/*built-in pipe support*/
int run_pipe()
{
puts("Sorry,pipe support is not yet implemented.");
return(0);
}
/*built-in redirect support*/
int redirect()
{
puts("Sorry,redirection is not yet implemented.");
return(0);
}
/*built-in version command*/
int version()
{
puts("nnsh,Version 0.5");
}
Code:
switch(c){
case '\n':if(tmp[0]=='\0'){
show_prompt();
}else{
fill_intern_argv(tmp);
if(strchr(tmp,'|'))
{
run_pipe();
}
else if(strchr(tmp,'>') || strchr(tmp,'<'))
{
redirect();
}
else if(strcmp(intern_argv[0],"cd")==0)
{
cd();
}
else if(strcmp(intern_argv[0],"version") == 0)
{
version();
}
else if(strcmp(intern_argv[0],"exit")==0)
{
exit(0);
}
else
{
call_execvp();
}
free_intern_argv();
These above are the two sections of code responsible for determining command line input and taking the appropriate action.
External commands run as they should. The functions that recognize attempted use of pipes or redirects works. The trouble is that the internal cd, version, and exit commands no longer work. I get the built-in command not found error and the prompt simply returns.
gdb shows no abnormal working of the program.
gcc -Q show all functions are present and being compiled.
a simple gcc filename.c -o filename compilation works without warning or error.
Running it through the Scite editor produces the following:
>gcc -pedantic -Os -c nnsh.c -o nnsh.o -std=c99
nnsh.c: In function ‘show_prompt’:
nnsh.c:40: warning: ignoring return value of ‘getcwd’, declared with attribute warn_unused_result
nnsh.c: In function ‘cd’:
nnsh.c:56: warning: ignoring return value of ‘chdir’, declared with attribute warn_unused_result
nnsh.c:58: warning: ignoring return value of ‘chdir’, declared with attribute warn_unused_result
>Exit code: 0
All I really know at this point is that the program once worked as it was intended, and now it doesn't. I haven't changed any of those functions since it was last working.
Any ideas?