Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 检查是否为空_C_Struct_Arguments - Fatal编程技术网

C 检查是否为空

C 检查是否为空,c,struct,arguments,C,Struct,Arguments,我使用这行代码作为在Linux上打印接口IP地址的代码部分的最后一部分 printf ("%s\n", inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr)); 这非常有效,但如果我没有键入接口名称作为参数,则会出现分段错误 是否有办法检查此代码: inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr) 是否提供了一个参数?谢谢 以

我使用这行代码作为在Linux上打印接口IP地址的代码部分的最后一部分

printf ("%s\n", inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));
这非常有效,但如果我没有键入接口名称作为参数,则会出现分段错误

是否有办法检查此代码:

inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr)
是否提供了一个参数?谢谢

以下是上下文的完整源代码

/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/********************************************************************
* Description:
* Author: John Cartwright <>
* Created at: Wed Jan 13 11:44:29 AEDT 2016
* Computer: ubuntu
* System: Linux 4.3.0-5-generic on x86_64
*
* Copyright (c) 2016 John Cartwright,,,  All rights reserved.
*
********************************************************************/

#include <linux/kernel.h>       /* for struct sysinfo */
#include <sys/sysinfo.h>        /* For the sysinfo struct. */
#include <stdio.h>
#include <string.h>     /* for strncpy */
#include <unistd.h>     /* for close */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <stdlib.h> /* For the system() function. */
#include <arpa/inet.h>

#include "info.h"

#define BUF 0x05

