Imported Upstream version 1.10
[platform/upstream/gdbm.git] / tests / d_creat_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 <string.h>
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <ndbm.h>
25
26 char *
27 ntos (int n, char *buf, size_t size)
28 {
29   char *p = buf + size;
30   *--p = 0;
31   do
32     {
33       int x = n % 10;
34       *--p = '0' + x;
35       n /= 10;
36     }
37   while (n);
38   return p;
39 }
40
41 #ifndef O_CLOEXEC
42 # define O_CLOEXEC 0
43 #endif
44
45 int
46 main (int argc, char *argv[])
47 {
48    DBM *d;
49    char fdbuf[2][80];
50    int i;
51    int flags = O_RDONLY;
52    
53    if (argc < 2)
54      {
55        fprintf (stderr, "usage: %s PATH-TO-FDOP [-creat] [-write]\n", argv[0]);
56        return 2;
57      }
58
59    for (i = 2; i < argc; i++)
60      {
61        if (strcmp (argv[i], "-creat") == 0)
62          flags = O_RDWR|O_CREAT;
63        else if (strcmp (argv[i], "-write") == 0)
64          flags = O_RDWR;
65        else
66          {
67            fprintf (stderr, "%s: unknown option: %s\n",
68                     argv[0], argv[i]);
69            return 2;
70          }
71        
72      }
73    
74    if (!O_CLOEXEC)
75      return 77;
76    
77    d = dbm_open ("file", flags|O_CLOEXEC, 0600);
78    if (!d)
79      {
80        perror ("dbm_open");
81        return 3;
82      }
83
84    execl (argv[1], "fdop",
85           ntos (dbm_pagfno (d), fdbuf[0], sizeof (fdbuf[0])),
86           ntos (dbm_dirfno (d), fdbuf[1], sizeof (fdbuf[1])),
87           NULL);
88    return 127;
89 }
90