用代碼獲得/etc/resolv.conf檔案的最後修改時間和檔案內資訊

來源:互聯網
上載者:User

用代碼獲得/etc/resolv.conf檔案的最後修改時間和檔案內資訊 /etc/resolv.conf檔案是DNS網域名稱解析的設定檔,每行以一個關鍵字開頭,後接配置參數。resolv.conf的關鍵字主要有四個,分別是:nameserver   #定義DNS伺服器的IP地址domain       #定義本地區名search       #定義網域名稱的搜尋列表sortlist     #對返回的網域名稱進行排序最主要是nameserver關鍵字,如果沒指定nameserver就找不到DNS伺服器,其它關鍵字是可選的。 我的ubuntu 12.04 resolv.conf檔案如下: [plain] # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)  #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN  nameserver 127.0.0.1  search www.google.com google.com   用代碼獲得/etc/resolv.conf檔案的最後修改時間和檔案內資訊,如下:[plain] #include <stdio.h>  #include <string.h>  #include <errno.h>  #include <time.h>  #include <sys/types.h>  #include <sys/stat.h>  #include <unistd.h>    #define RESOLV_CONF_PATH    "/etc/resolv.conf"  #define MAX_DNS_SERVERS     2  #define MAX_NET_STR_LEN     256    typedef struct dns_info_s {      char dns_svr[MAX_DNS_SERVERS][MAX_NET_STR_LEN];      char domain[MAX_NET_STR_LEN];  } dns_info_t;    time_t net_get_dns_info_mtime(void);  int net_get_dns_info(dns_info_t *dns_info);    time_t net_get_dns_info_mtime(void)  {      const char *file = RESOLV_CONF_PATH;      struct stat stat_buf;        if (stat(RESOLV_CONF_PATH, &stat_buf) < 0) {          if (errno == ENOENT)              printf("%s has been removed!\n", RESOLV_CONF_PATH);          else {              printf("stat(%s)\n", file);              return 0;          }      }      printf("%s: st_mtime=0x%08X\n", file, (int)stat_buf.st_mtime);      return stat_buf.st_mtime;  }    int net_get_dns_info(dns_info_t *dns_info)  {      FILE *file;      int ret, i, svr_cnt, df;      char name[16], domain[128];        file = fopen(RESOLV_CONF_PATH, "r");      if (!file) {          printf("fopen fail = %s(%d)\n", strerror(errno), errno);          return -errno;      }        i = svr_cnt = df = 0;      while ((ret = fscanf(file, "%s %s", name, domain)) != EOF) {          if (ret < 2) {              printf("fscanf ret=%d\n", ret);              ret = -1;              break;          }            if (svr_cnt < MAX_DNS_SERVERS && !strcmp(name, "nameserver")) {              strcpy(dns_info->dns_svr[svr_cnt++], domain);              printf("dns_svr=%s\n", domain);          }          else if (!strcmp(name, "search")) {              strcpy(dns_info->domain, domain);              printf("domain=%s\n", domain);              df++;          }      }        fclose(file);        printf("svr_cnt=%d, df=%d\n", svr_cnt, df);        if (svr_cnt && df)          ret = 0;      else          ret = -1;      return ret;  }    int main(void)  {      int ret;      dns_info_t dns_info;      time_t mtime;      mtime = net_get_dns_info_mtime();        printf("time = %ld\n",mtime); //time_t st_mtime; 檔案內容最後被修改的時間        if ((ret = net_get_dns_info(&dns_info)) < 0)      {          printf("Getting DNS Info error\n");      }        return 0;  }  編譯運行: [plain] zx@zx:~$ gcc main.c   zx@zx:~$ ./a.out   /etc/resolv.conf: st_mtime=0x5195A7A8  time = 1368762280  dns_svr=127.0.0.1  domain=www.google.com  fscanf ret=1  svr_cnt=1, df=1   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.