Use the code to obtain the last modification time and information in the/etc/resolv. conf file.

Source: Internet
Author: User
Tags nameserver

Use the code to obtain/etc/resolv. the last modification time of the conf file and the file information/etc/resolv. the conf file is the configuration file for DNS domain name resolution. Each line starts with a keyword, followed by configuration parameters. Resolv. conf has four keywords: nameserver # define the IP address domain of the DNS server # define the local domain name search # define the domain name search list sortlist # Sort the returned domain names mainly by the nameserver keyword, if no nameserver is specified, the DNS server cannot be found. Other keywords are optional. My ubuntu 12.04 resolv. the conf file is as follows: [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 get/etc/resolv BY code. the last modification time and information in the conf file are as follows: [plain] # include <stdio. h> # include <string. h> # include <errno. h> # include <time. h> # include <sys/types. h> # include <sy S/stat. h> # include <unistd. h> # define RESOLV_CONF_PATH "/etc/resolv. conf "# define into 2 # define MAX_NET_STR_LEN 256 typedef struct dns_info_s {char dns_svr [MAX_DNS_SERVERS] [region]; char domain [region];} dns_info_t; time_t region (void ); int net_get_dns_info (dns_info_t * dns_info); time_t net_get_dns_info_mtime (void) {const char * file = RESOLV_CONF_PA TH; 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 hour (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", 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; time when the file content is last modified if (ret = net_get_dns_info (& Dns_info) <0) {printf ("Getting DNS Info error \ n");} return 0;} compile and run: [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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.