home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / bin / p205.zip / exesrc / common.c < prev    next >
C/C++ Source or Header  |  1994-12-18  |  6KB  |  240 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 <stdio.h>
  11. #define INCL_KBD
  12. #define INCL_DOSPROCESS
  13. #include <os2.h>
  14. #include <stdarg.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <io.h>
  19. #include "typedefs.h"
  20. #include "common.h"
  21. #include "error.h"
  22. #include "modules.h"
  23. #include "global.h"
  24.  
  25. /* Returns time difference in verbal form. timer parameter is the difference */
  26. /* in number of seconds. */
  27.  
  28. U8 *d_time(U32 timer) {
  29.  
  30.   static U8 ret[32];
  31.  
  32.   if (timer < 60)
  33.     sprintf(ret, "%lu s", timer);
  34.   else if (timer < 60 * 60)
  35.     sprintf(ret, "%lu min %lu s", timer / 60, timer % 60);
  36.   else if (timer < 60 * 60 * 24)
  37.     sprintf(ret, "%lu h %lu min %lu s", timer / (60 * 60), (timer / 60) % 60, timer % 60);
  38.   else
  39.     sprintf(ret, "%lu d %lu h %lu min %lu s", timer / (60 * 60 * 24),
  40.         timer / (60 * 60) % 24, (timer / 60) % 60, timer % 60);
  41.   return(ret);
  42. }
  43.  
  44. /* Displays a string to the screen. If attr is MSG_NONE, a space will */
  45. /* separate this string from the next one. If MSG_CR then this string will */
  46. /* overwrite the possible previous message if it did not have MSG_LF attr. */
  47. /* MSG_LF adds a newline to the end of the string. */
  48.  
  49. void msg(U32 attr, U8 *fmt, ...) {
  50.  
  51.   static U32 prev_attr = MSG_LF;
  52.   static U32 line_len = 0;
  53.   U32 line_len_tmp;
  54.   U32 i;
  55.   va_list arg_ptr;
  56.  
  57.   va_start(arg_ptr, fmt);
  58.   if (attr & MSG_CR) {
  59.     printf("\r");
  60.   } else if (!(prev_attr & MSG_LF))
  61.     printf(" ");
  62.  
  63.   line_len_tmp = vprintf(fmt, arg_ptr);
  64.   if (line_len > line_len_tmp) { /* This line has data more than we just */
  65.                  /* printed, let's wipe that old data away */
  66.     for (i = line_len_tmp; i < line_len; i++)
  67.       printf(" ");
  68.     for (i = line_len_tmp; i < line_len; i++)
  69.       printf("\b");
  70.   }
  71.   if (attr & MSG_LF) {
  72.     printf("\n");
  73.     line_len = 0;
  74.  
  75.     if (opt_note != NULL) {
  76.       printf("\x1b[s");        /* Save the cursor position */
  77.       printf("\x1b[1;1H");    /* Set cursor position to column 1 on row 1*/
  78.       printf("\x1b[7m");    /* Set reverse attribute */
  79.       printf("%s", opt_note);
  80.       printf("\x1b[K");        /* Clear to end of line */
  81.       printf("\x1b[u");        /* Restore the cursor position */
  82.       printf("\x1b[0m");    /* Reset attribute */
  83.     }
  84.   } else {
  85.     line_len = line_len_tmp;
  86.     fflush(stdout);
  87.   }
  88.   prev_attr = attr;
  89.   va_end(arg_ptr);
  90. }
  91.  
  92. /* Pauses until a key is pressed. Call to this function is installed to the */
  93. /* exit functions list if -pause option is specified. */
  94.  
  95. void wait_for_keypress(void) {
  96.  
  97.   APIRET rc;
  98.   KBDKEYINFO kki;
  99.  
  100.   printf("Press any key to continue...");
  101.   fflush(stdout);
  102.   rc = KbdCharIn(&kki, IO_WAIT, 0);
  103.   if (rc)
  104.     os2_error(P_ERROR_KBDCHARIN, rc,
  105.           MODULE_COMMON, __LINE__,
  106.           NULL);
  107.   printf("\n");
  108. }
  109.  
  110. /* Makes a beep if -quiet option is not specified. Installed to the exit */
  111. /* functions list. */
  112.  
  113. void make_noise(void) {
  114.  
  115.   if (!opt_quiet) {
  116.     DosBeep(500, 100);
  117.     DosSleep(250);
  118.     DosBeep(1500, 50);
  119.   }
  120. }
  121.  
  122. /* Adds the receive directory specified with -directory option to the */
  123. /* beginning of file name being received */
  124.  
  125. void add_recv_dir_to_path(U8 **path) {
  126.  
  127.   U8 new_path[4096];
  128.   U32 i;
  129.   U32 l;
  130.  
  131.   if (opt_directory == NULL || opt_paths)
  132.     return;
  133.  
  134.   l = strlen(*path) + strlen(opt_directory) + 2; /* 2 is for possible */
  135.                             /* '\' and the null */
  136.   strcpy(new_path, opt_directory);
  137.   i = strlen(new_path);
  138.   if (new_path[i - 1] != '\\')
  139.     new_path[i++] = '\\';
  140.   strcpy(&new_path[i], *path);
  141.   memcpy(*path, new_path, 4096);
  142. }
  143.  
  144. /* Gets the length of directory portion of path */
  145.  
  146. U32 get_dir_len(U8 *path) {
  147.  
  148.   S32 i;
  149.  
  150.   i = strlen(path);
  151.   while (--i >= 0) {
  152.     if (path[i] == '\\' || path[i] == '/' || path[i] == ':')
  153.       break;
  154.   }
  155.   return(i + 1);
  156. }
  157.  
  158. /* Strips drive and directory information from a file path */
  159.  
  160. void strip_drive_and_dir(U8 *path) {
  161.  
  162.   U8 new_path[4096];
  163.  
  164.   strcpy(new_path, &path[get_dir_len(path)]);
  165.   strcpy(path, new_path);
  166. }
  167.  
  168. /* Creates a directory structure if it does not already exist */
  169.  
  170. U32 create_dirs(U8 *path) {
  171.  
  172.   U8 dir[4096];
  173.   U32 path_idx = 0;
  174.  
  175.   while (path[path_idx] != '\0') {
  176.     if (path[path_idx] == '\\' || path[path_idx] == '/') {
  177.       if (path_idx == 0 || path[path_idx - 1] == ':') {
  178.     memcpy(dir, path, path_idx + 1);
  179.     dir[path_idx + 1] = '\0';
  180.       } else {
  181.     memcpy(dir, path, path_idx);
  182.     dir[path_idx] = '\0';
  183.       }
  184.       if (access(dir, 0) != 0) {
  185. #ifdef __EMX__
  186.     if (mkdir(dir, 0) == -1)
  187.       return(1);
  188. #else /* __EMX__ */
  189.     if (_mkdir(dir, 0) == -1)
  190.       return(1);
  191. #endif /* __EMX__ */
  192.       }
  193.     }
  194.     path_idx++;
  195.   }
  196.   return(0);
  197. }
  198.  
  199. /* Sets a priority of current process */
  200.  
  201. void set_priority(U32 priority_class, U32 priority_delta) {
  202.  
  203.   APIRET rc;
  204.  
  205.   rc = DosSetPriority(PRTYS_PROCESS,
  206.               priority_class,
  207.               priority_delta,
  208.               0);
  209.   if (rc)
  210.     os2_error(P_ERROR_DOSSETPRIORITY, rc,
  211.           MODULE_COMMON, __LINE__,
  212.           NULL);
  213. }
  214.  
  215. /* Handler for SIGINT signals. Sets the aborted variable to non-zero, it */
  216. /* will be checked in status_func() and a proper return value will be */
  217. /* returned to the DLL. */
  218.  
  219. void interrupt_handler(int sig) {
  220.  
  221.   if (aborted)
  222.     msg(MSG_LF, "Please wait, transfer is being aborted...");
  223.   else {
  224.     msg(MSG_LF, "Ctrl-C pressed");
  225.     aborted = 1;
  226.   }
  227. }
  228.  
  229. /* Installs a handler for SIGINT signals */
  230.  
  231. void install_interrupt_handler(void) {
  232.  
  233.   if (signal(SIGINT, interrupt_handler) == SIG_ERR) { /* Install our own */
  234.                               /* handler for CTRL-C */
  235.     fprintf(stderr, "Failed to install an interrupt handler\n");
  236.     exit(1);
  237.   }
  238. }
  239.  
  240.