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