bool initwarning(const char *desc, int level, int type) { if(initing < level) { addchange(desc, type); return true; } return false; }
void readconf (void) { char *line; size_t linel; int linenum; FILE *ifh; char *p; int n; ifh = fopen (fname, "r"); if (!ifh) complain (_("opening configuration file %s: %s"), fname, strerror (errno)); line = NULL; linel = 0; linenum = 1; while (getdelim (&line, &linel, '\n', ifh) > 0) { struct change *ch = xmalloc (sizeof (struct change)); char pos[20]; sprintf (pos, _("line %d"), linenum); if ((p = strchr (line, '#')) != NULL) *p = '\0'; p = line; while (isspace (*p)) ++p; if (*p == '\0') continue; n = strcspn (p, " \t"); if (n > IFNAMSIZ) complain (_("interface name too long at line %d"), line); memcpy (ch->ifname, p, n); ch->ifname[n] = 0; p += n; p += strspn (p, " \t"); n = strspn (p, "0123456789ABCDEFabcdef:"); p[n] = 0; addchange (p, ch, pos); linenum++; } fclose (ifh); }
int main (int ac, char **av) { FILE *ifh; char *p; int n; int linenum; char *line = NULL; size_t linel = 0; for (;;) { int c = getopt_long (ac, av, "c:s", lopt, NULL); if (c == -1) break; switch (c) { default: case '?': usage (); case 'c': fname = optarg; break; case 's': use_syslog = 1; break; } } if (use_syslog) openlog ("nameif", 0, LOG_LOCAL0); while (optind < ac) { struct change *ch = xmalloc (sizeof (struct change)); char pos[30]; if ((ac - optind) & 1) usage (); if (strlen (av[optind]) + 1 > IFNAMSIZ) complain (_("interface name `%s' too long"), av[optind]); strcpy (ch->ifname, av[optind]); optind++; sprintf (pos, _("argument %d"), optind); addchange (av[optind], ch, pos); optind++; } if (!clist || fname != default_conf) readconf (); ifh = fopen ("/proc/net/dev", "r"); if (!ifh) complain (_("open of /proc/net/dev: %s"), strerror (errno)); linenum = 0; while (getdelim (&line, &linel, '\n', ifh) > 0) { struct change *ch; unsigned char mac[6]; if (linenum++ < 2) continue; p = line; while (isspace (*p)) ++p; n = strcspn (p, ": \t"); p[n] = 0; if (n > IFNAMSIZ - 1) complain (_("interface name `%s' too long"), p); if (getmac (p, mac) < 0) continue; ch = lookupmac (mac); if (!ch) continue; *ch->pprev = ch->next; if (strcmp (p, ch->ifname)) { if (setname (p, ch->ifname) < 0) complain (_("cannot change name of %s to %s: %s"), p, ch->ifname, strerror (errno)); } free (ch); } fclose (ifh); while (clist) { struct change *ch = clist; clist = clist->next; warning (_("interface '%s' not found"), ch->ifname); free (ch); } if (use_syslog) closelog (); return 0; }