5

I wrote a program to split an input string using ';' as the terminator and print the part of the string that is after ';'. The program shows correct output whenever substring following ';' in the input string is not a valid terminal command but also prints command not found. On the other hand, it does not prints anything when the substring followed by ';' is a valid terminal command and executes the substring as a command , e.g. in case input "sjhjh;ls" it will execute ls command.

How do I get rid of the command not found part? Here is the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char * input;
    char * str;
    char * word;
    char terminator = ';';

    if (argc < 2) {
         fprintf(stderr,"ERROR, no string provided\n");
         exit(1);
     }

     input = argv[1];
     word = strchr(input, terminator);
     if (word != NULL) printf("%s\n", word);
     return 0;
}
5
  • 1
    Guys, don't downvote this question. It's actually a quite rare and interesting example of interplay between the shell and an application.
    – DYZ
    Commented Jan 14, 2017 at 7:57
  • 1
    suggest adding the name of the shell as a tag
    – M.M
    Commented Jan 14, 2017 at 8:18
  • 2
    @DYZ I won't downvote, but its actually an example of not using the debugger or not creating an MCVE. Otherwise it would be clear, that the text never reaches the argv in main and the question could have a completely different focus.
    – grek40
    Commented Jan 14, 2017 at 8:20
  • 1
    @grek40 Fair enough. But I find it more educating the way it is.
    – DYZ
    Commented Jan 14, 2017 at 8:21
  • 1
    OP, please listen to @grek40. Get yourself an IDE like MS Visual Studio community edition, Eclipse CDT, Code::Blocks, etc. You can set breakpoints anywhere in your code and, as you debug, the program will stop at the breakpoint. When it does, you can look at the call stack and the value of your variables (you can also change their values), and more. If you do not use an IDE, you give yourself an unnecessary handicap. If you do, you won't need to ask so many questions here ;-)
    – Mawg
    Commented Jan 14, 2017 at 8:42

1 Answer 1

15

When you execute your program like:

your_program_name sjhjh;ls

on the command line, you actually invoke two programs. The first is your_program_name sjhjh (so, argv[1] is "sjhjh"), and the second is ls. What you need is to make sure that the rest of the command line goes unparsed by the shell, and this is accomplished by properly quoting it:

your_program_name 'sjhjh;ls'
3
  • Also, the OP could tighten the code a little, by changing if (argc < 2) to if (argc != 2), which would have shown the error. After all, the code does not cater fro more than a single parameter. An equality check would therefore have been better. @Romy, as you can see, just being more precise with your code can detect error for you, even avoid them. Ask your self, as you write each line of code, "what, exactly, am I trying to achieve here?"
    – Mawg
    Commented Jan 14, 2017 at 8:46
  • 3
    @Mawg hold your horses, the argc check would not reveal anything, because argv[1] would be shorter (not containing ';' and following text) but it would still exist with the text before the first ';'
    – grek40
    Commented Jan 14, 2017 at 8:56
  • You got me! (+1) Still the check would catch spaces, and, while it doesn't catch this error, it is certainly slightly better code. The main point though, as you pointed out, is to learn to use a debugger.
    – Mawg
    Commented Jan 14, 2017 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.