Pierre A. Humblet schrieb:
It works on normal files. I haven't tested with the
special names because I forgot how to create them !
Feedback welcome.
works fine on w2k.
attached is a test to create such files.
unlink works fine on these.
didn't test with wchar and unicode files yet, just char.
but coreutils/findutils don't work with unicode files anyway.
(just testing findutils-4.1.20)
On reflection, however, wouldn't it be a little easier just to prepend
the path being deleted with a: \\.\ so that "rm nul" would eventually
translate to DeleteFile("\\.\c:\foo\null") (I'm not using true C
backslash quoting here)? I don't know if that would work on Windows 9x,
though.
// c++ -I../src/winsup/cygwin -o testcreate testcreate.cc -lntdll
#include "winsup.h"
#include <stdio.h>
#include <ntdef.h>
#include "ntdll.h"
//#define NTCREATE
#undef NTCREATE
NTSTATUS
NtCreateDirectoryObject(
OUT PHANDLE DirectoryHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes
);
#ifndef NTCREATE
static HANDLE
create (char *path)
{
HANDLE hFile;
char pwd[CYG_MAX_PATH], dev[CYG_MAX_PATH];
int len;
//create ("\\\\.\\c:\\con");
if (len = GetCurrentDirectoryA (CYG_MAX_PATH, pwd)) {
strcpy(dev, "\\\\.\\");
strcat(dev, pwd);
strcat(dev, "\\");
strcat(dev, path);
}
hFile = CreateFile(dev, // file to create
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL | // normal file
FILE_FLAG_OVERLAPPED, // asynchronous I/O
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE) return 0;
printf("%s created\n", path);
CloseHandle(hFile);
return hFile;
}
#else
static HANDLE
nt_create (WCHAR *wpath)
{
WCHAR pwd[2*CYG_MAX_PATH];
UNICODE_STRING upath = {0, sizeof (wpath), wpath};
//UNICODE_STRING cpath = {0, 2, L"."};
int len;
HANDLE x, root = NULL;
OBJECT_ATTRIBUTES attr;
IO_STATUS_BLOCK io;
NTSTATUS status;
if (len = GetCurrentDirectoryW (2*CYG_MAX_PATH, pwd)) {
UNICODE_STRING upwd = {0, sizeof (pwd), pwd};
InitializeObjectAttributes (&attr, &upwd, OBJ_CASE_INSENSITIVE, NULL, NULL);
NtOpenFile(&root, STANDARD_RIGHTS_ALL, &attr, &io, 0, 0);
}
InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE, root, NULL);
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devnotes/winprog/ntcreatefile.asp
status = NtCreateFile (&x, STANDARD_RIGHTS_ALL, &attr, &io, NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ, FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0);
if (!NT_SUCCESS (status))
{
printf("error creating %ls\n", wpath);
return 0;
}
else {
CloseHandle(x);
return x;
}
}
#endif
int main(int argc, char** argv)
{
#ifndef NTCREATE
create ("con");
create ("com");
create ("nul");
create ("aux");
create ("prn");
create ("lpt1");
create ("...");
#else
nt_create (L"con");
nt_create (L"nul");
nt_create (L"aux");
nt_create (L"prn");
nt_create (L"lpt1");
nt_create (L"...");
#endif
return(0);
}