Coverage Report

Created: 2024-02-25 06:16

/src/gdbm/tools/input-file.c
Line
Count
Source (jump to first uncovered line)
1
/* This file is part of GDBM, the GNU data base manager.
2
   Copyright (C) 2018-2023 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 3, 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 "gdbmtool.h"
18
19
struct instream_file
20
{
21
  struct instream base;   /* Base structure */
22
  FILE *fp;               /* Opened file */
23
  dev_t dev;              /* Device number */
24
  ino_t ino;              /* Inode number */
25
};
26
27
static ssize_t
28
instream_file_read (instream_t istr, char *buf, size_t size)
29
0
{
30
0
  struct instream_file *file = (struct instream_file *)istr;
31
0
  return fread (buf, 1, size, file->fp);
32
0
}
33
34
static void
35
instream_file_close (instream_t istr)
36
0
{
37
0
  struct instream_file *file = (struct instream_file *)istr;
38
0
  fclose (file->fp);
39
0
  free (file->base.in_name);
40
0
  free (file);
41
0
}
42
43
static int
44
instream_file_eq (instream_t a, instream_t b)
45
0
{
46
0
  struct instream_file *file_a = (struct instream_file *)a;
47
0
  struct instream_file *file_b = (struct instream_file *)b;
48
0
  return file_a->dev == file_b->dev && file_a->ino == file_b->ino;
49
0
}
50
51
instream_t
52
instream_file_create (char const *name)
53
0
{
54
0
  struct instream_file *istr;
55
0
  struct stat st;
56
0
  FILE *fp;
57
58
0
  if (stat (name, &st))
59
0
    {
60
0
      terror (_("cannot open `%s': %s"), name, strerror (errno));
61
0
      return NULL;
62
0
    }
63
0
  else if (!S_ISREG (st.st_mode))
64
0
    {
65
0
      terror (_("%s is not a regular file"), name);
66
0
      return NULL;
67
0
    }
68
69
0
  fp = fopen (name, "r");
70
0
  if (!fp)
71
0
    {
72
0
      terror (_("cannot open %s for reading: %s"), name,
73
0
        strerror (errno));
74
0
      return NULL;
75
0
    }
76
  
77
0
  istr = emalloc (sizeof *istr);
78
0
  istr->base.in_name = estrdup (name);
79
0
  istr->base.in_inter = 0;
80
0
  istr->base.in_read = instream_file_read;
81
0
  istr->base.in_close = instream_file_close;
82
0
  istr->base.in_eq = instream_file_eq;
83
0
  istr->base.in_history_size = NULL;
84
0
  istr->base.in_history_get = NULL;  
85
0
  istr->fp = fp;
86
0
  istr->dev = st.st_dev;
87
0
  istr->ino = st.st_ino;
88
89
0
  return (instream_t) istr;
90
0
}