- if (!(file = fopen(CONFIG_ROOT "/main/hosts", "r")))
- {
- errorbox(_("Unable to open main hosts file."));
- return 0;
- }
- if (!(hosts = fopen("/etc/hosts", "w")))
- {
- errorbox(_("Unable to write /etc/hosts."));
- return 0;
- }
- fprintf(hosts, "127.0.0.1\tlocalhost\n");
- if (strlen(domainname))
- fprintf(hosts, "%s\t%s.%s\t%s\n",address,hostname,domainname,hostname);
- else
- fprintf(hosts, "%s\t%s\n",address,hostname);
- while (fgets(buffer, STRING_SIZE, file))
- {
- char *token, *ip, *host, *domain;
-
- buffer[strlen(buffer) - 1] = 0;
-
- token = strtok(buffer, ",");
-
- ip = strtok(NULL, ",");
- host = strtok(NULL, ",");
- domain = strtok(NULL, ",");
-
- if (!(ip && host))
- break;
-
- if (strlen(ip) < 7 || strlen(ip) > 15
- || strspn(ip, "0123456789.") != strlen(ip))
- break;
-
- if (strspn(host, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-") != strlen(host))
- break;
-
- if (domain)
- fprintf(hosts, "%s\t%s.%s\t%s\n",ip,host,domain,host);
- else
- fprintf(hosts, "%s\t%s\n",ip,host);
- }
- fclose(file);
- fclose(hosts);
-