xref: /PHP-7.4/win32/ftok.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 "php.h"
20 #include "ipc.h"
21 
22 #include <windows.h>
23 #include <sys/stat.h>
24 
25 #include "ioutil.h"
26 
27 PHP_WIN32_IPC_API key_t
ftok(const char * pathname,int proj_id)28 ftok(const char *pathname, int proj_id)
29 {/*{{{*/
30 	HANDLE fh;
31 	struct _stat st;
32 	BY_HANDLE_FILE_INFORMATION bhfi;
33 	key_t ret;
34 	PHP_WIN32_IOUTIL_INIT_W(pathname)
35 
36 	if (!pathw) {
37 		return (key_t)-1;
38 	}
39 
40 	if (_wstat(pathw, &st) < 0) {
41 		PHP_WIN32_IOUTIL_CLEANUP_W()
42 		return (key_t)-1;
43 	}
44 
45 	if ((fh = CreateFileW(pathw, FILE_GENERIC_READ, PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE, 0, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE) {
46 		PHP_WIN32_IOUTIL_CLEANUP_W()
47 		return (key_t)-1;
48 	}
49 
50 	if (!GetFileInformationByHandle(fh, &bhfi)) {
51 		PHP_WIN32_IOUTIL_CLEANUP_W()
52 		CloseHandle(fh);
53 		return (key_t)-1;
54 	}
55 
56 	ret = (key_t) ((proj_id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | ((bhfi.nFileIndexLow | (__int64)bhfi.nFileIndexHigh << 32) & 0xffff));
57 
58 	CloseHandle(fh);
59 	PHP_WIN32_IOUTIL_CLEANUP_W()
60 
61 	return ret;
62 }/*}}}*/
63