diff -uwrN aria2-0.11.1+1.orig/configure.ac aria2-0.11.1+1/configure.ac --- aria2-0.11.1+1.orig/configure.ac 2007-07-10 04:53:44.000000000 -0700 +++ aria2-0.11.1+1/configure.ac 2007-07-18 16:47:10.000000000 -0700 @@ -3,11 +3,20 @@ # AC_PREREQ(2.59) AC_INIT(aria2c, 0.11.1+1, t-tujikawa@users.sourceforge.net) +AC_CANONICAL_HOST +AC_CANONICAL_SYSTEM AM_INIT_AUTOMAKE() AM_PATH_CPPUNIT(1.10.2) AC_CONFIG_SRCDIR([src/Socket.h]) AC_CONFIG_HEADERS([config.h]) +case "$target" in + *mingw*|*cygwin*) + WINSOCK_LIBS="-lws2_32" + AC_SUBST(WINSOCK_LIBS) + ;; +esac + # Set localedir localedir=${datadir}/locale AC_SUBST(localedir) @@ -102,7 +111,7 @@ # Checks for header files. AC_FUNC_ALLOCA #AC_HEADER_STDC -AC_CHECK_HEADERS([argz.h arpa/inet.h fcntl.h inttypes.h langinfo.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdio_ext.h stdlib.h string.h strings.h sys/param.h sys/socket.h sys/time.h unistd.h]) +AC_CHECK_HEADERS([winsock2.h ws2tcpip.h argz.h arpa/inet.h fcntl.h inttypes.h io.h langinfo.h libgen.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdio_ext.h stdlib.h string.h strings.h sys/param.h sys/socket.h sys/time.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL @@ -125,7 +134,21 @@ AC_FUNC_SELECT_ARGTYPES AC_FUNC_STAT AC_FUNC_VPRINTF -AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify ftruncate getcwd getpagesize gettimeofday inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo rmdir select setlocale socket stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul]) +AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify daemon ftruncate getcwd getpagesize gettimeofday inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo random rmdir select setlocale setmode sigaction sleep socket srandom stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul timegm usleep]) + +AC_CHECK_FUNCS([basename], [AM_CONDITIONAL([HAVE_BASENAME], true)], [AM_CONDITIONAL([HAVE_BASENAME], false)]) +AC_CHECK_FUNCS([getaddrinfo], [AM_CONDITIONAL([HAVE_GETADDRINFO], true)], [AM_CONDITIONAL([HAVE_GETADDRINFO], false)]) +AC_CHECK_FUNCS([inet_aton], [AM_CONDITIONAL([HAVE_INET_ATON], true)], [AM_CONDITIONAL([HAVE_INET_ATON], false)]) +AC_CHECK_FUNCS([localtime_r], [AM_CONDITIONAL([HAVE_LOCALTIME_R], true)], [AM_CONDITIONAL([HAVE_LOCALTIME_R], false)]) +AC_CHECK_FUNCS([strptime], [AM_CONDITIONAL([HAVE_STRPTIME], true)], [AM_CONDITIONAL([HAVE_STRPTIME], false)]) + +case "$target" in + *mingw*) + dnl true if _WIN32_WINNT >= 0x0501 + AM_CONDITIONAL([HAVE_GETADDRINFO], true) + ;; +esac + AC_CONFIG_FILES([Makefile src/Makefile test/Makefile diff -uwrN aria2-0.11.1+1.orig/lib/libgen.h aria2-0.11.1+1/lib/libgen.h --- aria2-0.11.1+1.orig/lib/libgen.h 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/lib/libgen.h 2007-07-19 19:52:48.133125000 -0700 @@ -0,0 +1,60 @@ +/* */ + +#ifndef _D_LIBGEN_H +#define _D_LIBGEN_H 1 + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef __MINGW32__ + +char *basename (char *path); +char *dirname (char *path); + +#else + +char *basename (const char *path); +char *dirname (const char *path); + +#endif // __MINGW32__ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* not _D_LIBGEN_H */ + diff -uwrN aria2-0.11.1+1.orig/reconf aria2-0.11.1+1/reconf --- aria2-0.11.1+1.orig/reconf 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/reconf 2007-07-14 22:49:10.950875000 -0700 @@ -0,0 +1,15 @@ +#!/bin/sh +# +# $Id$ +# +# re autoconf/automake shell script +# + +die(){ + echo "$@" ; exit +} + +aclocal -I . -I m4 || die "The command 'aclocal -I .' failed" +autoheader || die "The command 'autoheader' failed" +automake || die "The command 'automake' failed" +autoconf || die "The command 'autoconf' failed" diff -uwrN aria2-0.11.1+1.orig/src/AbstractDiskWriter.cc aria2-0.11.1+1/src/AbstractDiskWriter.cc --- aria2-0.11.1+1.orig/src/AbstractDiskWriter.cc 2007-05-22 09:55:56.000000000 -0700 +++ aria2-0.11.1+1/src/AbstractDiskWriter.cc 2007-07-14 22:49:10.950875000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "AbstractDiskWriter.h" #include "DlAbortEx.h" #include "File.h" @@ -79,7 +80,7 @@ throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), "file not found"); } - if((fd = open(filename.c_str(), O_RDWR, OPEN_MODE)) < 0) { + if((fd = open(filename.c_str(), O_RDWR|O_BINARY, OPEN_MODE)) < 0) { throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); } if(f.size() < totalLength) { @@ -99,7 +100,7 @@ // if(filename.empty()) { // filename = "index.html"; // } - if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|addFlags, OPEN_MODE)) < 0) { + if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags, OPEN_MODE)) < 0) { throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); } } diff -uwrN aria2-0.11.1+1.orig/src/BitfieldMan.cc aria2-0.11.1+1/src/BitfieldMan.cc --- aria2-0.11.1+1.orig/src/BitfieldMan.cc 2007-07-09 05:54:19.000000000 -0700 +++ aria2-0.11.1+1/src/BitfieldMan.cc 2007-07-14 22:49:10.950875000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "BitfieldMan.h" #include "Util.h" #include diff -uwrN aria2-0.11.1+1.orig/src/BtPieceMessage.cc aria2-0.11.1+1/src/BtPieceMessage.cc --- aria2-0.11.1+1.orig/src/BtPieceMessage.cc 2007-06-12 03:50:40.000000000 -0700 +++ aria2-0.11.1+1/src/BtPieceMessage.cc 2007-07-14 22:49:11.029000000 -0700 @@ -73,7 +73,7 @@ PieceHandle piece = pieceStorage->getPiece(index); int64_t offset = ((int64_t)index)*btContext->getPieceLength()+begin; - logger->debug("CUID#%d - Piece received. index=%d, begin=%d, length=%d, offset=%llu, blockIndex=%d", + logger->debug("CUID#%d - Piece received. index=%d, begin=%d, length=%d, offset=" ULONGLONG_PRINTF ", blockIndex=%d", cuid, index, begin, blockLength, offset, slot.getBlockIndex()); pieceStorage->getDiskAdaptor()->writeData(block, blockLength, diff -uwrN aria2-0.11.1+1.orig/src/ByteArrayDiskWriter.cc aria2-0.11.1+1/src/ByteArrayDiskWriter.cc --- aria2-0.11.1+1.orig/src/ByteArrayDiskWriter.cc 2007-06-09 03:06:53.000000000 -0700 +++ aria2-0.11.1+1/src/ByteArrayDiskWriter.cc 2007-07-14 22:49:11.107125000 -0700 @@ -77,7 +77,7 @@ buf.write(data, dataLength); } -int ByteArrayDiskWriter::readData(char* data, int32_t len, int64_t position) { +int32_t ByteArrayDiskWriter::readData(char* data, int32_t len, int64_t position) { buf.seekg(position, ios_base::beg); buf.read(data, len); // TODO we have to call buf.clear() here? YES diff -uwrN aria2-0.11.1+1.orig/src/ByteArrayDiskWriter.h aria2-0.11.1+1/src/ByteArrayDiskWriter.h --- aria2-0.11.1+1.orig/src/ByteArrayDiskWriter.h 2007-06-09 03:06:53.000000000 -0700 +++ aria2-0.11.1+1/src/ByteArrayDiskWriter.h 2007-07-14 22:49:11.169625000 -0700 @@ -57,7 +57,7 @@ // position is ignored virtual void writeData(const char* data, int32_t len, int64_t position = 0); - virtual int readData(char* data, int32_t len, int64_t position); + virtual int32_t readData(char* data, int32_t len, int64_t position); // Not implemented yet virtual void truncate(int64_t length) {} diff -uwrN aria2-0.11.1+1.orig/src/CompactPeerListProcessor.cc aria2-0.11.1+1/src/CompactPeerListProcessor.cc --- aria2-0.11.1+1.orig/src/CompactPeerListProcessor.cc 2006-10-01 04:29:14.000000000 -0700 +++ aria2-0.11.1+1/src/CompactPeerListProcessor.cc 2007-07-14 22:49:11.216500000 -0700 @@ -31,9 +31,13 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "CompactPeerListProcessor.h" #include "Data.h" + +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H bool CompactPeerListProcessor::canHandle(const MetaEntry* peersEntry) const { return dynamic_cast(peersEntry) != 0; diff -uwrN aria2-0.11.1+1.orig/src/DefaultBtContext.cc aria2-0.11.1+1/src/DefaultBtContext.cc --- aria2-0.11.1+1.orig/src/DefaultBtContext.cc 2007-03-26 05:16:57.000000000 -0700 +++ aria2-0.11.1+1/src/DefaultBtContext.cc 2007-07-14 22:49:11.310250000 -0700 @@ -180,7 +180,7 @@ // retrieve infoHash ShaVisitor v; infoDic->accept(&v); - int32_t len; + int len; v.getHash(infoHash, len); infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH); // calculate the number of pieces diff -uwrN aria2-0.11.1+1.orig/src/DefaultBtProgressInfoFile.cc aria2-0.11.1+1/src/DefaultBtProgressInfoFile.cc --- aria2-0.11.1+1.orig/src/DefaultBtProgressInfoFile.cc 2006-11-28 03:33:43.000000000 -0800 +++ aria2-0.11.1+1/src/DefaultBtProgressInfoFile.cc 2007-07-14 22:49:11.310250000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "DefaultBtProgressInfoFile.h" #include "BtRegistry.h" #include "LogFactory.h" @@ -42,6 +43,10 @@ #include "Util.h" #include +#ifdef HAVE_IO_H +# include +#endif // HAVE_IO_H + DefaultBtProgressInfoFile::DefaultBtProgressInfoFile(const BtContextHandle& btContext, const Option* option): btContext(btContext), @@ -59,7 +64,7 @@ void DefaultBtProgressInfoFile::save() { logger->info(MSG_SAVING_SEGMENT_FILE, filename.c_str()); - FILE* file = openFile(filename, "w"); + FILE* file = openFile(filename, "wb"); try { if(fwrite(btContext->getInfoHash(), btContext->getInfoHashLength(), 1, file) < 1) { @@ -93,7 +98,7 @@ void DefaultBtProgressInfoFile::load() { logger->info(MSG_LOADING_SEGMENT_FILE, filename.c_str()); - FILE* file = openFile(filename, "r+"); + FILE* file = openFile(filename, "r+b"); unsigned char* savedInfoHash = 0; unsigned char* savedBitfield = 0; try { @@ -161,6 +166,9 @@ throw new DlAbortEx(EX_SEGMENT_FILE_OPEN, filename.c_str(), strerror(errno)); } +#ifdef HAVE_SETMODE + setmode(fileno(file), O_BINARY); +#endif return file; } diff -uwrN aria2-0.11.1+1.orig/src/DefaultPieceStorage.cc aria2-0.11.1+1/src/DefaultPieceStorage.cc --- aria2-0.11.1+1.orig/src/DefaultPieceStorage.cc 2007-03-26 05:16:57.000000000 -0700 +++ aria2-0.11.1+1/src/DefaultPieceStorage.cc 2007-07-14 22:49:11.388375000 -0700 @@ -372,11 +372,11 @@ } void DefaultPieceStorage::setBitfield(const unsigned char* bitfield, - int bitfieldLength) { + int32_t bitfieldLength) { bitfieldMan->setBitfield(bitfield, bitfieldLength); } -int DefaultPieceStorage::getBitfieldLength() { +int32_t DefaultPieceStorage::getBitfieldLength() { return bitfieldMan->getBitfieldLength(); } @@ -388,16 +388,16 @@ return diskAdaptor; } -int DefaultPieceStorage::getPieceLength(int index) { +int32_t DefaultPieceStorage::getPieceLength(int32_t index) { return bitfieldMan->getBlockLength(index); } -void DefaultPieceStorage::advertisePiece(int cuid, int index) { +void DefaultPieceStorage::advertisePiece(int32_t cuid, int32_t index) { HaveEntry entry(cuid, index); haves.push_front(entry); } -Integers DefaultPieceStorage::getAdvertisedPieceIndexes(int myCuid, +Integers DefaultPieceStorage::getAdvertisedPieceIndexes(int32_t myCuid, const Time& lastCheckTime) { Integers indexes; for(Haves::const_iterator itr = haves.begin(); itr != haves.end(); itr++) { @@ -429,7 +429,7 @@ } }; -void DefaultPieceStorage::removeAdvertisedPiece(int elapsed) { +void DefaultPieceStorage::removeAdvertisedPiece(int32_t elapsed) { Haves::iterator itr = find_if(haves.begin(), haves.end(), FindElapsedHave(elapsed)); if(itr != haves.end()) { diff -uwrN aria2-0.11.1+1.orig/src/DefaultPieceStorage.h aria2-0.11.1+1/src/DefaultPieceStorage.h --- aria2-0.11.1+1.orig/src/DefaultPieceStorage.h 2007-01-28 06:18:35.000000000 -0800 +++ aria2-0.11.1+1/src/DefaultPieceStorage.h 2007-07-14 22:49:11.404000000 -0700 @@ -123,9 +123,9 @@ virtual bool allDownloadFinished(); virtual void setBitfield(const unsigned char* bitfield, - int bitfieldLength); + int32_t bitfieldLength); - virtual int getBitfieldLength(); + virtual int32_t getBitfieldLength(); virtual const unsigned char* getBitfield(); @@ -145,14 +145,14 @@ virtual DiskAdaptorHandle getDiskAdaptor(); - virtual int getPieceLength(int index); + virtual int32_t getPieceLength(int32_t index); - virtual void advertisePiece(int cuid, int index); + virtual void advertisePiece(int32_t cuid, int32_t index); - virtual Integers getAdvertisedPieceIndexes(int myCuid, + virtual Integers getAdvertisedPieceIndexes(int32_t myCuid, const Time& lastCheckTime); - virtual void removeAdvertisedPiece(int elapsed); + virtual void removeAdvertisedPiece(int32_t elapsed); virtual void markAllPiecesDone(); diff -uwrN aria2-0.11.1+1.orig/src/Directory.cc aria2-0.11.1+1/src/Directory.cc --- aria2-0.11.1+1.orig/src/Directory.cc 2007-05-22 09:55:56.000000000 -0700 +++ aria2-0.11.1+1/src/Directory.cc 2007-07-14 22:49:11.404000000 -0700 @@ -39,6 +39,7 @@ #include #include #include +#include Directory::Directory(const string& name):name(name) {} @@ -61,7 +62,11 @@ throw new DlAbortEx(EX_NOT_DIRECTORY, path.c_str()); } } else { +#ifdef __MINGW32__ + if(mkdir(path.c_str()) == -1) { +#else if(mkdir(path.c_str(), OPEN_MODE) == -1) { +#endif // __MINGW32__ throw new DlAbortEx(EX_MAKE_DIR, path.c_str(), strerror(errno)); } } diff -uwrN aria2-0.11.1+1.orig/src/DiskWriter.h aria2-0.11.1+1/src/DiskWriter.h --- aria2-0.11.1+1.orig/src/DiskWriter.h 2007-05-20 06:51:52.000000000 -0700 +++ aria2-0.11.1+1/src/DiskWriter.h 2007-07-14 22:49:11.435250000 -0700 @@ -87,8 +87,8 @@ writeData((const char*)data, len, position); } - virtual int readData(char* data, int32_t len, int64_t position) = 0; - virtual int readData(unsigned char* data, int32_t len, int64_t position) { + virtual int32_t readData(char* data, int32_t len, int64_t position) = 0; + virtual int32_t readData(unsigned char* data, int32_t len, int64_t position) { return readData((char*)data, len, position); } #ifdef ENABLE_MESSAGE_DIGEST diff -uwrN aria2-0.11.1+1.orig/src/DownloadCommand.cc aria2-0.11.1+1/src/DownloadCommand.cc --- aria2-0.11.1+1.orig/src/DownloadCommand.cc 2007-07-04 09:04:57.000000000 -0700 +++ aria2-0.11.1+1/src/DownloadCommand.cc 2007-07-14 22:49:11.450875000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "DownloadCommand.h" #include "Util.h" #include "DlRetryEx.h" @@ -44,6 +45,7 @@ # include "ChecksumCommand.h" #endif // ENABLE_MESSAGE_DIGEST #include +#include DownloadCommand::DownloadCommand(int cuid, const RequestHandle req, @@ -71,11 +73,15 @@ // TODO we need to specify the sum of all segmentMan's download speed here. if(maxDownloadSpeedLimit > 0 && maxDownloadSpeedLimit < _requestGroup->getSegmentMan()->calculateDownloadSpeed()) { +#ifdef HAVE_USLEEP usleep(1); +#else + _sleep(1); +#endif // HAVE_USLEEP e->commands.push_back(this); return false; } - int32_t bufSize = 16*1024; + int bufSize = 16*1024; char buf[bufSize]; socket->readData(buf, bufSize); @@ -85,7 +91,7 @@ segment->writtenLength += bufSize; peerStat->updateDownloadLength(bufSize); } else { - int32_t infbufSize = 16*1024; + int infbufSize = 16*1024; char infbuf[infbufSize]; transferDecoder->inflate(infbuf, infbufSize, buf, bufSize); _requestGroup->getSegmentMan()->diskWriter->writeData(infbuf, infbufSize, diff -uwrN aria2-0.11.1+1.orig/src/DownloadEngine.h aria2-0.11.1+1/src/DownloadEngine.h --- aria2-0.11.1+1.orig/src/DownloadEngine.h 2007-07-04 09:04:57.000000000 -0700 +++ aria2-0.11.1+1/src/DownloadEngine.h 2007-07-14 22:49:11.513375000 -0700 @@ -35,10 +35,11 @@ #ifndef _D_DOWNLOAD_ENGINE_H_ #define _D_DOWNLOAD_ENGINE_H_ +#include "common.h" +#include "common.h" #include "Command.h" #include "Socket.h" #include "SegmentMan.h" -#include "common.h" #include "Logger.h" #include "Option.h" #include "NameResolver.h" diff -uwrN aria2-0.11.1+1.orig/src/File.cc aria2-0.11.1+1/src/File.cc --- aria2-0.11.1+1.orig/src/File.cc 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/File.cc 2007-07-14 22:49:11.513375000 -0700 @@ -68,6 +68,14 @@ return S_ISDIR(fstat.st_mode) == 1; } +bool File::isDir(const string& name) { + struct stat fstat; + if(stat(name.c_str(), &fstat) < 0) { + return false; + } + return S_ISDIR(fstat.st_mode) == 1; +} + bool File::remove() { if(isFile()) { return unlink(name.c_str()) == 0; @@ -106,7 +114,11 @@ if(File(accDir).isDir()) { continue; } +#ifdef __MINGW32__ + if(mkdir(accDir.c_str()) == -1) { +#else if(mkdir(accDir.c_str(), DIR_OPEN_MODE) == -1) { +#endif // __MINGW32__ return false; } } diff -uwrN aria2-0.11.1+1.orig/src/File.h aria2-0.11.1+1/src/File.h --- aria2-0.11.1+1.orig/src/File.h 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/File.h 2007-07-14 22:49:11.591500000 -0700 @@ -40,6 +40,43 @@ #include #include +#ifndef S_IRUSR +# define S_IRUSR 0000400 /* read permission, owner */ +#endif /* S_IRUSR */ +#ifndef S_IWUSR +# define S_IWUSR 0000200 /* write permission, owner */ +#endif /* S_IWUSR */ +#ifndef S_IXUSR +# define S_IXUSR 0000100/* execute/search permission, owner */ +#endif /* S_IXUSR */ +#ifndef S_IRWXU +# define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR) +#endif /* S_IRWXU */ +#ifndef S_IRGRP +# define S_IRGRP 0000040 /* read permission, group */ +#endif /* S_IRGRP */ +#ifndef S_IWGRP +# define S_IWGRP 0000020 /* write permission, grougroup */ +#endif /* S_IWGRP */ +#ifndef S_IXGRP +# define S_IXGRP 0000010/* execute/search permission, group */ +#endif /* S_IXGRP */ +#ifndef S_IRWXG +# define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP) +#endif /* S_IRWXG */ +#ifndef S_IROTH +# define S_IROTH 0000004 /* read permission, other */ +#endif /* S_IROTH */ +#ifndef S_IWOTH +# define S_IWOTH 0000002 /* write permission, other */ +#endif /* S_IWOTH */ +#ifndef S_IXOTH +# define S_IXOTH 0000001/* execute/search permission, other */ +#endif /* S_IXOTH */ +#ifndef S_IRWXO +# define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH) +#endif /* S_IRWXO */ + using namespace std; /** @@ -69,6 +106,11 @@ bool isDir(); /** + * Tests whether the file denoted by name is a directory. + */ + static bool isDir(const string& name); + + /** * Deletes the file or directory denoted by name. * If name denotes a directory, it must be empty in order to delete. */ diff -uwrN aria2-0.11.1+1.orig/src/FileEntry.cc aria2-0.11.1+1/src/FileEntry.cc --- aria2-0.11.1+1.orig/src/FileEntry.cc 2006-11-05 07:04:17.000000000 -0800 +++ aria2-0.11.1+1/src/FileEntry.cc 2007-07-18 17:10:32.000000000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ + #include "FileEntry.h" #include "File.h" #include "DlAbortEx.h" diff -uwrN aria2-0.11.1+1.orig/src/FtpConnection.cc aria2-0.11.1+1/src/FtpConnection.cc --- aria2-0.11.1+1.orig/src/FtpConnection.cc 2007-03-21 03:19:23.000000000 -0700 +++ aria2-0.11.1+1/src/FtpConnection.cc 2007-07-14 22:49:11.591500000 -0700 @@ -201,7 +201,7 @@ pair response; if(bulkReceiveResponse(response)) { if(response.first == 213) { - sscanf(response.second.c_str(), "%*d %Ld", &size); + sscanf(response.second.c_str(), "%*d " LONGLONG_SCANF, &size); } return response.first; } else { diff -uwrN aria2-0.11.1+1.orig/src/HttpConnection.cc aria2-0.11.1+1/src/HttpConnection.cc --- aria2-0.11.1+1.orig/src/HttpConnection.cc 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/HttpConnection.cc 2007-07-16 07:24:54.327250000 -0700 @@ -91,7 +91,7 @@ HttpHeaderProcessorHandle proc = entry->getHttpHeaderProcessor(); char buf[512]; - int32_t size = sizeof(buf); + int size = sizeof(buf); socket->peekData(buf, size); if(size == 0) { throw new DlRetryEx(EX_INVALID_RESPONSE); diff -uwrN aria2-0.11.1+1.orig/src/HttpConnection.h aria2-0.11.1+1/src/HttpConnection.h --- aria2-0.11.1+1.orig/src/HttpConnection.h 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/HttpConnection.h 2007-07-15 08:17:31.607125000 -0700 @@ -44,9 +44,12 @@ #include "Logger.h" #include "HttpResponse.h" #include "HttpHeaderProcessor.h" -#include #include +#ifdef HAVE_NETINET_IN_H +#include +#endif // HAVE_NETINET_IN_H + class HttpRequestEntry { private: HttpRequestHandle _httpRequest; diff -uwrN aria2-0.11.1+1.orig/src/HttpRequest.h aria2-0.11.1+1/src/HttpRequest.h --- aria2-0.11.1+1.orig/src/HttpRequest.h 2007-03-18 08:42:34.000000000 -0700 +++ aria2-0.11.1+1/src/HttpRequest.h 2007-07-14 22:49:11.607125000 -0700 @@ -40,7 +40,9 @@ #include "Range.h" #include "Request.h" #include "Option.h" +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H class HttpRequest { private: diff -uwrN aria2-0.11.1+1.orig/src/HttpResponse.cc aria2-0.11.1+1/src/HttpResponse.cc --- aria2-0.11.1+1.orig/src/HttpResponse.cc 2007-06-10 00:55:43.000000000 -0700 +++ aria2-0.11.1+1/src/HttpResponse.cc 2007-07-14 22:49:11.669625000 -0700 @@ -57,7 +57,9 @@ // compare the received range against the requested range RangeHandle responseRange = httpHeader->getRange(); if(!httpRequest->isRangeSatisfied(responseRange)) { - throw new DlRetryEx("Invalid range header. Request: %lld-%lld/%lld, Response: %lld-%lld/%lld", + throw new DlRetryEx("Invalid range header. Request: " + LONGLONG_PRINTF "-" LONGLONG_PRINTF "/" LONGLONG_PRINTF ", Response: " + LONGLONG_PRINTF "-" LONGLONG_PRINTF "/" LONGLONG_PRINTF, httpRequest->getStartByte(), httpRequest->getEndByte(), httpRequest->getEntityLength(), diff -uwrN aria2-0.11.1+1.orig/src/HttpResponseCommand.cc aria2-0.11.1+1/src/HttpResponseCommand.cc --- aria2-0.11.1+1.orig/src/HttpResponseCommand.cc 2007-06-10 00:55:43.000000000 -0700 +++ aria2-0.11.1+1/src/HttpResponseCommand.cc 2007-07-14 22:49:11.732125000 -0700 @@ -204,7 +204,7 @@ httpResponse->getHttpRequest()->getRequest()->setKeepAlive(false); _requestGroup->getSegmentMan()->isSplittable = false; _requestGroup->getSegmentMan()->downloadStarted = true; - _requestGroup->getSegmentMan()->diskWriter->initAndOpenFile("/tmp/aria2"+Util::itos((int32_t)getpid())); + _requestGroup->getSegmentMan()->diskWriter->initAndOpenFile(Util::getTempDir()+"/aria2"+Util::itos((int32_t)getpid())); e->commands.push_back(createHttpDownloadCommand(httpResponse)); return true; } diff -uwrN aria2-0.11.1+1.orig/src/LogFactory.cc aria2-0.11.1+1/src/LogFactory.cc --- aria2-0.11.1+1.orig/src/LogFactory.cc 2007-05-20 06:51:52.000000000 -0700 +++ aria2-0.11.1+1/src/LogFactory.cc 2007-07-14 22:49:11.732125000 -0700 @@ -32,10 +32,17 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "LogFactory.h" #include "SimpleLogger.h" -string LogFactory::filename = "/dev/null"; +#ifdef HAVE_WINSOCK2_H +# define DEV_NULL "nul" +#else +# define DEV_NULL "/dev/null" +#endif // HAVE_WINSOCK2_H + +string LogFactory::filename = DEV_NULL; Logger* LogFactory::logger = NULL; Logger* LogFactory::getInstance() { diff -uwrN aria2-0.11.1+1.orig/src/Makefile.am aria2-0.11.1+1/src/Makefile.am --- aria2-0.11.1+1.orig/src/Makefile.am 2007-07-07 20:22:37.000000000 -0700 +++ aria2-0.11.1+1/src/Makefile.am 2007-07-18 19:24:40.000000000 -0700 @@ -98,7 +98,8 @@ CookieParser.cc CookieParser.h\ CookieBoxFactory.cc CookieBoxFactory.h\ HttpHeaderProcessor.cc HttpHeaderProcessor.h\ - FileEntry.cc FileEntry.h + FileEntry.cc FileEntry.h\ + Platform.cc Platform.h # debug_new.cpp if ENABLE_MESSAGE_DIGEST @@ -226,11 +227,31 @@ MetalinkRequestInfo.cc MetalinkRequestInfo.h endif # ENABLE_METALINK +if !HAVE_BASENAME +SRCS += libgen.c libgen.h +endif # !HAVE_BASENAME + +if !HAVE_GETADDRINFO +SRCS += getaddrinfo.c getaddrinfo.h +endif # !HAVE_GETADDRINFO + +if !HAVE_INET_ATON +SRCS += inet_aton.c inet_aton.h +endif # !HAVE_INET_ATON + +if !HAVE_LOCALTIME_R +SRCS += localtime_r.c localtime_r.h +endif # !HAVE_LOCALTIME_R + +if !HAVE_STRPTIME +SRCS += strptime.c strptime.h +endif # !HAVE_STRPTIME + noinst_LIBRARIES = libaria2c.a libaria2c_a_SOURCES = $(SRCS) aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\ @LIBGCRYPT_LIBS@ @OPENSSL_LIBS@ @XML_LIBS@ @LIBARES_LIBS@\ - @LIBCARES_LIBS@ # -lprofiler + @LIBCARES_LIBS@ @WINSOCK_LIBS@ # -lprofiler #aria2c_LDFLAGS = #-pg AM_CPPFLAGS = -Wall\ -I../lib -I../intl -I$(top_srcdir)/intl\ diff -uwrN aria2-0.11.1+1.orig/src/MetaFileUtil.cc aria2-0.11.1+1/src/MetaFileUtil.cc --- aria2-0.11.1+1.orig/src/MetaFileUtil.cc 2007-01-11 08:32:31.000000000 -0800 +++ aria2-0.11.1+1/src/MetaFileUtil.cc 2007-07-14 22:49:11.810250000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "MetaFileUtil.h" #include "File.h" #include "DlAbortEx.h" @@ -39,15 +40,22 @@ #include #include +#ifdef HAVE_IO_H +# include +#endif // HAVE_IO_H + MetaEntry* MetaFileUtil::parseMetaFile(const string& file) { File f(file); int len = f.size(); char* buf = new char[len]; - FILE* fp = fopen(file.c_str(), "r+"); + FILE* fp = fopen(file.c_str(), "r+b"); try { if(fp == NULL) { throw new DlAbortEx("cannot open metainfo file"); } +#ifdef HAVE_SETMODE + setmode(fileno(fp), O_BINARY); +#endif if(fread(buf, len, 1, fp) != 1) { fclose(fp); throw new DlAbortEx("cannot read metainfo"); diff -uwrN aria2-0.11.1+1.orig/src/MultiDiskAdaptor.cc aria2-0.11.1+1/src/MultiDiskAdaptor.cc --- aria2-0.11.1+1.orig/src/MultiDiskAdaptor.cc 2007-01-25 08:47:29.000000000 -0800 +++ aria2-0.11.1+1/src/MultiDiskAdaptor.cc 2007-07-14 22:49:11.810250000 -0700 @@ -145,7 +145,7 @@ return length; } -int MultiDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t offset) +int32_t MultiDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t offset) { int64_t fileOffset = offset; bool reading = false; diff -uwrN aria2-0.11.1+1.orig/src/MultiDiskAdaptor.h aria2-0.11.1+1/src/MultiDiskAdaptor.h --- aria2-0.11.1+1.orig/src/MultiDiskAdaptor.h 2007-03-24 08:32:40.000000000 -0700 +++ aria2-0.11.1+1/src/MultiDiskAdaptor.h 2007-07-14 22:49:11.810250000 -0700 @@ -115,10 +115,13 @@ int64_t fileOffset, int32_t rem) const; +#if defined(HAVE_LIBSSL) || defined(HAVE_LIBGCRYPT) void hashUpdate(MessageDigestContext& ctx, const DiskWriterEntryHandle& entry, int64_t offset, int64_t length); +#endif /* defined(HAVE_LIBSSL) || defined(HAVE_LIBGCRYPT) */ + string getTopDirPath() const; public: MultiDiskAdaptor():pieceLength(0), @@ -140,11 +143,14 @@ virtual void writeData(const unsigned char* data, int32_t len, int64_t offset); - virtual int readData(unsigned char* data, int32_t len, int64_t offset); + virtual int32_t readData(unsigned char* data, int32_t len, int64_t offset); +#if defined(HAVE_LIBSSL) || defined(HAVE_LIBGCRYPT) virtual string messageDigest(int64_t offset, int64_t length, const MessageDigestContext::DigestAlgo& algo); +#endif /* defined(HAVE_LIBSSL) || defined(HAVE_LIBGCRYPT) */ + virtual bool fileExists(); virtual string getFilePath() { diff -uwrN aria2-0.11.1+1.orig/src/NameResolver.cc aria2-0.11.1+1/src/NameResolver.cc --- aria2-0.11.1+1.orig/src/NameResolver.cc 2007-03-28 08:08:22.000000000 -0700 +++ aria2-0.11.1+1/src/NameResolver.cc 2007-07-14 22:49:11.841500000 -0700 @@ -34,6 +34,10 @@ /* copyright --> */ #include "NameResolver.h" +#ifndef HAVE_GETADDRINFO +# include "getaddrinfo.h" +#endif // HAVE_GETADDRINFO + #ifdef ENABLE_ASYNC_DNS void callback(void* arg, int status, struct hostent* host) { diff -uwrN aria2-0.11.1+1.orig/src/NameResolver.h aria2-0.11.1+1/src/NameResolver.h --- aria2-0.11.1+1.orig/src/NameResolver.h 2007-03-28 08:08:22.000000000 -0700 +++ aria2-0.11.1+1/src/NameResolver.h 2007-07-14 22:49:11.841500000 -0700 @@ -36,10 +36,23 @@ #define _D_NAME_RESOLVER_H_ #include "common.h" + +#ifdef HAVE_NETDB_H #include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H #include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H #include +#endif // HAVE_ARPA_INET_H + +#ifndef HAVE_INET_ATON +# include "inet_aton.h" +#endif // HAVE_INET_ATON #ifdef ENABLE_ASYNC_DNS diff -uwrN aria2-0.11.1+1.orig/src/OptionHandlerImpl.h aria2-0.11.1+1/src/OptionHandlerImpl.h --- aria2-0.11.1+1.orig/src/OptionHandlerImpl.h 2007-03-26 05:16:57.000000000 -0700 +++ aria2-0.11.1+1/src/OptionHandlerImpl.h 2007-07-14 22:49:11.857125000 -0700 @@ -40,7 +40,9 @@ #include "Util.h" #include "FatalException.h" #include "prefs.h" +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H class NullOptionHandler : public OptionHandler { public: @@ -91,13 +93,13 @@ } else { string msg = _optName+" "; if(_min == -1 && _max != -1) { - msg += _("must be smaller than or equal to %lld."); + msg += _("must be smaller than or equal to " LONGLONG_PRINTF "."); throw new FatalException(msg.c_str(), _max); } else if(_min != -1 && _max != -1) { - msg += _("must be between %lld and %lld."); + msg += _("must be between " LONGLONG_PRINTF " and " LONGLONG_PRINTF "."); throw new FatalException(msg.c_str(), _min, _max); } else if(_min != -1 && _max == -1) { - msg += _("must be greater than or equal to %lld."); + msg += _("must be greater than or equal to " LONGLONG_PRINTF "."); throw new FatalException(msg.c_str(), _min); } else { msg += _("must be a number."); diff -uwrN aria2-0.11.1+1.orig/src/PeerConnection.cc aria2-0.11.1+1/src/PeerConnection.cc --- aria2-0.11.1+1.orig/src/PeerConnection.cc 2007-01-28 06:18:35.000000000 -0800 +++ aria2-0.11.1+1/src/PeerConnection.cc 2007-07-14 22:49:11.857125000 -0700 @@ -39,7 +39,9 @@ #include "Util.h" #include "LogFactory.h" #include "BtHandshakeMessage.h" +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H PeerConnection::PeerConnection(int32_t cuid, const SocketHandle& socket, diff -uwrN aria2-0.11.1+1.orig/src/PeerMessageUtil.cc aria2-0.11.1+1/src/PeerMessageUtil.cc --- aria2-0.11.1+1.orig/src/PeerMessageUtil.cc 2007-01-25 08:47:29.000000000 -0800 +++ aria2-0.11.1+1/src/PeerMessageUtil.cc 2007-07-14 22:49:11.857125000 -0700 @@ -32,10 +32,14 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "PeerMessageUtil.h" #include "DlAbortEx.h" #include "Util.h" + +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H int8_t PeerMessageUtil::getId(const unsigned char* msg) { return msg[0]; diff -uwrN aria2-0.11.1+1.orig/src/PieceStorage.h aria2-0.11.1+1/src/PieceStorage.h --- aria2-0.11.1+1.orig/src/PieceStorage.h 2007-01-28 06:18:35.000000000 -0800 +++ aria2-0.11.1+1/src/PieceStorage.h 2007-07-14 22:49:11.872750000 -0700 @@ -124,9 +124,9 @@ virtual const unsigned char* getBitfield() = 0; virtual void setBitfield(const unsigned char* bitfield, - int bitfieldLength) = 0; + int32_t bitfieldLength) = 0; - virtual int getBitfieldLength() = 0; + virtual int32_t getBitfieldLength() = 0; virtual bool isSelectiveDownloadingMode() = 0; @@ -136,26 +136,26 @@ virtual DiskAdaptorHandle getDiskAdaptor() = 0; - virtual int getPieceLength(int index) = 0; + virtual int32_t getPieceLength(int32_t index) = 0; /** * Adds piece index to advertise to other commands. They send have message * based on this information. */ - virtual void advertisePiece(int cuid, int index) = 0; + virtual void advertisePiece(int32_t cuid, int32_t index) = 0; /** * Returns piece index which is not advertised by the caller command and * newer than lastCheckTime. */ - virtual Integers getAdvertisedPieceIndexes(int myCuid, + virtual Integers getAdvertisedPieceIndexes(int32_t myCuid, const Time& lastCheckTime) = 0; /** * Removes have entry if specified seconds have elapsed since its * registration. */ - virtual void removeAdvertisedPiece(int elapsed) = 0; + virtual void removeAdvertisedPiece(int32_t elapsed) = 0; /** * Sets all bits in bitfield to 1. diff -uwrN aria2-0.11.1+1.orig/src/Platform.cc aria2-0.11.1+1/src/Platform.cc --- aria2-0.11.1+1.orig/src/Platform.cc 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/Platform.cc 2007-07-14 22:49:11.872750000 -0700 @@ -0,0 +1,63 @@ +/* */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef HAVE_WINSOCK2_H +# ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +# endif // _WIN32_WINNT +# include +#endif // HAVE_WINSOCK2_H + +#include "Platform.h" +#include "DlAbortEx.h" + +Platform::Platform() { +#ifdef HAVE_WINSOCK2_H + WSADATA wsaData; + memset((char*)&wsaData, 0, sizeof(wsaData)); + if (WSAStartup(MAKEWORD(1, 1), &wsaData)) { + throw new DlAbortEx(_("Windows socket library initialization failed")); + } +#endif // HAVE_WINSOCK2_H +} + +Platform::~Platform() { +#ifdef HAVE_WINSOCK2_H + WSACleanup(); +#endif // HAVE_WINSOCK2_H +} diff -uwrN aria2-0.11.1+1.orig/src/Platform.h aria2-0.11.1+1/src/Platform.h --- aria2-0.11.1+1.orig/src/Platform.h 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/Platform.h 2007-07-14 22:49:11.872750000 -0700 @@ -0,0 +1,45 @@ +/* */ +#ifndef _D_PLATFORM_H_ +#define _D_PLATFORM_H_ + +class Platform { +public: + Platform(); + + ~Platform(); +}; + +#endif // _D_PLATFORM_H_ diff -uwrN aria2-0.11.1+1.orig/src/Request.cc aria2-0.11.1+1/src/Request.cc --- aria2-0.11.1+1.orig/src/Request.cc 2007-06-10 00:55:43.000000000 -0700 +++ aria2-0.11.1+1/src/Request.cc 2007-07-14 22:49:11.888375000 -0700 @@ -84,9 +84,6 @@ port = 0; dir = ""; file = ""; - if(tempUrl.find_first_not_of(SAFE_CHARS) != string::npos) { - return false; - } string::size_type startQueryIndex = tempUrl.find("?"); if(startQueryIndex != string::npos) { query = tempUrl.substr(startQueryIndex); @@ -128,6 +125,10 @@ file = tempUrl.substr(direp+1); } file += query; + if(tempUrl.find_first_not_of(SAFE_CHARS) != string::npos) { + // some torrents have invalid characters + return false; + } return true; } diff -uwrN aria2-0.11.1+1.orig/src/SegmentMan.cc aria2-0.11.1+1/src/SegmentMan.cc --- aria2-0.11.1+1.orig/src/SegmentMan.cc 2007-05-20 06:51:52.000000000 -0700 +++ aria2-0.11.1+1/src/SegmentMan.cc 2007-07-14 22:49:11.919625000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "SegmentMan.h" #include "DlAbortEx.h" #include "Util.h" @@ -48,6 +49,10 @@ #include #include +#ifdef HAVE_IO_H +# include +#endif // HAVE_IO_H + SegmentMan::SegmentMan():logger(LogFactory::getInstance()), bitfield(0), totalSize(0), @@ -88,7 +93,7 @@ } string segFilename = getSegmentFilePath(); logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str()); - FILE* segFile = openSegFile(segFilename, "r+"); + FILE* segFile = openSegFile(segFilename, "r+b"); try { read(segFile); fclose(segFile); @@ -106,7 +111,7 @@ } string segFilename = getSegmentFilePath(); logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str()); - FILE* segFile = openSegFile(segFilename, "w"); + FILE* segFile = openSegFile(segFilename, "wb"); try { if(fwrite(&totalSize, sizeof(totalSize), 1, segFile) < 1) { throw string("writeError"); @@ -155,6 +160,9 @@ throw new DlAbortEx(EX_SEGMENT_FILE_OPEN, segFilename.c_str(), strerror(errno)); } +#ifdef HAVE_SETMODE + setmode(fileno(segFile), O_BINARY); +#endif return segFile; } diff -uwrN aria2-0.11.1+1.orig/src/SimpleLogger.cc aria2-0.11.1+1/src/SimpleLogger.cc --- aria2-0.11.1+1.orig/src/SimpleLogger.cc 2007-06-12 03:50:40.000000000 -0700 +++ aria2-0.11.1+1/src/SimpleLogger.cc 2007-07-14 22:49:11.919625000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "SimpleLogger.h" #include "Util.h" #include "DlAbortEx.h" @@ -41,6 +42,14 @@ #include #include +#ifdef HAVE_IO_H +# include +#endif // HAVE_IO_H + +#ifndef HAVE_LOCALTIME_R +# include "localtime_r.h" +#endif // HAVE_LOCALTIME_R + #define WRITE_LOG(LEVEL, MSG) \ va_list ap;\ va_start(ap, MSG);\ @@ -60,10 +69,13 @@ } void SimpleLogger::openFile(const string& filename) { - file = fopen(filename.c_str(), "a"); + file = fopen(filename.c_str(), "ab"); if(file == NULL) { throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); } +#ifdef HAVE_SETMODE + setmode(fileno(file), O_BINARY); +#endif } void SimpleLogger::closeFile() { diff -uwrN aria2-0.11.1+1.orig/src/SimpleRandomizer.h aria2-0.11.1+1/src/SimpleRandomizer.h --- aria2-0.11.1+1.orig/src/SimpleRandomizer.h 2007-02-06 06:49:22.000000000 -0800 +++ aria2-0.11.1+1/src/SimpleRandomizer.h 2007-07-14 22:49:11.919625000 -0700 @@ -54,13 +54,21 @@ } static void init() { - srandom(time(0)); +#ifdef HAVE_SRANDOM + srandom(time(NULL)); +#else + srand(time(NULL)); +#endif } virtual ~SimpleRandomizer() {} virtual int getRandomNumber() { +#ifdef HAVE_RANDOM return random(); +#else + return rand(); +#endif } virtual int getMaxRandomNumber() { diff -uwrN aria2-0.11.1+1.orig/src/SocketCore.cc aria2-0.11.1+1/src/SocketCore.cc --- aria2-0.11.1+1.orig/src/SocketCore.cc 2006-09-21 08:31:24.000000000 -0700 +++ aria2-0.11.1+1/src/SocketCore.cc 2007-07-14 22:49:11.950875000 -0700 @@ -32,20 +32,37 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "SocketCore.h" #include "DlRetryEx.h" #include "DlAbortEx.h" #include "message.h" #include #include -#include #include +#include +#include + +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H #include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H #include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H #include -#include -#include -#include +#endif // HAVE_ARPA_INET_H + +#ifndef HAVE_GETADDRINFO +# include "getaddrinfo.h" +#endif // HAVE_GETADDRINFO + +#ifndef HAVE_INET_ATON +# include "inet_aton.h" +#endif // HAVE_INET_ATON SocketCore::SocketCore():sockfd(-1) { init(); @@ -79,6 +96,31 @@ #endif // HAVE_LIBGNUTLS } +#ifdef __MINGW32__ + +static char *mingw_strerror(int err) { + err = WSAGetLastError(); + static char buf[2048]; + if (FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &buf, + sizeof(buf), + NULL + ) == 0) { + snprintf(buf, sizeof(buf), _("Unknown socket error %d"), err); + } + return buf; +} + +#define strerror mingw_strerror +#define gai_strerror mingw_strerror + +#endif // __MINGW32__ + void SocketCore::beginListen(int port) { closeConnection(); //sockfd = socket(AF_UNSPEC, SOCK_STREAM, PF_UNSPEC); @@ -86,7 +128,7 @@ if(sockfd == -1) { throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno)); } - socklen_t sockopt = 1; + SOCKOPT_T sockopt = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) { close(sockfd); sockfd = -1; @@ -151,7 +193,7 @@ if(sockfd == -1) { throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno)); } - socklen_t sockopt = 1; + SOCKOPT_T sockopt = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) { close(sockfd); sockfd = -1; @@ -188,15 +230,25 @@ } void SocketCore::setNonBlockingMode() const { +#ifdef __MINGW32__ + u_long flag = 0; + ::ioctlsocket(sockfd, FIONBIO, &flag); +#else int flags = fcntl(sockfd, F_GETFL, 0); // TODO add error handling fcntl(sockfd, F_SETFL, flags|O_NONBLOCK); +#endif } void SocketCore::setBlockingMode() const { +#ifdef __MINGW32__ + u_long flag = 1; + ::ioctlsocket(sockfd, FIONBIO, &flag); +#else int flags = fcntl(sockfd, F_GETFL, 0); // TODO add error handling fcntl(sockfd, F_SETFL, flags&(~O_NONBLOCK)); +#endif } void SocketCore::closeConnection() { diff -uwrN aria2-0.11.1+1.orig/src/SpeedCalc.h aria2-0.11.1+1/src/SpeedCalc.h --- aria2-0.11.1+1.orig/src/SpeedCalc.h 2007-06-20 07:43:34.000000000 -0700 +++ aria2-0.11.1+1/src/SpeedCalc.h 2007-07-16 07:44:33.827250000 -0700 @@ -71,7 +71,7 @@ int32_t getAvgSpeed() const; - void update(int bytes); + void update(int32_t bytes); void reset(); }; diff -uwrN aria2-0.11.1+1.orig/src/TorrentRequestInfo.cc aria2-0.11.1+1/src/TorrentRequestInfo.cc --- aria2-0.11.1+1.orig/src/TorrentRequestInfo.cc 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/TorrentRequestInfo.cc 2007-07-15 08:17:18.747750000 -0700 @@ -44,6 +44,10 @@ #include "DNSCache.h" #include +#ifndef SA_RESETHAND +# define SA_RESETHAND 0x80000000 +#endif // SA_RESETHAND + extern volatile sig_atomic_t btHaltRequested; void torrentHandler(int signal) { diff -uwrN aria2-0.11.1+1.orig/src/Util.cc aria2-0.11.1+1/src/Util.cc --- aria2-0.11.1+1.orig/src/Util.cc 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/Util.cc 2007-07-15 08:18:47.232125000 -0700 @@ -32,6 +32,7 @@ * files in the program, then also delete it here. */ /* copyright --> */ +#include "common.h" #include "Util.h" #include "DlAbortEx.h" #include "File.h" @@ -41,14 +42,31 @@ #include #include #include -#include -#include -#include -#include #include #include #include +#ifdef HAVE_SYS_SOCKET_H +#include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H +#include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H +#include +#endif // HAVE_ARPA_INET_H + +#ifdef HAVE_IO_H +# include +#endif // HAVE_IO_H + +#ifndef HAVE_INET_ATON +# include "inet_aton.h" +#endif // HAVE_INET_ATON + +#ifndef HAVE_STRPTIME +# include "strptime.h" +#endif // HAVE_STRPTIME template string uint2str(T value, bool comma) { @@ -84,8 +102,6 @@ return str; } - - string Util::uitos(uint16_t value, bool comma) { return uint2str(value, comma); } @@ -98,6 +114,12 @@ return uint2str(value, comma); } +#ifdef __CYGWIN__ +string Util::itos(int value, bool comma) { + return int2str(value, comma); +} +#endif // __CYGWIN__ + string Util::itos(int32_t value, bool comma) { return int2str(value, comma); } @@ -311,6 +333,9 @@ FILE* Util::openFile(const string& filename, const string& mode) { FILE* file = fopen(filename.c_str(), mode.c_str()); +#ifdef HAVE_SETMODE + setmode(fileno(file), O_BINARY); +#endif return file; } @@ -325,10 +350,10 @@ int destFd = -1; int srcFd = -1; try { - if((destFd = open(dest.c_str(), O_CREAT|O_WRONLY|O_TRUNC, OPEN_MODE)) == -1) { + if((destFd = open(dest.c_str(), O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, OPEN_MODE)) == -1) { throw new DlAbortEx(EX_FILE_OPEN, dest.c_str(), strerror(errno)); } - if((srcFd = open(src.c_str(), O_RDONLY, OPEN_MODE)) == -1) { + if((srcFd = open(src.c_str(), O_RDONLY|O_BINARY, OPEN_MODE)) == -1) { throw new DlAbortEx(EX_FILE_OPEN, src.c_str(), strerror(errno)); } if(lseek(srcFd, srcOffset, SEEK_SET) != srcOffset) { @@ -497,7 +522,7 @@ char buf[BUFLEN]; int fd; - if((fd = open(filename.c_str(), O_RDWR, OPEN_MODE)) < 0) { + if((fd = open(filename.c_str(), O_RDWR|O_BINARY, OPEN_MODE)) < 0) { throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno)); } while(1) { @@ -597,16 +622,16 @@ } string Util::randomAlpha(int length) { + static char *random_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{"; + string str; for(int i = 0; i < length; i++) { +#ifdef HAVE_RANDOM int index = (int)(((double)52)*random()/(RAND_MAX+1.0)); - char ch; - if(index < 26) { - ch = (char)('A'+index); - } else { - ch = (char)('a'+index-26); - } - str += ch; +#else + int index = (int)(((double)52)*rand()/(RAND_MAX+1.0)); +#endif // HAVE_RANDOM + str += random_chars[index]; } return str; } @@ -646,12 +671,16 @@ } } -void Util::setGlobalSignalHandler(int signal, void (*handler)(int), int flags) { +void Util::setGlobalSignalHandler(int sig, void (*handler)(int), int flags) { +#ifdef HAVE_SIGACTION struct sigaction sigact; sigact.sa_handler = handler; sigact.sa_flags = flags; sigemptyset(&sigact.sa_mask); - sigaction(signal, &sigact, NULL); + sigaction(sig, &sigact, NULL); +#else + signal(sig, handler); +#endif // HAVE_SIGACTION } void Util::indexRange(int32_t& startIndex, int32_t& endIndex, @@ -693,6 +722,32 @@ return strtoll(size.c_str(), 0, 10)*mult; } +string Util::getTempDir() { + static string tempdir; + + if (tempdir.empty()) { + tempdir = getenv("TMP"); + if (File::isDir(tempdir)) { + return tempdir; + } + tempdir = getenv("TMPDIR"); + if (File::isDir(tempdir)) { + return tempdir; + } + tempdir = getenv("TEMP"); + if (File::isDir(tempdir)) { + return tempdir; + } + tempdir = "/tmp"; + if (File::isDir(tempdir)) { + return tempdir; + } + tempdir = "."; + } + + return tempdir; +} + string Util::abbrevSize(int64_t size) { if(size < 1024) { @@ -713,9 +768,27 @@ time_t Util::httpGMT(const string& httpStdTime) { struct tm tm; + memset(&tm, 0, sizeof(tm)); strptime(httpStdTime.c_str(), "%a, %Y-%m-%d %H:%M:%S GMT", &tm); - time_t thetime = timegm(&tm); + + time_t thetime; +#ifdef HAVE_TIMEGM + thetime = timegm(&tm); +#else + char *tz; + + tz = getenv("TZ"); + putenv("TZ="); + tzset(); + thetime = mktime(&tm); + if (tz) { + char s[256]; + sprintf("TZ=%s", tz); + putenv(s); + } + tzset(); +#endif // HAVE_TIMEGM return thetime; } diff -uwrN aria2-0.11.1+1.orig/src/Util.h aria2-0.11.1+1/src/Util.h --- aria2-0.11.1+1.orig/src/Util.h 2007-06-30 02:52:39.000000000 -0700 +++ aria2-0.11.1+1/src/Util.h 2007-07-14 22:49:12.138375000 -0700 @@ -56,6 +56,9 @@ static string llitos(int64_t value, bool comma = false); static string ullitos(uint64_t value, bool comma = false); static string itos(int32_t value, bool comma = false); +#ifdef __CYGWIN__ + static string itos(int value, bool comma = false); +#endif // __CYGWIN__ static string uitos(uint32_t value, bool comma = false); static string itos(int16_t value, bool comma = false); static string uitos(uint16_t value, bool comma = false); @@ -147,6 +150,8 @@ static string getHomeDir(); + static string getTempDir(); + static int64_t getRealSize(const string& sizeWithUnit); static string abbrevSize(int64_t size); diff -uwrN aria2-0.11.1+1.orig/src/common.h aria2-0.11.1+1/src/common.h --- aria2-0.11.1+1.orig/src/common.h 2007-05-31 08:56:20.000000000 -0700 +++ aria2-0.11.1+1/src/common.h 2007-07-14 22:49:12.154000000 -0700 @@ -39,6 +39,7 @@ #ifdef HAVE_CONFIG_H # include #endif + #include #include #include @@ -55,6 +56,42 @@ # define _(String) (String) #endif +#ifdef __MINGW32__ +# define LONGLONG_PRINTF "%I64d" +# define ULONGLONG_PRINTF "%I64u" +# define LONGLONG_SCANF "%I64d" +# define ULONGLONG_SCANF "%I64u" +# ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +# endif // _WIN32_WINNT +# include +# undef ERROR +# include +# define SOCKOPT_T const char +# define HAVE_GETADDRINFO +typedef uint16_t in_port_t; +#else +# define LONGLONG_PRINTF "%lld" +# define ULONGLONG_PRINTF "%llu" +# define LONGLONG_SCANF "%Ld" +# define ULONGLONG_SCANF "%Lu" +# define SOCKOPT_T socklen_t +#endif // __MINGW32__ + +#ifndef EINPROGRESS +# define EINPROGRESS (WSAEINPROGRESS) +#endif // EINPROGRESS + +#include + +#ifndef O_NONBLOCK +# define O_NONBLOCK (O_NDELAY) +#endif // O_NONBLOCK + +#ifndef O_BINARY +# define O_BINARY (0) +#endif // O_BINARY + #define USER_AGENT "aria2" #define BITFIELD_LEN_FROM_PIECES(X) ((X)/8+((X)%8? 1 : 0)) diff -uwrN aria2-0.11.1+1.orig/src/getaddrinfo.c aria2-0.11.1+1/src/getaddrinfo.c --- aria2-0.11.1+1.orig/src/getaddrinfo.c 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/getaddrinfo.c 2007-07-18 21:43:04.000000000 -0700 @@ -0,0 +1,608 @@ +/* + * Copyright (c) 2001, 02 Motoyuki Kasahara + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * This program provides getaddrinfo() and getnameinfo() described in + * RFC2133, 2553 and 3493. These functions are mainly used for IPv6 + * application to resolve hostname or address. + * + * This program is designed to be working on traditional IPv4 systems + * which don't have those functions. Therefore, this implementation + * supports IPv4 only. + * + * This program is useful for application which should support both IPv6 + * and traditional IPv4 systems. Use genuine getaddrinfo() and getnameinfo() + * provided by system if the system supports IPv6. Otherwise, use this + * implementation. + * + * This program is intended to be used in combination with GNU Autoconf. + * + * This program also provides freeaddrinfo() and gai_strerror(). + * + * To use this program in your application, insert the following lines to + * C source files after including `sys/types.h', `sys/socket.h' and + * `netdb.h'. `getaddrinfo.h' defines `struct addrinfo' and AI_, NI_, + * EAI_ macros. + * + * #ifndef HAVE_GETADDRINFO + * #include "getaddrinfo.h" + * #endif + * + * Restriction: + * getaddrinfo() and getnameinfo() of this program are NOT thread + * safe, unless the cpp macro ENABLE_PTHREAD is defined. + */ + +/* + * Add the following code to your configure.ac (or configure.in). + * AC_C_CONST + * AC_HEADER_STDC + * AC_CHECK_HEADERS(string.h memory.h stdlib.h) + * AC_CHECK_FUNCS(memcpy) + * AC_REPLACE_FUNCS(memset) + * AC_TYPE_SOCKLEN_T + * AC_TYPE_IN_PORT_T + * AC_DECL_H_ERRNO + * + * AC_CHECK_FUNCS(getaddrinfo getnameinfo) + * if test "$ac_cv_func_getaddrinfo$ac_cv_func_getnameinfo" != yesyes ; then + * LIBOBJS="$LIBOBJS getaddrinfo.$ac_objext" + * fi + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef __MINGW32__ +# include +# undef ERROR +# include +#endif + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_ARPA_INET_H +# include +#endif +#ifdef HAVE_NETDB_H +# include +#endif + +#include +#include + +#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) +#include +#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H) +#include +#endif /* not STDC_HEADERS and HAVE_MEMORY_H */ +#else /* not STDC_HEADERS and not HAVE_STRING_H */ +#include +#endif /* not STDC_HEADERS and not HAVE_STRING_H */ + +#ifdef HAVE_STDLIB_H +#include +#endif + +#ifdef ENABLE_PTHREAD +#include +#endif + +#ifdef ENABLE_NLS +#include +#endif + +#ifndef HAVE_MEMCPY +#define memcpy(d, s, n) bcopy((s), (d), (n)) +#ifdef __STDC__ +void *memchr(const void *, int, size_t); +int memcmp(const void *, const void *, size_t); +void *memmove(void *, const void *, size_t); +void *memset(void *, int, size_t); +#else /* not __STDC__ */ +char *memchr(); +int memcmp(); +char *memmove(); +char *memset(); +#endif /* not __STDC__ */ +#endif /* not HAVE_MEMCPY */ + +#ifndef H_ERRNO_DECLARED +extern int h_errno; +#endif + +#include "getaddrinfo.h" + +#ifdef ENABLE_NLS +#define _(string) gettext(string) +#ifdef gettext_noop +#define N_(string) gettext_noop(string) +#else +#define N_(string) (string) +#endif +#else +#define gettext(string) (string) +#define _(string) (string) +#define N_(string) (string) +#endif + +/* + * Error messages for gai_strerror(). + */ +static char *eai_errlist[] = { + N_("Success"), + + /* EAI_ADDRFAMILY */ + N_("Address family for hostname not supported"), + + /* EAI_AGAIN */ + N_("Temporary failure in name resolution"), + + /* EAI_BADFLAGS */ + N_("Invalid value for ai_flags"), + + /* EAI_FAIL */ + N_("Non-recoverable failure in name resolution"), + + /* EAI_FAMILY */ + N_("ai_family not supported"), + + /* EAI_MEMORY */ + N_("Memory allocation failure"), + + /* EAI_NONAME */ + N_("hostname nor servname provided, or not known"), + + /* EAI_OVERFLOW */ + N_("An argument buffer overflowed"), + + /* EAI_SERVICE */ + N_("servname not supported for ai_socktype"), + + /* EAI_SOCKTYPE */ + N_("ai_socktype not supported"), + + /* EAI_SYSTEM */ + N_("System error returned in errno") +}; + +/* + * Default hints for getaddrinfo(). + */ +static struct addrinfo default_hints = { + 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL +}; + +/* + * Mutex. + */ +#ifdef ENABLE_PTHREAD +static pthread_mutex_t gai_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif + +/* + * Declaration of static functions. + */ +#ifdef __STDC__ +static int is_integer(const char *); +static int is_address(const char *); +static int itoa_length(int); +#else +static int is_integer(); +static int is_address(); +static int itoa_length(); +#endif + +/* + * gai_strerror(). + */ +const char * +gai_strerror(ecode) + int ecode; +{ + if (ecode < 0 || ecode > EAI_SYSTEM) + return _("Unknown error"); + + return gettext(eai_errlist[ecode]); +} + +/* + * freeaddrinfo(). + */ +void +freeaddrinfo(ai) + struct addrinfo *ai; +{ + struct addrinfo *next_ai; + + while (ai != NULL) { + if (ai->ai_canonname != NULL) + free(ai->ai_canonname); + if (ai->ai_addr != NULL) + free(ai->ai_addr); + next_ai = ai->ai_next; + free(ai); + ai = next_ai; + } +} + +/* + * Return 1 if the string `s' represents an integer. + */ +static int +is_integer(s) + const char *s; +{ + if (*s == '-' || *s == '+') + s++; + if (*s < '0' || '9' < *s) + return 0; + + s++; + while ('0' <= *s && *s <= '9') + s++; + + return (*s == '\0'); +} + +/* + * Return 1 if the string `s' represents an IPv4 address. + * Unlike inet_addr(), it doesn't permit malformed nortation such + * as "192.168". + */ +static int +is_address(s) + const char *s; +{ + const static char delimiters[] = {'.', '.', '.', '\0'}; + int i, j; + int octet; + + for (i = 0; i < 4; i++) { + if (*s == '0' && *(s + 1) != delimiters[i]) + return 0; + for (j = 0, octet = 0; '0' <= *s && *s <= '9' && j < 3; s++, j++) + octet = octet * 10 + (*s - '0'); + if (j == 0 || octet > 255 || *s != delimiters[i]) + return 0; + s++; + } + + return 1; +} + +/* + * Calcurate length of the string `s', where `s' is set by + * sprintf(s, "%d", n). + */ +static int +itoa_length(n) + int n; +{ + int result = 1; + + if (n < 0) { + n = -n; + result++; + } + + while (n >= 10) { + result++; + n /= 10; + } + + return result; +} + +/* + * getaddrinfo(). + */ +int +getaddrinfo(nodename, servname, hints, res) + const char *nodename; + const char *servname; + const struct addrinfo *hints; + struct addrinfo **res; +{ + struct addrinfo *head_res = NULL; + struct addrinfo *tail_res = NULL; + struct addrinfo *new_res; + struct sockaddr_in *sa_in; + struct in_addr **addr_list; + struct in_addr *addr_list_buf[2]; + struct in_addr addr_buf; + struct in_addr **ap; + struct servent *servent; + struct hostent *hostent; + const char *canonname = NULL; + in_port_t port; + int saved_h_errno; + int result = 0; + +#ifdef ENABLE_PTHREAD + pthread_mutex_lock(&gai_mutex); +#endif + + saved_h_errno = h_errno; + + if (nodename == NULL && servname == NULL) { + result = EAI_NONAME; + goto end; + } + + if (hints != NULL) { + if (hints->ai_family != PF_INET && hints->ai_family != PF_UNSPEC) { + result = EAI_FAMILY; + goto end; + } + if (hints->ai_socktype != SOCK_DGRAM + && hints->ai_socktype != SOCK_STREAM + && hints->ai_socktype != 0) { + result = EAI_SOCKTYPE; + goto end; + } + } else { + hints = &default_hints; + } + + if (servname != NULL) { + if (is_integer(servname)) + port = htons(atoi(servname)); + else { + if (hints->ai_flags & AI_NUMERICSERV) { + result = EAI_NONAME; + goto end; + } + + if (hints->ai_socktype == SOCK_DGRAM) + servent = getservbyname(servname, "udp"); + else if (hints->ai_socktype == SOCK_STREAM) + servent = getservbyname(servname, "tcp"); + else if (hints->ai_socktype == 0) + servent = getservbyname(servname, "tcp"); + else { + result = EAI_SOCKTYPE; + goto end; + } + + if (servent == NULL) { + result = EAI_SERVICE; + goto end; + } + port = servent->s_port; + } + } else { + port = htons(0); + } + + if (nodename != NULL) { + if (is_address(nodename)) { + addr_buf.s_addr = inet_addr(nodename); + addr_list_buf[0] = &addr_buf; + addr_list_buf[1] = NULL; + addr_list = addr_list_buf; + + if (hints->ai_flags & AI_CANONNAME + && !(hints->ai_flags & AI_NUMERICHOST)) { + hostent = gethostbyaddr((char *)&addr_buf, + sizeof(struct in_addr), AF_INET); + if (hostent != NULL) + canonname = hostent->h_name; + else + canonname = nodename; + } + } else { + if (hints->ai_flags & AI_NUMERICHOST) { + result = EAI_NONAME; + goto end; + } + + hostent = gethostbyname(nodename); + if (hostent == NULL) { + switch (h_errno) { + case HOST_NOT_FOUND: + case NO_DATA: + result = EAI_NONAME; + goto end; + case TRY_AGAIN: + result = EAI_AGAIN; + goto end; + default: + result = EAI_FAIL; + goto end; + } + } + addr_list = (struct in_addr **)hostent->h_addr_list; + + if (hints->ai_flags & AI_CANONNAME) + canonname = hostent->h_name; + } + } else { + if (hints->ai_flags & AI_PASSIVE) + addr_buf.s_addr = htonl(INADDR_ANY); + else + addr_buf.s_addr = htonl(0x7F000001); + addr_list_buf[0] = &addr_buf; + addr_list_buf[1] = NULL; + addr_list = addr_list_buf; + } + + for (ap = addr_list; *ap != NULL; ap++) { + new_res = (struct addrinfo *)malloc(sizeof(struct addrinfo)); + if (new_res == NULL) { + if (head_res != NULL) + freeaddrinfo(head_res); + result = EAI_MEMORY; + goto end; + } + + new_res->ai_family = PF_INET; + new_res->ai_socktype = hints->ai_socktype; + new_res->ai_protocol = hints->ai_protocol; + new_res->ai_addr = NULL; + new_res->ai_addrlen = sizeof(struct sockaddr_in); + new_res->ai_canonname = NULL; + new_res->ai_next = NULL; + + new_res->ai_addr = (struct sockaddr *) + malloc(sizeof(struct sockaddr_in)); + if (new_res->ai_addr == NULL) { + free(new_res); + if (head_res != NULL) + freeaddrinfo(head_res); + result = EAI_MEMORY; + goto end; + } + + sa_in = (struct sockaddr_in *)new_res->ai_addr; + memset(sa_in, 0, sizeof(struct sockaddr_in)); + sa_in->sin_family = PF_INET; + sa_in->sin_port = port; + memcpy(&sa_in->sin_addr, *ap, sizeof(struct in_addr)); + + if (head_res == NULL) + head_res = new_res; + else + tail_res->ai_next = new_res; + tail_res = new_res; + } + + if (canonname != NULL && head_res != NULL) { + head_res->ai_canonname = (char *)malloc(strlen(canonname) + 1); + if (head_res->ai_canonname != NULL) + strcpy(head_res->ai_canonname, canonname); + } + + *res = head_res; + + end: + h_errno = saved_h_errno; +#ifdef ENABLE_PTHREAD + pthread_mutex_unlock(&gai_mutex); +#endif + return result; +} + +/* + * getnameinfo(). + */ +int +getnameinfo(sa, salen, node, nodelen, serv, servlen, flags) + const struct sockaddr *sa; + socklen_t salen; + char *node; + socklen_t nodelen; + char *serv; + socklen_t servlen; + int flags; +{ + const struct sockaddr_in *sa_in = (const struct sockaddr_in *)sa; + struct hostent *hostent; + struct servent *servent; + char *ntoa_address; + int saved_h_errno; + int result = 0; + +#ifdef ENABLE_PTHREAD + pthread_mutex_lock(&gai_mutex); +#endif + + saved_h_errno = h_errno; + + if (sa_in->sin_family != PF_INET) { + result = EAI_FAMILY; + goto end; + } else if (node == NULL && serv == NULL) { + result = EAI_NONAME; + goto end; + } + + if (serv != NULL && servlen > 0) { + if (flags & NI_NUMERICSERV) + servent = NULL; + else if (flags & NI_DGRAM) + servent = getservbyport(sa_in->sin_port, "udp"); + else + servent = getservbyport(sa_in->sin_port, "tcp"); + + if (servent != NULL) { + if (servlen <= strlen(servent->s_name)) { + result = EAI_OVERFLOW; + goto end; + } + strcpy(serv, servent->s_name); + } else { + if (servlen <= itoa_length(ntohs(sa_in->sin_port))) { + result = EAI_OVERFLOW; + goto end; + } + sprintf(serv, "%d", ntohs(sa_in->sin_port)); + } + } + + if (node != NULL && nodelen > 0) { + if (flags & NI_NUMERICHOST) + hostent = NULL; + else { + hostent = gethostbyaddr((char *)&sa_in->sin_addr, + sizeof(struct in_addr), AF_INET); + } + if (hostent != NULL) { + if (nodelen <= strlen(hostent->h_name)) { + result = EAI_OVERFLOW; + goto end; + } + strcpy(node, hostent->h_name); + } else { + if (flags & NI_NAMEREQD) { + result = EAI_NONAME; + goto end; + } + ntoa_address = inet_ntoa(sa_in->sin_addr); + if (nodelen <= strlen(ntoa_address)) { + result = EAI_OVERFLOW; + goto end; + } + strcpy(node, ntoa_address); + } + + } + + end: + h_errno = saved_h_errno; +#ifdef ENABLE_PTHREAD + pthread_mutex_unlock(&gai_mutex); +#endif + return result; +} + diff -uwrN aria2-0.11.1+1.orig/src/getaddrinfo.h aria2-0.11.1+1/src/getaddrinfo.h --- aria2-0.11.1+1.orig/src/getaddrinfo.h 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/getaddrinfo.h 2007-07-14 22:49:12.169625000 -0700 @@ -0,0 +1,249 @@ +/* + * Copyright (c) 2001, 02 Motoyuki Kasahara + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _D_GETADDRINFO_H +#define _D_GETADDRINFO_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +# endif // _WIN32_WINNT +# include +# undef ERROR +# include +#endif // __MINGW32__ + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H + +#include + +/********************************************************************/ +/* + * Undefine all the macros. + * might defines some of them. + */ +#ifdef EAI_ADDRFAMILY +#undef EAI_ADDRFAMILY +#endif +#ifdef EAI_AGAIN +#undef EAI_AGAIN +#endif +#ifdef EAI_BADFLAGS +#undef EAI_BADFLAGS +#endif +#ifdef EAI_FAIL +#undef EAI_FAIL +#endif +#ifdef EAI_FAMILY +#undef EAI_FAMILY +#endif +#ifdef EAI_MEMORY +#undef EAI_MEMORY +#endif +#ifdef EAI_NONAME +#undef EAI_NONAME +#endif +#ifdef EAI_OVERFLOW +#undef EAI_OVERFLOW +#endif +#ifdef EAI_SERVICE +#undef EAI_SERVICE +#endif +#ifdef EAI_SOCKTYPE +#undef EAI_SOCKTYPE +#endif +#ifdef EAI_SYSTEM +#undef EAI_SYSTEM +#endif + +#ifdef AI_PASSIVE +#undef AI_PASSIVE +#endif +#ifdef AI_CANONNAME +#undef AI_CANONNAME +#endif +#ifdef AI_NUMERICHOST +#undef AI_NUMERICHOST +#endif +#ifdef AI_NUMERICSERV +#undef AI_NUMERICSERV +#endif +#ifdef AI_V4MAPPED +#undef AI_V4MAPPED +#endif +#ifdef AI_ALL +#undef AI_ALL +#endif +#ifdef AI_ADDRCONFIG +#undef AI_ADDRCONFIG +#endif +#ifdef AI_DEFAULT +#undef AI_DEFAULT +#endif + +#ifdef NI_NOFQDN +#undef NI_NOFQDN +#endif +#ifdef NI_NUMERICHOST +#undef NI_NUMERICHOST +#endif +#ifdef NI_NAMEREQD +#undef NI_NAMEREQD +#endif +#ifdef NI_NUMERICSERV +#undef NI_NUMERICSERV +#endif +#ifdef NI_NUMERICSCOPE +#undef NI_NUMERICSCOPE +#endif + +#ifdef NI_DGRAM +#undef NI_DGRAM +#endif +#ifdef NI_MAXHOST +#undef NI_MAXHOST +#endif +#ifdef NI_MAXSERV +#undef NI_MAXSERV +#endif + +/* + * Fake struct and function names. + * might declares all or some of them. + */ +#if defined(HAVE_GETADDRINFO) || defined(HAVE_GETNAMEINFO) +#define addrinfo my_addrinfo +#define gai_strerror my_gai_strerror +#define freeaddrinfo my_freeaddrinfo +#define getaddrinfo my_getaddrinfo +#define getnameinfo my_getnameinfo +#endif + +/********************************************************************/ +/* + * Error codes. + */ +#define EAI_ADDRFAMILY 1 +#define EAI_AGAIN 2 +#define EAI_BADFLAGS 3 +#define EAI_FAIL 4 +#define EAI_FAMILY 5 +#define EAI_MEMORY 6 +#define EAI_NONAME 7 +#define EAI_OVERFLOW 8 +#define EAI_SERVICE 9 +#define EAI_SOCKTYPE 10 +#define EAI_SYSTEM 11 + +/* + * Flags for getaddrinfo(). + */ +#define AI_ADDRCONFIG 0x0001 +#define AI_ALL 0x0002 +#define AI_CANONNAME 0x0004 +#define AI_NUMERICHOST 0x0008 +#define AI_NUMERICSERV 0x0010 +#define AI_PASSIVE 0x0020 +#define AI_V4MAPPED 0x0040 +#define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG) + +/* + * Flags for getnameinfo(). + */ +#define NI_DGRAM 0x0001 +#define NI_NAMEREQD 0x0002 +#define NI_NOFQDN 0x0004 +#define NI_NUMERICHOST 0x0008 +#define NI_NUMERICSCOPE 0x0010 +#define NI_NUMERICSERV 0x0020 + +/* + * Maximum length of FQDN and servie name for getnameinfo(). + */ +#define NI_MAXHOST 1025 +#define NI_MAXSERV 32 + +/* + * Address families and Protocol families. + */ +#ifndef AF_UNSPEC +#define AF_UNSPEC AF_INET +#endif +#ifndef PF_UNSPEC +#define PF_UNSPEC PF_INET +#endif + +/* + * struct addrinfo. + */ +struct addrinfo { + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + socklen_t ai_addrlen; + char *ai_canonname; + struct sockaddr *ai_addr; + struct addrinfo *ai_next; +}; + +/* + * Functions. + */ +#ifdef __STDC__ +const char *gai_strerror(int); +void freeaddrinfo(struct addrinfo *); +int getaddrinfo(const char *, const char *, const struct addrinfo *, + struct addrinfo **); +int getnameinfo(const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int); +#else +const char *gai_strerror(); +void freeaddrinfo(); +int getaddrinfo(); +int getnameinfo(); +#endif + +#ifdef __cplusplus +}; +#endif /* __cplusplus */ + +#endif /* not _D_GETADDRINFO_H */ diff -uwrN aria2-0.11.1+1.orig/src/inet_aton.c aria2-0.11.1+1/src/inet_aton.c --- aria2-0.11.1+1.orig/src/inet_aton.c 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/inet_aton.c 2007-07-18 21:43:04.000000000 -0700 @@ -0,0 +1,69 @@ +/* */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# define _WIN32_WINNT 0x501 +# include +# undef ERROR +# include +#endif // __MINGW32__ + +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H +# include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H +# include +#endif // HAVE_ARPA_INET_H + +#include + +int inet_aton(const char *cp, struct in_addr *inp) { + unsigned long res = inet_addr(cp); + if (res == INADDR_NONE && strcmp(cp, "255.255.255.255")) + return 0; + if (inp) + inp->s_addr = res; + return 1; +} diff -uwrN aria2-0.11.1+1.orig/src/inet_aton.h aria2-0.11.1+1/src/inet_aton.h --- aria2-0.11.1+1.orig/src/inet_aton.h 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/inet_aton.h 2007-07-14 22:49:12.294625000 -0700 @@ -0,0 +1,75 @@ +/* */ + +#ifndef _D_INET_ATON_H +#define _D_INET_ATON_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +# endif // _WIN32_WINNT +# include +# undef ERROR +# include +#endif // __MINGW32__ + +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H +# include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H +# include +#endif // HAVE_ARPA_INET_H + +int inet_aton(const char *cp, struct in_addr *inp); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* not _D_INET_ATON_H */ diff -uwrN aria2-0.11.1+1.orig/src/libgen.c aria2-0.11.1+1/src/libgen.c --- aria2-0.11.1+1.orig/src/libgen.c 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/libgen.c 2007-07-18 21:43:04.000000000 -0700 @@ -0,0 +1,89 @@ +/* */ + +#include +#include +#include + +#if defined(__CYGWIN__) || defined(__DJGPP__) || defined(__MINGW32__) +# define IS_PATH_SEPARATOR(c) (((c) == '/') || ((c) == '\\')) +#else +# define IS_PATH_SEPARATOR(c) ((c) == '/') +#endif + +/* per http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+basename */ +char* basename(char* s) { + char* rv; + + if (!s || !*s) + return "."; + + rv = s + strlen(s) - 1; + + do { + if (IS_PATH_SEPARATOR(*rv)) + return rv + 1; + --rv; + } while (rv >= s); + + return s; +} + +/* per http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+dirname */ +char* dirname(char* path) { + char *p; + + if (path == NULL || *path == '\0') + return "."; + p = path + strlen(path) - 1; + while (IS_PATH_SEPARATOR(*p)) { + if (p == path) + return path; + *p-- = '\0'; + } + + while (p >= path && !IS_PATH_SEPARATOR(*p)) + p--; + + if (p < path) + return "."; + + if (p == path) + return "/"; + + *p = '\0'; + + return path; +} diff -uwrN aria2-0.11.1+1.orig/src/localtime.cc aria2-0.11.1+1/src/localtime.cc --- aria2-0.11.1+1.orig/src/localtime.cc 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.11.1+1/src/localtime.cc 2007-07-14 22:49:12.310250000 -0700 @@ -0,0 +1,2166 @@ +/* +** This file is in the public domain, so clarified as of +** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov). +*/ +/* Temporarily merged private.h and tzfile.h for ease of management - DJ */ + +//#include "winsup.h" +//#include "cygerrno.h" +#include +#define STD_INSPIRED +#define lint + +#define USG_COMPAT + +#ifndef lint +#ifndef NOID +static char elsieid[] = "@(#)localtime.c 7.66"; +#endif /* !defined NOID */ +#endif /* !defined lint */ + +/* +** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu). +** POSIX-style TZ environment variable handling from Guy Harris +** (guy@auspex.com). +*/ + +/*LINTLIBRARY*/ + +#ifndef PRIVATE_H + +#define PRIVATE_H + +/* +** This file is in the public domain, so clarified as of +** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov). +*/ + +/* +** This header is for use ONLY with the time conversion code. +** There is no guarantee that it will remain unchanged, +** or that it will remain at all. +** Do NOT copy it to any system include directory. +** Thank you! +*/ + +/* +** ID +*/ + +#ifndef lint +#ifndef NOID +static char privatehid[] = "@(#)private.h 7.48"; +#endif /* !defined NOID */ +#endif /* !defined lint */ + +/* +** Defaults for preprocessor symbols. +** You can override these in your C compiler options, e.g. `-DHAVE_ADJTIME=0'. +*/ + +#ifndef HAVE_ADJTIME +#define HAVE_ADJTIME 1 +#endif /* !defined HAVE_ADJTIME */ + +#ifndef HAVE_GETTEXT +#define HAVE_GETTEXT 0 +#endif /* !defined HAVE_GETTEXT */ + +#ifndef HAVE_SETTIMEOFDAY +#define HAVE_SETTIMEOFDAY 3 +#endif /* !defined HAVE_SETTIMEOFDAY */ + +#ifndef HAVE_STRERROR +#define HAVE_STRERROR 0 +#endif /* !defined HAVE_STRERROR */ + +#ifndef HAVE_SYMLINK +#define HAVE_SYMLINK 1 +#endif /* !defined HAVE_SYMLINK */ + +#ifndef HAVE_UNISTD_H +#define HAVE_UNISTD_H 1 +#endif /* !defined HAVE_UNISTD_H */ + +#ifndef HAVE_UTMPX_H +#define HAVE_UTMPX_H 0 +#endif /* !defined HAVE_UTMPX_H */ + +#ifndef LOCALE_HOME +#define LOCALE_HOME "/usr/lib/locale" +#endif /* !defined LOCALE_HOME */ + +/* +** Nested includes +*/ + +#include "sys/types.h" /* for time_t */ +#include "stdio.h" +#include "limits.h" /* for CHAR_BIT */ +#include "time.h" +#include "stdlib.h" + +#if HAVE_GETTEXT - 0 +#include "libintl.h" +#endif /* HAVE_GETTEXT - 0 */ + +#if HAVE_UNISTD_H - 0 +#include "unistd.h" /* for F_OK and R_OK */ +#endif /* HAVE_UNISTD_H - 0 */ + +#if !(HAVE_UNISTD_H - 0) +#ifndef F_OK +#define F_OK 0 +#endif /* !defined F_OK */ +#ifndef R_OK +#define R_OK 4 +#endif /* !defined R_OK */ +#endif /* !(HAVE_UNISTD_H - 0) */ + +/* Unlike 's isdigit, this also works if c < 0 | c > UCHAR_MAX. */ +#define is_digit(c) ((unsigned)(c) - '0' <= 9) + +/* +** Workarounds for compilers/systems. +*/ + +/* +** SunOS 4.1.1 cc lacks const. +*/ + +#ifndef const +#ifndef __STDC__ +#define const +#endif /* !defined __STDC__ */ +#endif /* !defined const */ + +/* +** SunOS 4.1.1 cc lacks prototypes. +*/ + +#ifndef P +#ifdef __STDC__ +#define P(x) x +#endif /* defined __STDC__ */ +#ifndef __STDC__ +#define P(x) () +#endif /* !defined __STDC__ */ +#endif /* !defined P */ + +/* +** SunOS 4.1.1 headers lack EXIT_SUCCESS. +*/ + +#ifndef EXIT_SUCCESS +#define EXIT_SUCCESS 0 +#endif /* !defined EXIT_SUCCESS */ + +/* +** SunOS 4.1.1 headers lack EXIT_FAILURE. +*/ + +#ifndef EXIT_FAILURE +#define EXIT_FAILURE 1 +#endif /* !defined EXIT_FAILURE */ + +/* +** SunOS 4.1.1 headers lack FILENAME_MAX. +*/ + +#ifndef FILENAME_MAX + +#ifndef MAXPATHLEN +#ifdef unix +#include "sys/param.h" +#endif /* defined unix */ +#endif /* !defined MAXPATHLEN */ + +#ifdef MAXPATHLEN +#define FILENAME_MAX MAXPATHLEN +#endif /* defined MAXPATHLEN */ +#ifndef MAXPATHLEN +#define FILENAME_MAX 1024 /* Pure guesswork */ +#endif /* !defined MAXPATHLEN */ + +#endif /* !defined FILENAME_MAX */ + +/* +** SunOS 4.1.1 libraries lack remove. +*/ + +#ifndef remove +extern int unlink P((const char * filename)); +#define remove unlink +#endif /* !defined remove */ + +/* +** Finally, some convenience items. +*/ + +#ifndef TYPE_BIT +#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT) +#endif /* !defined TYPE_BIT */ + +#ifndef TYPE_SIGNED +#define TYPE_SIGNED(type) (((type) -1) < 0) +#endif /* !defined TYPE_SIGNED */ + +#ifndef INT_STRLEN_MAXIMUM +/* +** 302 / 1000 is log10(2.0) rounded up. +** Subtract one for the sign bit if the type is signed; +** add one for integer division truncation; +** add one more for a minus sign if the type is signed. +*/ +#define INT_STRLEN_MAXIMUM(type) \ + ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type)) +#endif /* !defined INT_STRLEN_MAXIMUM */ + +/* +** INITIALIZE(x) +*/ + +#ifndef GNUC_or_lint +#ifdef lint +#define GNUC_or_lint +#endif /* defined lint */ +#ifndef lint +#ifdef __GNUC__ +#define GNUC_or_lint +#endif /* defined __GNUC__ */ +#endif /* !defined lint */ +#endif /* !defined GNUC_or_lint */ + +#ifndef INITIALIZE +#ifdef GNUC_or_lint +#define INITIALIZE(x) ((x) = 0) +#endif /* defined GNUC_or_lint */ +#ifndef GNUC_or_lint +#define INITIALIZE(x) +#endif /* !defined GNUC_or_lint */ +#endif /* !defined INITIALIZE */ + +/* +** For the benefit of GNU folk... +** `_(MSGID)' uses the current locale's message library string for MSGID. +** The default is to use gettext if available, and use MSGID otherwise. +*/ + +#ifndef _ +#if HAVE_GETTEXT - 0 +#define _(msgid) gettext(msgid) +#else /* !(HAVE_GETTEXT - 0) */ +#define _(msgid) msgid +#endif /* !(HAVE_GETTEXT - 0) */ +#endif /* !defined _ */ + +#ifndef TZ_DOMAIN +#define TZ_DOMAIN "tz" +#endif /* !defined TZ_DOMAIN */ + +/* +** UNIX was a registered trademark of UNIX System Laboratories in 1993. +*/ + +#endif /* !defined PRIVATE_H */ + +#ifndef TZFILE_H + +#define TZFILE_H + +/* +** This file is in the public domain, so clarified as of +** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov). +*/ + +/* +** This header is for use ONLY with the time conversion code. +** There is no guarantee that it will remain unchanged, +** or that it will remain at all. +** Do NOT copy it to any system include directory. +** Thank you! +*/ + +/* +** ID +*/ + +#ifndef lint +#ifndef NOID +static char tzfilehid[] = "@(#)tzfile.h 7.14"; +#endif /* !defined NOID */ +#endif /* !defined lint */ + +/* +** Information about time zone files. +*/ + +#ifndef TZDIR +#define TZDIR "/usr/share/zoneinfo" /* Time zone object file directory */ +#endif /* !defined TZDIR */ + +#ifndef TZDEFAULT +#define TZDEFAULT "localtime" +#endif /* !defined TZDEFAULT */ + +#ifndef TZDEFRULES +#define TZDEFRULES "posixrules" +#endif /* !defined TZDEFRULES */ + +/* +** Each file begins with. . . +*/ + +#define TZ_MAGIC "TZif" + +struct tzhead { + char tzh_magic[4]; /* TZ_MAGIC */ + char tzh_reserved[16]; /* reserved for future use */ + char tzh_ttisgmtcnt[4]; /* coded number of trans. time flags */ + char tzh_ttisstdcnt[4]; /* coded number of trans. time flags */ + char tzh_leapcnt[4]; /* coded number of leap seconds */ + char tzh_timecnt[4]; /* coded number of transition times */ + char tzh_typecnt[4]; /* coded number of local time types */ + char tzh_charcnt[4]; /* coded number of abbr. chars */ +}; + +/* +** . . .followed by. . . +** +** tzh_timecnt (char [4])s coded transition times a la time(2) +** tzh_timecnt (unsigned char)s types of local time starting at above +** tzh_typecnt repetitions of +** one (char [4]) coded UTC offset in seconds +** one (unsigned char) used to set tm_isdst +** one (unsigned char) that's an abbreviation list index +** tzh_charcnt (char)s '\0'-terminated zone abbreviations +** tzh_leapcnt repetitions of +** one (char [4]) coded leap second transition times +** one (char [4]) total correction after above +** tzh_ttisstdcnt (char)s indexed by type; if true, transition +** time is standard time, if false, +** transition time is wall clock time +** if absent, transition times are +** assumed to be wall clock time +** tzh_ttisgmtcnt (char)s indexed by type; if true, transition +** time is UTC, if false, +** transition time is local time +** if absent, transition times are +** assumed to be local time +*/ + +/* +** In the current implementation, "tzset()" refuses to deal with files that +** exceed any of the limits below. +*/ + +#ifndef TZ_MAX_TIMES +/* +** The TZ_MAX_TIMES value below is enough to handle a bit more than a +** year's worth of solar time (corrected daily to the nearest second) or +** 138 years of Pacific Presidential Election time +** (where there are three time zone transitions every fourth year). +*/ +#define TZ_MAX_TIMES 370 +#endif /* !defined TZ_MAX_TIMES */ + +#ifndef TZ_MAX_TYPES +#ifndef NOSOLAR +#define TZ_MAX_TYPES 256 /* Limited by what (unsigned char)'s can hold */ +#endif /* !defined NOSOLAR */ +#ifdef NOSOLAR +/* +** Must be at least 14 for Europe/Riga as of Jan 12 1995, +** as noted by Earl Chew . +*/ +#define TZ_MAX_TYPES 20 /* Maximum number of local time types */ +#endif /* !defined NOSOLAR */ +#endif /* !defined TZ_MAX_TYPES */ + +#ifndef TZ_MAX_CHARS +#define TZ_MAX_CHARS 50 /* Maximum number of abbreviation characters */ + /* (limited by what unsigned chars can hold) */ +#endif /* !defined TZ_MAX_CHARS */ + +#ifndef TZ_MAX_LEAPS +#define TZ_MAX_LEAPS 50 /* Maximum number of leap second corrections */ +#endif /* !defined TZ_MAX_LEAPS */ + +#define SECSPERMIN 60 +#define MINSPERHOUR 60 +#define HOURSPERDAY 24 +#define DAYSPERWEEK 7 +#define DAYSPERNYEAR 365 +#define DAYSPERLYEAR 366 +#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) +#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY) +#define MONSPERYEAR 12 + +#define TM_SUNDAY 0 +#define TM_MONDAY 1 +#define TM_TUESDAY 2 +#define TM_WEDNESDAY 3 +#define TM_THURSDAY 4 +#define TM_FRIDAY 5 +#define TM_SATURDAY 6 + +#define TM_JANUARY 0 +#define TM_FEBRUARY 1 +#define TM_MARCH 2 +#define TM_APRIL 3 +#define TM_MAY 4 +#define TM_JUNE 5 +#define TM_JULY 6 +#define TM_AUGUST 7 +#define TM_SEPTEMBER 8 +#define TM_OCTOBER 9 +#define TM_NOVEMBER 10 +#define TM_DECEMBER 11 + +#define TM_YEAR_BASE 1900 + +#define EPOCH_YEAR 1970 +#define EPOCH_WDAY TM_THURSDAY + +/* +** Accurate only for the past couple of centuries; +** that will probably do. +*/ + +#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) + +#ifndef USG + +/* +** Use of the underscored variants may cause problems if you move your code to +** certain System-V-based systems; for maximum portability, use the +** underscore-free variants. The underscored variants are provided for +** backward compatibility only; they may disappear from future versions of +** this file. +*/ + +#define SECS_PER_MIN SECSPERMIN +#define MINS_PER_HOUR MINSPERHOUR +#define HOURS_PER_DAY HOURSPERDAY +#define DAYS_PER_WEEK DAYSPERWEEK +#define DAYS_PER_NYEAR DAYSPERNYEAR +#define DAYS_PER_LYEAR DAYSPERLYEAR +#define SECS_PER_HOUR SECSPERHOUR +#define SECS_PER_DAY SECSPERDAY +#define MONS_PER_YEAR MONSPERYEAR + +#endif /* !defined USG */ + +#endif /* !defined TZFILE_H */ + +#include "fcntl.h" + +/* +** SunOS 4.1.1 headers lack O_BINARY. +*/ + +#ifdef O_BINARY +#define OPEN_MODE (O_RDONLY | O_BINARY) +#endif /* defined O_BINARY */ +#ifndef O_BINARY +#define OPEN_MODE O_RDONLY +#endif /* !defined O_BINARY */ + +#ifndef WILDABBR +/* +** Someone might make incorrect use of a time zone abbreviation: +** 1. They might reference tzname[0] before calling tzset (explicitly +** or implicitly). +** 2. They might reference tzname[1] before calling tzset (explicitly +** or implicitly). +** 3. They might reference tzname[1] after setting to a time zone +** in which Daylight Saving Time is never observed. +** 4. They might reference tzname[0] after setting to a time zone +** in which Standard Time is never observed. +** 5. They might reference tm.TM_ZONE after calling offtime. +** What's best to do in the above cases is open to debate; +** for now, we just set things up so that in any of the five cases +** WILDABBR is used. Another possibility: initialize tzname[0] to the +** string "tzname[0] used before set", and similarly for the other cases. +** And another: initialize tzname[0] to "ERA", with an explanation in the +** manual page of what this "time zone abbreviation" means (doing this so +** that tzname[0] has the "normal" length of three characters). +*/ +#define WILDABBR " " +#endif /* !defined WILDABBR */ + +static char wildabbr[] = WILDABBR; + +static char gmt[] = "GMT"; + +struct ttinfo { /* time type information */ + long tt_gmtoff; /* UTC offset in seconds */ + int tt_isdst; /* used to set tm_isdst */ + int tt_abbrind; /* abbreviation list index */ + int tt_ttisstd; /* true if transition is std time */ + int tt_ttisgmt; /* true if transition is UTC */ +}; + +struct lsinfo { /* leap second information */ + time_t ls_trans; /* transition time */ + long ls_corr; /* correction to apply */ +}; + +#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) + +#ifdef TZNAME_MAX +#define MY_TZNAME_MAX TZNAME_MAX +#endif /* defined TZNAME_MAX */ +#ifndef TZNAME_MAX +#define MY_TZNAME_MAX 255 +#endif /* !defined TZNAME_MAX */ + +struct state { + int leapcnt; + int timecnt; + int typecnt; + int charcnt; + time_t ats[TZ_MAX_TIMES]; + unsigned char types[TZ_MAX_TIMES]; + struct ttinfo ttis[TZ_MAX_TYPES]; + char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), + (2 * (MY_TZNAME_MAX + 1)))]; + struct lsinfo lsis[TZ_MAX_LEAPS]; +}; + +struct rule { + int r_type; /* type of rule--see below */ + int r_day; /* day number of rule */ + int r_week; /* week number of rule */ + int r_mon; /* month number of rule */ + long r_time; /* transition time of rule */ +}; + +#define JULIAN_DAY 0 /* Jn - Julian day */ +#define DAY_OF_YEAR 1 /* n - day of year */ +#define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ + +/* +** Prototypes for static functions. +*/ + +static long detzcode P((const char * codep)); +static const char * getzname P((const char * strp)); +static const char * getnum P((const char * strp, int * nump, int min, + int max)); +static const char * getsecs P((const char * strp, long * secsp)); +static const char * getoffset P((const char * strp, long * offsetp)); +static const char * getrule P((const char * strp, struct rule * rulep)); +static void gmtload P((struct state * sp)); +static void gmtsub P((const time_t * timep, long offset, + struct tm * tmp)); +static void localsub P((const time_t * timep, long offset, + struct tm * tmp)); +static int increment_overflow P((int * number, int delta)); +static int normalize_overflow P((int * tensptr, int * unitsptr, + int base)); +static void settzname P((void)); +static time_t time1 P((struct tm * tmp, + void(*funcp) P((const time_t *, + long, struct tm *)), + long offset)); +static time_t time2 P((struct tm *tmp, + void(*funcp) P((const time_t *, + long, struct tm*)), + long offset, int * okayp)); +static time_t time2sub P((struct tm *tmp, + void(*funcp) P((const time_t *, + long, struct tm*)), + long offset, int * okayp, int do_norm_secs)); +static void timesub P((const time_t * timep, long offset, + const struct state * sp, struct tm * tmp)); +static int tmcomp P((const struct tm * atmp, + const struct tm * btmp)); +static time_t transtime P((time_t janfirst, int year, + const struct rule * rulep, long offset)); +static int tzload P((const char * name, struct state * sp)); +static int tzparse P((const char * name, struct state * sp, + int lastditch)); + +#ifdef ALL_STATE +static struct state * lclptr; +static struct state * gmtptr; +#endif /* defined ALL_STATE */ + +#ifndef ALL_STATE +static struct state lclmem; +static struct state gmtmem; +#define lclptr (&lclmem) +#define gmtptr (&gmtmem) +#endif /* State Farm */ + +#ifndef TZ_STRLEN_MAX +#define TZ_STRLEN_MAX 255 +#endif /* !defined TZ_STRLEN_MAX */ + +static char lcl_TZname[TZ_STRLEN_MAX + 1]; +static int lcl_is_set; +static int gmt_is_set; + +#define tzname _tzname +#undef _tzname + +char * tzname[2] = { + wildabbr, + wildabbr +}; + +/* +** Section 4.12.3 of X3.159-1989 requires that +** Except for the strftime function, these functions [asctime, +** ctime, gmtime, localtime] return values in one of two static +** objects: a broken-down time structure and an array of char. +** Thanks to Paul Eggert (eggert@twinsun.com) for noting this. +*/ + +static struct tm tm; + + +/* These variables are initialized by tzset. The macro versions are + defined in time.h, and indirect through the __imp_ pointers. */ + +#define timezone _timezone +#define daylight _daylight +#undef _timezone +#undef _daylight + +#ifdef USG_COMPAT +long timezone; /* was time_t but POSIX requires long. */ +int daylight; +#endif /* defined USG_COMPAT */ + +#ifdef ALTZONE +time_t altzone; +#endif /* defined ALTZONE */ + +static long +detzcode(const char *codep) +{ + register long result; + register int i; + + result = (codep[0] & 0x80) ? ~0L : 0L; + for (i = 0; i < 4; ++i) + result = (result << 8) | (codep[i] & 0xff); + return result; +} + +static void +settzname P((void)) +{ + register struct state * const sp = lclptr; + register int i; + + tzname[0] = wildabbr; + tzname[1] = wildabbr; +#ifdef USG_COMPAT + daylight = 0; + timezone = 0; +#endif /* defined USG_COMPAT */ +#ifdef ALTZONE + altzone = 0; +#endif /* defined ALTZONE */ +#ifdef ALL_STATE + if (sp == NULL) { + tzname[0] = tzname[1] = gmt; + return; + } +#endif /* defined ALL_STATE */ + for (i = 0; i < sp->typecnt; ++i) { + register const struct ttinfo * const ttisp = &sp->ttis[i]; + + tzname[ttisp->tt_isdst] = + &sp->chars[ttisp->tt_abbrind]; +#ifdef USG_COMPAT + if (ttisp->tt_isdst) + daylight = 1; + if (i == 0 || !ttisp->tt_isdst) + timezone = -(ttisp->tt_gmtoff); +#endif /* defined USG_COMPAT */ +#ifdef ALTZONE + if (i == 0 || ttisp->tt_isdst) + altzone = -(ttisp->tt_gmtoff); +#endif /* defined ALTZONE */ + } + /* + ** And to get the latest zone names into tzname. . . + */ + for (i = 0; i < sp->timecnt; ++i) { + register const struct ttinfo * const ttisp = + &sp->ttis[ + sp->types[i]]; + + tzname[ttisp->tt_isdst] = + &sp->chars[ttisp->tt_abbrind]; + } +} + +#include "tz_posixrules.h" + +static int +tzload(const char *name, struct state *sp) +{ + register const char * p; + register int i; + register int fid; + save_errno save; + + if (name == NULL && (name = TZDEFAULT) == NULL) + return -1; + { + register int doaccess; + /* + ** Section 4.9.1 of the C standard says that + ** "FILENAME_MAX expands to an integral constant expression + ** that is the size needed for an array of char large enough + ** to hold the longest file name string that the implementation + ** guarantees can be opened." + */ + char fullname[FILENAME_MAX + 1]; + + if (name[0] == ':') + ++name; + doaccess = name[0] == '/'; + if (!doaccess) { + if ((p = TZDIR) == NULL) + return -1; + if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) + return -1; + strcpy(fullname, p); + strcat(fullname, "/"); + strcat(fullname, name); + /* + ** Set doaccess if '.' (as in "../") shows up in name. + */ + if (strchr(name, '.') != NULL) + doaccess = true; + name = fullname; + } +#if 0 + if (doaccess && access(name, R_OK) != 0) + return -1; +#endif + if ((fid = open(name, OPEN_MODE)) == -1) + { + const char *base = strrchr(name, '/'); + if (base) + base++; + else + base = name; + if (strcmp(base, "posixrules")) + return -1; + + /* We've got a built-in copy of posixrules just in case */ + fid = -2; + } + } + { + struct tzhead * tzhp; + union { + struct tzhead tzhead; + char buf[sizeof *sp + sizeof *tzhp]; + } u; + int ttisstdcnt; + int ttisgmtcnt; + + if (fid == -2) + { + memcpy(u.buf, _posixrules_data, sizeof (_posixrules_data)); + i = sizeof (_posixrules_data); + } + else + { + i = read(fid, u.buf, sizeof u.buf); + if (close(fid) != 0) + return -1; + } + ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt); + ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt); + sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt); + sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt); + sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt); + sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt); + p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt; + if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || + sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || + sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || + sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || + (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || + (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) + return -1; + if (i - (p - u.buf) < sp->timecnt * 4 + /* ats */ + sp->timecnt + /* types */ + sp->typecnt * (4 + 2) + /* ttinfos */ + sp->charcnt + /* chars */ + sp->leapcnt * (4 + 4) + /* lsinfos */ + ttisstdcnt + /* ttisstds */ + ttisgmtcnt) /* ttisgmts */ + return -1; + for (i = 0; i < sp->timecnt; ++i) { + sp->ats[i] = detzcode(p); + p += 4; + } + for (i = 0; i < sp->timecnt; ++i) { + sp->types[i] = (unsigned char) *p++; + if (sp->types[i] >= sp->typecnt) + return -1; + } + for (i = 0; i < sp->typecnt; ++i) { + register struct ttinfo * ttisp; + + ttisp = &sp->ttis[i]; + ttisp->tt_gmtoff = detzcode(p); + p += 4; + ttisp->tt_isdst = (unsigned char) *p++; + if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) + return -1; + ttisp->tt_abbrind = (unsigned char) *p++; + if (ttisp->tt_abbrind < 0 || + ttisp->tt_abbrind > sp->charcnt) + return -1; + } + for (i = 0; i < sp->charcnt; ++i) + sp->chars[i] = *p++; + sp->chars[i] = '\0'; /* ensure '\0' at end */ + for (i = 0; i < sp->leapcnt; ++i) { + register struct lsinfo * lsisp; + + lsisp = &sp->lsis[i]; + lsisp->ls_trans = detzcode(p); + p += 4; + lsisp->ls_corr = detzcode(p); + p += 4; + } + for (i = 0; i < sp->typecnt; ++i) { + register struct ttinfo * ttisp; + + ttisp = &sp->ttis[i]; + if (ttisstdcnt == 0) + ttisp->tt_ttisstd = false; + else { + ttisp->tt_ttisstd = *p++; + if (ttisp->tt_ttisstd != true && + ttisp->tt_ttisstd != false) + return -1; + } + } + for (i = 0; i < sp->typecnt; ++i) { + register struct ttinfo * ttisp; + + ttisp = &sp->ttis[i]; + if (ttisgmtcnt == 0) + ttisp->tt_ttisgmt = false; + else { + ttisp->tt_ttisgmt = *p++; + if (ttisp->tt_ttisgmt != true && + ttisp->tt_ttisgmt != false) + return -1; + } + } + } + return 0; +} + +static const int mon_lengths[2][MONSPERYEAR] = { + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, + { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } +}; + +static const int year_lengths[2] = { + DAYSPERNYEAR, DAYSPERLYEAR +}; + +/* +** Given a pointer into a time zone string, scan until a character that is not +** a valid character in a zone name is found. Return a pointer to that +** character. +*/ + +static const char * +getzname(const char *strp) +{ + register char c; + + while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && + c != '+') + ++strp; + return strp; +} + +/* +** Given a pointer into a time zone string, extract a number from that string. +** Check that the number is within a specified range; if it is not, return +** NULL. +** Otherwise, return a pointer to the first character not part of the number. +*/ + +static const char * +getnum(const char *strp, int *nump, const int min, const int max) +{ + register char c; + register int num; + + if (strp == NULL || !is_digit(c = *strp)) + return NULL; + num = 0; + do { + num = num * 10 + (c - '0'); + if (num > max) + return NULL; /* illegal value */ + c = *++strp; + } while (is_digit(c)); + if (num < min) + return NULL; /* illegal value */ + *nump = num; + return strp; +} + +/* +** Given a pointer into a time zone string, extract a number of seconds, +** in hh[:mm[:ss]] form, from the string. +** If any error occurs, return NULL. +** Otherwise, return a pointer to the first character not part of the number +** of seconds. +*/ + +static const char * +getsecs(const char *strp, long *secsp) +{ + int num; + + /* + ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like + ** "M10.4.6/26", which does not conform to Posix, + ** but which specifies the equivalent of + ** ``02:00 on the first Sunday on or after 23 Oct''. + */ + strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); + if (strp == NULL) + return NULL; + *secsp = num * (long) SECSPERHOUR; + if (*strp == ':') { + ++strp; + strp = getnum(strp, &num, 0, MINSPERHOUR - 1); + if (strp == NULL) + return NULL; + *secsp += num * SECSPERMIN; + if (*strp == ':') { + ++strp; + /* `SECSPERMIN' allows for leap seconds. */ + strp = getnum(strp, &num, 0, SECSPERMIN); + if (strp == NULL) + return NULL; + *secsp += num; + } + } + return strp; +} + +/* +** Given a pointer into a time zone string, extract an offset, in +** [+-]hh[:mm[:ss]] form, from the string. +** If any error occurs, return NULL. +** Otherwise, return a pointer to the first character not part of the time. +*/ + +static const char * +getoffset(const char *strp, long *offsetp) +{ + register int neg = 0; + + if (*strp == '-') { + neg = 1; + ++strp; + } else if (*strp == '+') + ++strp; + strp = getsecs(strp, offsetp); + if (strp == NULL) + return NULL; /* illegal time */ + if (neg) + *offsetp = -*offsetp; + return strp; +} + +/* +** Given a pointer into a time zone string, extract a rule in the form +** date[/time]. See POSIX section 8 for the format of "date" and "time". +** If a valid rule is not found, return NULL. +** Otherwise, return a pointer to the first character not part of the rule. +*/ + +static const char * +getrule(const char *strp, struct rule *rulep) +{ + if (*strp == 'J') { + /* + ** Julian day. + */ + rulep->r_type = JULIAN_DAY; + ++strp; + strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); + } else if (*strp == 'M') { + /* + ** Month, week, day. + */ + rulep->r_type = MONTH_NTH_DAY_OF_WEEK; + ++strp; + strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); + if (strp == NULL) + return NULL; + if (*strp++ != '.') + return NULL; + strp = getnum(strp, &rulep->r_week, 1, 5); + if (strp == NULL) + return NULL; + if (*strp++ != '.') + return NULL; + strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); + } else if (is_digit(*strp)) { + /* + ** Day of year. + */ + rulep->r_type = DAY_OF_YEAR; + strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); + } else return NULL; /* invalid format */ + if (strp == NULL) + return NULL; + if (*strp == '/') { + /* + ** Time specified. + */ + ++strp; + strp = getsecs(strp, &rulep->r_time); + } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ + return strp; +} + +/* +** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the +** year, a rule, and the offset from UTC at the time that rule takes effect, +** calculate the Epoch-relative time that rule takes effect. +*/ + +static time_t +transtime(const time_t janfirst, const int year, const struct rule *rulep, + long offset) +{ + register int leapyear; + register time_t value; + register int i; + int d, m1, yy0, yy1, yy2, dow; + + INITIALIZE(value); + leapyear = isleap(year); + switch (rulep->r_type) { + + case JULIAN_DAY: + /* + ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap + ** years. + ** In non-leap years, or if the day number is 59 or less, just + ** add SECSPERDAY times the day number-1 to the time of + ** January 1, midnight, to get the day. + */ + value = janfirst + (rulep->r_day - 1) * SECSPERDAY; + if (leapyear && rulep->r_day >= 60) + value += SECSPERDAY; + break; + + case DAY_OF_YEAR: + /* + ** n - day of year. + ** Just add SECSPERDAY times the day number to the time of + ** January 1, midnight, to get the day. + */ + value = janfirst + rulep->r_day * SECSPERDAY; + break; + + case MONTH_NTH_DAY_OF_WEEK: + /* + ** Mm.n.d - nth "dth day" of month m. + */ + value = janfirst; + for (i = 0; i < rulep->r_mon - 1; ++i) + value += mon_lengths[leapyear][i] * SECSPERDAY; + + /* + ** Use Zeller's Congruence to get day-of-week of first day of + ** month. + */ + m1 = (rulep->r_mon + 9) % 12 + 1; + yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; + yy1 = yy0 / 100; + yy2 = yy0 % 100; + dow = ((26 * m1 - 2) / 10 + + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; + if (dow < 0) + dow += DAYSPERWEEK; + + /* + ** "dow" is the day-of-week of the first day of the month. Get + ** the day-of-month (zero-origin) of the first "dow" day of the + ** month. + */ + d = rulep->r_day - dow; + if (d < 0) + d += DAYSPERWEEK; + for (i = 1; i < rulep->r_week; ++i) { + if (d + DAYSPERWEEK >= + mon_lengths[leapyear][rulep->r_mon - 1]) + break; + d += DAYSPERWEEK; + } + + /* + ** "d" is the day-of-month (zero-origin) of the day we want. + */ + value += d * SECSPERDAY; + break; + } + + /* + ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in + ** question. To get the Epoch-relative time of the specified local + ** time on that day, add the transition time and the current offset + ** from UTC. + */ + return value + rulep->r_time + offset; +} + +/* +** Given a POSIX section 8-style TZ string, fill in the rule tables as +** appropriate. +*/ + +static int +tzparse(const char *name, struct state *sp, const int lastditch) +{ + const char * stdname; + const char * dstname; + size_t stdlen; + size_t dstlen; + long stdoffset; + long dstoffset; + register time_t * atp; + register unsigned char * typep; + register char * cp; + register int load_result; + + INITIALIZE(dstname); + stdname = name; + if (lastditch) { + stdlen = strlen(name); /* length of standard zone name */ + name += stdlen; + if (stdlen >= sizeof sp->chars) + stdlen = (sizeof sp->chars) - 1; + stdoffset = 0; + } else { + name = getzname(name); + stdlen = name - stdname; + if (stdlen < 3) + return -1; + if (*name == '\0') + return -1; + name = getoffset(name, &stdoffset); + if (name == NULL) + return -1; + } + load_result = tzload(TZDEFRULES, sp); + if (load_result != 0) + sp->leapcnt = 0; /* so, we're off a little */ + if (*name != '\0') { + dstname = name; + name = getzname(name); + dstlen = name - dstname; /* length of DST zone name */ + if (dstlen < 3) + return -1; + if (*name != '\0' && *name != ',' && *name != ';') { + name = getoffset(name, &dstoffset); + if (name == NULL) + return -1; + } else dstoffset = stdoffset - SECSPERHOUR; + if (*name == ',' || *name == ';') { + struct rule start; + struct rule end; + register int year; + register time_t janfirst; + time_t starttime; + time_t endtime; + + ++name; + if ((name = getrule(name, &start)) == NULL) + return -1; + if (*name++ != ',') + return -1; + if ((name = getrule(name, &end)) == NULL) + return -1; + if (*name != '\0') + return -1; + sp->typecnt = 2; /* standard time and DST */ + /* + ** Two transitions per year, from EPOCH_YEAR to 2037. + */ + sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1); + if (sp->timecnt > TZ_MAX_TIMES) + return -1; + sp->ttis[0].tt_gmtoff = -dstoffset; + sp->ttis[0].tt_isdst = 1; + sp->ttis[0].tt_abbrind = stdlen + 1; + sp->ttis[1].tt_gmtoff = -stdoffset; + sp->ttis[1].tt_isdst = 0; + sp->ttis[1].tt_abbrind = 0; + atp = sp->ats; + typep = sp->types; + janfirst = 0; + for (year = EPOCH_YEAR; year <= 2037; ++year) { + starttime = transtime(janfirst, year, &start, + stdoffset); + endtime = transtime(janfirst, year, &end, + dstoffset); + if (starttime > endtime) { + *atp++ = endtime; + *typep++ = 1; /* DST ends */ + *atp++ = starttime; + *typep++ = 0; /* DST begins */ + } else { + *atp++ = starttime; + *typep++ = 0; /* DST begins */ + *atp++ = endtime; + *typep++ = 1; /* DST ends */ + } + janfirst += year_lengths[isleap(year)] * + SECSPERDAY; + } + __gettzinfo ()->__tzrule[0].offset + = -sp->ttis[1].tt_gmtoff; + __gettzinfo ()->__tzrule[1].offset + = -sp->ttis[0].tt_gmtoff; + } else { + register long theirstdoffset; + register long theirdstoffset; + register long theiroffset; + register int isdst; + register int i; + register int j; + + if (*name != '\0') + return -1; + if (load_result != 0) + return -1; + /* + ** Initial values of theirstdoffset and theirdstoffset. + */ + theirstdoffset = 0; + for (i = 0; i < sp->timecnt; ++i) { + j = sp->types[i]; + if (!sp->ttis[j].tt_isdst) { + theirstdoffset = + -sp->ttis[j].tt_gmtoff; + break; + } + } + theirdstoffset = 0; + for (i = 0; i < sp->timecnt; ++i) { + j = sp->types[i]; + if (sp->ttis[j].tt_isdst) { + theirdstoffset = + -sp->ttis[j].tt_gmtoff; + break; + } + } + /* + ** Initially we're assumed to be in standard time. + */ + isdst = false; + theiroffset = theirstdoffset; + /* + ** Now juggle transition times and types + ** tracking offsets as you do. + */ + for (i = 0; i < sp->timecnt; ++i) { + j = sp->types[i]; + sp->types[i] = sp->ttis[j].tt_isdst; + if (sp->ttis[j].tt_ttisgmt) { + /* No adjustment to transition time */ + } else { + /* + ** If summer time is in effect, and the + ** transition time was not specified as + ** standard time, add the summer time + ** offset to the transition time; + ** otherwise, add the standard time + ** offset to the transition time. + */ + /* + ** Transitions from DST to DDST + ** will effectively disappear since + ** POSIX provides for only one DST + ** offset. + */ + if (isdst && !sp->ttis[j].tt_ttisstd) { + sp->ats[i] += dstoffset - + theirdstoffset; + } else { + sp->ats[i] += stdoffset - + theirstdoffset; + } + } + theiroffset = -sp->ttis[j].tt_gmtoff; + if (sp->ttis[j].tt_isdst) + theirdstoffset = theiroffset; + else theirstdoffset = theiroffset; + } + /* + ** Finally, fill in ttis. + ** ttisstd and ttisgmt need not be handled. + */ + sp->ttis[0].tt_gmtoff = -stdoffset; + sp->ttis[0].tt_isdst = false; + sp->ttis[0].tt_abbrind = 0; + sp->ttis[1].tt_gmtoff = -dstoffset; + sp->ttis[1].tt_isdst = true; + sp->ttis[1].tt_abbrind = stdlen + 1; + sp->typecnt = 2; + __gettzinfo ()->__tzrule[0].offset + = -sp->ttis[0].tt_gmtoff; + __gettzinfo ()->__tzrule[1].offset + = -sp->ttis[1].tt_gmtoff; + } + } else { + dstlen = 0; + sp->typecnt = 1; /* only standard time */ + sp->timecnt = 0; + sp->ttis[0].tt_gmtoff = -stdoffset; + sp->ttis[0].tt_isdst = 0; + sp->ttis[0].tt_abbrind = 0; + __gettzinfo ()->__tzrule[0].offset = -sp->ttis[0].tt_gmtoff; + __gettzinfo ()->__tzrule[1].offset = -sp->ttis[0].tt_gmtoff; + } + sp->charcnt = stdlen + 1; + if (dstlen != 0) + sp->charcnt += dstlen + 1; + if ((size_t) sp->charcnt > sizeof sp->chars) + return -1; + cp = sp->chars; + strncpy(cp, stdname, stdlen); + cp += stdlen; + *cp++ = '\0'; + if (dstlen != 0) { + strncpy(cp, dstname, dstlen); + *(cp + dstlen) = '\0'; + } + return 0; +} + +static void +gmtload(struct state *sp) +{ + if (tzload(gmt, sp) != 0) + tzparse(gmt, sp, true); +} + +#ifndef STD_INSPIRED +/* +** A non-static declaration of tzsetwall in a system header file +** may cause a warning about this upcoming static declaration... +*/ +static +#endif /* !defined STD_INSPIRED */ +void +tzsetwall P((void)) +{ + if (lcl_is_set < 0) + return; + lcl_is_set = -1; + +#ifdef ALL_STATE + if (lclptr == NULL) { + lclptr = (struct state *) malloc(sizeof *lclptr); + if (lclptr == NULL) { + settzname(); /* all we can do */ + return; + } + } +#endif /* defined ALL_STATE */ +#if defined (_WIN32) || defined (__CYGWIN__) +#define is_upper(c) ((unsigned)(c) - 'A' <= 26) + { + TIME_ZONE_INFORMATION tz; + char buf[BUFSIZ]; + char *cp, *dst; + wchar_t *src; + div_t d; + GetTimeZoneInformation(&tz); + dst = cp = buf; + for (src = tz.StandardName; *src; src++) + if (is_upper(*src)) *dst++ = *src; + if ((dst - cp) < 3) + { + /* In non-english Windows, converted tz.StandardName + may not contain a valid standard timezone name. */ + strcpy(cp, wildabbr); + cp += strlen(wildabbr); + } + else + cp = dst; + d = div(tz.Bias+tz.StandardBias, 60); + sprintf(cp, "%d", d.quot); + if (d.rem) + sprintf(cp=strchr(cp, 0), ":%d", abs(d.rem)); + if(tz.StandardDate.wMonth) { + cp = strchr(cp, 0); + dst = cp; + for (src = tz.DaylightName; *src; src++) + if (is_upper(*src)) *dst++ = *src; + if ((dst - cp) < 3) + { + /* In non-english Windows, converted tz.DaylightName + may not contain a valid daylight timezone name. */ + strcpy(cp, wildabbr); + cp += strlen(wildabbr); + } + else + cp = dst; + d = div(tz.Bias+tz.DaylightBias, 60); + sprintf(cp, "%d", d.quot); + if (d.rem) + sprintf(cp=strchr(cp, 0), ":%d", abs(d.rem)); + cp = strchr(cp, 0); + sprintf(cp=strchr(cp, 0), ",M%d.%d.%d/%d", + tz.DaylightDate.wMonth, + tz.DaylightDate.wDay, + tz.DaylightDate.wDayOfWeek, + tz.DaylightDate.wHour); + if (tz.DaylightDate.wMinute || tz.DaylightDate.wSecond) + sprintf(cp=strchr(cp, 0), ":%d", tz.DaylightDate.wMinute); + if (tz.DaylightDate.wSecond) + sprintf(cp=strchr(cp, 0), ":%d", tz.DaylightDate.wSecond); + cp = strchr(cp, 0); + sprintf(cp=strchr(cp, 0), ",M%d.%d.%d/%d", + tz.StandardDate.wMonth, + tz.StandardDate.wDay, + tz.StandardDate.wDayOfWeek, + tz.StandardDate.wHour); + if (tz.StandardDate.wMinute || tz.StandardDate.wSecond) + sprintf(cp=strchr(cp, 0), ":%d", tz.StandardDate.wMinute); + if (tz.StandardDate.wSecond) + sprintf(cp=strchr(cp, 0), ":%d", tz.StandardDate.wSecond); + } + /* printf("TZ deduced as `%s'\n", buf); */ + if (tzparse(buf, lclptr, false) == 0) { + settzname(); + lcl_is_set = 1; + strlcpy(lcl_TZname, buf, sizeof (lcl_TZname)); + setenv("TZ", lcl_TZname, 1); + return; + } + } +#endif + if (tzload((char *) NULL, lclptr) != 0) + gmtload(lclptr); + settzname(); +} + +extern "C" void +tzset P((void)) +{ + const char * name = getenv("TZ"); + + if (name == NULL) { + tzsetwall(); + return; + } + + if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) + return; + lcl_is_set = (strlen(name) < sizeof (lcl_TZname)); + if (lcl_is_set) + strcpy(lcl_TZname, name); + +#ifdef ALL_STATE + if (lclptr == NULL) { + lclptr = (struct state *) malloc(sizeof *lclptr); + if (lclptr == NULL) { + settzname(); /* all we can do */ + return; + } + } +#endif /* defined ALL_STATE */ + if (*name == '\0') { + /* + ** User wants it fast rather than right. + */ + lclptr->leapcnt = 0; /* so, we're off a little */ + lclptr->timecnt = 0; + lclptr->ttis[0].tt_gmtoff = 0; + lclptr->ttis[0].tt_abbrind = 0; + strcpy(lclptr->chars, gmt); + } else if (tzload(name, lclptr) != 0) { + if (name[0] == ':' || tzparse(name, lclptr, false) != 0) + gmtload(lclptr); + } + settzname(); +} + +/* +** The easy way to behave "as if no library function calls" localtime +** is to not call it--so we drop its guts into "localsub", which can be +** freely called. (And no, the PANS doesn't require the above behavior-- +** but it *is* desirable.) +** +** The unused offset argument is for the benefit of mktime variants. +*/ + +/*ARGSUSED*/ +static void +localsub (const time_t * const timep, + const long offset, + struct tm * const tmp) +{ + register struct state * sp; + register const struct ttinfo * ttisp; + register int i; + const time_t t = *timep; + + sp = lclptr; +#ifdef ALL_STATE + if (sp == NULL) { + gmtsub(timep, offset, tmp); + return; + } +#endif /* defined ALL_STATE */ + if (sp->timecnt == 0 || t < sp->ats[0]) { + i = 0; + while (sp->ttis[i].tt_isdst) + if (++i >= sp->typecnt) { + i = 0; + break; + } + } else { + for (i = 1; i < sp->timecnt; ++i) + if (t < sp->ats[i]) + break; + i = sp->types[i - 1]; + } + ttisp = &sp->ttis[i]; + /* + ** To get (wrong) behavior that's compatible with System V Release 2.0 + ** you'd replace the statement below with + ** t += ttisp->tt_gmtoff; + ** timesub(&t, 0L, sp, tmp); + */ + timesub(&t, ttisp->tt_gmtoff, sp, tmp); + tmp->tm_isdst = ttisp->tt_isdst; + tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; +#ifdef TM_ZONE + tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; +#endif /* defined TM_ZONE */ +} + +extern "C" struct tm * +localtime(const time_t *timep) +{ + tzset(); + localsub(timep, 0L, &tm); + return &tm; +} + +/* + * Re-entrant version of localtime + */ +extern "C" struct tm * +localtime_r(const time_t *timep, struct tm *tm) +{ + tzset(); + localsub(timep, 0L, tm); + return tm; +} + +/* +** gmtsub is to gmtime as localsub is to localtime. +*/ + +static void +gmtsub(const time_t *timep, const long offset, struct tm *tmp) +{ + if (!gmt_is_set) { + gmt_is_set = true; +#ifdef ALL_STATE + gmtptr = (struct state *) malloc(sizeof *gmtptr); + if (gmtptr != NULL) +#endif /* defined ALL_STATE */ + gmtload(gmtptr); + } + timesub(timep, offset, gmtptr, tmp); +#ifdef TM_ZONE + /* + ** Could get fancy here and deliver something such as + ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero, + ** but this is no time for a treasure hunt. + */ + if (offset != 0) + tmp->TM_ZONE = wildabbr; + else { +#ifdef ALL_STATE + if (gmtptr == NULL) + tmp->TM_ZONE = gmt; + else tmp->TM_ZONE = gmtptr->chars; +#endif /* defined ALL_STATE */ +#ifndef ALL_STATE + tmp->TM_ZONE = gmtptr->chars; +#endif /* State Farm */ + } +#endif /* defined TM_ZONE */ +} + +extern "C" struct tm * +gmtime(const time_t *timep) +{ + gmtsub(timep, 0L, &tm); + return &tm; +} + +/* + * Re-entrant version of gmtime + */ +extern "C" struct tm * +gmtime_r(const time_t *timep, struct tm *tm) +{ + gmtsub(timep, 0L, tm); + return tm; +} + +#ifdef STD_INSPIRED + +extern "C" struct tm * +offtime(const time_t *timep, const long offset) +{ + gmtsub(timep, offset, &tm); + return &tm; +} + +#endif /* defined STD_INSPIRED */ + +static void +timesub(const time_t *timep, const long offset, const struct state *sp, + struct tm *tmp) +{ + register const struct lsinfo * lp; + register long days; + register long rem; + register int y; + register int yleap; + register const int * ip; + register long corr; + register int hit; + register int i; + + corr = 0; + hit = 0; +#ifdef ALL_STATE + i = (sp == NULL) ? 0 : sp->leapcnt; +#endif /* defined ALL_STATE */ +#ifndef ALL_STATE + i = sp->leapcnt; +#endif /* State Farm */ + while (--i >= 0) { + lp = &sp->lsis[i]; + if (*timep >= lp->ls_trans) { + if (*timep == lp->ls_trans) { + hit = ((i == 0 && lp->ls_corr > 0) || + lp->ls_corr > sp->lsis[i - 1].ls_corr); + if (hit) + while (i > 0 && + sp->lsis[i].ls_trans == + sp->lsis[i - 1].ls_trans + 1 && + sp->lsis[i].ls_corr == + sp->lsis[i - 1].ls_corr + 1) { + ++hit; + --i; + } + } + corr = lp->ls_corr; + break; + } + } + days = *timep / SECSPERDAY; + rem = *timep % SECSPERDAY; +#ifdef mc68k + if (*timep == 0x80000000) { + /* + ** A 3B1 muffs the division on the most negative number. + */ + days = -24855; + rem = -11648; + } +#endif /* defined mc68k */ + rem += (offset - corr); + while (rem < 0) { + rem += SECSPERDAY; + --days; + } + while (rem >= SECSPERDAY) { + rem -= SECSPERDAY; + ++days; + } + tmp->tm_hour = (int) (rem / SECSPERHOUR); + rem = rem % SECSPERHOUR; + tmp->tm_min = (int) (rem / SECSPERMIN); + /* + ** A positive leap second requires a special + ** representation. This uses "... ??:59:60" et seq. + */ + tmp->tm_sec = (int) (rem % SECSPERMIN) + hit; + tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK); + if (tmp->tm_wday < 0) + tmp->tm_wday += DAYSPERWEEK; + y = EPOCH_YEAR; +#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400) + while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) { + register int newy; + + newy = y + days / DAYSPERNYEAR; + if (days < 0) + --newy; + days -= (newy - y) * DAYSPERNYEAR + + LEAPS_THRU_END_OF(newy - 1) - + LEAPS_THRU_END_OF(y - 1); + y = newy; + } + tmp->tm_year = y - TM_YEAR_BASE; + tmp->tm_yday = (int) days; + ip = mon_lengths[yleap]; + for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon)) + days = days - (long) ip[tmp->tm_mon]; + tmp->tm_mday = (int) (days + 1); + tmp->tm_isdst = 0; +#ifdef TM_GMTOFF + tmp->TM_GMTOFF = offset; +#endif /* defined TM_GMTOFF */ +} + +extern "C" char * +ctime(const time_t *timep) +{ +/* +** Section 4.12.3.2 of X3.159-1989 requires that +** The ctime function converts the calendar time pointed to by timer +** to local time in the form of a string. It is equivalent to +** asctime(localtime(timer)) +*/ + return asctime(localtime(timep)); +} + +extern "C" char * +ctime_r(const time_t *timep, char *buf) +{ + struct tm tm; + + return asctime_r(localtime_r(timep, &tm), buf); +} + +/* +** Adapted from code provided by Robert Elz, who writes: +** The "best" way to do mktime I think is based on an idea of Bob +** Kridle's (so its said...) from a long time ago. +** [kridle@xinet.com as of 1996-01-16.] +** It does a binary search of the time_t space. Since time_t's are +** just 32 bits, its a max of 32 iterations (even at 64 bits it +** would still be very reasonable). +*/ + +#ifndef WRONG +#define WRONG (-1) +#endif /* !defined WRONG */ + +/* +** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com). +*/ + +static int +increment_overflow(int *number, int delta) +{ + int number0; + + number0 = *number; + *number += delta; + return (*number < number0) != (delta < 0); +} + +static int +normalize_overflow(int *tensptr, int *unitsptr, const int base) +{ + register int tensdelta; + + tensdelta = (*unitsptr >= 0) ? + (*unitsptr / base) : + (-1 - (-1 - *unitsptr) / base); + *unitsptr -= tensdelta * base; + return increment_overflow(tensptr, tensdelta); +} + +static int +tmcomp(register const struct tm *atmp, register const struct tm *btmp) +{ + register int result; + + if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && + (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && + (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && + (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && + (result = (atmp->tm_min - btmp->tm_min)) == 0) + result = atmp->tm_sec - btmp->tm_sec; + return result; +} + +static time_t +time2sub(struct tm *tmp, void (*funcp) P((const time_t*, long, struct tm*)), + const long offset, int *okayp, const int do_norm_secs) +{ + register const struct state * sp; + register int dir; + register int bits; + register int i, j ; + register int saved_seconds; + time_t newt; + time_t t; + struct tm yourtm, mytm; + + *okayp = false; + yourtm = *tmp; + if (do_norm_secs) { + if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec, + SECSPERMIN)) + return WRONG;