int main (int argc, char **argv)
{
    int fd;
    struct ifreq ifr;
    char *myarg1 = argv[2];
    char hostname[128];
    char *iface = myarg1;
    unsigned char *mac;
    char command[50];

    if (argv[1] == NULL)
    {
        printf ("********************************************\n");
        printf ("*          Simple system information.      *\n");
        printf ("*                                          *\n");
        printf ("* IP info: --ip <IFACE>                    *\n");
        printf ("* Print ARP table: --arp                   *\n");
        printf ("* Print sys info: --system                 *\n");
        printf ("********************************************\n");
        printf ("\nThe PID of this program was: %i\n", getpid ());
    }

    /* Getting information about the routing table. */

    if (argc > 1 && strncmp (argv[1], "--arp", BUF) == 0)
    {
        printf ("This is the routing table.\n");
        char *arg[] = { "cat", "/proc/net/arp", NULL };
        execvp (arg[0], arg);
    }

    if (argc > 1 && strncmp (argv[1], "--system", BUF) == 0)
    {
        information();
    }


    if (argc > 1 && strncmp (argv[1], "--hostname", BUF) == 0)
    {
        gethostname(hostname, sizeof hostname);
        printf("Hostname: %s\n", hostname);
    }

    if (argc > 1 && strncmp (argv[1], "--ip", BUF) == 0)
    {
        // This code from: http://www.binarytides.com/c-program-to-get-mac-address-from-interface-name-on-linux/
        FILE *f;
        char line[100] , *p , *c;
        char myip;

        f = fopen("/proc/net/route" , "r");

        while(fgets(line , 100 , f))
        {
            p = strtok(line , " \t");
            c = strtok(NULL , " \t");

            if(p!=NULL && c!=NULL)
            {
                if(strcmp(c , "00000000") == 0)
                {
                    printf("Default interface is : %s \n" , p);
                    break;
                }
            }
        }

        fd = socket(AF_INET, SOCK_DGRAM, 0);

        ifr.ifr_addr.sa_family = AF_INET;
        strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);

        ioctl(fd, SIOCGIFHWADDR, &ifr);

        close(fd);

        mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;

        //display mac address
        printf("Mac : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n" , mac[0], mac[1], mac[2], \
        mac[3], mac[4], mac[5]);

        fd = socket (AF_INET, SOCK_DGRAM, 0);
        /* I want to get an IPv4 IP address */
        ifr.ifr_addr.sa_family = AF_INET;

        /* I want IP address attached to the specified interface. */
        strncpy (ifr.ifr_name, myarg1, IFNAMSIZ - 1);

        ioctl (fd, SIOCGIFADDR, &ifr);

        close (fd);

        // End binarytides code.

        /* display result */
        printf ("IP information.\n");
        printf("Gateway IP:\n");

        strcpy( command, "/sbin/ip route | awk '/default/ { print $3 }'" );
        system(command);

        printf("IP address:\n");
        printf ("%s\n", inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));
    }

    return 0;
}
/*
*此程序是免费软件:您可以重新发布和/或修改它
*它是根据GNU通用公共许可证的条款发布的
*自由软件基金会,或者许可证的第3版,或者
*(由您选择)任何更高版本。
*
*这个节目的发布是希望它会有用,
*但无任何保证;甚至没有任何关于
*适销性或适合某一特定目的。见
*有关更多详细信息,请参阅GNU通用公共许可证。
*
*您应该已经收到GNU通用公共许可证的副本
*和这个节目一起。如果没有,请参阅。
*/
/********************************************************************
*说明:
*作者:约翰·卡特赖特
*创建时间:2016年1月13日星期三11:44:29 AEDT
*计算机:ubuntu
*系统:x86_64上的Linux 4.3.0-5-generic
*
*版权所有(c)2016 John Cartwright,保留所有权利。
*
********************************************************************/
#包含/*用于结构sysinfo*/
#包含/*用于sysinfo结构*/
#包括
#包括/*用于strncpy*/
#包括/*用于关闭*/
#包括
#包括
#包括
#包括
#包括
#对于system()函数,包括/*项*/
#包括
#包括“info.h”
#定义BUF 0x05
int main(int argc,字符**argv)
{
int-fd;
结构ifreq-ifr;
char*myarg1=argv[2];
字符主机名[128];
char*iface=myarg1;
无符号字符*mac;
char命令[50];
如果(argv[1]==NULL)
{
printf(“******************************************************************\n”);
printf(“*简单系统信息。*\n”);
printf(“***\n”);
printf(“*IP信息:--IP*\n”);
printf(“*Print ARP table:--ARP*\n”);
printf(“*打印系统信息:--system*\n”);
printf(“******************************************************************\n”);
printf(“\n此程序的PID为:%i\n”,getpid());
}
/*正在获取有关路由表的信息*/
如果(argc>1&&strncmp(argv[1],“--arp”,BUF)==0)
{
printf(“这是路由表。\n”);
char*arg[]={cat',“/proc/net/arp”,NULL};
execvp(arg[0],arg);
}
如果(argc>1&&strncmp(argv[1],“--system”,BUF)==0)
{
信息();
}
如果(argc>1&&strncmp(argv[1],“--hostname”,BUF)==0)
{
gethostname(主机名,主机名大小);
printf(“主机名:%s\n”,主机名);
}
如果(argc>1&&strncmp(argv[1],“--ip”,BUF)==0)
{
//此代码来自:http://www.binarytides.com/c-program-to-get-mac-address-from-interface-name-on-linux/
文件*f;
字符行[100],*p,*c;
char-myip;
f=fopen(“/proc/net/route”,“r”);
而(fgets(第100行,f))
{
p=strtok(第“\t”行);
c=strtok(空,“\t”);
如果(p!=NULL&&c!=NULL)
{
如果(strcmp(c,“00000000”)==0)
{
printf(“默认接口是:%s\n”,p);
打破
}
}
}
fd=插座(AF INET,插座DGRAM,0);
ifr.ifr\u addr.sa\u family=AF\u INET;
strncpy(ifr.ifr_名称、iface、IFNAMSIZ-1);
ioctl(fd、SIOCGIFHWADDR和ifr);
关闭(fd);
mac=(无符号字符*)ifr.ifr\u hwaddr.sa\u数据;
//显示mac地址
printf(“Mac:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n”,Mac[0],Mac[1],Mac[2]\
mac[3],mac[4],mac[5]);
fd=插座(AF INET,插座DGRAM,0);
/*我想要一个IPv4 IP地址*/
ifr.ifr\u addr.sa\u family=AF\u INET;
/*我希望IP地址附加到指定的接口*/
strncpy(ifr.ifr_名称,myarg1,IFNAMSIZ-1);
ioctl(fd、SIOCGIFADDR和ifr);
关闭(fd);
//结束二进制代码。
/*显示结果*/
printf(“IP信息”。\n”);
printf(“网关IP:\n”);
strcpy(命令“/sbin/ip-route | awk'/default/{print$3}”);
系统(指挥部);
printf(“IP地址:\n”);
printf(“%s\n”,inet\u ntoa(((struct sockaddr\u in*)和ifr.ifr\u addr)->sin\u addr));
}
返回0;
}

我怀疑问题在于sin_add为空,但被取消引用,这就是导致段错误的原因


按照M.M的建议,从这里开始向后操作。

您的问题应该是我如何知道在命令名之后至少给了我两个参数

argc>1
更改为
argc>=3
。然后参数是程序名、
“--ip”
和接口名

if (argc >= 3 && strncmp (argv[1], "--ip", BUF) == 0)

否则,您可以以
programname--ip
的形式执行程序,该程序将与
SIGSEGV
一起崩溃。这不是你要问的吗?

你真正的问题是“我如何知道
ioctl(fd,SIOCGIFADDR,&ifr);
成功了?”要回答这个问题,请阅读
ioctl
的文档。另外,如果你使用
strncpy
不正确,一般来说,你必须空终止缓冲区,代码中的许可条款不适用于堆栈溢出。在
printf()
语句中,也确实不需要在行尾加反斜杠;编译器不关心普通代码中的换行符(所以现在在宏中通常只在行的末尾使用反斜杠-字符串连接是由C89/C90标准化的)