home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / k95source / p_common.c < prev    next >
C/C++ Source or Header  |  2000-06-11  |  6KB  |  300 lines

  1. /*****************************************************************************/
  2. /*             Copyright (c) 1994 by Jyrki Salmi <jytasa@jyu.fi>             */
  3. /*        You may modify, recompile and distribute this file freely.         */
  4. /*****************************************************************************/
  5.  
  6. /*
  7.    Some common utility functions
  8. */
  9.  
  10. #include "ckcdeb.h"
  11. #ifndef NOXFER
  12. #include "ckcker.h"
  13.  
  14. #include <stdio.h>
  15. #ifdef OS2
  16. #include <stdarg.h>
  17. #endif
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <io.h>
  22.  
  23. #ifdef OS2
  24. #ifdef NT
  25. #include <windows.h>
  26. #define DosBeep Beep
  27. #define DosSleep Sleep
  28. #define access _access
  29. #else
  30. #define INCL_KBD
  31. #define INCL_DOSPROCESS
  32. #include <os2.h>
  33. #undef COMMENT
  34. #endif
  35. #include "ckocon.h"
  36. #endif /* OS2 */
  37.  
  38. #include "p_type.h"
  39. #include "p_common.h"
  40. #include "p_error.h"
  41. #include "p_module.h"
  42. #include "p_global.h"
  43.  
  44. /* Returns time difference in verbal form. timer parameter is the difference */
  45. /* in number of seconds. */
  46.  
  47. U8 *
  48. #ifdef CK_ANSIC
  49. d_time(U32 timer)
  50. #else
  51. d_time(timer) U32 timer ;
  52. #endif
  53. {
  54.  
  55.   static U8 ret[32];
  56.  
  57.   if (timer < 60)
  58.     sprintf(ret, "%lu s", timer);
  59.   else if (timer < 60 * 60)
  60.     sprintf(ret, "%lu min %lu s", timer / 60, timer % 60);
  61.   else if (timer < 60 * 60 * 24)
  62.     sprintf(ret, "%lu h %lu min %lu s", timer / (60 * 60), (timer / 60) % 60, timer % 60);
  63.   else
  64.     sprintf(ret, "%lu d %lu h %lu min %lu s", timer / (60 * 60 * 24),
  65.             timer / (60 * 60) % 24, (timer / 60) % 60, timer % 60);
  66.   return(ret);
  67. }
  68.  
  69.  
  70. /* Pauses until a key is pressed. Call to this function is installed to the */
  71. /* exit functions list if -pause option is specified. */
  72.  
  73. VOID
  74. #ifdef CK_ANSIC
  75. wait_for_keypress(void)
  76. #else
  77. wait_for_keypress()
  78. #endif
  79. {
  80.  
  81.   APIRET rc=0;
  82. #ifdef OS2
  83. #ifndef NT
  84.    KBDKEYINFO kki;
  85. #endif
  86. #endif /* OS2 */
  87.  
  88.   printf("Press any key to continue...");
  89.   fflush(stdout);
  90. #ifdef OS2
  91. #ifdef NT
  92.    getchar() ;
  93. #else
  94.    rc = KbdCharIn(&kki, IO_WAIT, 0);
  95.   if (rc)
  96.     os2_error(P_ERROR_KBDCHARIN, rc,
  97.               MODULE_COMMON, __LINE__,
  98.               NULL);
  99. #endif /* NT */
  100. #else
  101.     getchar();
  102. #endif  /* OS2 */
  103.  
  104.   printf("\n");
  105. }
  106.  
  107. /* Makes a beep if -quiet option is not specified. Installed to the exit */
  108. /* functions list. */
  109.  
  110. VOID
  111. #ifdef CK_ANSIC
  112. make_noise(void)
  113. #else
  114. make_noise()
  115. #endif
  116. {
  117.   if (!opt_quiet) {
  118.     DosBeep(500, 100);
  119.     DosSleep(250);
  120.     DosBeep(1500, 50);
  121.   }
  122. }
  123.  
  124. /* Adds the receive directory specified with -directory option to the */
  125. /* beginning of file name being received */
  126.  
  127. VOID
  128. #ifdef CK_ANSIC
  129. add_recv_dir_to_path(U8 **path)
  130. #else
  131. add_recv_dir_to_path(path) U8 **path;
  132. #endif
  133. {
  134.  
  135.   U8 new_path[4096];
  136.   U32 i;
  137.   U32 l;
  138.  
  139.   if (opt_directory == NULL || opt_paths)
  140.     return;
  141.  
  142.   l = strlen(*path) + strlen(opt_directory) + 2; /* 2 is for possible */
  143.                                                     /* '\' and the null */
  144.   ckstrncpy(new_path, opt_directory, 4096);
  145.   i = strlen(new_path);
  146.   if (new_path[i - 1] != '\\')
  147.     new_path[i++] = '\\';
  148.   ckstrncpy(&new_path[i], *path, 4096-i);
  149.   memcpy(*path, new_path, 4096);
  150. }
  151.  
  152. /* Gets the length of directory portion of path */
  153.  
  154. U32
  155. #ifdef CK_ANSIC
  156. get_dir_len(U8 *path)
  157. #else
  158. get_dir_len(path) U8 *path;
  159. #endif
  160. {
  161.  
  162.   S32 i;
  163.  
  164.   i = strlen(path);
  165.   while (--i >= 0) {
  166.     if (path[i] == '\\' || path[i] == '/' || path[i] == ':')
  167.       break;
  168.   }
  169.   return(i + 1);
  170. }
  171.  
  172. /* Strips drive and directory information from a file path */
  173.  
  174. VOID
  175. #ifdef CK_ANSIC
  176. strip_drive_and_dir(U8 *path)
  177. #else
  178. strip_drive_and_dir(path) U8 *path ;
  179. #endif
  180. {
  181.  
  182.   U8 new_path[4096];
  183.  
  184.   ckstrncpy(new_path, &path[get_dir_len(path)], 4096);
  185.   strcpy(path, new_path);
  186. }
  187.  
  188. /* Creates a directory structure if it does not already exist */
  189.  
  190. U32
  191. #ifdef CK_ANSIC
  192. create_dirs(U8 *path)
  193. #else
  194. create_dirs(path) U8 * path ;
  195. #endif
  196. {
  197.  
  198.   U8 dir[4096];
  199.   U32 path_idx = 0;
  200.  
  201.   while (path[path_idx] != '\0' && path_idx < 4096) {
  202.     if (path[path_idx] == '\\' || path[path_idx] == '/') {
  203.       if (path_idx == 0 || path[path_idx - 1] == ':') {
  204.         memcpy(dir, path, path_idx + 1);
  205.         dir[path_idx + 1] = '\0';
  206.       } else {
  207.         memcpy(dir, path, path_idx);
  208.         dir[path_idx] = '\0';
  209.       }
  210.       if (access(dir, 0) != 0) {
  211.         if (_mkdir(dir) == -1)
  212.           return(1);
  213.       }
  214.     }
  215.     path_idx++;
  216.   }
  217.   return(0);
  218. }
  219.  
  220. /* Sets a priority of current process */
  221.  
  222. VOID
  223. #ifdef CK_ANSIC
  224. set_priority(U32 priority_class, U32 priority_delta)
  225. #else
  226. set_priority(priority_class,priority_delta)
  227.      U32 priority_class; U32 priority_delta ;
  228. #endif
  229. {
  230. #ifdef OS2
  231.   APIRET rc=0;
  232.  
  233. #ifdef NT
  234.    SetPriorityClass( GetCurrentProcess(), priority_class ) ;
  235.    SetThreadPriority( GetCurrentThread(), priority_delta ) ;
  236. #else
  237.    rc = DosSetPriority(PRTYS_PROCESS,
  238.                       priority_class,
  239.                       priority_delta,
  240.                       0);
  241. #endif
  242.   if (rc)
  243.     os2_error(P_ERROR_DOSSETPRIORITY, rc,
  244.               MODULE_COMMON, __LINE__,
  245.               NULL);
  246. #endif /* OS2 */
  247. }
  248.  
  249. /* Handler for SIGINT signals. Sets the aborted variable to non-zero, it */
  250. /* will be checked in status_func() and a proper return value will be */
  251. /* returned to the DLL. */
  252.  
  253. VOID
  254. #ifdef CK_ANSIC
  255. interrupt_handler(int sig)
  256. #else
  257. interrupt_handler(sig) int sig ;
  258. #endif
  259. {
  260.  
  261.   if (aborted)
  262.     ckscreen(SCR_EM,0,0l, "Please wait, transfer is being cancelled...");
  263.   else {
  264.     ckscreen(SCR_EM,0,0l, "Ctrl-C pressed, transfer is being cancelled...");
  265.     aborted = 1;
  266.   }
  267.     signal(SIGINT,interrupt_handler);
  268. }
  269.  
  270. /* Installs a handler for SIGINT signals */
  271.  
  272. VOID
  273. #ifdef CK_ANSIC
  274. install_interrupt_handler(U8 phandler)
  275. #else
  276. install_interrupt_handler(phandler) U8 phandler ;
  277. #endif
  278. {
  279.    static void (*saved)(int) = NULL ;
  280.  
  281.    if ( phandler )
  282.    {
  283.       if ((saved = signal(SIGINT, interrupt_handler)) == SIG_ERR) { /* Install our own */
  284.          /* handler for CTRL-C */
  285.          fprintf(stderr, "Failed to install an interrupt handler\n");
  286.          exit(1);
  287.       }
  288.    }
  289.    else
  290.    {
  291.       if (signal(SIGINT, saved) == SIG_ERR) { /* Restore previous */
  292.          /* handler for CTRL-C */
  293.          fprintf(stderr, "Failed to install an interrupt handler\n");
  294.          exit(1);
  295.       }
  296.    }
  297. }
  298.  
  299. #endif /* NOXFER */
  300.