Find the answer to your Linux question:
Results 1 to 2 of 2
### check_mac.c##### #include <fcntl.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    84

    how to add many argv with my command

    ### check_mac.c#####

    #include <fcntl.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <netinet/in.h>
    #include <net/if.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    int main(int argc, char **argv )
    {

    if(strcmp(argv[1],"mac_arp")==0)
    {
    system("arp -en | grep $1 | awk '{print $3}'");
    }

    if(strcmp(argv[1],"ip_arp")==0)
    {
    system("arp -en | grep $1 | awk '{print $1}'");
    }

    }

    i use c programing for call system command on linux fedora and how to call argv ($1) for filter ip address


    check_mac mac_arp 172.168.1.200
    result it's show mac address of 172.168.1.200


    check_mac ip_arp 00:00:00:00:00:01
    result it's show ip address of 00:00:00:00:00:01


    (and if i want to add another argv such as check_mac mac_arp 172.168.1.200 $2 $3 $4,how to!!)


    i can try this it's not working. please help me

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    If I understand your question correctly, it's about multiple parameters to a C program.

    All of the parameters are placed in the argv array. So for instance, if I call:
    Code:
    my_program arg1 arg2 arg3
    then my argv array will be:
    Code:
    argv[0] = "my_program"
    argv[1] = "arg1"
    argv[2] = "arg2"
    argv[3] = "arg3"
    Furthermore, argc is set to the length of the argv array. So in the above example, argc = 4.

    This allows you to write loops that go through every argument:
    Code:
    int i;
    for(i = 1; i < argc; i++)
    {
        char *arg = argv[i];
        /* ... process arg ... */
    }
    I hope that this will help point you in the right direction.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...