1278 |
mateusz.vi |
1 |
/* This file is part of the svarlang project and is published under the terms
|
|
|
2 |
* of the MIT license.
|
|
|
3 |
*
|
|
|
4 |
* Copyright (C) 2021-2023 Mateusz Viste
|
|
|
5 |
*
|
|
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
7 |
* copy of this software and associated documentation files (the "Software"),
|
|
|
8 |
* to deal in the Software without restriction, including without limitation
|
|
|
9 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
10 |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
11 |
* Software is furnished to do so, subject to the following conditions:
|
|
|
12 |
*
|
|
|
13 |
* The above copyright notice and this permission notice shall be included in
|
|
|
14 |
* all copies or substantial portions of the Software.
|
|
|
15 |
*
|
|
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
22 |
* DEALINGS IN THE SOFTWARE.
|
|
|
23 |
*/
|
|
|
24 |
|
1279 |
mateusz.vi |
25 |
#include <stdlib.h> /* NULL */
|
|
|
26 |
#include <stdio.h>
|
1278 |
mateusz.vi |
27 |
|
|
|
28 |
#include "svarlang.h"
|
|
|
29 |
|
1279 |
mateusz.vi |
30 |
int svarlang_autoload_exepath(const char *selfexe, const char *lang) {
|
|
|
31 |
unsigned short selflen;
|
|
|
32 |
char self_ext_backup[3];
|
|
|
33 |
char *self_ext_ptr;
|
1278 |
mateusz.vi |
34 |
int res;
|
|
|
35 |
|
1279 |
mateusz.vi |
36 |
/* validate selfexe: must be at least 5 bytes long and 4th char from end must
|
|
|
37 |
* be a dot (like "a.exe" or "c:\b.com" or "..\..\test\run.exe") */
|
|
|
38 |
if (selfexe == NULL) return(-1);
|
|
|
39 |
for (selflen = 0; selfexe[selflen] != 0; selflen++);
|
|
|
40 |
if ((selflen < 5) || (selfexe[selflen - 4] != '.')) return(-2);
|
1278 |
mateusz.vi |
41 |
|
1279 |
mateusz.vi |
42 |
self_ext_ptr = (char *)selfexe + selflen - 3; /* disregard CONST (I revert original content later, so the caller won't notice */
|
1278 |
mateusz.vi |
43 |
|
|
|
44 |
/* copy extension to buffer and replace it with "lng" */
|
1279 |
mateusz.vi |
45 |
self_ext_backup[0] = self_ext_ptr[0];
|
|
|
46 |
self_ext_backup[1] = self_ext_ptr[1];
|
|
|
47 |
self_ext_backup[2] = self_ext_ptr[2];
|
1278 |
mateusz.vi |
48 |
|
1279 |
mateusz.vi |
49 |
self_ext_ptr[0] = 'L';
|
|
|
50 |
self_ext_ptr[1] = 'N';
|
|
|
51 |
self_ext_ptr[2] = 'G';
|
1278 |
mateusz.vi |
52 |
|
|
|
53 |
/* try loading it now */
|
1279 |
mateusz.vi |
54 |
res = svarlang_load(selfexe, lang);
|
1278 |
mateusz.vi |
55 |
|
|
|
56 |
/* restore the original filename and quit */
|
1279 |
mateusz.vi |
57 |
self_ext_ptr[0] = self_ext_backup[0];
|
|
|
58 |
self_ext_ptr[1] = self_ext_backup[1];
|
|
|
59 |
self_ext_ptr[2] = self_ext_backup[2];
|
1278 |
mateusz.vi |
60 |
|
|
|
61 |
return(res);
|
|
|
62 |
}
|