diff options
-rw-r--r-- | libibex/ChangeLog | 9 | ||||
-rw-r--r-- | libibex/file.c | 55 | ||||
-rw-r--r-- | libibex/ibex.h | 4 | ||||
-rw-r--r-- | libibex/lookup.c | 2 | ||||
-rw-r--r-- | libibex/mkindex.c | 2 |
5 files changed, 61 insertions, 11 deletions
diff --git a/libibex/ChangeLog b/libibex/ChangeLog index ba785db21a..ef27870817 100644 --- a/libibex/ChangeLog +++ b/libibex/ChangeLog @@ -1,3 +1,12 @@ +2000-03-26 NotZed <NotZed@HelixCode.com> + + * lookup.c (main): Fixed call to ibex_open. + + * mkindex.c (main): Fixed call to ibex_open. + + * file.c (ibex_open): Changed to accept flags and mode equivalent + to open(2). + 2000-02-25 Dan Winship <danw@helixcode.com> * *.c: add gtk-doc-style comments diff --git a/libibex/file.c b/libibex/file.c index 0cf637764e..f011312cab 100644 --- a/libibex/file.c +++ b/libibex/file.c @@ -56,14 +56,17 @@ static void free_word (gpointer key, gpointer value, gpointer data); /** * ibex_open: open (or possibly create) an ibex index * @file: the name of the file - * @create: whether or not to create the file if it doesn't exist. + * @flags: open flags, see open(2). + * @mode: If O_CREAT is passed in flags, then the file mode + * to create the new file with. It will be anded with the current + * umask. * * Open and/or create the named ibex file and return a handle to it. * * Return value: an ibex handle, or NULL if an error occurred. **/ ibex * -ibex_open (char *file, gboolean create) +ibex_open (char *file, int flags, int mode) { ibex *ib; FILE *f; @@ -73,9 +76,44 @@ ibex_open (char *file, gboolean create) ibex_file **ibfs = NULL; int i; GPtrArray *refs; + int fd; + char *modestr; - f = fopen (file, "r"); - if (!f && (errno != ENOENT || !create)) { + fd = open(file, flags, mode); + if (fd == -1) { + printf("open failed :(\n"); + return NULL; + } + + /* yuck, this is because we use FILE * interface + internally */ + switch (flags & O_ACCMODE) { + case O_RDONLY: + modestr = "r"; + break; + case O_RDWR: + if (flags & O_APPEND) + modestr = "a+"; + else + modestr = "w+"; + break; + case O_WRONLY: + if (flags & O_APPEND) + modestr = "a"; + else + modestr = "w"; + break; + default: + if (flags & O_APPEND) + modestr = "a+"; + else + modestr = "r+"; + break; + } + + f = fdopen(fd, modestr); + if (f == NULL) { + printf("fdopen failed, modestr = '%s'\n", modestr); if (errno == 0) errno = ENOMEM; return NULL; @@ -91,11 +129,11 @@ ibex_open (char *file, gboolean create) if (!f) return ib; - /* Check version. */ + /* Check version. If its empty, then we have just created it */ if (fread (vbuf, 1, sizeof (vbuf), f) != sizeof (vbuf)) { - if (feof (f)) - errno = EINVAL; - goto errout; + if (feof (f)) { + return ib; + } } if (strncmp (vbuf, IBEX_VERSION, sizeof (vbuf) != 0)) { errno = EINVAL; @@ -141,6 +179,7 @@ ibex_open (char *file, gboolean create) return ib; errout: + fclose (f); g_tree_traverse (ib->files, free_file, G_IN_ORDER, NULL); g_tree_destroy (ib->files); diff --git a/libibex/ibex.h b/libibex/ibex.h index b74db1ed93..bb818e6d34 100644 --- a/libibex/ibex.h +++ b/libibex/ibex.h @@ -22,6 +22,8 @@ #define IBEX_H #include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include <glib.h> struct ibex; @@ -34,7 +36,7 @@ typedef struct ibex ibex; /* Open the named ibex index file. If CREATE is true, create the file * if it doesn't already exist. */ -ibex *ibex_open (char *file, gboolean create); +ibex *ibex_open (char *file, int flags, int mode); /* Write the ibex to disk. */ int ibex_write (ibex *ib); diff --git a/libibex/lookup.c b/libibex/lookup.c index aa09fc6557..2d01dbf850 100644 --- a/libibex/lookup.c +++ b/libibex/lookup.c @@ -61,7 +61,7 @@ main (int argc, char **argv) if (argc == 0) usage (); - ib = ibex_open (file, FALSE); + ib = ibex_open (file, O_RDWR|O_CREAT, 0600); if (!ib) { printf ("Couldn't open %s: %s\n", file, strerror (errno)); exit (1); diff --git a/libibex/mkindex.c b/libibex/mkindex.c index 9d6841e90b..151dcecb2d 100644 --- a/libibex/mkindex.c +++ b/libibex/mkindex.c @@ -60,7 +60,7 @@ main (int argc, char **argv) if (argc == 0) usage (); - ib = ibex_open (file, TRUE); + ib = ibex_open (file, O_CREAT|O_RDWR, 0600); if (!ib) { fprintf (stderr, "Couldn't open index file %s: %s\n", file, strerror (errno)); |