add changelog
[platform/upstream/gdbm.git] / tests / g_reorg_ce.c
1 /* This file is part of GDBM test suite.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
4    GDBM is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    GDBM is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with GDBM. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "autoconf.h"
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <gdbm.h>
23
24 char *
25 ntos (int n, char *buf, size_t size)
26 {
27   char *p = buf + size;
28   *--p = 0;
29   do
30     {
31       int x = n % 10;
32       *--p = '0' + x;
33       n /= 10;
34     }
35   while (n);
36   return p;
37 }
38
39 #ifndef O_CLOEXEC
40 # define O_CLOEXEC 0
41 #endif
42
43 int
44 main (int argc, char *argv[])
45 {
46   GDBM_FILE d;
47   char fdbuf[80];
48
49   if (argc != 2)
50     {
51       fprintf (stderr, "usage: %s PATH-TO-FDOP\n", argv[0]);
52       return 2;
53     }
54   if (!O_CLOEXEC)
55     return 77;
56   d = gdbm_open ("file.db", 0, GDBM_NEWDB|GDBM_CLOEXEC, 0600, NULL);
57   if (!d)
58     {
59       fprintf (stderr, "gdbm_open: %s\n", gdbm_strerror (gdbm_errno));
60       return 3;
61     }
62   if (gdbm_reorganize (d))
63     {
64       fprintf (stderr, "gdbm_reorganize: %s\n",
65                gdbm_strerror (gdbm_errno));
66       return 3;
67     }
68   execl (argv[1], "fdop",
69          ntos (gdbm_fdesc (d), fdbuf, sizeof (fdbuf)), NULL);
70   return 127;
71 }
72