Skip to content

Instantly share code, notes, and snippets.

@thebirk
Last active April 5, 2020 12:09
Show Gist options
  • Save thebirk/bb55842a8489d0dee6c76565d4b8dc3f to your computer and use it in GitHub Desktop.
Save thebirk/bb55842a8489d0dee6c76565d4b8dc3f to your computer and use it in GitHub Desktop.
Basic touch clone for windows
// Copyright (c) 2020 Aleksander B. Birkeland (thebirk)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this softwareand associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright noticeand this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// I don't know is this is how we are supposed to enable all the W stuff
// But I couldn't find any reasonable info on it so /shrug
#define UNICODE
#define _UNICODE
#include <Windows.h>
#include <stdio.h>
#include <locale.h>
#include <stdbool.h>
// linking wsetargv.obj makes windows pass globbed arguments as argv
// build command: "cl /nologo touch.c /link wsetargv.obj"
bool touch(wchar_t* path)
{
HANDLE file = CreateFile(
path,
FILE_WRITE_ATTRIBUTES,
0,
0,
OPEN_ALWAYS,
0,
0
);
if (file == INVALID_HANDLE_VALUE) {
DWORD dLastError = GetLastError();
LPCTSTR strErrorMessage = NULL;
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
dLastError,
0,
(LPWSTR)& strErrorMessage,
0,
NULL);
wprintf(L"failed to open '%ls': %ls\n", path, strErrorMessage);
return false;
}
FILETIME now;
GetSystemTimeAsFileTime(&now);
SetFileTime(file, 0, &now, &now);
CloseHandle(file);
return true;
}
int wmain(int argc, wchar_t **argv)
{
setlocale(LC_ALL, "");
for (int i = 1; i < argc; i++) {
if (touch(argv[i])) {
wprintf(L"touched %ls\n", argv[i]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment