Thread: Need advice, program no longer fully functional.

  1. #1
    Registered User bchaffin72's Avatar
    Join Date
    Mar 2009
    Location
    Ontario
    Posts
    13

    Need advice, program no longer fully functional.

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > nnsh.c:40: warning: ignoring return value of ‘getcwd’, declared with attribute warn_unused_result
    Have you read the manual page for getcwd()

    Does it return a result?

    Are you paying attention to that result, or are you blindly assuming that it will always succeed?

    It is TELLING you that you are ignoring the result, and that might be a problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User bchaffin72's Avatar
    Join Date
    Mar 2009
    Location
    Ontario
    Posts
    13
    Thanks Salem, I'll take a look at that. Those functions do have return values that can be checked. It seems the Scite editor uses compiler options I rarely had before. These warnings could have always been there, and I just wasn't aware of them until now. I don't know if that is the source of the problem, but I will be re-coding for those checks to see and because I don't like warnings to persist once I am aware of them.

  4. #4
    Registered User bchaffin72's Avatar
    Join Date
    Mar 2009
    Location
    Ontario
    Posts
    13
    I figured it out. While using Scite's find and replace function, I accidentally replaced an argument name I shouldn't have. Since it was in an area seemingly unrelated to the problem, it took a while of just staring at the code to see it. On the other hand, I did fix the unused results errors by coding in the appropriate checks, so thanks again for pointing that out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Chat program advice
    By gogo in forum C++ Programming
    Replies: 11
    Last Post: 01-04-2002, 10:45 AM
  3. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM