TIP2.C 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* tip2.c - Example program to demonstrate how to send or receive files */
  2. /* using DSZ, from within an OpenDoors program. */
  3. /* Include required header files. */
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include "opendoor.h"
  7. /* File transfer protocol enumeration. */
  8. typedef enum
  9. {
  10. XModem,
  11. XModemCRC,
  12. XModem1K,
  13. YModem,
  14. YModemG,
  15. ZModem
  16. } eProtocol;
  17. /* Function prototypes. */
  18. int TransferFile(char *pszFilename, eProtocol Protocol, char bReceive);
  19. eProtocol ChooseProtocol(void);
  20. void AddParameter(char **papszArguments, int *pnCount, char *pszNewArgument);
  21. /* Manifest constants. */
  22. #define ARGS_ARRAY_SIZE 10
  23. /* Global variable with DSZ filename. */
  24. char szDSZFilename[80] = "DSZ";
  25. /* Program's execution begins here. */
  26. main()
  27. {
  28. char chAnswer;
  29. char bReceive;
  30. eProtocol Protocol;
  31. char szFilename[73];
  32. int bSuccess;
  33. od_printf("OpenDoors file transfer demo.\n\r\n\r");
  34. /* Get file transfer direction from user. */
  35. od_printf("Do you wish to [U]pload or [D]ownload? ");
  36. chAnswer = od_get_answer("UD");
  37. if(chAnswer == 'U')
  38. {
  39. od_printf("Upload\n\r\n\r");
  40. bReceive = TRUE;
  41. }
  42. else
  43. {
  44. od_printf("Download\n\r\n\r");
  45. bReceive = FALSE;
  46. }
  47. /* Get file transfer protocol from user. */
  48. Protocol = ChooseProtocol();
  49. /* Get filename. */
  50. od_printf("\n\rEnter filename(s) : ");
  51. od_input_str(szFilename, 72, ' ', 255);
  52. od_printf("\n\r");
  53. /* Perform file transfer. */
  54. bSuccess = TransferFile(szFilename, Protocol, bReceive);
  55. /* Display result of file transfer to user. */
  56. od_clr_scr();
  57. if(bSuccess)
  58. {
  59. od_printf("File transfer successful.\n\r");
  60. }
  61. else
  62. {
  63. od_printf("File transfer not completed.\n\r");
  64. }
  65. /* Prompt user to exit program. */
  66. od_printf("Press [Enter] to return to BBS.\n\r");
  67. od_get_answer("\n\r");
  68. /* Return control to calling BBS software. */
  69. od_exit(0, FALSE);
  70. return(0);
  71. }
  72. /* Function to allow user to choose a file transfer protocol. */
  73. eProtocol ChooseProtocol(void)
  74. {
  75. eProtocol Protocol;
  76. char chAnswer;
  77. od_printf("Available file transfer protocols:\n\r");
  78. od_printf(" [X] XModem\n\r");
  79. od_printf(" [C] XModem/CRC\n\r");
  80. od_printf(" [1] XModem/1K\n\r");
  81. od_printf(" [Y] YModem\n\r");
  82. od_printf(" [G] YModem-G\n\r");
  83. od_printf(" [Z] ZModem\n\r\n\r");
  84. od_printf("Please select a protocol: ");
  85. chAnswer = od_get_answer("XC1YGZ");
  86. switch(chAnswer)
  87. {
  88. case 'X':
  89. od_printf("XModem\n\r");
  90. Protocol = XModem;
  91. break;
  92. case 'C':
  93. od_printf("XModem/CRC\n\r");
  94. Protocol = XModemCRC;
  95. break;
  96. case '1':
  97. od_printf("XModem/1K\n\r");
  98. Protocol = XModem1K;
  99. break;
  100. case 'Y':
  101. od_printf("YModem\n\r");
  102. Protocol = YModem;
  103. break;
  104. case 'G':
  105. od_printf("YModem-G\n\r");
  106. Protocol = YModemG;
  107. break;
  108. case 'Z':
  109. default:
  110. od_printf("ZModem\n\r");
  111. Protocol = ZModem;
  112. break;
  113. }
  114. return(Protocol);
  115. }
  116. /* Function to send or receive a file to/from remote system. */
  117. int TransferFile(char *pszFilename, eProtocol Protocol, char bReceive)
  118. {
  119. char szPort[7];
  120. char *apszArguments[ARGS_ARRAY_SIZE];
  121. int nArgCount = 0;
  122. /* Filename cannot be NULL. */
  123. assert(pszFilename != NULL);
  124. /* Ensure that we are not operating in local mode. */
  125. if(od_control.baud == 0)
  126. {
  127. od_printf("Operating in local mode; file transfer not possible.\n\r");
  128. return(FALSE);
  129. }
  130. /* Generate DSZ command line */
  131. /* Begin with executable filename. */
  132. AddParameter(apszArguments, &nArgCount, szDSZFilename);
  133. /* Add port parameter. */
  134. AddParameter(apszArguments, &nArgCount, "port");
  135. sprintf(szPort, "%d", od_control.port + 1);
  136. AddParameter(apszArguments, &nArgCount, szPort);
  137. /* Restrict inbound files to current drive and directory. */
  138. AddParameter(apszArguments, &nArgCount, "restrict");
  139. /* Generate DSZ protocol parameters from specified protocol and direction. */
  140. if(bReceive)
  141. {
  142. switch(Protocol)
  143. {
  144. case XModem:
  145. case XModem1K:
  146. AddParameter(apszArguments, &nArgCount, "rx");
  147. break;
  148. case XModemCRC:
  149. AddParameter(apszArguments, &nArgCount, "rc");
  150. break;
  151. case YModem:
  152. AddParameter(apszArguments, &nArgCount, "rb");
  153. break;
  154. case YModemG:
  155. AddParameter(apszArguments, &nArgCount, "rb");
  156. AddParameter(apszArguments, &nArgCount, "-g");
  157. break;
  158. case ZModem:
  159. AddParameter(apszArguments, &nArgCount, "rz");
  160. break;
  161. default:
  162. assert(FALSE);
  163. }
  164. }
  165. else
  166. {
  167. switch(Protocol)
  168. {
  169. case XModem:
  170. case XModemCRC:
  171. AddParameter(apszArguments, &nArgCount, "sx");
  172. break;
  173. case XModem1K:
  174. AddParameter(apszArguments, &nArgCount, "sx");
  175. AddParameter(apszArguments, &nArgCount, "-k");
  176. break;
  177. case YModem:
  178. case YModemG:
  179. AddParameter(apszArguments, &nArgCount, "sb");
  180. break;
  181. case ZModem:
  182. AddParameter(apszArguments, &nArgCount, "sz");
  183. break;
  184. default:
  185. assert(FALSE);
  186. }
  187. }
  188. /* Add filename parameter to command line. */
  189. AddParameter(apszArguments, &nArgCount, pszFilename);
  190. /* Display prompt to user providing */
  191. od_printf("Begin your transfer now, or press [Ctrl]-[X] several times to abort.\n\r");
  192. /* Execute command using the OpenDoors od_spawn() function. */
  193. return(od_spawnvpe(P_WAIT, apszArguments[0], apszArguments, NULL) == 0);
  194. }
  195. /* Function to add next parameter to array of parameters to pass to */
  196. /* od_spawnvpe(). */
  197. void AddParameter(char **papszArguments, int *pnCount, char *pszNewArgument)
  198. {
  199. assert(*pnCount < ARGS_ARRAY_SIZE - 1);
  200. assert(papszArguments != NULL);
  201. assert(pnCount != NULL);
  202. assert(pszNewArgument != NULL);
  203. /* Add next argument to array. */
  204. papszArguments[(*pnCount)++] = pszNewArgument;
  205. /* Ensure that the array is always NULL-terminated. */
  206. papszArguments[*pnCount] = NULL;
  207. }