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.

363 lines
10 KiB

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #include <memory.h>
  5. #include <stdlib.h>
  6. #include <windows.h>
  7. #include "list.h"
  8. static char iniFlag = 0; /* If ini found, but not list change to 1 */
  9. /* Will print a warning upon exit */
  10. char szScrollBarUp[2];
  11. char szScrollBarDown[2];
  12. char szScrollBarOff[2];
  13. char szScrollBarOn[2];
  14. void
  15. init_list ()
  16. {
  17. LPVOID lpParameter = NULL;
  18. DWORD dwThreadId;
  19. switch (GetConsoleCP()) {
  20. case 932:
  21. szScrollBarUp[0] = '\x1c';
  22. szScrollBarDown[0] = '\x07';
  23. szScrollBarOff[0] = '\x1a';
  24. szScrollBarOn[0] = '\x14';
  25. break;
  26. default:
  27. szScrollBarUp[0] = '\x18';
  28. szScrollBarDown[0] = '\x19';
  29. szScrollBarOff[0] = '\xB1';
  30. szScrollBarOn[0] = '\xDB';
  31. break;
  32. }
  33. /*
  34. * Init Misc
  35. */
  36. ResetEvent (vSemSync);
  37. ResetEvent (vSemMoreData);
  38. /*
  39. * Init screen parameters
  40. */
  41. GetConsoleScreenBufferInfo( vStdOut,
  42. &vConsoleOrigScrBufferInfo );
  43. vConsoleOrigScrBufferInfo.dwSize.X=
  44. vConsoleOrigScrBufferInfo.srWindow.Right-
  45. vConsoleOrigScrBufferInfo.srWindow.Left + 1;
  46. vConsoleOrigScrBufferInfo.dwSize.Y=
  47. vConsoleOrigScrBufferInfo.srWindow.Bottom-
  48. vConsoleOrigScrBufferInfo.srWindow.Top + 1;
  49. vConsoleOrigScrBufferInfo.dwMaximumWindowSize=
  50. vConsoleOrigScrBufferInfo.dwSize;
  51. set_mode( 0, 0, 0 );
  52. /*
  53. * Start reading the first file. Displaying can't start until
  54. * the ini file (if one is found) is processed.
  55. */
  56. vReaderFlag = F_NEXT;
  57. /*
  58. * Init priority setting for display & reader thread.
  59. *
  60. * THREAD_PRIORITY_NORMAL = reader thread normal pri.
  61. * THREAD_PRIORITY_ABOVE_NORMAL = display thread pri
  62. * THREAD_PRIORITY_HIGHEST = reader thread in boosted pri.
  63. */
  64. vReadPriNormal = THREAD_PRIORITY_NORMAL;
  65. SetThreadPriority( GetCurrentThread(),
  66. THREAD_PRIORITY_ABOVE_NORMAL );
  67. vReadPriBoost = THREAD_PRIORITY_NORMAL;
  68. /*
  69. * Start reader thread
  70. */
  71. CreateThread( NULL,
  72. STACKSIZE,
  73. (LPTHREAD_START_ROUTINE) ReaderThread,
  74. NULL, // lpParameter,
  75. 0, // THREAD_ALL_ACCESS,
  76. &dwThreadId );
  77. /*
  78. * Read INI information.
  79. */
  80. vSetWidth = vWidth; /* Set defaults */
  81. vSetLines = vLines + 2;
  82. FindIni ();
  83. if (vSetBlks < vMaxBlks)
  84. vSetBlks = DEFBLKS;
  85. vSetThres = (long) (vSetBlks/2-2) * BLOCKSIZE;
  86. /*
  87. * Must wait for reader thread to at least read in the
  88. * first block. Also, if the file was not found and only
  89. * one file was specifed the reader thread will display
  90. * an error and exit... if we don't wait we could have
  91. * changed the screen before this was possible.
  92. */
  93. WaitForSingleObject(vSemMoreData, WAITFOREVER);
  94. ResetEvent(vSemMoreData);
  95. /*
  96. * Now that ini file has been read. Set parameters.
  97. * Pause reader thread while adjusting buffer size.
  98. */
  99. SyncReader ();
  100. vMaxBlks = vSetBlks;
  101. vThreshold = vSetThres;
  102. vReaderFlag = F_CHECK;
  103. SetEvent (vSemReader);
  104. /*
  105. * Now set to user's default video mode
  106. */
  107. set_mode (vSetLines, vSetWidth, 0);
  108. SetConsoleActiveScreenBuffer( vhConsoleOutput );
  109. }
  110. /***
  111. * Warning: Reader thread must not be running when this routine
  112. * is called.
  113. */
  114. void
  115. AddFileToList (
  116. char *fname
  117. )
  118. {
  119. unsigned rbLen;
  120. HANDLE hDir;
  121. struct {
  122. WIN32_FIND_DATA rb;
  123. char overflow[256]; /* HACK! OS/2 1.2? longer */
  124. } x;
  125. struct Flist *pOrig, *pSort;
  126. char *pTmp, *fpTmp;
  127. char s[_MAX_PATH]; /* Max filename length */
  128. BOOL fNextFile;
  129. rbLen = sizeof (x); /* rb+tmp. For large fnames */
  130. pOrig = NULL;
  131. if (strpbrk (fname, "*?")) { /* Wildcard in filename? */
  132. /* Yes, explode it */
  133. hDir = FindFirstFile (fname, &x.rb);
  134. fNextFile = ( hDir == INVALID_HANDLE_VALUE )? FALSE : TRUE;
  135. pTmp = strrchr (fname, '\\');
  136. if (pTmp == NULL) pTmp = strrchr (fname, ':');
  137. if (pTmp) pTmp[1] = 0;
  138. while (fNextFile) {
  139. if( ( x.rb.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 ) {
  140. //
  141. // The file found is not a directory
  142. //
  143. if (pTmp) {
  144. strcpy (s, fname); /* Was there a releative path? */
  145. strncat (s, x.rb.cFileName, sizeof(s) - strlen(s)); /* Yes, it's needed for Open */
  146. AddOneName (s);
  147. } else {
  148. AddOneName (x.rb.cFileName);
  149. }
  150. }
  151. fNextFile = FindNextFile (hDir, &x.rb);
  152. if (pOrig == NULL)
  153. pOrig = vpFlCur;
  154. }
  155. }
  156. if (!pOrig) /* Did not explode, then add */
  157. AddOneName (fname); /* original name to list */
  158. else { /* Yes, then sort the new fnames*/
  159. while (pOrig != vpFlCur) {
  160. pSort = pOrig->next;
  161. for (; ;) {
  162. if (strcmp (pOrig->fname, pSort->fname) > 0) {
  163. /*
  164. * Can simply switch names at this time, since no
  165. * other information has been stored into the new
  166. * file structs
  167. */
  168. fpTmp = pOrig->fname;
  169. pOrig->fname = pSort->fname;
  170. pSort->fname = fpTmp;
  171. fpTmp = pOrig->rootname;
  172. pOrig->rootname = pSort->rootname;
  173. pSort->rootname = fpTmp;
  174. }
  175. if (pSort == vpFlCur)
  176. break;
  177. pSort = pSort->next;
  178. }
  179. pOrig = pOrig->next;
  180. }
  181. }
  182. }
  183. void
  184. AddOneName (
  185. char *fname
  186. )
  187. {
  188. struct Flist *npt;
  189. char *pt;
  190. char s[30];
  191. int i;
  192. npt = (struct Flist *) malloc (sizeof (struct Flist));
  193. if (!npt) {
  194. printf("Out of memory\n");
  195. exit(1);
  196. }
  197. npt->fname = _strdup (fname);
  198. pt = strrchr (fname, '\\');
  199. pt = pt == NULL ? fname : pt+1;
  200. i = strlen (pt);
  201. if (i > 20) {
  202. memcpy (s, pt, 17);
  203. strcpy (s+17, "...");
  204. npt->rootname = _strdup (s);
  205. } else
  206. npt->rootname = _strdup (pt);
  207. npt->FileTime.dwLowDateTime = (unsigned)-1; /* Cause info to be invalid */
  208. npt->FileTime.dwHighDateTime = (unsigned)-1; /* Cause info to be invalid */
  209. npt->HighTop = -1;
  210. npt->SlimeTOF = 0L;
  211. npt->Wrap = 0;
  212. npt->prev = vpFlCur;
  213. npt->next = NULL;
  214. memset (npt->prgLineTable, 0, sizeof (long *) * MAXTPAGE);
  215. if (vpFlCur) {
  216. if (vpFlCur->next) {
  217. npt->next = vpFlCur->next;
  218. vpFlCur->next->prev = npt;
  219. }
  220. vpFlCur->next = npt;
  221. }
  222. vpFlCur = npt;
  223. }
  224. void
  225. FindIni ()
  226. {
  227. static char Delim[] = " :=;\t\r\n";
  228. FILE *fp;
  229. char *env, *verb, *value;
  230. char s [200];
  231. long l;
  232. env = getenv ("INIT");
  233. if (env == NULL)
  234. return;
  235. if ((strlen(env) + sizeof ("\\TOOLS.INI") + 1) > 200)
  236. return;
  237. strcpy (s, env);
  238. strcat (s, "\\TOOLS.INI");
  239. fp = fopen (s, "r");
  240. if (fp == NULL)
  241. return;
  242. iniFlag = 1;
  243. while (fgets (s, 200, fp) != NULL) {
  244. if ((s[0] != '[')||(s[5] != ']'))
  245. continue;
  246. _strupr (s);
  247. if (strstr (s, "LIST") == NULL)
  248. continue;
  249. /*
  250. * ini file found w/ "list" keyword. Now read it.
  251. */
  252. iniFlag = 0;
  253. while (fgets (s, 200, fp) != NULL) {
  254. if (s[0] == '[')
  255. break;
  256. if (s[0] == ';')
  257. continue;
  258. verb = strtok (s, Delim);
  259. value = strtok (NULL, Delim);
  260. if (verb == NULL)
  261. continue;
  262. if (value == NULL)
  263. value = "";
  264. _strupr (verb);
  265. if (strcmp (verb, "TAB") == 0) vDisTab = (Uchar)atoi(value);
  266. else if (strcmp (verb, "WIDTH") == 0) vSetWidth = atoi(value);
  267. else if (strcmp (verb, "HEIGHT") == 0) vSetLines = atoi(value);
  268. else if (strcmp (verb, "LCOLOR") == 0) vAttrList = (WORD)xtoi(value);
  269. else if (strcmp (verb, "TCOLOR") == 0) vAttrTitle= (WORD)xtoi(value);
  270. else if (strcmp (verb, "CCOLOR") == 0) vAttrCmd = (WORD)xtoi(value);
  271. else if (strcmp (verb, "HCOLOR") == 0) vAttrHigh = (WORD)xtoi(value);
  272. else if (strcmp (verb, "KCOLOR") == 0) vAttrKey = (WORD)xtoi(value);
  273. else if (strcmp (verb, "BCOLOR") == 0) vAttrBar = (WORD)xtoi(value);
  274. else if (strcmp (verb, "BUFFERS") == 0) {
  275. l = atoi (value) * 1024L / ((long) BLOCKSIZE);
  276. vSetBlks = (int)l;
  277. }
  278. else if (strcmp (verb, "HACK") == 0) vIniFlag |= I_SLIME;
  279. else if (strcmp (verb, "NOBEEP") == 0) vIniFlag |= I_NOBEEP;
  280. }
  281. break;
  282. }
  283. fclose (fp);
  284. }
  285. /*** xtoi - Hex to int
  286. *
  287. * Entry:
  288. * pt - pointer to hex number
  289. *
  290. * Return:
  291. * value of hex number
  292. *
  293. */
  294. unsigned
  295. xtoi (
  296. char *pt
  297. )
  298. {
  299. unsigned u;
  300. char c;
  301. u = 0;
  302. while (c = *(pt++)) {
  303. if (c >= 'a' && c <= 'f')
  304. c -= 'a' - 'A';
  305. if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))
  306. u = u << 4 | c - (c >= 'A' ? 'A'-10 : '0');
  307. }
  308. return (u);
  309. }
  310. void
  311. CleanUp (
  312. void
  313. )
  314. {
  315. SetConsoleActiveScreenBuffer( vStdOut );
  316. }