home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / osdep / nt / yunchan.c < prev    next >
C/C++ Source or Header  |  1998-09-16  |  7KB  |  218 lines

  1. /*
  2.  * Program:    Unix compatibility routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    14 September 1996
  13.  * Last Edited:    4 June 1998
  14.  *
  15.  * Copyright 1998 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made available
  24.  * "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. /*                DEDICATION
  38.  *
  39.  *  This file is dedicated to my dog, Unix, also known as Yun-chan and
  40.  * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
  41.  * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
  42.  * a two-month bout with cirrhosis of the liver.
  43.  *
  44.  *  He was a dear friend, and I miss him terribly.
  45.  *
  46.  *  Lift a leg, Yunie.  Luv ya forever!!!!
  47.  */
  48.  
  49. /* Emulator for BSD flock() call
  50.  * Accepts: file descriptor
  51.  *        operation bitmask
  52.  * Returns: 0 if successful, -1 if failure
  53.  */
  54.  
  55. /*  Our friends in Redmond have decided that you can not write to any segment
  56.  * which has a shared lock.  This screws up the shared-write mailbox drivers
  57.  * (mbx, mtx, and tenex).  As a workaround, we'll only lock the first byte of
  58.  * the file, meaning that you can't write that byte shared.
  59.  *  This behavior seems to be new as of NT 4.0.
  60.  */
  61.  
  62. int flock (int fd,int op)
  63. {
  64.   HANDLE hdl = (HANDLE) _get_osfhandle (fd);
  65.   DWORD flags = (op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0;
  66.   OVERLAPPED offset = {NIL,NIL,0,0,NIL};
  67.   if (hdl < 0) {        /* error in file descriptor */
  68.     errno = EBADF;
  69.     return -1;
  70.   }
  71.   switch (op & ~LOCK_NB) {    /* translate to LockFileEx() op */
  72.   case LOCK_EX:            /* exclusive */
  73.     flags |= LOCKFILE_EXCLUSIVE_LOCK;
  74.   case LOCK_SH:            /* shared */
  75.     if (!check_nt ()) return 0;    /* always succeeds if not NT */
  76.                 /* bug for bug compatible with Unix */
  77.     UnlockFileEx (hdl,NIL,1,0,&offset);
  78.                 /* lock the file as requested */
  79.     if (LockFileEx (hdl,flags,NIL,1,0,&offset)) return 0;
  80.                 /* failed */
  81.     errno = (op & LOCK_NB) ? EAGAIN : EBADF;
  82.     break;
  83.   case LOCK_UN:            /* unlock */
  84.     if (check_nt ()) UnlockFileEx (hdl,NIL,1,0,&offset);
  85.     return 0;            /* always succeeds */
  86.   default:            /* default */
  87.     errno = EINVAL;        /* bad call */
  88.     break;
  89.   }
  90.   return -1;
  91. }
  92.  
  93. /* Local storage */
  94.  
  95. static char *loghdr;        /* log file header string */
  96. static HANDLE loghdl = NIL;    /* handle of event source */
  97.  
  98. /* Emulator for BSD syslog() routine
  99.  * Accepts: priority
  100.  *        message
  101.  *        parameters
  102.  */
  103.  
  104. void syslog (int priority,const char *message,...)
  105. {
  106.   va_list args;
  107.   LPTSTR strs[2];
  108.   char tmp[MAILTMPLEN];        /* callers must be careful not to pop this */
  109.   unsigned short etype;
  110.   if (!check_nt ()) return;    /* no-op on non-NT system */
  111.                 /* default event source */
  112.   if (!loghdl) openlog ("c-client",LOG_PID,LOG_MAIL);
  113.   switch (priority) {        /* translate UNIX type into NT type */
  114.   case LOG_ALERT:
  115.     etype = EVENTLOG_ERROR_TYPE;
  116.     break;
  117.   case LOG_INFO:
  118.     etype = EVENTLOG_INFORMATION_TYPE;
  119.     break;
  120.   default:
  121.     etype = EVENTLOG_WARNING_TYPE;
  122.   }
  123.   va_start (args,message);    /* initialize vararg mechanism */
  124.   vsprintf (tmp,message,args);    /* build message */
  125.   strs[0] = loghdr;        /* write header */
  126.   strs[1] = tmp;        /* then the message */
  127.                 /* report the event */
  128.   ReportEvent (loghdl,etype,(unsigned short) priority,2000,NIL,2,0,strs,NIL);
  129.   va_end (args);
  130. }
  131.  
  132.  
  133. /* Emulator for BSD openlog() routine
  134.  * Accepts: identity
  135.  *        options
  136.  *        facility
  137.  */
  138.  
  139. void openlog (const char *ident,int logopt,int facility)
  140. {
  141.   char tmp[MAILTMPLEN];
  142.   if (!check_nt ()) return;    /* no-op on non-NT system */
  143.   if (loghdl) fatal ("Duplicate openlog()!");
  144.   loghdl = RegisterEventSource (NIL,ident);
  145.   sprintf (tmp,(logopt & LOG_PID) ? "%s[%d]" : "%s",ident,getpid ());
  146.   loghdr = cpystr (tmp);    /* save header for later */
  147.   setmode (0,O_BINARY);        /* make the stdio mode be binary */
  148.   setmode (1,O_BINARY);        /* make the stdio mode be binary */
  149.   setmode (2,O_BINARY);        /* make the stdio mode be binary */
  150. }
  151.  
  152. /* Copy Unix string with CRLF newlines
  153.  * Accepts: destination string
  154.  *        pointer to size of destination string buffer
  155.  *        source string
  156.  *        length of source string
  157.  * Returns: length of copied string
  158.  */
  159.  
  160. unsigned long unix_crlfcpy (char **dst,unsigned long *dstl,char *src,
  161.                 unsigned long srcl)
  162. {
  163.   unsigned long i,j;
  164.   char *d = src;
  165.                 /* count number of LF's in source string(s) */
  166.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  167.                 /* flush destination buffer if too small */
  168.   if (*dst && (i > *dstl)) fs_give ((void **) dst);
  169.   if (!*dst) {            /* make a new buffer if needed */
  170.     *dst = (char *) fs_get ((*dstl = i) + 1);
  171.     if (dstl) *dstl = i;    /* return new buffer length to main program */
  172.   }
  173.   d = *dst;            /* destination string */
  174.                 /* copy strings, inserting CR's before LF's */
  175.   while (srcl--) switch (*src) {
  176.   case '\015':            /* unlikely carriage return */
  177.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  178.     if (srcl && *src == '\012') {
  179.       *d++ = *src++;
  180.       srcl--;
  181.     }
  182.     break;
  183.   case '\012':            /* line feed? */
  184.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  185.   default:            /* ordinary chararacter */
  186.     *d++ = *src++;        /* just copy character */
  187.     break;
  188.   }
  189.   *d = '\0';            /* tie off destination */
  190.   return d - *dst;        /* return length */
  191. }
  192.  
  193. /* Length of Unix string after unix_crlfcpy applied
  194.  * Accepts: source string
  195.  * Returns: length of string
  196.  */
  197.  
  198. unsigned long unix_crlflen (STRING *s)
  199. {
  200.   unsigned long pos = GETPOS (s);
  201.   unsigned long i = SIZE (s);
  202.   unsigned long j = i;
  203.   while (j--) switch (SNX (s)) {/* search for newlines */
  204.   case '\015':            /* unlikely carriage return */
  205.     if (j && (CHR (s) == '\012')) {
  206.       SNX (s);            /* eat the line feed */
  207.       j--;
  208.     }
  209.     break;
  210.   case '\012':            /* line feed? */
  211.     i++;
  212.   default:            /* ordinary chararacter */
  213.     break;
  214.   }
  215.   SETPOS (s,pos);        /* restore old position */
  216.   return i;
  217. }
  218.