続 pwd_mkdb(8)

/etc/pwd.db に VERSION キーがあるか確認するためのソース。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <db.h>
#include <pwd.h>

int
main(void)
{
	const char *dbfile = "/etc/pwd.db";
	DB *db;
	DBT key, value;
	int version;
	int found = 0;

	db = dbopen(dbfile, O_RDONLY, 0, DB_HASH, NULL);
	if (db == NULL) {
		fprintf(stderr, "couldn't open %s\n", dbfile);
		return 1;
	}

	key.data = "VERSION";
	key.size = strlen((char *)key.data) + 1;
	switch ((*db->get)(db, &key, &value, 0)) {
	case 0:
		if (sizeof(version) == value.size) {
			/* found */
			found = 1;
			(void)memcpy(&version, value.data, value.size);
			break;
		}
		fprintf(stderr, "error: key size incollect\n");
		/*FALLTROUGH*/
	case 1:		/* not found */
		break;

	case -1:	/* error in db routines */
	default:
		fprintf(stderr, "error: error in db routines\n");
		break;
	}

	(*db->close)(db);

	printf("file: %s\n", dbfile);
	if (found) {
		printf("version: %d\n", version);
	} else {
		printf("not found\n");
	}

	return 0;
}
広告を非表示にする