From f9efb48450b1152564b535bf1d5fb9f9591462b1 Mon Sep 17 00:00:00 2001 From: dolphin6k Date: Sun, 31 Oct 2021 15:49:55 +0900 Subject: [PATCH] win32_trash() to work correctly on various version of windows. Careful tests are still required. --- src/win32_trash.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/win32_trash.c b/src/win32_trash.c index 96af7d984..ca5cb9262 100644 --- a/src/win32_trash.c +++ b/src/win32_trash.c @@ -1,15 +1,37 @@ #include +#include -int win32_trash(char *path); -int win32_trash(char *path) +int MoveFileToRecycleBin(const TCHAR *fullPathName); +int win32_trash(const char *path); + +int MoveFileToRecycleBin(const TCHAR *fullPathName) { - SHFILEOPSTRUCT op; + SHFILEOPSTRUCT fileOp; + const TCHAR *src = fullPathName; + TCHAR *dest; + + fileOp.pFrom = dest = alloca(sizeof(*dest) * (_tcslen(fullPathName) + 2)); + while((*dest++ = *src++) != _T('\0')) {} + *dest = _T('\0'); + + fileOp.hwnd = NULL; + fileOp.wFunc = FO_DELETE; + fileOp.pTo = NULL; + fileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; + return SHFileOperation(&fileOp); +} + +int win32_trash(const char *path) +{ + char *p, *src; int ret; - - op.wFunc = FO_DELETE; - op.pFrom = path; - op.fFlags = FOF_SILENT|FOF_ALLOWUNDO; - ret = SHFileOperationA(&op); - + + src = p = strdup(path); + while(*p != '\0'){ + if (*p == '/') *p = '\\'; + p++; + } + ret = MoveFileToRecycleBin(src); + free(p); return ret; }