2008-10-09 14:56:31 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2012-02-17 12:48:11 -08:00
|
|
|
#include <stdlib.h>
|
2008-09-13 14:19:22 -07:00
|
|
|
#include <stdio.h>
|
2012-02-17 12:48:11 -08:00
|
|
|
#include <io.h>
|
2019-05-15 04:27:37 -07:00
|
|
|
#include <direct.h>
|
2012-02-17 12:48:11 -08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2013-03-30 08:09:37 -07:00
|
|
|
#include <sys/types.h>
|
2010-02-27 23:57:03 -08:00
|
|
|
|
2013-01-10 12:25:44 -08:00
|
|
|
#include <errno.h>
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2019-05-15 04:27:37 -07:00
|
|
|
#include "evil_private.h"
|
2008-09-13 14:19:22 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Environment variable related functions
|
|
|
|
*
|
|
|
|
* int setenv (const char *name, const char *value, int overwrite);
|
|
|
|
* void unsetenv (const char *name);
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
evil: Rename EAPI macro to EVIL_API in Evil library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
```__attribute__ ((visibility ("default")))``` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
```
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
```__atttribute__((visibility("default")))```
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: raster, vtorri, jptiz, lucas, woohyun
Reviewed By: vtorri, jptiz
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
|
|
|
EVIL_API int
|
2008-09-13 14:19:22 -07:00
|
|
|
setenv(const char *name,
|
|
|
|
const char *value,
|
|
|
|
int overwrite)
|
|
|
|
{
|
2009-01-11 22:03:51 -08:00
|
|
|
char *old_name;
|
|
|
|
char *str;
|
|
|
|
size_t length;
|
|
|
|
int res;
|
2008-09-13 14:19:22 -07:00
|
|
|
|
|
|
|
if (!name || !*name)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* if '=' is found, return EINVAL */
|
|
|
|
if (strchr (name, '='))
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if name is already set and overwrite is 0, we exit with success */
|
|
|
|
old_name = getenv(name);
|
2008-09-14 00:07:39 -07:00
|
|
|
if (!overwrite && old_name)
|
2008-09-13 14:19:22 -07:00
|
|
|
return 0;
|
|
|
|
|
2009-04-08 10:22:12 -07:00
|
|
|
length = value ? strlen(value) : 0;
|
|
|
|
length += strlen(name) + 2;
|
2008-09-13 14:19:22 -07:00
|
|
|
str = (char *)malloc(length);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!value)
|
|
|
|
sprintf(str, "%s=", name);
|
|
|
|
else
|
|
|
|
sprintf(str, "%s=%s", name, value);
|
|
|
|
res = _putenv(str);
|
|
|
|
free(str);
|
|
|
|
|
2008-09-14 00:07:39 -07:00
|
|
|
return res;
|
2008-09-13 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
evil: Rename EAPI macro to EVIL_API in Evil library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
```__attribute__ ((visibility ("default")))``` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
```
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
```__atttribute__((visibility("default")))```
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: raster, vtorri, jptiz, lucas, woohyun
Reviewed By: vtorri, jptiz
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
|
|
|
EVIL_API int
|
2008-09-13 14:19:22 -07:00
|
|
|
unsetenv(const char *name)
|
|
|
|
{
|
2008-09-14 23:56:14 -07:00
|
|
|
return setenv(name, NULL, 1);
|
2008-09-13 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Files related functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
evil: Rename EAPI macro to EVIL_API in Evil library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
```__attribute__ ((visibility ("default")))``` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
```
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
```__atttribute__((visibility("default")))```
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: raster, vtorri, jptiz, lucas, woohyun
Reviewed By: vtorri, jptiz
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
|
|
|
EVIL_API char *
|
2008-09-13 14:19:22 -07:00
|
|
|
realpath(const char *file_name, char *resolved_name)
|
|
|
|
{
|
2012-07-03 01:57:07 -07:00
|
|
|
char *retname = NULL; /* we will return this, if we fail */
|
|
|
|
|
|
|
|
/* SUSv3 says we must set `errno = EINVAL', and return NULL,
|
|
|
|
* if `name' is passed as a NULL pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (file_name == NULL)
|
|
|
|
errno = EINVAL;
|
|
|
|
|
|
|
|
/* Otherwise, `name' must refer to a readable filesystem object,
|
|
|
|
* if we are going to resolve its absolute path name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (access(file_name, 4) == 0)
|
|
|
|
{
|
|
|
|
/* If `name' didn't point to an existing entity,
|
|
|
|
* then we don't get to here; we simply fall past this block,
|
|
|
|
* returning NULL, with `errno' appropriately set by `access'.
|
|
|
|
*
|
|
|
|
* When we _do_ get to here, then we can use `_fullpath' to
|
|
|
|
* resolve the full path for `name' into `resolved', but first,
|
|
|
|
* check that we have a suitable buffer, in which to return it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((retname = resolved_name) == NULL)
|
|
|
|
{
|
|
|
|
/* Caller didn't give us a buffer, so we'll exercise the
|
|
|
|
* option granted by SUSv3, and allocate one.
|
|
|
|
*
|
|
|
|
* `_fullpath' would do this for us, but it uses `malloc', and
|
|
|
|
* Microsoft's implementation doesn't set `errno' on failure.
|
|
|
|
* If we don't do this explicitly ourselves, then we will not
|
|
|
|
* know if `_fullpath' fails on `malloc' failure, or for some
|
|
|
|
* other reason, and we want to set `errno = ENOMEM' for the
|
|
|
|
* `malloc' failure case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
retname = malloc(_MAX_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By now, we should have a valid buffer.
|
|
|
|
* If we don't, then we know that `malloc' failed,
|
|
|
|
* so we can set `errno = ENOMEM' appropriately.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (retname == NULL)
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
|
|
|
/* Otherwise, when we do have a valid buffer,
|
|
|
|
* `_fullpath' should only fail if the path name is too long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if ((retname = _fullpath(retname, file_name, _MAX_PATH)) == NULL)
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* By the time we get to here,
|
|
|
|
* `retname' either points to the required resolved path name,
|
|
|
|
* or it is NULL, with `errno' set appropriately, either of which
|
|
|
|
* is our required return condition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return retname;
|
2008-09-13 14:19:22 -07:00
|
|
|
}
|