1 /* mischelp.c - Miscellaneous helper functions
2 * Copyright (C) 1998, 2000, 2001, 2006, 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify it
7 * under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * GnuPG is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copies of the GNU General Public License
27 * and the GNU Lesser General Public License along with this program;
28 * if not, see <https://www.gnu.org/licenses/>.
35 #ifdef HAVE_W32_SYSTEM
36 # define WIN32_LEAN_AND_MEAN
38 #else /*!HAVE_W32_SYSTEM*/
39 # include <sys/types.h>
40 # include <sys/stat.h>
42 #endif /*!HAVE_W32_SYSTEM*/
46 #include "common-defs.h"
47 #include "stringhelp.h"
52 /* Check whether the files NAME1 and NAME2 are identical. This is for
53 example achieved by comparing the inode numbers of the files. */
55 same_file_p (const char *name1, const char *name2)
59 /* First try a shortcut. */
60 if (!compare_filenames (name1, name2))
64 #ifdef HAVE_W32_SYSTEM
66 BY_HANDLE_FILE_INFORMATION info1, info2;
68 #ifdef HAVE_W32CE_SYSTEM
70 wchar_t *wname = utf8_to_wchar (name1);
72 file1 = CreateFile (wname, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
74 file1 = INVALID_HANDLE_VALUE;
78 file1 = CreateFile (name1, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
80 if (file1 == INVALID_HANDLE_VALUE)
81 yes = 0; /* If we can't open the file, it is not the same. */
84 #ifdef HAVE_W32CE_SYSTEM
86 wchar_t *wname = utf8_to_wchar (name2);
88 file2 = CreateFile (wname, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
90 file2 = INVALID_HANDLE_VALUE;
94 file2 = CreateFile (name2, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
96 if (file2 == INVALID_HANDLE_VALUE)
97 yes = 0; /* If we can't open the file, it is not the same. */
100 yes = (GetFileInformationByHandle (file1, &info1)
101 && GetFileInformationByHandle (file2, &info2)
102 && info1.dwVolumeSerialNumber==info2.dwVolumeSerialNumber
103 && info1.nFileIndexHigh == info2.nFileIndexHigh
104 && info1.nFileIndexLow == info2.nFileIndexLow);
109 #else /*!HAVE_W32_SYSTEM*/
110 struct stat info1, info2;
112 yes = (!stat (name1, &info1) && !stat (name2, &info2)
113 && info1.st_dev == info2.st_dev && info1.st_ino == info2.st_ino);
114 #endif /*!HAVE_W32_SYSTEM*/
121 timegm() is a GNU function that might not be available everywhere.
122 It's basically the inverse of gmtime() - you give it a struct tm,
123 and get back a time_t. It differs from mktime() in that it handles
124 the case where the struct tm is UTC and the local environment isn't.
126 Note, that this replacement implementation might not be thread-safe!
128 Some BSDs don't handle the putenv("foo") case properly, so we use
129 unsetenv if the platform has it to remove environment variables.
133 timegm (struct tm *tm)
135 #ifdef HAVE_W32_SYSTEM
136 /* This one is thread safe. */
139 unsigned long long cnsecs;
141 st.wYear = tm->tm_year + 1900;
142 st.wMonth = tm->tm_mon + 1;
143 st.wDay = tm->tm_mday;
144 st.wHour = tm->tm_hour;
145 st.wMinute = tm->tm_min;
146 st.wSecond = tm->tm_sec;
147 st.wMilliseconds = 0; /* Not available. */
148 st.wDayOfWeek = 0; /* Ignored. */
150 /* System time is UTC thus the conversion is pretty easy. */
151 if (!SystemTimeToFileTime (&st, &ft))
153 gpg_err_set_errno (EINVAL);
157 cnsecs = (((unsigned long long)ft.dwHighDateTime << 32)
159 cnsecs -= 116444736000000000ULL; /* The filetime epoch is 1601-01-01. */
160 return (time_t)(cnsecs / 10000000ULL);
162 #else /* (Non thread safe implementation!) */
173 static char *old_zone;
177 old_zone = malloc(3+strlen(zone)+1);
180 strcpy(old_zone,"TZ=");
181 strcat(old_zone,zone);
188 gnupg_unsetenv("TZ");
194 #endif /*!HAVE_TIMEGM*/