home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Networking / msend-3.3-I / compat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-26  |  3.5 KB  |  214 lines

  1. /*
  2.  * msave.c
  3.  *
  4.  * Copyright (c) 1993 Zik Saleeba <zik@zikzak.apana.org.au>
  5.  *
  6.  * This is portability code to allow msend to run on
  7.  * as many systems as possible. All of this code is optional.
  8.  * The parts which are compiled depends on the options
  9.  * selected.
  10.  */
  11.     
  12. /* $Id: compat.c,v 1.1 1997/10/26 07:31:23 lukeh Exp $ */
  13.  
  14.  
  15. #include "common.h"
  16.  
  17. #ifdef USE_LOCKFILES
  18. int
  19. lock_file(fullpath)
  20.     char *fullpath;
  21. {
  22.     char lockpath[PATH_MAX+1];
  23.     char procnum[10];
  24.     char readprocnum[10];
  25.     int lock;
  26.     int failcount;
  27.     int nread;
  28.  
  29.     lockpath[PATH_MAX] = '\0';
  30.     strcpy(lockpath, fullpath);
  31.     strncat(lockpath, ".lock", PATH_MAX);
  32.     failcount = 0;
  33.     do {
  34.         do  {
  35.             lock = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0644);
  36.             if (lock < 0 && errno == EEXIST) {
  37.                 failcount++;
  38.                 if (failcount < MAX_LOCK_FAILS)
  39.                     sleep(1);    /* wait a bit and try again */
  40.                 else {
  41.                     lock = open(lockpath, O_CREAT, 0644);
  42.                     if (lock < 0)
  43.                         return -1;
  44.                 }
  45.             }
  46.             else {
  47.                 if (lock < 0 && errno != EEXIST)
  48.                     return -1;
  49.             }
  50.         } while (lock < 0);
  51.     
  52.         /* write our pid to it */
  53.         sprintf(procnum, "%d\n", getpid());
  54.         write(lock, procnum, strlen(procnum));
  55.         close(lock);
  56.  
  57.         /* check if our pid is there */
  58.         lock = open(lockpath, O_RDONLY);
  59.         if (lock >= 0) {
  60.             if ((nread = read(lock, readprocnum, 9)) <= 0 || strncmp(procnum, readprocnum, nread) != 0) {
  61.                 close(lock);
  62.                 lock = -1;
  63.             }
  64.             else 
  65.                 close(lock);
  66.         }
  67.  
  68.         /* check failures */
  69.         if (lock < 0) {
  70.             failcount++;
  71.             sleep(1);
  72.             if (failcount >= MAX_LOCK_FAILS)
  73.                 return -1;
  74.         }
  75.     } while (lock < 0);
  76.  
  77.     return 0;
  78. }
  79.  
  80.  
  81. int
  82. unlock_file(fullpath)
  83.     char *fullpath;
  84. {
  85.     char lockpath[PATH_MAX+1];
  86.  
  87.     lockpath[PATH_MAX] = '\0';
  88.     strcpy(lockpath, fullpath);
  89.     strncat(lockpath, ".lock", PATH_MAX);
  90.     return unlink(lockpath);
  91. }
  92. #endif /* USE_LOCKFILES */
  93.  
  94. #ifdef NO_POSIX_MEMFUNCS
  95. void
  96. compat_memset(dest, cchar, length)
  97.     char *dest;
  98.     char cchar;
  99.     int length;
  100. {
  101.     register char *pos;
  102.     register int count;
  103.  
  104.     for (pos = dest, count = length; length > 0; length--)
  105.         *pos++ = cchar;
  106. }
  107. #endif /* NO_POSIX_MEMFUNCS */
  108.  
  109. #ifdef NO_STRCASECMP
  110. char
  111. compat_toupper(a)
  112.     char a;
  113. {
  114. if (a >= 'a' && a <= 'z')
  115.     return a - ('a'-'A');
  116. else
  117.     return a;
  118. }
  119.  
  120. int
  121. compat_strcasecmp(a, b)
  122.     char *a, *b;
  123. {
  124.     register char *x, *y;
  125.     register char i, j;
  126.  
  127.     for (x=a, y=b; i=compat_toupper(*x), j=compat_toupper(*y), i!=0 && j!=0; x++, y++) {
  128.         if (i < j)
  129.             return -1;
  130.         else if (i > j)
  131.             return 1;
  132.     }
  133.  
  134.     if (i == 0) {
  135.         if (j == 0)
  136.             return 0;
  137.         else
  138.             return -1;
  139.     }
  140.     else
  141.         return 1;
  142. }
  143.  
  144.  
  145. int
  146. compat_strncasecmp(a, b, n)
  147.     char *a, *b;
  148. {
  149.     register char *x, *y;
  150.     register char i, j;
  151.  
  152.     for (x=a, y=b; i=compat_toupper(*x), j=compat_toupper(*y), i!=0 && j!=0 && n>0; x++, y++, n--) {
  153.         if (i < j)
  154.             return -1;
  155.         else if (i > j)
  156.             return 1;
  157.     }
  158.  
  159.     if (n == 0)
  160.         return 0;
  161.  
  162.     if (i == 0) {
  163.         if (j == 0)
  164.             return 0;
  165.         else
  166.             return -1;
  167.     }
  168.     else
  169.         return 1;
  170. }
  171. #endif /* NO_STRCASECMP */
  172.  
  173. #ifdef USE_READLINE
  174. void *xmalloc(int size)
  175. {
  176.     void *where;
  177.  
  178.     where = malloc(size);
  179.     if (where == NULL) {
  180.         fprintf(stderr, "Out of virtual memory\n");
  181.         exit(2);
  182.     }
  183.     return where;
  184. }
  185.  
  186. void *xrealloc(void *oldone, int size)
  187. {
  188.     void *where;
  189.  
  190.     where = realloc(oldone, size);
  191.     if (where == NULL) {
  192.         fprintf(stderr, "Out of virtual memory\n");
  193.         exit(2);
  194.     }
  195.     return where;
  196. }
  197. #endif /* USE_READLINE */
  198.  
  199. #ifdef NO_POSIX_STRFUNCS
  200. char *compat_strstr(d, s)
  201.     char *d, *s;
  202. {
  203.     register char *dscan, *sscan;
  204.     register char *pos;
  205.  
  206.     for (pos = d; *pos != 0; pos++) {
  207.         for (dscan = pos, sscan = s; *dscan == *sscan && *dscan != '\0'; dscan++, sscan++);
  208.         if (*sscan == '\0')
  209.             return pos;
  210.     }
  211.     return NULL;
  212. }
  213. #endif /* NO_POSIX_STRFUNCS */
  214.