xref: /PHP-7.3/win32/ftok.c (revision 03f3b847)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Anatol Belski <ab@php.net>                                   |
16    +----------------------------------------------------------------------+
17  */
18 
19 #include "ipc.h"
20 
21 #include <windows.h>
22 #include <sys/stat.h>
23 
24 #include "ioutil.h"
25 
26 PHP_WIN32_IPC_API key_t
ftok(const char * pathname,int proj_id)27 ftok(const char *pathname, int proj_id)
28 {/*{{{*/
29 	HANDLE fh;
30 	struct _stat st;
31 	BY_HANDLE_FILE_INFORMATION bhfi;
32 	key_t ret;
33 	PHP_WIN32_IOUTIL_INIT_W(pathname)
34 
35 	if (!pathw) {
36 		return (key_t)-1;
37 	}
38 
39 	if (_wstat(pathw, &st) < 0) {
40 		PHP_WIN32_IOUTIL_CLEANUP_W()
41 		return (key_t)-1;
42 	}
43 
44 	if ((fh = CreateFileW(pathw, FILE_GENERIC_READ, PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE, 0, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE) {
45 		PHP_WIN32_IOUTIL_CLEANUP_W()
46 		return (key_t)-1;
47 	}
48 
49 	if (!GetFileInformationByHandle(fh, &bhfi)) {
50 		PHP_WIN32_IOUTIL_CLEANUP_W()
51 		CloseHandle(fh);
52 		return (key_t)-1;
53 	}
54 
55 	ret = (key_t) ((proj_id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | ((bhfi.nFileIndexLow | (__int64)bhfi.nFileIndexHigh << 32) & 0xffff));
56 
57 	CloseHandle(fh);
58 	PHP_WIN32_IOUTIL_CLEANUP_W()
59 
60 	return ret;
61 }/*}}}*/
62 
63 /*
64  * Local variables:
65  * tab-width: 4
66  * c-basic-offset: 4
67  * End:
68  * vim600: sw=4 ts=4 fdm=marker
69  * vim<600: sw=4 ts=4
70  */
71