aboutsummaryrefslogtreecommitdiffstats
path: root/src/shvar.c
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin@redhat.com>2000-04-21 13:18:59 +0000
committerNalin Dahyabhai <nalin@redhat.com>2000-04-21 13:18:59 +0000
commit2c2252797eb92bc268b53dcf2aae76d529ea95ef (patch)
tree0bf8d5d316edbc0091efe573a7d6a4fff62bcdc6 /src/shvar.c
parent7e1e8cab56cb8be9cfaf16f54c972a738551667a (diff)
downloadinitscripts-2c2252797eb92bc268b53dcf2aae76d529ea95ef.tar
initscripts-2c2252797eb92bc268b53dcf2aae76d529ea95ef.tar.gz
initscripts-2c2252797eb92bc268b53dcf2aae76d529ea95ef.tar.bz2
initscripts-2c2252797eb92bc268b53dcf2aae76d529ea95ef.tar.xz
initscripts-2c2252797eb92bc268b53dcf2aae76d529ea95ef.zip
update from the rp3 CVS repository
Diffstat (limited to 'src/shvar.c')
-rw-r--r--src/shvar.c65
1 files changed, 21 insertions, 44 deletions
diff --git a/src/shvar.c b/src/shvar.c
index 473793bd..48f62ce0 100644
--- a/src/shvar.c
+++ b/src/shvar.c
@@ -10,7 +10,7 @@
* Furthermore, they are only intended for one level of inheritance;
* the value setting algorithm assumes this.
*
- * Copyright 1999 Red Hat, Inc.
+ * Copyright 1999,2000 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -38,9 +38,11 @@
#include "shvar.h"
-/* Open the file <name>, return shvarFile on success, NULL on failure */
-shvarFile *
-svNewFile(const char *name)
+/* Open the file <name>, returning a shvarFile on success and NULL on failure.
+ Add a wrinkle to let the caller specify whether or not to create the file
+ (actually, return a structure anyway) if it doesn't exist. */
+static shvarFile *
+svOpenFile(const char *name, gboolean create)
{
shvarFile *s = NULL;
int closefd = 0;
@@ -64,10 +66,13 @@ svNewFile(const char *name)
if (read(s->fd, s->arena, buf.st_size) < 0) goto bail;
+ /* we'd use g_strsplit() here, but we want a list, not an array */
for(p = s->arena; (q = strchr(p, '\n')) != NULL; p = q + 1) {
s->lineList = g_list_append(s->lineList, g_strndup(p, q - p));
}
+ /* closefd is set if we opened the file read-only, so go ahead and
+ close it, because we can't write to it anyway */
if (closefd) {
close(s->fd);
s->fd = -1;
@@ -76,6 +81,10 @@ svNewFile(const char *name)
return s;
}
+ if (create) {
+ return s;
+ }
+
bail:
if (s->fd != -1) close(s->fd);
if (s->arena) g_free (s->arena);
@@ -84,51 +93,19 @@ bail:
return NULL;
}
+/* Open the file <name>, return shvarFile on success, NULL on failure */
+shvarFile *
+svNewFile(const char *name)
+{
+ return svOpenFile(name, FALSE);
+}
+
/* Create a new file structure, returning actual data if the file exists,
* and a suitable starting point if it doesn't. */
shvarFile *
svCreateFile(const char *name)
{
- shvarFile *s = NULL;
- int closefd = 0;
-
- s = g_malloc0(sizeof(shvarFile));
-
- s->fd = open(name, O_RDWR); /* NOT O_CREAT */
- if (s->fd == -1) {
- /* try read-only */
- s->fd = open(name, O_RDONLY); /* NOT O_CREAT */
- if (s->fd != -1) closefd = 1;
- }
- s->fileName = g_strdup(name);
-
- if (s->fd != -1) {
- struct stat buf;
- char *p, *q;
-
- if (fstat(s->fd, &buf) < 0) goto bail;
- s->arena = g_malloc0(buf.st_size + 1);
-
- if (read(s->fd, s->arena, buf.st_size) < 0) goto bail;
-
- for(p = s->arena; (q = strchr(p, '\n')) != NULL; p = q + 1) {
- s->lineList = g_list_append(s->lineList, g_strndup(p, q - p));
- }
-
- if (closefd) {
- close(s->fd);
- s->fd = -1;
- }
-
- }
- return s;
-
-bail:
- if (s->fd != -1) close(s->fd);
- if (s->arena) g_free (s->arena);
- if (s->fileName) g_free (s->fileName);
- g_free (s);
- return NULL;
+ return svOpenFile(name, TRUE);
}
/* remove escaped characters in place */