common/safeunistd.h

Go to the documentation of this file.
00001 /* safeunistd.h: <unistd.h>, but with compat. and large file support for MSVC.
00002  *
00003  * Copyright (C) 2007 Olly Betts
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00018  * USA
00019  */
00020 
00021 #ifndef XAPIAN_INCLUDED_SAFEUNISTD_H
00022 #define XAPIAN_INCLUDED_SAFEUNISTD_H
00023 
00024 #ifndef _MSC_VER
00025 # include <unistd.h>
00026 #else
00027 
00028 // sys/types.h has a typedef for off_t so make sure we've seen that before
00029 // we hide it behind a #define.
00030 # include <sys/types.h>
00031 
00032 // MSVC doesn't even HAVE unistd.h - io.h seems the nearest equivalent.
00033 // We also need to do some renaming of functions to get versions which
00034 // work on large files.
00035 # include <io.h>
00036 
00037 # ifdef lseek
00038 #  undef lseek
00039 # endif
00040 
00041 # ifdef off_t
00042 #  undef off_t
00043 # endif
00044 
00045 # define lseek(FD, OFF, WHENCE) _lseeki64(FD, OFF, WHENCE)
00046 # define off_t __int64
00047 
00048 // process.h is needed for getpid().
00049 # include <process.h>
00050 
00051 #endif
00052 
00053 #ifdef __WIN32__
00054 
00055 inline unsigned int
00056 sleep(unsigned int seconds)
00057 {
00058     // Use our own little helper function to avoid pulling in <windows.h>.
00059     extern void xapian_sleep_milliseconds(unsigned int millisecs);
00060 
00061     // Sleep takes a time interval in milliseconds, whereas POSIX sleep takes
00062     // a time interval in seconds, so we need to multiply 'seconds' by 1000.
00063     //
00064     // But make sure the multiplication won't overflow!  4294967 seconds is
00065     // nearly 50 days, so just sleep for that long and return the number of
00066     // seconds left to sleep for.  The common case of sleep(CONSTANT) should
00067     // optimise to just xapian_sleep_milliseconds(CONSTANT).
00068     if (seconds > 4294967u) {
00069         xapian_sleep_milliseconds(4294967000u);
00070         return seconds - 4294967u;
00071     }
00072     xapian_sleep_milliseconds(seconds * 1000u);
00073     return 0;
00074 }
00075 
00076 #endif
00077 
00078 #endif /* XAPIAN_INCLUDED_SAFEUNISTD_H */

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.