]> www.fi.muni.cz Git - evince.git/blobdiff - pdf/goo/gfile.cc
distribute gpdf-window-ui.xml
[evince.git] / pdf / goo / gfile.cc
index b57d9c8ba5f6b3bfe647441ed45dd20cc84455b3..d6d2363e5134249d106ae815b8ebbaee477e30ac 100644 (file)
@@ -4,31 +4,35 @@
 //
 // Miscellaneous file and directory name manipulation.
 //
 //
 // Miscellaneous file and directory name manipulation.
 //
-// Copyright 1996 Derek B. Noonburg
+// Copyright 1996-2002 Glyph & Cog, LLC
 //
 //========================================================================
 
 //
 //========================================================================
 
-extern "C" {
+#include <aconf.h>
+
 #ifdef WIN32
 #ifdef WIN32
-#  include <sys/stat.h>
+   extern "C" {
 #  ifndef _MSC_VER
 #    include <kpathsea/win32lib.h>
 #  endif
 #  ifndef _MSC_VER
 #    include <kpathsea/win32lib.h>
 #  endif
+   }
 #else // !WIN32
 #else // !WIN32
-#  ifndef ACORN
+#  if defined(MACOS)
+#    include <sys/stat.h>
+#  elif !defined(ACORN)
 #    include <sys/types.h>
 #    include <sys/stat.h>
 #    include <sys/types.h>
 #    include <sys/stat.h>
+#    include <fcntl.h>
 #  endif
 #  include <limits.h>
 #  include <string.h>
 #  endif
 #  include <limits.h>
 #  include <string.h>
-#  if !defined(VMS) && !defined(ACORN)
+#  if !defined(VMS) && !defined(ACORN) && !defined(MACOS)
 #    include <pwd.h>
 #  endif
 #  if defined(VMS) && (__DECCXX_VER < 50200000)
 #    include <unixlib.h>
 #  endif
 #endif // WIN32
 #    include <pwd.h>
 #  endif
 #  if defined(VMS) && (__DECCXX_VER < 50200000)
 #    include <unixlib.h>
 #  endif
 #endif // WIN32
-}
 #include "GString.h"
 #include "gfile.h"
 
 #include "GString.h"
 #include "gfile.h"
 
