Add scratch file handles.
[pspp-builds.git] / src / filename.c
index 4db36adebe27c1bf11928cc812a81d6333812d5b..0c085776f5b889aefc23dfc9fb8174273bcc759e 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
    Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
@@ -20,6 +20,7 @@
 #include <config.h>
 #include "error.h"
 #include "filename.h"
+#include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <errno.h>
 #include "settings.h"
 #include "str.h"
 #include "version.h"
+#include "xreadlink.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
 
 #include "debug-print.h"
 
@@ -38,7 +43,8 @@
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#include "stat.h"
+#include <sys/stat.h>
+#include "stat-macros.h"
 #endif
 
 #ifdef __WIN32__
@@ -502,7 +508,7 @@ fn_dirname (const char *filename)
   if (len == 1 && filename[0] == '/')
     p = filename + 1;
   else if (len && filename[len - 1] == DIR_SEPARATOR)
-    p = mm_find_reverse (filename, len - 1, filename + len - 1, 1);
+    p = buf_find_reverse (filename, len - 1, filename + len - 1, 1);
   else
     p = strrchr (filename, DIR_SEPARATOR);
   if (p == NULL)
@@ -524,6 +530,18 @@ fn_basename (const char *filename)
   abort ();
 }
 #endif
+
+/* Returns the extension part of FILENAME as a malloc()'d string.
+   If FILENAME does not have an extension, returns an empty
+   string. */
+char *
+fn_extension (const char *filename) 
+{
+  const char *extension = strrchr (filename, '.');
+  if (extension == NULL)
+    extension = "";
+  return xstrdup (extension);
+}
 \f
 #if unix
 /* Returns the current working directory, as a malloc()'d string.
@@ -619,40 +637,13 @@ fn_exists_p (const char *name)
 #endif
 }
 
-#ifdef unix
-/* Stolen from libc.info but heavily modified, this is a wrapper
-   around readlink() that allows for arbitrary filename length. */
+/* Returns the symbolic link value for FILENAME as a dynamically
+   allocated buffer, or a null pointer on failure. */
 char *
 fn_readlink (const char *filename)
 {
-  int size = 128;
-
-  for (;;)
-    {
-      char *buffer = xmalloc (size);
-      int nchars  = readlink (filename, buffer, size);
-      if (nchars == -1)
-       {
-         free (buffer);
-         return NULL;
-       }
-
-      if (nchars < size - 1)
-       {
-         buffer[nchars] = 0;
-         return buffer;
-       }
-      free (buffer);
-      size *= 2;
-    }
-}
-#else /* Not UNIX. */
-char *
-fn_readlink (const char *filename)
-{
-  return NULL;
+  return xreadlink (filename, 32);
 }
-#endif /* Not UNIX. */
 \f
 /* Environment variables. */
 
@@ -712,7 +703,7 @@ fn_open (const char *fn, const char *mode)
 #ifdef unix
   if (fn[0] == '|')
     {
-      if (safer_mode())
+      if (get_safer_mode ())
        return safety_violation (fn);
 
       return popen (&fn[1], mode);
@@ -722,7 +713,7 @@ fn_open (const char *fn, const char *mode)
       char *s;
       FILE *f;
 
-      if (safer_mode())
+      if (get_safer_mode ())
        return safety_violation (fn);
       
       s = local_alloc (strlen (fn));