Beispiel #1
0
//!
//! link() wrapper
//!
int link(const String& oldPath, const String& newPath)
{
    String::W oldPathW(oldPath.widen());
    String::W newPathW(newPath.widen());
    int rc = (CreateHardLinkW(newPathW, oldPathW, 0) != 0)? resetErr(): setErr();
    return rc; //0==success
}
Beispiel #2
0
//!
//! rename() wrapper
//!
int rename(const String& oldPath, const String& newPath)
{
    String::W oldPathW(oldPath.widen());
    String::W newPathW(newPath.widen());
    int rc = (MoveFileW(oldPathW, newPathW) != 0)? resetErr(): setErr();
    return rc; //0==success
}
Beispiel #3
0
//!
//! CopyFile() wrapper. This Win32 API is technically not part of the
//! c-runtime library, but it fits here best.
//!
int copy(const String& srcPath, const String& dstPath, bool failIfExists)
{
    String::W srcPathW(srcPath.widen());
    String::W dstPathW(dstPath.widen());
    int rc = (CopyFileW(srcPathW, dstPathW, failIfExists) != 0)? resetErr(): setErr();
    return rc; //0==success
}
Beispiel #4
0
//!
//! Minimal chmod() emulation. Change given path accessibility specified by
//! mode (00=readonly, 02=writable). Return 0 if successful. Return non-zero
//! otherwise.
//!
int chmod(const String& path, int mode)
{
    int rc;
    String::W pathW(path.widen());
    unsigned int attr = GetFileAttributesW(pathW);
    if (attr != INVALID_FILE_ATTRIBUTES)
    {
        switch (mode)
        {
        case 00:
            rc = ((attr & FILE_ATTRIBUTE_READONLY) == 0)?
                setAttr(pathW, attr | FILE_ATTRIBUTE_READONLY):
                resetErr();
            break;
        case 02:
            rc = ((attr & FILE_ATTRIBUTE_READONLY) != 0)?
                setAttr(pathW, attr & ~FILE_ATTRIBUTE_READONLY):
                resetErr();
            break;
        default:
            rc = (_set_errno(EINVAL), -1);
            break;
        }
    }
    else
    {
        rc = setErr();
    }

    return rc; //0==success
}
Beispiel #5
0
//!
//! putenv() wrapper. If v is zero, remove the environment variable named k.
//! Otherwise, create or update the environment key-value pair.
//!
bool putenv(const String& k, const String* v)
{
    String::W kW(k.widen());
    bool ok;
    if (v == 0)
    {
        // SetEnvironmentVariableW() in win7 (but not other platforms?) is ok for a nonexistent variable.
        wchar_t* vW = 0;
        ok = (GetEnvironmentVariableW(kW, vW, 0) > 0)? (SetEnvironmentVariableW(kW, 0) != 0): false;
    }
    else
    {
        String::W vW(v->widen());
        ok = (SetEnvironmentVariableW(kW, vW) != 0);
    }

    return ok;
}
Beispiel #6
0
//!
//! getenv() wrapper. Return true if found. 
//!
bool getenv(const String& k, String& v, bool expandEnv)
{
    String::W kW(k.widen());
    wchar_t* vW = new wchar_t[EBUF_SIZ];
    size_t numWchars = GetEnvironmentVariableW(kW, vW, EBUF_SIZ - 1);
    bool ok = ((numWchars > 0) || (GetLastError() != ERROR_ENVVAR_NOT_FOUND));

    if (ok && expandEnv)
    {
        vW[numWchars] = 0;
        wchar_t* xW = new wchar_t[EBUF_SIZ];
        size_t n = ExpandEnvironmentStringsW(vW, xW, EBUF_SIZ - 1);
        n? v.reset(xW, n): v.reset(vW, numWchars);
        delete[] xW;
    }
    else
    {
        v.reset(vW, numWchars);
    }

    delete[] vW;
    return ok;
}
Beispiel #7
0
//!
//! unlink() wrapper
//!
int unlink(const String& path)
{
    String::W pathW(path.widen());
    int rc = (DeleteFileW(pathW) != 0)? resetErr(): setErr();
    return rc; //0==success
}
Beispiel #8
0
//!
//! rmdir() wrapper
//!
int rmdir(const String& path)
{
    String::W pathW(path.widen());
    int rc = (RemoveDirectoryW(pathW) != 0)? resetErr(): setErr();
    return rc; //0==success
}
Beispiel #9
0
//!
//! mkdir() wrapper
//!
int mkdir(const String& path)
{
    String::W pathW(path.widen());
    int rc = (CreateDirectoryW(pathW, 0) != 0)? resetErr(): setErr();
    return rc; //0==success
}
Beispiel #10
0
//!
//! chdir() wrapper
//!
int chdir(const String& path)
{
    String::W pathW(path.widen());
    int rc = (SetCurrentDirectoryW(pathW) != 0)? resetErr(): setErr();
    return rc; //0==success
}