]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/fnameat.c
Merge branch 'readline'
[empserver] / src / lib / gen / fnameat.c
index 1f4a942cfcf5ad09ff7ea26367f4db803925be7d..5a44bb25fea2251fab45f481341124255f139646 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
@@ -26,7 +25,7 @@
  *  ---
  *
  *  fnameat.c: Interpret file names relative to a directory
- * 
+ *
  *  Known contributors to this file:
  *     Markus Armbruster, 2008
  */
 #include <config.h>
 
 #include <errno.h>
-#include "prototypes.h"
+#include <stdlib.h>
+#include <string.h>
+#include "fnameat.h"
 
 static int fname_is_abs(const char *);
 
 /*
- * Interpret FNAME relative to directory DIR.
- * Return FNAME if it is absolute, or DIR is null or empty.
- * Else return a malloc'ed string containing DIR/FNAME, or null
+ * Interpret @fname relative to directory @dir.
+ * Return @fname if it is absolute, or @dir is null or empty.
+ * Else return a malloc'ed string containing @dir/@fname, or null
  * pointer when that fails.
  */
 char *
@@ -71,3 +72,26 @@ fname_is_abs(const char *fname)
     return fname[0] == '/';
 #endif
 }
+
+/*
+ * Open a stream like fopen(), optionally relative to a directory.
+ * This is exactly like fopen(), except @fname is interpreted relative
+ * to @dir if that is neither null nor empty.
+ */
+FILE *
+fopenat(const char *fname, const char *mode, const char *dir)
+{
+    char *fnat;
+    FILE *fp;
+
+    fnat = fnameat(fname, dir);
+    if (!fnat)
+       return NULL;
+
+    fp = fopen(fnat, mode);
+
+    if (fnat != fname)
+       free(fnat);
+
+    return fp;
+}