ODList.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODList.c
  20. *
  21. * Description: Implements the od_list_files() function for displaying
  22. * a FILES.BBS file.
  23. *
  24. * Revisions: Date Ver Who Change
  25. * ---------------------------------------------------------------
  26. * Oct 13, 1994 6.00 BP New file header format.
  27. * Oct 21, 1994 6.00 BP Further isolated com routines.
  28. * Dec 09, 1994 6.00 BP Use new directory access functions.
  29. * Dec 31, 1994 6.00 BP Remove #ifndef USEINLINE DOS code.
  30. * Aug 19, 1995 6.00 BP 32-bit portability.
  31. * Nov 11, 1995 6.00 BP Moved functions from odcore.c
  32. * Nov 11, 1995 6.00 BP Removed register keyword.
  33. * Nov 16, 1995 6.00 BP Removed oddoor.h, added odcore.h.
  34. * Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
  35. * Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
  36. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  37. * Mar 03, 1996 6.10 BP Begin version 6.10.
  38. * Aug 10, 2003 6.23 SH *nix support
  39. */
  40. #define BUILDING_OPENDOORS
  41. #include <stdio.h>
  42. #include <ctype.h>
  43. #include <string.h>
  44. #include "OpenDoor.h"
  45. #include "ODCore.h"
  46. #include "ODGen.h"
  47. #include "ODCom.h"
  48. #include "ODPlat.h"
  49. #include "ODKrnl.h"
  50. #include "ODUtil.h"
  51. /* Filename component identifies. */
  52. #define WILDCARDS 0x01
  53. #define EXTENSION 0x02
  54. #define FILENAME 0x04
  55. #define DIRECTORY 0x08
  56. #define DRIVE 0x10
  57. /* Local private helper function prototypes. */
  58. static void ODListFilenameMerge(char *pszEntirePath, const char *pszDrive,
  59. const char *pszDir, const char *pszName, const char *pszExtension);
  60. static char *ODListGetFirstWord(char *pszInStr, char *pszOutStr);
  61. static char *ODListGetRemainingWords(char *pszInStr);
  62. static INT ODListFilenameSplit(const char *pszEntirePath, char *pszDrive,
  63. char *pszDir, char *pszName, char *pszExtension);
  64. /* ----------------------------------------------------------------------------
  65. * od_list_files()
  66. *
  67. * Displays a list of files available for download, using an extended version
  68. * of the standard FILES.BBS format index file.
  69. *
  70. * Parameters: pszFileSpec - Directory name where the FILES.BBS file can be
  71. * found, or full path and filename of a FILES.BBS
  72. * format index file.
  73. *
  74. * Return: TRUE on success or FALSE on failure.
  75. */
  76. ODAPIDEF BOOL ODCALL od_list_files(char *pszFileSpec)
  77. {
  78. BYTE btLineCount = 2;
  79. BOOL bPausing;
  80. static char szLine[513];
  81. static char szFilename[80];
  82. static char szDrive[3];
  83. static char szDir[70];
  84. static char szTemp1[9];
  85. static char szTemp2[5];
  86. static char szBaseName[9];
  87. static char szExtension[5];
  88. static char szDirectory[100];
  89. INT nFilenameInfo;
  90. FILE *pfFilesBBS;
  91. static char *pszCurrent;
  92. BOOL bIsDir;
  93. BOOL bUseNextLine = TRUE;
  94. tODDirHandle hDir;
  95. tODDirEntry DirEntry;
  96. /* Log function entry if running in trace mode. */
  97. TRACE(TRACE_API, "od_list_files()");
  98. /* Initialize OpenDoors if it hasn't already been done. */
  99. if(!bODInitialized) od_init();
  100. OD_API_ENTRY();
  101. /* Check user's page pausing setting. */
  102. bPausing = od_control.od_page_pausing;
  103. if(od_control.od_extended_info) bPausing = od_control.user_attribute & 0x04;
  104. /* Parse directory parameter. */
  105. if(pszFileSpec == NULL)
  106. {
  107. strcpy(szODWorkString, ".");
  108. strcpy(szDirectory, "."DIRSEP_STR);
  109. }
  110. else if(*pszFileSpec == '\0')
  111. {
  112. strcpy(szODWorkString, ".");
  113. strcpy(szDirectory, "."DIRSEP_STR);
  114. }
  115. else
  116. {
  117. strcpy(szODWorkString, pszFileSpec);
  118. strcpy(szDirectory, pszFileSpec);
  119. if(szODWorkString[strlen(szODWorkString) - 1] == DIRSEP)
  120. {
  121. szODWorkString[strlen(szODWorkString) - 1] = '\0';
  122. }
  123. }
  124. /* Get directory information on path. */
  125. if(ODDirOpen(szODWorkString, DIR_ATTRIB_ARCH | DIR_ATTRIB_RDONLY
  126. | DIR_ATTRIB_DIREC, &hDir) != kODRCSuccess)
  127. {
  128. od_control.od_error = ERR_FILEOPEN;
  129. OD_API_EXIT();
  130. return(FALSE);
  131. }
  132. if(ODDirRead(hDir, &DirEntry) != kODRCSuccess)
  133. {
  134. ODDirClose(hDir);
  135. od_control.od_error = ERR_FILEOPEN;
  136. OD_API_EXIT();
  137. return(FALSE);
  138. }
  139. ODDirClose(hDir);
  140. /* If it is a directory. */
  141. if(DirEntry.wAttributes & DIR_ATTRIB_DIREC)
  142. {
  143. /* Append FILES.BBS to directory name & open. */
  144. bIsDir = TRUE;
  145. ODMakeFilename(szODWorkString, szODWorkString, "FILES.BBS",
  146. sizeof(szODWorkString));
  147. if((pfFilesBBS = fopen(szODWorkString, "r")) == NULL)
  148. {
  149. od_control.od_error = ERR_FILEOPEN;
  150. OD_API_EXIT();
  151. return(FALSE);
  152. }
  153. }
  154. /* If it is not a directory. */
  155. else
  156. {
  157. bIsDir = FALSE;
  158. if((pfFilesBBS = fopen(szODWorkString,"r")) == NULL)
  159. {
  160. od_control.od_error = ERR_FILEOPEN;
  161. OD_API_EXIT();
  162. return(FALSE);
  163. }
  164. }
  165. /* Ignore previously pressed control keys. */
  166. chLastControlKey = 0;
  167. /* Loop until the end of the FILES.BBS file has been reached. */
  168. for(;;)
  169. {
  170. if(fgets(szLine, 512, pfFilesBBS) == NULL) break;
  171. if(!bUseNextLine)
  172. {
  173. if(szLine[strlen(szLine) - 1] == '\n')
  174. {
  175. bUseNextLine = TRUE;
  176. }
  177. continue;
  178. }
  179. if(szLine[strlen(szLine) - 1] == '\n')
  180. {
  181. szLine[strlen(szLine) - 1] = '\0';
  182. }
  183. else
  184. {
  185. bUseNextLine = FALSE;
  186. }
  187. if(szLine[strlen(szLine) - 1] == '\r')
  188. {
  189. szLine[strlen(szLine) - 1] = '\0';
  190. }
  191. if(chLastControlKey != 0)
  192. {
  193. switch(chLastControlKey)
  194. {
  195. case 's':
  196. if(od_control.od_list_stop)
  197. {
  198. if(od_control.baud)
  199. {
  200. ODComClearOutbound(hSerialPort);
  201. }
  202. od_clear_keybuffer();
  203. fclose(pfFilesBBS);
  204. OD_API_EXIT();
  205. return(TRUE);
  206. }
  207. break;
  208. case 'p':
  209. if(od_control.od_list_pause)
  210. {
  211. od_clear_keybuffer();
  212. od_get_key(TRUE);
  213. }
  214. }
  215. chLastControlKey = 0;
  216. }
  217. /* Determine whether or not this is a comment line. */
  218. if(szLine[0] == ' ' || strlen(szLine) == 0)
  219. {
  220. /* If so, display the line in comment color. */
  221. od_set_attrib(od_control.od_list_title_col);
  222. od_disp_str(szLine);
  223. od_disp_str("\n\r");
  224. ++btLineCount;
  225. }
  226. /* If the line is not a comment. */
  227. else
  228. {
  229. /* Extract the first word of the line, */
  230. ODListGetFirstWord(szLine, szFilename);
  231. /* And extract the filename. */
  232. nFilenameInfo = ODListFilenameSplit(szFilename, szDrive, szDir,
  233. szBaseName, szExtension);
  234. if(!((nFilenameInfo & DRIVE) || (nFilenameInfo & DIRECTORY)))
  235. {
  236. if(bIsDir)
  237. {
  238. ODMakeFilename(szDirectory, szDirectory, szFilename,
  239. sizeof(szDirectory));
  240. strcpy(szFilename, szDirectory);
  241. }
  242. else
  243. {
  244. ODListFilenameSplit(szDirectory, szDrive, szDir, szTemp1,
  245. szTemp2);
  246. ODListFilenameMerge(szFilename, szDrive, szDir, szBaseName,
  247. szExtension);
  248. }
  249. }
  250. /* Search for the filespec in directory. */
  251. if(ODDirOpen(szFilename, DIR_ATTRIB_ARCH | DIR_ATTRIB_RDONLY, &hDir)
  252. == kODRCSuccess)
  253. {
  254. /* Display information on every file that matches. */
  255. while(ODDirRead(hDir, &DirEntry) == kODRCSuccess)
  256. {
  257. od_set_attrib(od_control.od_list_name_col);
  258. od_printf("%-12.12s ", DirEntry.szFileName);
  259. od_set_attrib(od_control.od_list_size_col);
  260. od_printf("%-6ld ", DirEntry.dwFileSize);
  261. od_set_attrib(od_control.od_list_comment_col);
  262. pszCurrent = ODListGetRemainingWords(szLine);
  263. if(strlen(pszCurrent) <= 56)
  264. {
  265. od_disp_str(pszCurrent);
  266. od_disp_str("\n\r");
  267. }
  268. else
  269. {
  270. od_printf("%-56.56s\n\r", pszCurrent);
  271. }
  272. ++btLineCount;
  273. }
  274. ODDirClose(hDir);
  275. }
  276. /* Otherwise, indicate that the file is "Offline". */
  277. else
  278. {
  279. ODListFilenameMerge(szFilename, "", "", szBaseName, szExtension);
  280. od_set_attrib(od_control.od_list_name_col);
  281. od_printf("%-12.12s ", szFilename);
  282. od_set_attrib(od_control.od_list_offline_col);
  283. od_disp_str(od_control.od_offline);
  284. od_set_attrib(od_control.od_list_comment_col);
  285. od_printf("%-56.56s\n\r", ODListGetRemainingWords(szLine));
  286. ++btLineCount;
  287. }
  288. }
  289. /* Check for end of screen & page pausing. */
  290. if(btLineCount >= od_control.user_screen_length && bPausing)
  291. {
  292. /* Provide page pausing at end of each screen. */
  293. if(ODPagePrompt(&bPausing))
  294. {
  295. fclose(pfFilesBBS);
  296. OD_API_EXIT();
  297. return(TRUE);
  298. }
  299. /* Reset the line number counter. */
  300. btLineCount = 2;
  301. }
  302. }
  303. /* When finished, close the file. */
  304. fclose(pfFilesBBS);
  305. /* Return with success. */
  306. OD_API_EXIT();
  307. return(TRUE);
  308. }
  309. /* ----------------------------------------------------------------------------
  310. * ODListFilenameMerge() *** PRIVATE FUNCTION ***
  311. *
  312. * Builds a fully-qualified path name from the provided path component
  313. * strings.
  314. *
  315. * Parameters: pszEntirePath - Pointer to the destination string where the
  316. * generated path should be stored.
  317. *
  318. * pszDrive - Pointer to the drive string.
  319. *
  320. * pszDir - Pointer to the directory string.
  321. *
  322. * pszName - Pointer to the base filename string.
  323. *
  324. * pszExtension - Pointer to the extension name string.
  325. *
  326. * Return: void
  327. */
  328. static void ODListFilenameMerge(char *pszEntirePath, const char *pszDrive,
  329. const char *pszDir, const char *pszName, const char *pszExtension)
  330. {
  331. if(pszEntirePath == NULL) return;
  332. pszEntirePath[0] = '\0';
  333. if(pszDrive != NULL)
  334. {
  335. strcpy(pszEntirePath, pszDrive);
  336. }
  337. if(pszDir != NULL)
  338. {
  339. strcat(pszEntirePath, pszDir);
  340. }
  341. if(pszName != NULL)
  342. {
  343. strcat(pszEntirePath,pszName);
  344. }
  345. if(pszExtension != NULL)
  346. {
  347. strcat(pszEntirePath,pszExtension);
  348. }
  349. }
  350. /* ----------------------------------------------------------------------------
  351. * ODListFilenameSplit() *** PRIVATE FUNCTION ***
  352. *
  353. * Splits the provided path string into drive, directory name, file base name
  354. * and file extension components.
  355. *
  356. * Parameters: pszEntirePath - A string containing the path to split.
  357. *
  358. * pszDrive - A string where the drive letter should be stored.
  359. *
  360. * pszDir - A string where the directory name should be
  361. * stored.
  362. *
  363. * pszName - A string where the base filename should be
  364. * stored.
  365. *
  366. * pszExtension - A string where the filename extension should be
  367. * stored.
  368. *
  369. * Return: One or more flags indicating which components where found in the
  370. * provided path name.
  371. */
  372. static INT ODListFilenameSplit(const char *pszEntirePath, char *pszDrive,
  373. char *pszDir, char *pszName, char *pszExtension)
  374. {
  375. char *pchCurrentPos;
  376. char *pchStart;
  377. BYTE btSize;
  378. INT nToReturn;
  379. ASSERT(pszEntirePath != NULL);
  380. ASSERT(pszDrive != NULL);
  381. ASSERT(pszDir != NULL);
  382. ASSERT(pszName != NULL);
  383. ASSERT(pszExtension != NULL);
  384. pchStart = (char *)pszEntirePath;
  385. nToReturn = 0;
  386. if((pchCurrentPos = strrchr(pchStart,':')) == NULL)
  387. {
  388. pszDrive[0] = '\0';
  389. }
  390. else
  391. {
  392. btSize = (int)(pchCurrentPos - pchStart) + 1;
  393. if(btSize > 2) btSize = 2;
  394. strncpy(pszDrive, pchStart, btSize);
  395. pszDrive[btSize] = '\0';
  396. pchStart = pchCurrentPos + 1;
  397. nToReturn |= DRIVE;
  398. }
  399. if((pchCurrentPos = strrchr(pchStart, DIRSEP))==NULL)
  400. {
  401. pszDir[0] = '\0';
  402. }
  403. else
  404. {
  405. btSize = (int)(pchCurrentPos - pchStart) + 1;
  406. strncpy(pszDir,pchStart,btSize);
  407. pszDir[btSize] = '\0';
  408. pchStart = pchCurrentPos + 1;
  409. nToReturn |= DIRECTORY;
  410. }
  411. if(strchr(pchStart,'*') != NULL || strchr(pchStart, '?') != NULL)
  412. {
  413. nToReturn |= WILDCARDS;
  414. }
  415. if((pchCurrentPos = strrchr(pchStart, '.')) == NULL)
  416. {
  417. if(pchStart =='\0')
  418. {
  419. pszExtension[0] = '\0';
  420. pszName[0] = '\0';
  421. }
  422. else
  423. {
  424. pszExtension[0] = '\0';
  425. btSize = strlen(pchStart);
  426. if (btSize > 8) btSize = 0;
  427. strncpy(pszName, pchStart, btSize);
  428. pszName[btSize] = '\0';
  429. nToReturn |= FILENAME;
  430. }
  431. }
  432. else
  433. {
  434. nToReturn |= FILENAME;
  435. nToReturn |= EXTENSION;
  436. btSize = (int)(pchCurrentPos - pchStart);
  437. if(btSize > 8) btSize = 8;
  438. strncpy(pszName, pchStart, btSize);
  439. pszName[btSize] = '\0';
  440. btSize = strlen(pchCurrentPos);
  441. if(btSize > 4) btSize = 4;
  442. strncpy(pszExtension, pchCurrentPos, btSize);
  443. pszExtension[btSize]='\0';
  444. }
  445. return(nToReturn);
  446. }
  447. /* ----------------------------------------------------------------------------
  448. * ODListGetFirstWord() *** PRIVATE FUNCTION ***
  449. *
  450. * Returns the first word in a string containing a series of words separated by
  451. * one or more spaced.
  452. *
  453. * Parameters: pszInStr - String to look in.
  454. *
  455. * pszOutStr - Buffer to store result in. This buffer should be at
  456. * least as long as the pszInStr string.
  457. *
  458. * Return: Pointer to the pszOutStr that was passed in.
  459. */
  460. static char *ODListGetFirstWord(char *pszInStr, char *pszOutStr)
  461. {
  462. char *pchOut = (char *)pszOutStr;
  463. ASSERT(pszInStr != NULL);
  464. ASSERT(pszOutStr != NULL);
  465. while(*pszInStr && *pszInStr != ' ')
  466. {
  467. *pchOut++ = *pszInStr++;
  468. }
  469. *pchOut = '\0';
  470. return(pszOutStr);
  471. }
  472. /* ----------------------------------------------------------------------------
  473. * ODListGetRemainingWords() *** PRIVATE FUNCTION ***
  474. *
  475. * Obtains the remaining words in a string, after the first word. This function
  476. * is a companion to ODListGetFirstWord(), which obtains just the first word
  477. * in a string of many words.
  478. *
  479. * Parameters: pszInStr - String to look at.
  480. *
  481. * Return: A pointer to the position in a string of the second word.
  482. */
  483. static char *ODListGetRemainingWords(char *pszInStr)
  484. {
  485. char *pchStartOfRemaining = (char *)pszInStr;
  486. /* Skip over the first word in the string. */
  487. while(*pchStartOfRemaining && *pchStartOfRemaining != ' ')
  488. {
  489. ++pchStartOfRemaining;
  490. }
  491. /* Skip over any spaces after the first word. */
  492. while(*pchStartOfRemaining && *pchStartOfRemaining == ' ')
  493. {
  494. ++pchStartOfRemaining;
  495. }
  496. /* Return pointer to the rest of the string. */
  497. return((char *)pchStartOfRemaining);
  498. }