Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
8.1 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. /* fileres.c -- functions from file.c that are usually resident */
  5. #define NOCLIPBOARD
  6. #define NOGDICAPMASKS
  7. #define NOCTLMGR
  8. #define NOVIRTUALKEYCODES
  9. #define NOWINMESSAGES
  10. #define NOWINSTYLES
  11. #define NOSYSMETRICS
  12. #define NOMENUS
  13. #define NOICON
  14. #define NOKEYSTATE
  15. #define NORASTEROPS
  16. #define NOSHOWWINDOW
  17. #define NOSYSCOMMANDS
  18. #define NOATOM
  19. #define NOCOLOR
  20. #define NOCREATESTRUCT
  21. #define NOCTLMGR
  22. #define NODRAWTEXT
  23. #define NOMETAFILE
  24. #define NOMSG
  25. #define NOHDC
  26. #define NOGDI
  27. #define NOMB
  28. #define NOFONT
  29. #define NOPEN
  30. #define NOBRUSH
  31. #define NOWNDCLASS
  32. #define NOSOUND
  33. #define NOCOMM
  34. #define NOPOINT
  35. #define NORECT
  36. #define NOREGION
  37. #define NOSCROLL
  38. #define NOTEXTMETRIC
  39. #define NOWH
  40. #define NOWINOFFSETS
  41. #include <windows.h>
  42. #include "mw.h"
  43. #include "doslib.h"
  44. #define NOUAC
  45. #include "cmddefs.h"
  46. #include "docdefs.h"
  47. #include "filedefs.h"
  48. #include "str.h"
  49. #include "debug.h"
  50. extern int vfDiskFull;
  51. extern int vfSysFull;
  52. extern int vfnWriting;
  53. extern CHAR (*rgbp)[cbSector];
  54. extern typeTS tsMruRfn;
  55. extern struct BPS *mpibpbps;
  56. extern int ibpMax;
  57. extern struct FCB (**hpfnfcb)[];
  58. extern typeTS tsMruBps;
  59. extern struct ERFN dnrfn[rfnMax];
  60. extern int iibpHashMax;
  61. extern CHAR *rgibpHash;
  62. extern int rfnMac;
  63. extern int ferror;
  64. extern CHAR szWriteDocPrompt[];
  65. extern CHAR szScratchFilePrompt[];
  66. extern CHAR szSaveFilePrompt[];
  67. #define IibpHash(fn,pn) ((int) ((fn + 1) * (pn + 1)) & 077777) % iibpHashMax
  68. #ifdef DEBUG
  69. #define STATIC
  70. #else
  71. #define STATIC static
  72. #define ErrorWithMsg( idpmt, szModule ) Error( idpmt )
  73. #define DiskErrorWithMsg( idpmt, szModule ) DiskError( idpmt )
  74. #endif
  75. #define osfnNil (-1)
  76. CHAR *PchFromFc(fn, fc, pcch)
  77. int fn;
  78. typeFC fc;
  79. int *pcch;
  80. { /*
  81. Description: Reads from a file, starting at virtual character
  82. position fc. Reads until end of buffer page.
  83. Returns: Pointer to char buffer starting at fc.
  84. The number of characters read is returned in *pcch.
  85. */
  86. int dfc;
  87. CHAR *pch;
  88. typePN pn;
  89. int ibp;
  90. struct BPS *pbps;
  91. dfc = (int) (fc % cfcPage);
  92. pn = (typePN) (fc / cfcPage);
  93. ibp = IbpEnsureValid(fn, pn);
  94. pbps = &mpibpbps[ibp];
  95. *pcch = pbps->cch - dfc;
  96. return &rgbp[ibp][dfc];
  97. }
  98. /* end of P c h F r o m F c */
  99. /*** PchGetPn - Assure file page loaded, return pointer
  100. *
  101. */
  102. CHAR *PchGetPn(fn, pn, pcch, fWrite)
  103. int fn;
  104. typePN pn;
  105. int *pcch;
  106. BOOL fWrite; // missing before?? (2.11.91) D. Kent
  107. { /*
  108. Description: Get char pointer to page buffer, option to mark
  109. page as dirty.
  110. Returns: Pointer to buffer.
  111. cch in *pcch
  112. */
  113. int ibp = IbpEnsureValid(fn, pn);
  114. struct BPS *pbps = &mpibpbps[ibp];
  115. *pcch = pbps->cch;
  116. pbps->fDirty |= fWrite;
  117. return rgbp[ibp];
  118. } /* end of P c h G e t P n */
  119. int IbpEnsureValid(fn, pn)
  120. int fn;
  121. typePN pn;
  122. { /*
  123. Description: Get page pn of file fn into memory.
  124. If already in memory, return.
  125. Returns: Bp index (buffer slot #) where the page resides
  126. in memory.
  127. */
  128. int ibp;
  129. register struct BPS *pbps;
  130. #ifdef DEBUG
  131. CheckIbp();
  132. #endif /* DEBUG */
  133. /* Is the page currently in memory? */
  134. ibp = rgibpHash[IibpHash(fn,pn)];
  135. /* ibp is the first in a linked list of possible matches */
  136. /* resident in memory */
  137. Scribble(3,'V');
  138. while (ibp != ibpNil) /* while not end of linked list */
  139. { /* check if any buffers in memory match */
  140. pbps = &mpibpbps[ibp];
  141. if (pbps->fn == fn && pbps->pn == pn)
  142. { /* Found it */
  143. pbps->ts = ++tsMruBps; /* mark page as MRUsed */
  144. Scribble(3,' ');
  145. return ibp;
  146. }
  147. ibp = pbps->ibpHashNext;
  148. }
  149. /* page is not currently in memory */
  150. return IbpMakeValid( fn, pn );
  151. } /* end of I b p E n s u r e V a l i d */
  152. CloseEveryRfn( fHardToo )
  153. { /* Close all files we have open. Close only files on removable media
  154. if fHardToo is FALSE; ALL files if fHardToo is TRUE */
  155. int rfn;
  156. for (rfn = 0; rfn < rfnMac; rfn++)
  157. {
  158. int fn = dnrfn [rfn].fn;
  159. if (fn != fnNil)
  160. if ( fHardToo ||
  161. !((POFSTRUCT)((**hpfnfcb)[fn].rgbOpenFileBuf))->fFixedDisk )
  162. {
  163. CloseRfn( rfn );
  164. }
  165. }
  166. }
  167. typeFC FcWScratch(pch, cch)
  168. CHAR *pch;
  169. int cch;
  170. { /*
  171. Description: Write chars at end of scratch file.
  172. Returns: first fc written.
  173. */
  174. typeFC fc = (**hpfnfcb)[fnScratch].fcMac;
  175. #if 0
  176. extern BOOL bNo64KLimit;
  177. if ((!bNo64KLimit) && (((long) fc) + ((long) cch) > 65536L)) /* scratch file to big */
  178. {
  179. DiskErrorWithMsg(IDPMTSFER, " FcWScratch"); /* session too long */
  180. vfSysFull = fTrue;
  181. /* recovery is accomplished: all that happens is that a few
  182. characters do not get written to the scratch file - the
  183. user loses only a little bit of his work. */
  184. }
  185. else
  186. #endif
  187. WriteRgch(fnScratch, pch, cch);
  188. return fc;
  189. }
  190. WriteRgch(fn, pch, cch)
  191. int fn;
  192. CHAR *pch;
  193. int cch;
  194. { /*
  195. Description: Writes char string pch, length cch, to end of
  196. file fn.
  197. Returns: nothing
  198. */
  199. extern vfDiskError;
  200. struct FCB *pfcb = &(**hpfnfcb)[fn];
  201. typePN pn = (typePN) (pfcb->fcMac / cfcPage);
  202. #ifdef WIN30
  203. /* Error checking was horrendous in these early days, right?
  204. Ha. It still is. In any case, don't know WHAT we can do
  205. if the page number has gotten too large, so just fake a
  206. disk error so that IbpEnsureValid() doesn't go off into
  207. never-never land! This catch effectively limits us to
  208. 4M files ..pault 11/1/89 */
  209. if (pn > pgnMax)
  210. #ifdef DEBUG
  211. DiskErrorWithMsg(IDPMTSDE2, "writergch");
  212. #else
  213. DiskError(IDPMTSDE2);
  214. #endif
  215. else
  216. #endif
  217. while (cch > 0)
  218. { /* One page at a time */
  219. int ibp = IbpEnsureValid(fn, pn++);
  220. struct BPS *pbps = &mpibpbps[ibp];
  221. int cchBp = pbps->cch;
  222. int cchBlt = min((int)cfcPage - cchBp, cch);
  223. Assert( vfDiskError ||
  224. cchBp == pfcb->fcMac - (pn - 1) * cfcPage);
  225. bltbyte(pch, &rgbp[ibp][cchBp], cchBlt);
  226. pbps->cch += cchBlt;
  227. pbps->fDirty = true;
  228. pfcb->fcMac += cchBlt;
  229. pfcb->pnMac = pn;
  230. pch += cchBlt;
  231. cch -= cchBlt;
  232. }
  233. } /* end of W r i t e R g c h */
  234. CloseRfn( rfn )
  235. int rfn;
  236. {/*
  237. Description: Close a file and delete its Rfn entry
  238. Returns: nothing
  239. */
  240. struct ERFN *perfn = &dnrfn[rfn];
  241. int fn = perfn->fn;
  242. Assert (rfn >= 0 &&
  243. rfn < rfnMac &&
  244. perfn->osfn != osfnNil &&
  245. fn != fnNil);
  246. #ifdef DEBUG
  247. #ifdef DFILE
  248. CommSzSz( "Closing file: ", &(**(**hpfnfcb)[fn].hszFile)[0] );
  249. #endif
  250. #endif
  251. /* Close may fail if windows already closed the file for us,
  252. but that's OK */
  253. FCloseDoshnd( perfn->osfn );
  254. { /* Just like the statement below, but 28 bytes less
  255. under CMERGE V13 */
  256. REG1 struct FCB *pfcb = &(**hpfnfcb) [fn];
  257. pfcb->rfn = rfnNil;
  258. }
  259. /* (**hpfnfcb)[fn].rfn = rfnNil; */
  260. perfn->fn = fnNil;
  261. }
  262.