home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / mt-misc.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  6KB  |  255 lines

  1. /* $Id: mt-misc.c,v 2.3 1992/12/14 00:14:02 davison Trn $
  2. */
  3.  
  4. /*** OS2: We need to include OS2.H if we want to
  5.           use OS2 System-calls  ***/
  6. #include <OS2.H>
  7.  
  8. #include "EXTERN.h"
  9. #include "common.h"
  10. #include "threads.h"
  11. #include "mthreads.h"
  12.  
  13. char *lib, *rnlib, *mtlib, *spool, *threaddir, *homedir;
  14. int locked = 0, cron_locking = 0;
  15.  
  16. void
  17. mt_init()
  18. {
  19.     /* Set up a nice friendly umask. */
  20.     umask(002);
  21.  
  22.     /* Init the directory strings, possibly translating them into real paths */
  23. /*** OS2: if home is not set, let's take the setting out of
  24.           the uupc-rc-files, where it is definitely set. ***/
  25.     homedir = getenv("HOME");
  26.     if (homedir == Nullch)
  27. /*    homedir = getenv("LOGDIR");  */
  28.     homedir = uupc_rc_settings.home;
  29.     change_bsl2sl(homedir);
  30.  
  31. /*** OS2: we don't need to expand the filenames,
  32.           which are defined in the uupc-rc-files. ***/
  33. /*    spool = savestr(file_exp(SPOOL));           */
  34. /*    lib = savestr(file_exp(LIB));               */
  35. /*    rnlib = savestr(file_exp(RNLIB));           */
  36. /*    mtlib = savestr(file_exp(MTLIB));           */
  37. /*    threaddir = savestr(file_exp(THREAD_DIR));  */
  38.     spool = savestr(SPOOL);
  39.     change_bsl2sl(spool);
  40.     lib = savestr(LIB);
  41.     change_bsl2sl(lib);
  42.     rnlib = savestr(RNLIB);
  43.     change_bsl2sl(rnlib);
  44.     mtlib = savestr(MTLIB);
  45.     change_bsl2sl(mtlib);
  46.     threaddir = savestr(THREAD_DIR);
  47.     change_bsl2sl(threaddir);
  48. }
  49.  
  50. /* Make sure we're not already running by creating a lock file. */
  51. long
  52. mt_lock(which_lock, sig)
  53. int which_lock;
  54. int sig;
  55. {
  56.     char buff[LBUFLEN], *filename;
  57.     FILE *fp;
  58.  
  59.     sprintf(buff, "%s.%ld", file_exp(MTPRELOCK), (long)getpid());
  60.     if ((fp = fos2open(buff, "w")) == Nullfp) {
  61.     log_entry("Unable to create lock temporary `%s'.\n", buff);
  62.     wrap_it_up(1);
  63.     }
  64.     fprintf(fp, "%s%ld\n", which_lock == DAEMON_LOCK ? "pid " : nullstr,
  65.     (long)getpid());
  66.     fclose(fp);
  67.  
  68.     /* Try to link to lock file. */
  69.     if (which_lock == DAEMON_LOCK) {
  70.     filename = file_exp(MTDLOCK);
  71.     } else {
  72.     filename = file_exp(MTLOCK);
  73.     }
  74.   dolink:
  75. /*** OS2: another link, another DosCopy.... ***
  76.           OK, let's hope that it will work ***/
  77. /*    while (link(buff, filename) < 0)   */
  78.       while (DosCopy(buff, filename, (ULONG)0) > 0) {
  79.       long otherpid;
  80.     if ((fp = fos2open(filename, "r")) == Nullfp) {
  81.         log_entry("unable to open %s\n", filename);
  82.         if (cron_locking) {
  83.         goto Sleep;
  84.         }
  85.         unlink(buff);
  86.         wrap_it_up(1);
  87.     }
  88.     if (fscanf(fp, "%ld", &otherpid) != 1) { 
  89.         log_entry("unable to read pid from %s\n", filename);
  90.         fclose(fp);
  91.         if (cron_locking) {
  92.         goto Sleep;
  93.         }
  94.         unlink(buff);
  95.         wrap_it_up(1);
  96.     }
  97.     fclose(fp);
  98.     if (kill(otherpid, sig) == -1 && errno == ESRCH) {
  99.         if (unlink(filename) == -1) {
  100.         log_entry("unable to unlink lockfile %s\n", filename);
  101.         unlink(buff);
  102.         wrap_it_up(1);
  103.         }
  104.         goto dolink;
  105.     }
  106.     if (cron_locking) {
  107.       Sleep:
  108.         sleep(60);
  109.         continue;
  110.     }
  111.     unlink(buff);
  112.     return otherpid;
  113.     }
  114.     unlink(buff);            /* remove temporary LOCK.<pid> file */
  115.     locked |= which_lock;
  116.     return 0;                /* return success */
  117. }
  118.  
  119. void
  120. mt_unlock(which_lock)
  121. int which_lock;
  122. {
  123.     which_lock &= locked;
  124.     if (which_lock & PASS_LOCK) {
  125.     unlink(file_exp(MTLOCK));        /* remove single-pass lock */
  126.     }
  127.     if (which_lock & DAEMON_LOCK) {
  128.     unlink(file_exp(MTDLOCK));        /* remove daemon lock */
  129.     }
  130.     locked &= ~which_lock;
  131. }
  132.  
  133. /* Interpret rn's %x prefixes and ~name expansions without including tons
  134. ** of useless source.  NOTE:  names that don't start with '/', '%' or '~'
  135. ** are prefixed with the SPOOL directory.  (Note that ~'s don't work yet.)
  136. */
  137. char *
  138. file_exp(name)
  139. char *name;
  140. {
  141.     static char namebuf[MAXFILENAME];
  142.  
  143. /*** OS2: it is not so easy to determine if the filename
  144.           is fully qualified under OS/2 than under Unix ***/
  145.  
  146.     if ((*name == '/') ||
  147.         ((*(name+2) == '/') && (*(name+1) == ':')) ||
  148.         ((*(name+2) == '\\') && (*(name+1) == ':')) ||
  149.         (*name == '\\')) { /* fully qualified names are left alone */
  150.     return name;
  151.     }
  152.     switch (name[0]) {
  153.     case '%':            /* interpret certain %x values */
  154.     switch (name[1]) {
  155.     case 'P':
  156.         strcpy(namebuf, spool);
  157.         break;
  158.     case 'w':
  159.         strcpy(namebuf, mtlib);
  160.         break;
  161.     case 'W':
  162.         strcpy(namebuf, threaddir);
  163.         break;
  164.     case 'x':
  165.         strcpy(namebuf, lib);
  166.         break;
  167.     case 'X':
  168.         strcpy(namebuf, rnlib);
  169.         break;
  170.     default:
  171.         log_entry("Unknown expansion: %s\n", name);
  172.         wrap_it_up(1);
  173.         break;
  174.     }
  175.     strcat(namebuf, name+2);
  176.     break;
  177.     case '~':
  178.     {
  179.     char *s = name + 1;
  180.  
  181.         if ((!*s) || (*s == '/')) {
  182.         sprintf(namebuf, "%s%s", homedir, s);
  183.     } else {
  184.         log_entry("~name expansions not implemented.");
  185.         wrap_it_up(1);
  186.     }
  187.         break;
  188.     }
  189.     default:            /* all "normal" names are relative to SPOOL */
  190.     sprintf(namebuf, "%s/%s", spool, name);
  191.     break;
  192.     }
  193.     return namebuf;
  194. }
  195.  
  196. #ifndef lint
  197. /* A malloc that bombs-out when memory is exhausted. */
  198. char *
  199. safemalloc(amount)
  200. MEM_SIZE amount;
  201. {
  202.     register char *cp;
  203.     char *malloc();
  204.  
  205.     if ((cp = malloc(amount)) == Nullch) {
  206.     log_error("malloc(%ld) failed.\n", (long)amount);
  207.     wrap_it_up(1);
  208.     }
  209.     return cp;
  210. }
  211.  
  212. /* paranoid version of realloc */
  213. char *
  214. saferealloc(where,size)
  215. char *where;
  216. MEM_SIZE size;
  217. {
  218.     char *ptr;
  219.     char *realloc();
  220.  
  221.     ptr = realloc(where,size?size:1);    /* realloc(0) is NASTY on our system */
  222.     if (ptr == Nullch) {
  223.     log_error("realloc(..., %ld) failed.\n", (long)size);
  224.     wrap_it_up(1);
  225.     }
  226.     return ptr;
  227. }
  228. #endif
  229.  
  230. /* Create a malloc'ed copy of a string. */
  231. char *
  232. savestr(str)
  233. char *str;
  234. {
  235.     register MEM_SIZE len = strlen(str) + 1;
  236.     register char *newaddr = safemalloc(len);
  237.  
  238.     bcopy(str, newaddr, (int)len);
  239.  
  240.     return newaddr;
  241. }
  242.  
  243. #ifndef lint
  244. /* Free some memory if it hasn't already been freed. */
  245. void
  246. safefree(pp)
  247. char **pp;
  248. {
  249.     if (*pp) {
  250.     free(*pp);
  251.     *pp = Nullch;
  252.     }
  253. }
  254. #endif
  255.