Statistics
| Branch: | Revision:

root / src / utils / ln.c @ cbd2c699

History | View | Annotate | Download (981 Bytes)

1
/*
2
* Utility to create a hard link on Windows.
3
*
4
* Author: Andras Varga
5
*/
6

    
7
#define _WIN32_WINNT  0x0500  // Windows 2000 or later
8

    
9
#include <stdio.h>
10
#include <windows.h>
11

    
12
void main(int argc, char *argv[])
13
{
14
    if (argc != 3) {
15
        fprintf(stderr, "ln: creates a Windows hard link.\nUsage: ln <existingFile> <targetFile>\n");
16
        exit(argc==0 ? 0 : 1);
17
    }
18

    
19
    if (!CreateHardLink(argv[2], argv[1], NULL)) {
20
        LPVOID lpMsgBuf;
21
        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
22
                       FORMAT_MESSAGE_FROM_SYSTEM |
23
                       FORMAT_MESSAGE_IGNORE_INSERTS,
24
                       NULL,
25
                       GetLastError(),
26
                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
27
                       (LPTSTR) &lpMsgBuf,
28
                       0,
29
                       NULL);
30
        fprintf(stderr, "ERROR: Cannot create hard link: %s", lpMsgBuf);
31
        LocalFree(lpMsgBuf);
32
        exit(1);
33
    }
34
    exit(0);
35
}