use xmalloc in x_get_window_property
[bpt/emacs.git] / lib-src / ntlib.c
index 8dd16ba..23d7ba7 100644 (file)
@@ -1,6 +1,9 @@
 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
-   Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
-                 2008, 2009, 2010, 2011, 2012  Free Software Foundation, Inc.
+
+Copyright (C) 1994, 2001-2014 Free Software Foundation, Inc.
+
+Author: Geoff Voelker (voelker@cs.washington.edu)
+Created: 10-8-94
 
 This file is part of GNU Emacs.
 
@@ -15,11 +18,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
-
-
-   Geoff Voelker (voelker@cs.washington.edu)                         10-8-94
-*/
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <windows.h>
 #include <stdlib.h>
@@ -29,18 +28,32 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <ctype.h>
+#include <sys/timeb.h>
+#include <mbstring.h>
 
 #include "ntlib.h"
 
+/* MinGW64 defines _TIMEZONE_DEFINED and defines 'struct timespec' in
+   its system headers.  */
+#ifndef _TIMEZONE_DEFINED
+struct timezone
+{
+  int          tz_minuteswest; /* minutes west of Greenwich */
+  int          tz_dsttime;     /* type of dst correction */
+};
+#endif
+
 #define MAXPATHLEN _MAX_PATH
 
 /* Emulate sleep...we could have done this with a define, but that
    would necessitate including windows.h in the files that used it.
    This is much easier.  */
-void
-sleep(unsigned long seconds)
+unsigned
+sleep (unsigned seconds)
 {
   Sleep (seconds * 1000);
+  return 0;
 }
 
 /* Get the current working directory.  */
@@ -56,7 +69,7 @@ static HANDLE getppid_parent;
 static int    getppid_ppid;
 
 int
-getppid(void)
+getppid (void)
 {
   char *ppid;
   DWORD result;
@@ -64,7 +77,7 @@ getppid(void)
   ppid = getenv ("EM_PARENT_PROCESS_ID");
   if (!ppid)
     {
-      printf("no pid.\n");
+      printf ("no pid.\n");
       return 0;
     }
   else
@@ -74,11 +87,11 @@ getppid(void)
 
   if (!getppid_parent)
     {
-      getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid));
+      getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
       if (!getppid_parent)
        {
          printf ("Failed to open handle to parent process: %d\n",
-                GetLastError());
+                GetLastError ());
          exit (1);
        }
     }
@@ -94,13 +107,13 @@ getppid(void)
       return 1;
     case WAIT_FAILED:
     default:
-      printf ("Checking parent status failed: %d\n", GetLastError());
+      printf ("Checking parent status failed: %d\n", GetLastError ());
       exit (1);
     }
 }
 
 char *
-getlogin ()
+getlogin (void)
 {
   static char user_name[256];
   DWORD  length = sizeof (user_name);
@@ -120,19 +133,25 @@ cuserid (char * s)
 }
 
 unsigned
-getuid ()
+getuid (void)
 {
   return 0;
 }
 
 unsigned
-getgid ()
+geteuid (void)
+{
+  return getuid ();
+}
+
+unsigned
+getgid (void)
 {
   return 0;
 }
 
 unsigned
-getegid ()
+getegid (void)
 {
   return 0;
 }
@@ -202,6 +221,29 @@ getpass (const char * prompt)
   return NULL;
 }
 
+/* This is needed because lib/gettime.c calls gettimeofday, which MSVC
+   doesn't have.  Copied from w32.c.  */
+void
+gettimeofday (struct timeval *tv, struct timezone *tz)
+{
+  struct _timeb tb;
+  _ftime (&tb);
+
+  tv->tv_sec = tb.time;
+  tv->tv_usec = tb.millitm * 1000L;
+  /* Implementation note: _ftime sometimes doesn't update the dstflag
+     according to the new timezone when the system timezone is
+     changed.  We could fix that by using GetSystemTime and
+     GetTimeZoneInformation, but that doesn't seem necessary, since
+     Emacs always calls gettimeofday with the 2nd argument NULL (see
+     current_emacs_time).  */
+  if (tz)
+    {
+      tz->tz_minuteswest = tb.timezone;        /* minutes west of Greenwich  */
+      tz->tz_dsttime = tb.dstflag;     /* type of dst correction  */
+    }
+}
+
 int
 fchown (int fd, unsigned uid, unsigned gid)
 {
@@ -219,7 +261,7 @@ sys_ctime (const time_t *t)
 }
 
 FILE *
-sys_fopen(const char * path, const char * mode)
+sys_fopen (const char * path, const char * mode)
 {
   return fopen (path, mode);
 }
@@ -260,6 +302,7 @@ is_exec (const char * name)
         stricmp (p, ".cmd") == 0));
 }
 
+/* FIXME?  This is in config.nt now - is this still needed?  */
 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
 
 /* We need this because nt/inc/sys/stat.h defines struct stat that is
@@ -374,5 +417,77 @@ stat (const char * path, struct stat * buf)
   return 0;
 }
 
-/* arch-tag: 7b63fb83-70ee-4124-8822-54e53e5d0773
-   (do not change this comment) */
+int
+lstat (const char * path, struct stat * buf)
+{
+  return stat (path, buf);
+}
+
+/* Implementation of mkostemp for MS-Windows, to avoid race conditions
+   when using mktemp.  Copied from w32.c.
+
+   This is used only in update-game-score.c.  It is overkill for that
+   use case, since update-game-score renames the temporary file into
+   the game score file, which isn't atomic on MS-Windows anyway, when
+   the game score already existed before running the program, which it
+   almost always does.  But using a simpler implementation just to
+   make a point is uneconomical...  */
+
+int
+mkostemp (char * template, int flags)
+{
+  char * p;
+  int i, fd = -1;
+  unsigned uid = GetCurrentThreadId ();
+  int save_errno = errno;
+  static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
+
+  errno = EINVAL;
+  if (template == NULL)
+    return -1;
+
+  p = template + strlen (template);
+  i = 5;
+  /* replace up to the last 5 X's with uid in decimal */
+  while (--p >= template && p[0] == 'X' && --i >= 0)
+    {
+      p[0] = '0' + uid % 10;
+      uid /= 10;
+    }
+
+  if (i < 0 && p[0] == 'X')
+    {
+      i = 0;
+      do
+       {
+         p[0] = first_char[i];
+         if ((fd = open (template,
+                         flags | _O_CREAT | _O_EXCL | _O_RDWR,
+                         S_IRUSR | S_IWUSR)) >= 0
+             || errno != EEXIST)
+           {
+             if (fd >= 0)
+               errno = save_errno;
+             return fd;
+           }
+       }
+      while (++i < sizeof (first_char));
+    }
+
+  /* Template is badly formed or else we can't generate a unique name.  */
+  return -1;
+}
+
+/* On Windows, you cannot rename into an existing file.  */
+int
+sys_rename (const char *from, const char *to)
+{
+  int retval = rename (from, to);
+
+  if (retval < 0 && errno == EEXIST)
+    {
+      if (unlink (to) == 0)
+       retval = rename (from, to);
+    }
+  return retval;
+}