aboutsummaryrefslogtreecommitdiffstats
path: root/libibex/file.c
diff options
context:
space:
mode:
authorNotZed <NotZed@HelixCode.com>2000-03-28 08:39:36 +0800
committerMichael Zucci <zucchi@src.gnome.org>2000-03-28 08:39:36 +0800
commit03bcc319cfa2f92e86da72d0bd9f6040f4800aa0 (patch)
treeb7d65a7a51f8261228c4e9cb31eb1b10014c2779 /libibex/file.c
parent9eec5a12db8b2e53fc1e346478e320fd6efb2a70 (diff)
downloadgsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.tar
gsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.tar.gz
gsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.tar.bz2
gsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.tar.lz
gsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.tar.xz
gsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.tar.zst
gsoc2013-evolution-03bcc319cfa2f92e86da72d0bd9f6040f4800aa0.zip
Fixed call to ibex_open.
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). svn path=/trunk/; revision=2202
Diffstat (limited to 'libibex/file.c')
-rw-r--r--libibex/file.c55
1 files changed, 47 insertions, 8 deletions
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);