home *** CD-ROM | disk | FTP | other *** search
- /*
- * msave.c
- *
- * Copyright (c) 1993 Zik Saleeba <zik@zikzak.apana.org.au>
- *
- * This is portability code to allow msend to run on
- * as many systems as possible. All of this code is optional.
- * The parts which are compiled depends on the options
- * selected.
- */
-
- /* $Id: compat.c,v 1.1 1997/10/26 07:31:23 lukeh Exp $ */
-
-
- #include "common.h"
-
- #ifdef USE_LOCKFILES
- int
- lock_file(fullpath)
- char *fullpath;
- {
- char lockpath[PATH_MAX+1];
- char procnum[10];
- char readprocnum[10];
- int lock;
- int failcount;
- int nread;
-
- lockpath[PATH_MAX] = '\0';
- strcpy(lockpath, fullpath);
- strncat(lockpath, ".lock", PATH_MAX);
- failcount = 0;
- do {
- do {
- lock = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0644);
- if (lock < 0 && errno == EEXIST) {
- failcount++;
- if (failcount < MAX_LOCK_FAILS)
- sleep(1); /* wait a bit and try again */
- else {
- lock = open(lockpath, O_CREAT, 0644);
- if (lock < 0)
- return -1;
- }
- }
- else {
- if (lock < 0 && errno != EEXIST)
- return -1;
- }
- } while (lock < 0);
-
- /* write our pid to it */
- sprintf(procnum, "%d\n", getpid());
- write(lock, procnum, strlen(procnum));
- close(lock);
-
- /* check if our pid is there */
- lock = open(lockpath, O_RDONLY);
- if (lock >= 0) {
- if ((nread = read(lock, readprocnum, 9)) <= 0 || strncmp(procnum, readprocnum, nread) != 0) {
- close(lock);
- lock = -1;
- }
- else
- close(lock);
- }
-
- /* check failures */
- if (lock < 0) {
- failcount++;
- sleep(1);
- if (failcount >= MAX_LOCK_FAILS)
- return -1;
- }
- } while (lock < 0);
-
- return 0;
- }
-
-
- int
- unlock_file(fullpath)
- char *fullpath;
- {
- char lockpath[PATH_MAX+1];
-
- lockpath[PATH_MAX] = '\0';
- strcpy(lockpath, fullpath);
- strncat(lockpath, ".lock", PATH_MAX);
- return unlink(lockpath);
- }
- #endif /* USE_LOCKFILES */
-
- #ifdef NO_POSIX_MEMFUNCS
- void
- compat_memset(dest, cchar, length)
- char *dest;
- char cchar;
- int length;
- {
- register char *pos;
- register int count;
-
- for (pos = dest, count = length; length > 0; length--)
- *pos++ = cchar;
- }
- #endif /* NO_POSIX_MEMFUNCS */
-
- #ifdef NO_STRCASECMP
- char
- compat_toupper(a)
- char a;
- {
- if (a >= 'a' && a <= 'z')
- return a - ('a'-'A');
- else
- return a;
- }
-
- int
- compat_strcasecmp(a, b)
- char *a, *b;
- {
- register char *x, *y;
- register char i, j;
-
- for (x=a, y=b; i=compat_toupper(*x), j=compat_toupper(*y), i!=0 && j!=0; x++, y++) {
- if (i < j)
- return -1;
- else if (i > j)
- return 1;
- }
-
- if (i == 0) {
- if (j == 0)
- return 0;
- else
- return -1;
- }
- else
- return 1;
- }
-
-
- int
- compat_strncasecmp(a, b, n)
- char *a, *b;
- {
- register char *x, *y;
- register char i, j;
-
- for (x=a, y=b; i=compat_toupper(*x), j=compat_toupper(*y), i!=0 && j!=0 && n>0; x++, y++, n--) {
- if (i < j)
- return -1;
- else if (i > j)
- return 1;
- }
-
- if (n == 0)
- return 0;
-
- if (i == 0) {
- if (j == 0)
- return 0;
- else
- return -1;
- }
- else
- return 1;
- }
- #endif /* NO_STRCASECMP */
-
- #ifdef USE_READLINE
- void *xmalloc(int size)
- {
- void *where;
-
- where = malloc(size);
- if (where == NULL) {
- fprintf(stderr, "Out of virtual memory\n");
- exit(2);
- }
- return where;
- }
-
- void *xrealloc(void *oldone, int size)
- {
- void *where;
-
- where = realloc(oldone, size);
- if (where == NULL) {
- fprintf(stderr, "Out of virtual memory\n");
- exit(2);
- }
- return where;
- }
- #endif /* USE_READLINE */
-
- #ifdef NO_POSIX_STRFUNCS
- char *compat_strstr(d, s)
- char *d, *s;
- {
- register char *dscan, *sscan;
- register char *pos;
-
- for (pos = d; *pos != 0; pos++) {
- for (dscan = pos, sscan = s; *dscan == *sscan && *dscan != '\0'; dscan++, sscan++);
- if (*sscan == '\0')
- return pos;
- }
- return NULL;
- }
- #endif /* NO_POSIX_STRFUNCS */
-