1 | #define INCL_BASE
|
---|
2 | #include <os2.h>
|
---|
3 | #include <stdio.h>
|
---|
4 |
|
---|
5 |
|
---|
6 | int main(int argc, char **argv)
|
---|
7 | {
|
---|
8 | const char *pszFilename = "./tstfile";
|
---|
9 | unsigned cbSize = 0;
|
---|
10 | if (argc >= 2)
|
---|
11 | cbSize = atol(argv[1]);
|
---|
12 | if (!cbSize)
|
---|
13 | cbSize = 1024*1024*1024;
|
---|
14 | if (argc >= 3)
|
---|
15 | pszFilename = argv[2];
|
---|
16 |
|
---|
17 | ULONG ulAction;
|
---|
18 | HFILE hFile;
|
---|
19 | int rc = DosOpen(pszFilename, &hFile, &ulAction, cbSize / 2, FILE_NORMAL,
|
---|
20 | OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
|
---|
21 | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE, NULL);
|
---|
22 | if (!rc)
|
---|
23 | {
|
---|
24 | static char achBuf[0xff00];
|
---|
25 | ULONG off = 0;
|
---|
26 | unsigned cNonZero = 0;
|
---|
27 | printf("info: successfully create file %s with %d bytes size\n", pszFilename, cbSize);
|
---|
28 | while (off < cbSize / 2)
|
---|
29 | {
|
---|
30 | char *pch;
|
---|
31 | ULONG cbRead = 0;
|
---|
32 | rc = DosRead(hFile, &achBuf, sizeof(achBuf), &cbRead);
|
---|
33 | if (rc)
|
---|
34 | {
|
---|
35 | printf("DosRead -> %d, off=%d\n", rc);
|
---|
36 | break;
|
---|
37 | }
|
---|
38 | if (!cbRead && !off)
|
---|
39 | {
|
---|
40 | printf("Using samba. DosOpen(,,,size/2,,) didn't set the filesize it seems...\n");
|
---|
41 | break;
|
---|
42 | }
|
---|
43 |
|
---|
44 | pch = achBuf;
|
---|
45 | while (pch < &achBuf[cbRead])
|
---|
46 | {
|
---|
47 | if (*pch)
|
---|
48 | {
|
---|
49 | printf(".");
|
---|
50 | cNonZero++;
|
---|
51 | }
|
---|
52 | pch++;
|
---|
53 | }
|
---|
54 |
|
---|
55 | off += cbRead;
|
---|
56 | }
|
---|
57 | printf("info: checked %d bytes, found %d non-zero bytes\n", off, cNonZero);
|
---|
58 |
|
---|
59 | rc = DosSetFileSize(hFile, cbSize);
|
---|
60 | if (!rc)
|
---|
61 | {
|
---|
62 | printf("info: expanded the file to %d bytes\n", cbSize);
|
---|
63 | LONG lPos = 0;
|
---|
64 | DosSetFilePtr(hFile, 0, FILE_BEGIN, &lPos);
|
---|
65 | off = 0;
|
---|
66 | cNonZero = 0;
|
---|
67 | while (off < cbSize)
|
---|
68 | {
|
---|
69 | char *pch;
|
---|
70 | ULONG cbRead = 0;
|
---|
71 | rc = DosRead(hFile, &achBuf, sizeof(achBuf), &cbRead);
|
---|
72 | if (rc)
|
---|
73 | {
|
---|
74 | printf("DosRead -> %d, off=%d\n", rc);
|
---|
75 | break;
|
---|
76 | }
|
---|
77 |
|
---|
78 | pch = achBuf;
|
---|
79 | while (pch < &achBuf[cbRead])
|
---|
80 | {
|
---|
81 | if (*pch)
|
---|
82 | {
|
---|
83 | printf(".");
|
---|
84 | cNonZero++;
|
---|
85 | }
|
---|
86 | pch++;
|
---|
87 | }
|
---|
88 |
|
---|
89 | off += cbRead;
|
---|
90 | }
|
---|
91 | printf("info: checked all %d bytes, found %d non-zero bytes\n", off, cNonZero);
|
---|
92 |
|
---|
93 | /* fill the file with non-zero bits */
|
---|
94 | memset(achBuf, 0xab, sizeof(achBuf));
|
---|
95 | DosSetFilePtr(hFile, 0, FILE_BEGIN, &lPos);
|
---|
96 | while (off < cbSize)
|
---|
97 | {
|
---|
98 | ULONG cbWritten = 0;
|
---|
99 | rc = DosWrite(hFile, &achBuf, sizeof(achBuf), &cbWritten);
|
---|
100 | if (rc)
|
---|
101 | {
|
---|
102 | printf("DosRead -> %d, off=%d\n", rc);
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | off += cbWritten;
|
---|
106 | }
|
---|
107 | printf("info: filled %d with 0xab\n", off);
|
---|
108 | }
|
---|
109 |
|
---|
110 | DosClose(hFile);
|
---|
111 | DosDelete(pszFilename);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | printf("error: failed to open '%s', rc=%d\n", pszFilename, rc);
|
---|
115 |
|
---|
116 | return 0;
|
---|
117 | }
|
---|