| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================ |
| 6 | ** |
| 7 | ** Source: CreateProcessW/test1/childprocess.c |
| 8 | ** |
| 9 | ** Purpose: Test to ensure CreateProcessW starts a new process. This test |
| 10 | ** launches a child process, and examines a file written by the child. |
| 11 | ** This code is the child code. |
| 12 | ** |
| 13 | ** Dependencies: GetCurrentDirectory |
| 14 | ** MultiByteToWideChar |
| 15 | ** wcslen |
| 16 | ** strlen |
| 17 | ** WideCharToMultiByte |
| 18 | ** fopen |
| 19 | ** fclose |
| 20 | ** fprintf |
| 21 | ** |
| 22 | |
| 23 | ** |
| 24 | **=========================================================*/ |
| 25 | |
| 26 | #define UNICODE |
| 27 | #include <palsuite.h> |
| 28 | |
| 29 | const WCHAR szCommonFileW[] = |
| 30 | {'c','h','i','l','d','d','a','t','a','.','t','m','p','\0'}; |
| 31 | |
| 32 | const WCHAR szPathDelimW[] = {'\\','\0'}; |
| 33 | |
| 34 | const char *szCommonStringA = "058d2d057111a313aa82401c2e856002\0" ; |
| 35 | |
| 36 | /* |
| 37 | * Take two wide strings representing file and directory names |
| 38 | * (dirName, fileName), join the strings with the appropriate path |
| 39 | * delimiter and populate a wide character buffer (absPathName) with |
| 40 | * the resulting string. |
| 41 | * |
| 42 | * Returns: The number of wide characters in the resulting string. |
| 43 | * 0 is returned on Error. |
| 44 | */ |
| 45 | int |
| 46 | mkAbsoluteFilenameW ( |
| 47 | LPWSTR dirName, |
| 48 | DWORD dwDirLength, |
| 49 | LPCWSTR fileName, |
| 50 | DWORD dwFileLength, |
| 51 | LPWSTR absPathName ) |
| 52 | { |
| 53 | extern const WCHAR szPathDelimW[]; |
| 54 | |
| 55 | DWORD sizeDN, sizeFN, sizeAPN; |
| 56 | |
| 57 | sizeDN = wcslen( dirName ); |
| 58 | sizeFN = wcslen( fileName ); |
| 59 | sizeAPN = (sizeDN + 1 + sizeFN + 1); |
| 60 | |
| 61 | /* insure ((dirName + DELIM + fileName + \0) =< _MAX_PATH ) */ |
| 62 | if ( sizeAPN > _MAX_PATH ) |
| 63 | { |
| 64 | return ( 0 ); |
| 65 | } |
| 66 | |
| 67 | wcsncpy(absPathName, dirName, dwDirLength +1); |
| 68 | wcsncpy(absPathName, szPathDelimW, 2); |
| 69 | wcsncpy(absPathName, fileName, dwFileLength +1); |
| 70 | |
| 71 | return (sizeAPN); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | int __cdecl main( int argc, char **argv ) |
| 76 | { |
| 77 | |
| 78 | static FILE * fp; |
| 79 | |
| 80 | DWORD dwFileLength; |
| 81 | DWORD dwDirLength; |
| 82 | DWORD dwSize; |
| 83 | |
| 84 | char *szAbsPathNameA; |
| 85 | WCHAR szDirNameW[_MAX_DIR]; |
| 86 | WCHAR szAbsPathNameW[_MAX_PATH]; |
| 87 | |
| 88 | if(0 != (PAL_Initialize(argc, argv))) |
| 89 | { |
| 90 | return ( FAIL ); |
| 91 | } |
| 92 | |
| 93 | dwDirLength = GetCurrentDirectory( _MAX_PATH, szDirNameW ); |
| 94 | |
| 95 | if (0 == dwDirLength) |
| 96 | { |
| 97 | Fail ("GetCurrentDirectory call failed. Could not get " |
| 98 | "current working directory\n. Exiting.\n" ); |
| 99 | } |
| 100 | |
| 101 | dwFileLength = wcslen( szCommonFileW ); |
| 102 | |
| 103 | dwSize = mkAbsoluteFilenameW( szDirNameW, dwDirLength, szCommonFileW, |
| 104 | dwFileLength, szAbsPathNameW ); |
| 105 | |
| 106 | if (0 == dwSize) |
| 107 | { |
| 108 | Fail ("Palsuite Code: mkAbsoluteFilename() call failed. Could " |
| 109 | "not build absolute path name to file\n. Exiting.\n" ); |
| 110 | } |
| 111 | |
| 112 | /* set the string length for the open call */ |
| 113 | szAbsPathNameA = (char*)malloc(dwSize +1); |
| 114 | |
| 115 | if (NULL == szAbsPathNameA) |
| 116 | { |
| 117 | Fail ("Unable to malloc (%d) bytes. Exiting\n" , (dwSize +1) ); |
| 118 | } |
| 119 | |
| 120 | WideCharToMultiByte (CP_ACP, 0, szAbsPathNameW, -1, szAbsPathNameA, |
| 121 | (dwSize + 1), NULL, NULL); |
| 122 | |
| 123 | if ( NULL == ( fp = fopen ( szAbsPathNameA , "w+" ) ) ) |
| 124 | { |
| 125 | /* |
| 126 | * A return value of NULL indicates an error condition or an |
| 127 | * EOF condition |
| 128 | */ |
| 129 | Fail ("%s unable to open %s for writing. Exiting.\n" , argv[0] |
| 130 | , szAbsPathNameA ); |
| 131 | } |
| 132 | |
| 133 | free (szAbsPathNameA); |
| 134 | |
| 135 | if ( 0 >= ( fprintf ( fp, "%s" , szCommonStringA ))) |
| 136 | { |
| 137 | Fail("%s unable to write to %s. Exiting.\n" , argv[0] |
| 138 | , szAbsPathNameA ); |
| 139 | } |
| 140 | |
| 141 | if (0 != (fclose ( fp ))) |
| 142 | { |
| 143 | Fail ("%s unable to close file %s. Pid may not be " |
| 144 | "written to file. Exiting.\n" , argv[0], szAbsPathNameA ); |
| 145 | } |
| 146 | |
| 147 | PAL_Terminate(); |
| 148 | return ( PASS ); |
| 149 | |
| 150 | } |
| 151 | |