static void version_rename(log_channel chan) { unsigned int ver; char old_name[PATH_MAX+1]; char new_name[PATH_MAX+1]; ver = chan->out.file.versions; if (ver < 1) return; if (ver > LOG_MAX_VERSIONS) ver = LOG_MAX_VERSIONS; /* * Need to have room for '.nn' (XXX assumes LOG_MAX_VERSIONS < 100) */ if (strlen(chan->out.file.name) > (size_t)(PATH_MAX-3)) return; for (ver--; ver > 0; ver--) { sprintf(old_name, "%s.%d", chan->out.file.name, ver-1); sprintf(new_name, "%s.%d", chan->out.file.name, ver); (void)isc_movefile(old_name, new_name); } sprintf(new_name, "%s.0", chan->out.file.name); (void)isc_movefile(chan->out.file.name, new_name); }
int main(int argc, char **argv) { DB *db; DBT key; DBT data; const char *filename = _PATH_SERVICES; const char *tmpdatabase = _PATH_SERVICES_DB_TMP; const char *database = _PATH_SERVICES_DB; char dbuf[1024]; char kbuf[512]; u_short *ports; struct lcl_sv lcl_sv; struct servent *sv; int n, r; char *p; unlink(tmpdatabase); if (argc > 1) filename = argv[1]; lcl_sv.fp = fopen(filename, "r"); if (lcl_sv.fp == NULL) err(1, "%s", filename); db = dbopen(tmpdatabase, O_CREAT|O_RDWR, 0444, DB_BTREE, NULL); if (db == NULL) err(1, "%s", tmpdatabase); while ((sv = irs_lclsv_fnxt(&lcl_sv)) != NULL) { if (sv->s_proto == NULL) continue; key.data = kbuf; data.data = dbuf; /* Note that (sizeof "/") == 2. */ if (strlen(sv->s_name) + sizeof "/" + strlen(sv->s_proto) > sizeof kbuf) continue; key.size = SPRINTF((kbuf, "%s/%s", sv->s_name, sv->s_proto))+1; ((u_short *)dbuf)[0] = sv->s_port; p = dbuf; p += sizeof(u_short); if (sv->s_aliases) for (n = 0; sv->s_aliases[n]; ++n) { strcpy(p, sv->s_aliases[n]); p += strlen(p) + 1; } data.size = p - dbuf; if ((r = db->put(db, &key, &data, R_NOOVERWRITE))) { if (r < 0) errx(1, "failed to write %s", (char *)key.data); else warnx("will not overwrite %s", (char *)key.data); } for (n = 0; sv->s_aliases[n]; ++n) { if (strlen(sv->s_aliases[n]) + sizeof "/" + strlen(sv->s_proto) > sizeof kbuf) continue; key.size = SPRINTF((kbuf, "%s/%s", sv->s_aliases[n], sv->s_proto))+1; if ((r = db->put(db, &key, &data, R_NOOVERWRITE))) { if (r < 0) errx(1, "failed to write %s", (char *)key.data); else warnx("will not overwrite %s", (char *)key.data); } } ports = (u_short *)kbuf; ports[0] = 0; ports[1] = sv->s_port; strcpy((char *)(ports+2), sv->s_proto); key.size = sizeof(u_short) * 2 + strlen((char *)(ports+2)) + 1; if (strlen(sv->s_name) + sizeof "/" + strlen(sv->s_proto) > sizeof dbuf) continue; p = dbuf; p += SPRINTF((p, "%s/%s", sv->s_name, sv->s_proto)) + 1; if (sv->s_aliases != NULL) for (n = 0; sv->s_aliases[n] != NULL; n++) if ((p + strlen(sv->s_aliases[n]) + 1) - dbuf <= (int)sizeof dbuf) { strcpy(p, sv->s_aliases[n]); p += strlen(p) + 1; } data.size = p - dbuf; if ((r = db->put(db, &key, &data, R_NOOVERWRITE))) { if (r < 0) errx(1, "failed to write %d/%s", ntohs(sv->s_port), sv->s_proto); else warnx("will not overwrite %d/%s", ntohs(sv->s_port), sv->s_proto); } } db->close(db); if (isc_movefile(tmpdatabase, database)) err(1, "rename %s -> %s", tmpdatabase, database); exit(0); }