Fixed header warnings
[libtar.git] / lib / handle.c
blobae974b9f040b97a70f96337aa4c74db1035b8b7d
1 /*
2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
5 **
6 ** handle.c - libtar code for initializing a TAR handle
7 **
8 ** Mark D. Roth <roth@uiuc.edu>
9 ** Campus Information Technologies and Educational Services
10 ** University of Illinois at Urbana-Champaign
13 #include <internal.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <errno.h>
19 #ifdef HAVE_UNISTD_H
20 # include <unistd.h>
21 #endif
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 #endif
28 const char libtar_version[] = PACKAGE_VERSION;
30 static tartype_t default_type = { open, close, read, write };
33 static int
34 tar_init(TAR **t, char *pathname, tartype_t *type,
35 int oflags, int mode, int options)
37 if ((oflags & O_ACCMODE) == O_RDWR)
39 errno = EINVAL;
40 return -1;
43 *t = (TAR *)calloc(1, sizeof(TAR));
44 if (*t == NULL)
45 return -1;
47 (*t)->pathname = pathname;
48 (*t)->options = options;
49 (*t)->type = (type ? type : &default_type);
50 (*t)->oflags = oflags;
52 if ((oflags & O_ACCMODE) == O_RDONLY)
53 (*t)->h = libtar_hash_new(256,
54 (libtar_hashfunc_t)path_hashfunc);
55 else
56 (*t)->h = libtar_hash_new(16, (libtar_hashfunc_t)dev_hash);
57 if ((*t)->h == NULL)
59 free(*t);
60 return -1;
63 return 0;
67 /* open a new tarfile handle */
68 int
69 tar_open(TAR **t, char *pathname, tartype_t *type,
70 int oflags, int mode, int options)
72 if (tar_init(t, pathname, type, oflags, mode, options) == -1)
73 return -1;
75 if ((options & TAR_NOOVERWRITE) && (oflags & O_CREAT))
76 oflags |= O_EXCL;
78 #ifdef O_BINARY
79 oflags |= O_BINARY;
80 #endif
82 (*t)->fd = (*((*t)->type->openfunc))(pathname, oflags, mode);
83 if ((*t)->fd == -1)
85 free(*t);
86 return -1;
89 return 0;
93 int
94 tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
95 int oflags, int mode, int options)
97 if (tar_init(t, pathname, type, oflags, mode, options) == -1)
98 return -1;
100 (*t)->fd = fd;
101 return 0;
106 tar_fd(TAR *t)
108 return t->fd;
112 /* close tarfile handle */
114 tar_close(TAR *t)
116 int i;
118 i = (*(t->type->closefunc))(t->fd);
120 if (t->h != NULL)
121 libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
122 ? free
123 : (libtar_freefunc_t)tar_dev_free));
124 free(t);
126 return i;