unix.cpp

Go to the documentation of this file.
00001 /* $Id: unix.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "../../textbuf_gui.h"
00014 #include "../../functions.h"
00015 #include "../../crashlog.h"
00016 #include "../../core/random_func.hpp"
00017 
00018 
00019 #include <dirent.h>
00020 #include <unistd.h>
00021 #include <sys/stat.h>
00022 #include <time.h>
00023 #include <signal.h>
00024 
00025 #ifdef __APPLE__
00026   #include <sys/mount.h>
00027 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
00028   #define HAS_STATVFS
00029 #endif
00030 
00031 #ifdef HAS_STATVFS
00032 #include <sys/statvfs.h>
00033 #endif
00034 
00035 
00036 #ifdef __MORPHOS__
00037 #include <exec/types.h>
00038 ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;)
00039 
00040 /* The system supplied definition of SIG_IGN does not match */
00041 #undef SIG_IGN
00042 #define SIG_IGN (void (*)(int))1
00043 #endif /* __MORPHOS__ */
00044 
00045 #ifdef __AMIGA__
00046 #warning add stack symbol to avoid that user needs to set stack manually (tokai)
00047 // ULONG __stack =
00048 #endif
00049 
00050 #if defined(__APPLE__)
00051   #if defined(WITH_SDL)
00052     /* the mac implementation needs this file included in the same file as main() */
00053     #include <SDL.h>
00054   #endif
00055 #endif
00056 
00057 bool FiosIsRoot(const char *path)
00058 {
00059 #if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
00060   return path[1] == '\0';
00061 #else
00062   /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
00063   const char *s = strchr(path, ':');
00064   return s != NULL && s[1] == '\0';
00065 #endif
00066 }
00067 
00068 void FiosGetDrives()
00069 {
00070   return;
00071 }
00072 
00073 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
00074 {
00075   uint64 free = 0;
00076 
00077 #ifdef __APPLE__
00078   struct statfs s;
00079 
00080   if (statfs(path, &s) != 0) return false;
00081   free = (uint64)s.f_bsize * s.f_bavail;
00082 #elif defined(HAS_STATVFS)
00083   struct statvfs s;
00084 
00085   if (statvfs(path, &s) != 0) return false;
00086   free = (uint64)s.f_frsize * s.f_bavail;
00087 #endif
00088   if (tot != NULL) *tot = free;
00089   return true;
00090 }
00091 
00092 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
00093 {
00094   char filename[MAX_PATH];
00095 
00096 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
00097   /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
00098   if (FiosIsRoot(path)) {
00099     snprintf(filename, lengthof(filename), "%s:%s", path, ent->d_name);
00100   } else // XXX - only next line!
00101 #else
00102   assert(path[strlen(path) - 1] == PATHSEPCHAR);
00103   if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
00104 #endif
00105   snprintf(filename, lengthof(filename), "%s%s", path, ent->d_name);
00106 
00107   return stat(filename, sb) == 0;
00108 }
00109 
00110 bool FiosIsHiddenFile(const struct dirent *ent)
00111 {
00112   return ent->d_name[0] == '.';
00113 }
00114 
00115 #ifdef WITH_ICONV
00116 
00117 #include <iconv.h>
00118 #include <errno.h>
00119 #include "../../debug.h"
00120 #include "../../string_func.h"
00121 
00122 const char *GetCurrentLocale(const char *param);
00123 
00124 #define INTERNALCODE "UTF-8"
00125 
00129 static const char *GetLocalCode()
00130 {
00131 #if defined(__APPLE__)
00132   return "UTF-8-MAC";
00133 #else
00134   /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
00135   const char *locale = GetCurrentLocale("LC_CTYPE");
00136   if (locale != NULL) locale = strchr(locale, '.');
00137 
00138   return (locale == NULL) ? "" : locale + 1;
00139 #endif
00140 }
00141 
00146 static const char *convert_tofrom_fs(iconv_t convd, const char *name)
00147 {
00148   static char buf[1024];
00149   /* Work around buggy iconv implementation where inbuf is wrongly typed as
00150    * non-const. Correct implementation is at
00151    * http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.html */
00152 #ifdef HAVE_BROKEN_ICONV
00153   char *inbuf = const_cast<char*>(name);
00154 #else
00155   const char *inbuf = name;
00156 #endif
00157 
00158   char *outbuf  = buf;
00159   size_t outlen = sizeof(buf) - 1;
00160   size_t inlen  = strlen(name);
00161 
00162   strecpy(outbuf, name, outbuf + outlen);
00163 
00164   iconv(convd, NULL, NULL, NULL, NULL);
00165   if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (size_t)(-1)) {
00166     DEBUG(misc, 0, "[iconv] error converting '%s'. Errno %d", name, errno);
00167   }
00168 
00169   *outbuf = '\0';
00170   /* FIX: invalid characters will abort conversion, but they shouldn't occur? */
00171   return buf;
00172 }
00173 
00177 const char *OTTD2FS(const char *name)
00178 {
00179   static iconv_t convd = (iconv_t)(-1);
00180 
00181   if (convd == (iconv_t)(-1)) {
00182     const char *env = GetLocalCode();
00183     convd = iconv_open(env, INTERNALCODE);
00184     if (convd == (iconv_t)(-1)) {
00185       DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
00186       return name;
00187     }
00188   }
00189 
00190   return convert_tofrom_fs(convd, name);
00191 }
00192 
00196 const char *FS2OTTD(const char *name)
00197 {
00198   static iconv_t convd = (iconv_t)(-1);
00199 
00200   if (convd == (iconv_t)(-1)) {
00201     const char *env = GetLocalCode();
00202     convd = iconv_open(INTERNALCODE, env);
00203     if (convd == (iconv_t)(-1)) {
00204       DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
00205       return name;
00206     }
00207   }
00208 
00209   return convert_tofrom_fs(convd, name);
00210 }
00211 
00212 #else
00213 const char *FS2OTTD(const char *name) {return name;}
00214 const char *OTTD2FS(const char *name) {return name;}
00215 #endif /* WITH_ICONV */
00216 
00217 void ShowInfo(const char *str)
00218 {
00219   fprintf(stderr, "%s\n", str);
00220 }
00221 
00222 #if !defined(__APPLE__)
00223 void ShowOSErrorBox(const char *buf, bool system)
00224 {
00225   /* All unix systems, except OSX. Only use escape codes on a TTY. */
00226   if (isatty(fileno(stderr))) {
00227     fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
00228   } else {
00229     fprintf(stderr, "Error: %s\n", buf);
00230   }
00231 }
00232 #endif
00233 
00234 #ifdef WITH_COCOA
00235 void cocoaSetupAutoreleasePool();
00236 void cocoaReleaseAutoreleasePool();
00237 #endif
00238 
00239 int CDECL main(int argc, char *argv[])
00240 {
00241   int ret;
00242 
00243 #ifdef WITH_COCOA
00244   cocoaSetupAutoreleasePool();
00245   /* This is passed if we are launched by double-clicking */
00246   if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
00247     argv[1] = NULL;
00248     argc = 1;
00249   }
00250 #endif
00251   CrashLog::InitialiseCrashLog();
00252 
00253   SetRandomSeed(time(NULL));
00254 
00255   signal(SIGPIPE, SIG_IGN);
00256 
00257   ret = ttd_main(argc, argv);
00258 
00259 #ifdef WITH_COCOA
00260   cocoaReleaseAutoreleasePool();
00261 #endif
00262 
00263   return ret;
00264 }
00265 
00266 #ifndef WITH_COCOA
00267 bool GetClipboardContents(char *buffer, size_t buff_len)
00268 {
00269   return false;
00270 }
00271 #endif
00272 
00273 
00274 /* multi os compatible sleep function */
00275 
00276 #ifdef __AMIGA__
00277 /* usleep() implementation */
00278 # include <devices/timer.h>
00279 # include <dos/dos.h>
00280 
00281   extern struct Device      *TimerBase    = NULL;
00282   extern struct MsgPort     *TimerPort    = NULL;
00283   extern struct timerequest *TimerRequest = NULL;
00284 #endif /* __AMIGA__ */
00285 
00286 void CSleep(int milliseconds)
00287 {
00288   #if defined(PSP)
00289     sceKernelDelayThread(milliseconds * 1000);
00290   #elif defined(__BEOS__)
00291     snooze(milliseconds * 1000);
00292   #elif defined(__AMIGA__)
00293   {
00294     ULONG signals;
00295     ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
00296 
00297     /* send IORequest */
00298     TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
00299     TimerRequest->tr_time.tv_secs    = (milliseconds * 1000) / 1000000;
00300     TimerRequest->tr_time.tv_micro   = (milliseconds * 1000) % 1000000;
00301     SendIO((struct IORequest *)TimerRequest);
00302 
00303     if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
00304       AbortIO((struct IORequest *)TimerRequest);
00305     }
00306     WaitIO((struct IORequest *)TimerRequest);
00307   }
00308   #else
00309     usleep(milliseconds * 1000);
00310   #endif
00311 }

Generated on Wed Jan 20 23:38:37 2010 for OpenTTD by  doxygen 1.5.6