@@ -60,6 +64,10 @@ GString *getHomeDir() {
   //---------- RISCOS ----------
   return new GString("@");
 
   //---------- RISCOS ----------
   return new GString("@");
 
+#elif defined(MACOS)
+  //---------- MacOS ----------
+  return new GString(":");
+
 #else
   //---------- Unix ----------
   char *s;
 #else
   //---------- Unix ----------
   char *s;
@@ -91,6 +99,8 @@ GString *getCurrentDir() {
   if (GetCurrentDirectory(sizeof(buf), buf))
 #elif defined(ACORN)
   if (strcpy(buf, "@"))
   if (GetCurrentDirectory(sizeof(buf), buf))
 #elif defined(ACORN)
   if (strcpy(buf, "@"))
+#elif defined(MACOS)
+  if (strcpy(buf, ":"))
 #else
   if (getcwd(buf, sizeof(buf)))
 #endif
 #else
   if (getcwd(buf, sizeof(buf)))
 #endif
@@ -171,6 +181,23 @@ GString *appendToPath(GString *path, char *fileName) {
   }
   return path;
 
   }
   return path;
 
+#elif defined(MACOS)
+  //---------- MacOS ----------
+  char *p;
+  int i;
+
+  path->append(":");
+  i = path->getLength();
+  path->append(fileName);
+  for (p = path->getCString() + i; *p; ++p) {
+    if (*p == '/') {
+      *p = ':';
+    } else if (*p == '.') {
+      *p = ':';
+    }
+  }
+  return path;
+
 #elif defined(__EMX__)
   //---------- OS/2+EMX ----------
   int i;
 #elif defined(__EMX__)
   //---------- OS/2+EMX ----------
   int i;
@@ -278,6 +305,14 @@ GString *grabPath(char *fileName) {
     return new GString(fileName, p - fileName);
   return new GString();
 
     return new GString(fileName, p - fileName);
   return new GString();
 
+#elif defined(MACOS)
+  //---------- MacOS ----------
+  char *p;
+
+  if ((p = strrchr(fileName, ':')))
+    return new GString(fileName, p - fileName);
+  return new GString();
+
 #else
   //---------- Unix ----------
   char *p;
 #else
   //---------- Unix ----------
   char *p;
@@ -302,6 +337,10 @@ GBool isAbsolutePath(char *path) {
   //---------- RISCOS ----------
   return path[0] == '$';
 
   //---------- RISCOS ----------
   return path[0] == '$';
 
+#elif defined(MACOS)
+  //---------- MacOS ----------
+  return path[0] != ':';
+
 #else
   //---------- Unix ----------
   return path[0] == '/';
 #else
   //---------- Unix ----------
   return path[0] == '/';
@@ -315,15 +354,7 @@ GString *makePathAbsolute(GString *path) {
 
   if (!isAbsolutePath(path->getCString())) {
     if (getcwd(buf, sizeof(buf))) {
 
   if (!isAbsolutePath(path->getCString())) {
     if (getcwd(buf, sizeof(buf))) {
-      if (path->getChar(0) == '[') {
-       if (path->getChar(1) != '.')
-         path->insert(0, '.');
-       path->insert(0, buf);
-      } else {
-       path->insert(0, '[');
-       path->insert(1, ']');
-       path->insert(1, buf);
-      }
+      path->insert(0, buf);
     }
   }
   return path;
     }
   }
   return path;
@@ -347,6 +378,11 @@ GString *makePathAbsolute(GString *path) {
   path->insert(0, '@');
   return path;
 
   path->insert(0, '@');
   return path;
 
+#elif defined(MACOS)
+  //---------- MacOS ----------
+  path->del(0, 1);
+  return path;
+
 #else
   //---------- Unix and OS/2+EMX ----------
   struct passwd *pw;
 #else
   //---------- Unix and OS/2+EMX ----------
   struct passwd *pw;
@@ -393,11 +429,149 @@ GString *makePathAbsolute(GString *path) {
 #endif
 }
 
 #endif
 }
 
+time_t getModTime(char *fileName) {
+#ifdef WIN32
+  //~ should implement this, but it's (currently) only used in xpdf
+  return 0;
+#else
+  struct stat statBuf;
+
+  if (stat(fileName, &statBuf)) {
+    return 0;
+  }
+  return statBuf.st_mtime;
+#endif
+}
+
+GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) {
+#if defined(WIN32)
+  //---------- Win32 ----------
+  char *s;
+  char buf[_MAX_PATH];
+  char *fp;
+
+  if (!(s = _tempnam(getenv("TEMP"), NULL))) {
+    return gFalse;
+  }
+  *name = new GString(s);
+  free(s);
+  if (ext) {
+    (*name)->append(ext);
+  }
+  if (!(*f = fopen((*name)->getCString(), mode))) {
+    delete (*name);
+    return gFalse;
+  }
+  return gTrue;
+#elif defined(VMS) || defined(__EMX__) || defined(ACORN) || defined(MACOS)
+  //---------- non-Unix ----------
+  char *s;
+
+  // There is a security hole here: an attacker can create a symlink
+  // with this file name after the tmpnam call and before the fopen
+  // call.  I will happily accept fixes to this function for non-Unix
+  // OSs.
+  if (!(s = tmpnam(NULL))) {
+    return gFalse;
+  }
+  *name = new GString(s);
+  if (ext) {
+    (*name)->append(ext);
+  }
+  if (!(*f = fopen((*name)->getCString(), mode))) {
+    delete (*name);
+    return gFalse;
+  }
+  return gTrue;
+#else
+  //---------- Unix ----------
+  char *s;
+  int fd;
+
+  if (ext) {
+#if HAVE_MKSTEMPS
+    if ((s = getenv("TMPDIR"))) {
+      *name = new GString(s);
+    } else {
+      *name = new GString("/tmp");
+    }
+    (*name)->append("/XXXXXX")->append(ext);
+    fd = mkstemps((*name)->getCString(), strlen(ext));
+#else
+    if (!(s = tmpnam(NULL))) {
+      return gFalse;
+    }
+    *name = new GString(s);
+    (*name)->append(ext);
+    fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600);
+#endif
+  } else {
+#if HAVE_MKSTEMP
+    if ((s = getenv("TMPDIR"))) {
+      *name = new GString(s);
+    } else {
+      *name = new GString("/tmp");
+    }
+    (*name)->append("/XXXXXX");
+    fd = mkstemp((*name)->getCString());
+#else // HAVE_MKSTEMP
+    if (!(s = tmpnam(NULL))) {
+      return gFalse;
+    }
+    *name = new GString(s);
+    fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600);
+#endif // HAVE_MKSTEMP
+  }
+  if (fd < 0 || !(*f = fdopen(fd, mode))) {
+    delete *name;
+    return gFalse;
+  }
+  return gTrue;
+#endif
+}
+
+GBool executeCommand(char *cmd) {
+#ifdef VMS
+  return system(cmd) ? gTrue : gFalse;
+#else
+  return system(cmd) ? gFalse : gTrue;
+#endif
+}
+
+char *getLine(char *buf, int size, FILE *f) {
+  int c, i;
+
+  i = 0;
+  while (i < size - 1) {
+    if ((c = fgetc(f)) == EOF) {
+      break;
+    }
+    buf[i++] = (char)c;
+    if (c == '\x0a') {
+      break;
+    }
+    if (c == '\x0d') {
+      c = fgetc(f);
+      if (c == '\x0a' && i < size - 1) {
+       buf[i++] = (char)c;
+      } else if (c != EOF) {
+       ungetc(c, f);
+      }
+      break;
+    }
+  }
+  buf[i] = '\0';
+  if (i == 0) {
+    return NULL;
+  }
+  return buf;
+}
+
 //------------------------------------------------------------------------
 // GDir and GDirEntry
 //------------------------------------------------------------------------
 
 //------------------------------------------------------------------------
 // GDir and GDirEntry
 //------------------------------------------------------------------------
 
-GDirEntry::GDirEntry(char *dirPath, char *name1, GBool doStat) {
+GDirEntry::GDirEntry(char *dirPath, char *nameA, GBool doStat) {
 #ifdef VMS
   char *p;
 #elif defined(WIN32)
 #ifdef VMS
   char *p;
 #elif defined(WIN32)
@@ -409,17 +583,17 @@ GDirEntry::GDirEntry(char *dirPath, char *name1, GBool doStat) {
   GString *s;
 #endif
 
   GString *s;
 #endif
 
-  name = new GString(name1);
+  name = new GString(nameA);
   dir = gFalse;
   if (doStat) {
 #ifdef VMS
   dir = gFalse;
   if (doStat) {
 #ifdef VMS
-    if (!strcmp(name1, "-") ||
-       ((p = strrchr(name1, '.')) && !strncmp(p, ".DIR;", 5)))
+    if (!strcmp(nameA, "-") ||
+       ((p = strrchr(nameA, '.')) && !strncmp(p, ".DIR;", 5)))
       dir = gTrue;
 #elif defined(ACORN)
 #else
     s = new GString(dirPath);
       dir = gTrue;
 #elif defined(ACORN)
 #else
     s = new GString(dirPath);
-    appendToPath(s, name1);
+    appendToPath(s, nameA);
 #ifdef WIN32
     fa = GetFileAttributes(s->getCString());
     dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
 #ifdef WIN32
     fa = GetFileAttributes(s->getCString());
     dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
@@ -436,9 +610,9 @@ GDirEntry::~GDirEntry() {
   delete name;
 }
 
   delete name;
 }
 
-GDir::GDir(char *name, GBool doStat1) {
+GDir::GDir(char *name, GBool doStatA) {
   path = new GString(name);
   path = new GString(name);
-  doStat = doStat1;
+  doStat = doStatA;
 #if defined(WIN32)
   GString *tmp;
 
 #if defined(WIN32)
   GString *tmp;
 
@@ -447,6 +621,7 @@ GDir::GDir(char *name, GBool doStat1) {
   hnd = FindFirstFile(tmp->getCString(), &ffd);
   delete tmp;
 #elif defined(ACORN)
   hnd = FindFirstFile(tmp->getCString(), &ffd);
   delete tmp;
 #elif defined(ACORN)
+#elif defined(MACOS)
 #else
   dir = opendir(name);
 #ifdef VMS
 #else
   dir = opendir(name);
 #ifdef VMS
@@ -463,6 +638,7 @@ GDir::~GDir() {
     hnd = NULL;
   }
 #elif defined(ACORN)
     hnd = NULL;
   }
 #elif defined(ACORN)
+#elif defined(MACOS)
 #else
   if (dir)
     closedir(dir);
 #else
   if (dir)
     closedir(dir);
@@ -481,6 +657,7 @@ GDirEntry *GDir::getNextEntry() {
     hnd = NULL;
   }
 #elif defined(ACORN)
     hnd = NULL;
   }
 #elif defined(ACORN)
+#elif defined(MACOS)
 #else
   if (dir) {
 #ifdef VMS
 #else
   if (dir) {
 #ifdef VMS
@@ -512,6 +689,7 @@ void GDir::rewind() {
   tmp->append("/*.*");
   hnd = FindFirstFile(tmp->getCString(), &ffd);
 #elif defined(ACORN)
   tmp->append("/*.*");
   hnd = FindFirstFile(tmp->getCString(), &ffd);
 #elif defined(ACORN)
+#elif defined(MACOS)
 #else
   if (dir)
     rewinddir(dir);
 #else
   if (dir)
     rewinddir(dir);