47a0d990000823f7c03c961db8229320c9258f9f
[gpgme.git] / src / data-fd.c
1 /* data-fd.c - A file descripor based data object.
2    Copyright (C) 2002, 2004 g10 Code GmbH
3  
4    This file is part of GPGME.
5  
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10    
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include "debug.h"
29 #include "data.h"
30
31 \f
32 static ssize_t
33 fd_read (gpgme_data_t dh, void *buffer, size_t size)
34 {
35   return read (dh->data.fd, buffer, size);
36 }
37
38
39 static ssize_t
40 fd_write (gpgme_data_t dh, const void *buffer, size_t size)
41 {
42   return write (dh->data.fd, buffer, size);
43 }
44
45
46 static off_t
47 fd_seek (gpgme_data_t dh, off_t offset, int whence)
48 {
49   return lseek (dh->data.fd, offset, whence);
50 }
51
52
53 static int
54 fd_get_fd (gpgme_data_t dh)
55 {
56   return (dh->data.fd);
57 }
58
59
60 static struct _gpgme_data_cbs fd_cbs =
61   {
62     fd_read,
63     fd_write,
64     fd_seek,
65     NULL,
66     fd_get_fd
67   };
68
69 \f
70 gpgme_error_t
71 gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd)
72 {
73   gpgme_error_t err;
74   TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_fd", r_dh, "fd=0x%x", fd);
75
76   err = _gpgme_data_new (r_dh, &fd_cbs);
77   if (err)
78     return TRACE_ERR (err);
79
80   (*r_dh)->data.fd = fd;
81   return TRACE_SUC1 ("dh=%p", *r_dh);
82 }