agent/
[gnupg.git] / jnlib / mischelp.c
blobf7df5c154123757002fb2f6656cc7aefbff153a5
1 /* mischelp.c - Miscellaneous helper functions
2 * Copyright (C) 1998, 2000, 2001, 2006, 2007 Free Software Foundation, Inc.
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #ifdef HAVE_W32_SYSTEM
25 # define WIN32_LEAN_AND_MEAN
26 # include <windows.h>
27 #else /*!HAVE_W32_SYSTEM*/
28 # include <sys/types.h>
29 # include <sys/stat.h>
30 # include <unistd.h>
31 #endif /*!HAVE_W32_SYSTEM*/
33 #include "libjnlib-config.h"
34 #include "stringhelp.h"
35 #include "mischelp.h"
38 /* Check whether the files NAME1 and NAME2 are identical. This is for
39 example achieved by comparing the inode numbers of the files. */
40 int
41 same_file_p (const char *name1, const char *name2)
43 int yes;
45 /* First try a shortcut. */
46 if (!compare_filenames (name1, name2))
47 yes = 1;
48 else
50 #ifdef HAVE_W32_SYSTEM
51 HANDLE file1, file2;
52 BY_HANDLE_FILE_INFORMATION info1, info2;
54 file1 = CreateFile (name1, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
55 if (file1 == INVALID_HANDLE_VALUE)
56 yes = 0; /* If we can't open the file, it is not the same. */
57 else
59 file2 = CreateFile (name2, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
60 if (file1 == INVALID_HANDLE_VALUE)
61 yes = 0; /* If we can't open the file, it is not the same. */
62 else
64 yes = (GetFileInformationByHandle (file1, &info1)
65 && GetFileInformationByHandle (file2, &info2)
66 && info1.dwVolumeSerialNumber==info2.dwVolumeSerialNumber
67 && info1.nFileIndexHigh == info2.nFileIndexHigh
68 && info1.nFileIndexLow == info2.nFileIndexLow);
69 CloseHandle (file2);
71 CloseHandle (file1);
73 #else /*!HAVE_W32_SYSTEM*/
74 struct stat info1, info2;
76 yes = (!stat (name1, &info1) && !stat (name2, &info2)
77 && info1.st_dev == info2.st_dev && info1.st_ino == info2.st_ino);
78 #endif /*!HAVE_W32_SYSTEM*/
80 return yes;
85 timegm() is a GNU function that might not be available everywhere.
86 It's basically the inverse of gmtime() - you give it a struct tm,
87 and get back a time_t. It differs from mktime() in that it handles
88 the case where the struct tm is UTC and the local environment isn't.
90 Note, that this replacement implementaion is not thread-safe!
92 Some BSDs don't handle the putenv("foo") case properly, so we use
93 unsetenv if the platform has it to remove environment variables.
95 #ifndef HAVE_TIMEGM
96 time_t
97 timegm (struct tm *tm)
99 time_t answer;
100 char *zone;
102 zone=getenv("TZ");
103 putenv("TZ=UTC");
104 tzset();
105 answer=mktime(tm);
106 if(zone)
108 static char *old_zone;
110 if (!old_zone)
112 old_zone = malloc(3+strlen(zone)+1);
113 if (old_zone)
115 strcpy(old_zone,"TZ=");
116 strcat(old_zone,zone);
119 if (old_zone)
120 putenv (old_zone);
122 else
123 #ifdef HAVE_UNSETENV
124 unsetenv("TZ");
125 #else
126 putenv("TZ");
127 #endif
129 tzset();
130 return answer;
132 #endif /*!HAVE_TIMEGM*/