- int fd;
- char *buf;
-
- if (filename == NULL) {
- errno = EINVAL;
- return -1;
- }
-
- if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
- errno = EOPNOTSUPP;
- return -1;
- }
-
- if (blocksize <= 100) {
- errno = EINVAL;
- return -1;
- }
-
- if (keysize < 1) {
- errno = EINVAL;
- return -1;
- }
-
- if (recsize < 0) {
- errno = EINVAL;
- return -1;
- }
-
- if (ptrsize != 4 && ptrsize != 8) {
- errno = EINVAL;
- return -1;
- }
-
- if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
- errno = EINVAL;
- return -1;
- }
-
- fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
- if (fd < 0) {
- return -1;
- }
-
- buf = malloc(blocksize);
- if (buf == NULL) {
- close(fd);
- return -1;
- }
-
- memset(buf, 0, blocksize);
- if (fmt == FMT_LFIX)
- lfix_root(buf, blocksize, keysize, ptrsize, recsize);
- else
- lvar_root(buf, blocksize, keysize, ptrsize, recsize);
-
- if (write(fd, buf, blocksize) != blocksize) {
- close(fd);
- free(buf);
- return -1;
- }
-
- memset(buf, 0, blocksize);
- if (fmt == FMT_LFIX)
- lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
- else
- lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
-
- if (write(fd, buf, blocksize) != blocksize) {
- close(fd);
- free(buf);
- return -1;
- }
-
- close(fd);
- free(buf);
- return 0;
+ int fd;
+ char *buf;
+
+ if (!filename) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if ((fmt != FMT_LFIX) && (fmt != FMT_LVAR)) {
+ errno = EOPNOTSUPP;
+ return -1;
+ }
+
+ if (blocksize <= 100) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (keysize < 1) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (recsize < 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (ptrsize != 4 && ptrsize != 8) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (keysize + recsize + sizeof(struct iam_leaf_head) > blocksize / 3) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600);
+ if (fd < 0)
+ return -1;
+
+ buf = malloc(blocksize);
+ if (!buf) {
+ close(fd);
+ return -1;
+ }
+
+ memset(buf, 0, blocksize);
+ if (fmt == FMT_LFIX)
+ lfix_root(buf, blocksize, keysize, ptrsize, recsize);
+ else
+ lvar_root(buf, blocksize, keysize, ptrsize, recsize);
+
+ if (write(fd, buf, blocksize) != blocksize) {
+ close(fd);
+ free(buf);
+ return -1;
+ }
+
+ memset(buf, 0, blocksize);
+ if (fmt == FMT_LFIX)
+ lfix_leaf(buf, blocksize, keysize, ptrsize, recsize);
+ else
+ lvar_leaf(buf, blocksize, keysize, ptrsize, recsize);
+
+ if (write(fd, buf, blocksize) != blocksize) {
+ close(fd);
+ free(buf);
+ return -1;
+ }
+
+ close(fd);
+ free(buf);
+ return 0;