Thread: system call with user input

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    system call with user input

    after my first week of reading and doing some tutorials i decided to try my own program. i run a liux machine with enlightenment desktop. so i decided to try a menu c program to make some things easy for me. i know bash shell scripting and wrote a nice script, but want to import it to the C lanquage.

    i am trying to call the whois command with the domain name from the user input for the domain name or ip address. the problem is after calling the whois -h whois.arin.net command i need the doman or ip to follow to be able to look it up. please help with suggestions. if i can learn this, that will help me out a lot with other programs i want to take from the bash script to the C language. this is just one of many that will be linked to one large c program.

    here is the code.

    do i use the gets or scanf or would it be better to use a pointer. ?????????

    #include <stdio.h>
    #include <stdlib.h>
    #define exit 4

    int menu(void); /* main menu */
    int arin(void); /* 1. arin.net */
    int apnic(void); /* 2. apnic.net */
    int ripe(void); /* 3. ripe.net */
    char input[100];

    int main()
    {
    int choice = 0;
    while (choice != exit)
    {
    choice = menu();
    if (choice == 1)
    arin();
    else
    if (choice == 2)
    apnic();
    else
    if (choice == 3)
    ripe();
    }
    printf("\n\nThank you for using linshell\n\n");

    return 0;
    }

    /* arin.net */
    int arin(void)
    {
    printf("Domain or IP: ");
    gets(input);
    system("whois -h whois.arine.net %d",input);
    }


    /* apnic.net */
    int apnic(void)
    {
    printf("Domain or IP: ");
    }


    /* ripe.net */
    int ripe(void)
    {
    printf("Domain or IP: ");
    }



    /* display the main menu */
    int menu(void)
    {
    int option = 0;
    do
    {
    printf("\n\n");
    printf("\t1. arin\n");
    printf("\t2. apnic\n");
    printf("\t3. ripe\n");
    printf("\t4. exit");
    printf("\n\n");
    printf(" Select: ");
    scanf("%d", &option);
    }

    while (option < 1 || option > 4);
    return option;
    }

    after reading many websites and doing the tutorials i will be getting a book.

    thanks,

    enlinux

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    great colors

    just read the board about getting color in text. works great. from know on, i don't think i will be doing any more shell scripting. ok so maybe both. ha ha ha ha

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    First of all: use CODE TAGS

    > gets(input);

    This function is WRONG, never use it.
    Instead use the fgets function (read the BUGS section on the fgets man page)

    fgets(input, sizeof(input), stdin)

    Remember that the fgets function will also store the newline character.

    > scanf("%d", &option);

    Better check the return value of scanf.
    Another option to do this is read the input with the fgets function and then use the sscanf function to get the option.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>system("whois -h whois.arine.net %d",input);
    Use sprintf(), something like in here. It doesn't have a sprintf() example, but hopefully can get the idea.

    [edit]
    A sprintf() example
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    did not work

    still can't get it. looked on the message board and on google. just will have to stay with the shell script i wrote for now.

    it works when i use printf but not with system

    lets say i want to use the linux command ping to test my server is up.

    [code] /*

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

    int main()

    {
    char ipadd;

    printf("enter ip address: ");
    scanf("%d", &ipadd);
    system("ping -c 4 %s",ipadd);

    return 0;
    }

    this will not compile. i have messed around and tried everything.

    maybe this is over my head, since i have been reading about c for 1 week. but it can't be that hard. i can write it in perl and shell scripting.

    can some one give me a very simple example. i will take it from there. or give me a website with a tutorial on it.

    thanks,

    enlinux

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    About the problem with system(), try something like that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
        char target[] = " 10.200.1.1";
        char whole[100] = "ping -t";
        
        strcat(whole,target);
        printf("%s\n",whole);
        system(whole);
        system("PAUSE");
        return 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf("%d", &ipadd);
    An IP address is not a valid int. Remember it looks like this:
    123.123.123.123
    scanf() will simply read the first 123, hit the dot, and stop. You'll have to read the input as a string with fgets(), just don't forget to remove the newline character (mentioned in the links I posted).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    thanks for all your help

    i finally got it to work. thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  2. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  3. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  4. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  5. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM