--- deliantra/server/common/porting.C 2006/09/07 09:28:44 1.4 +++ deliantra/server/common/porting.C 2006/09/12 23:22:32 1.6 @@ -1,6 +1,7 @@ + /* * static char *rcsid_porting_c = - * "$Id: porting.C,v 1.4 2006/09/07 09:28:44 root Exp $"; + * "$Id: porting.C,v 1.6 2006/09/12 23:22:32 root Exp $"; */ /* @@ -34,32 +35,34 @@ */ -#ifdef WIN32 /* ---win32 exclude/include headers */ -#include "process.h" -#define pid_t int /* we include it non global, because there is a redefinition in python.h */ +#ifdef WIN32 /* ---win32 exclude/include headers */ +# include "process.h" +# define pid_t int /* we include it non global, because there is a redefinition in python.h */ #else -#include -#include -#include +# include +# include +# include -#include -#include +# include +# include /* Need to pull in the HAVE_... values somehow */ + /* win32 reminder: always put this in a ifndef win32 block */ -#include +# include #endif #ifdef HAVE_STDLIB_H -#include +# include #endif #ifdef HAVE_UNISTD_H -#include +# include #endif #include + /* Has to be after above includes so we don't redefine some values */ #include "global.h" @@ -82,7 +85,7 @@ /* HURD does not have a hard limit, but we do */ #ifndef MAXPATHLEN -#define MAXPATHLEN 4096 +# define MAXPATHLEN 4096 #endif if (!(name = (char *) malloc (MAXPATHLEN))) @@ -99,14 +102,14 @@ if (dir != NULL) { do - { + { #ifdef HAVE_SNPRINTF - (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); + (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); #else - (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); + (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); #endif - curtmp++; - } + curtmp++; + } while (access (name, F_OK) != -1); return (name); } @@ -129,30 +132,30 @@ struct dirent *de; for (de = readdir (dirp); de; de = readdir (dirp)) - { - /* Don't remove '.' or '..' In theory we should do a better - * check for .., but the directories we are removing are fairly - * limited and should not have dot files in them. - */ - if (de->d_name[0] == '.') - continue; - - /* Linux actually has a type field in the dirent structure, - * but that is not portable - stat should be portable - */ - status = stat (de->d_name, &statbuf); - if ((status != -1) && (S_ISDIR (statbuf.st_mode))) - { - sprintf (buf, "%s/%s", path, de->d_name); - remove_directory (buf); - continue; - } - sprintf (buf, "%s/%s", path, de->d_name); - if (unlink (buf)) - { - LOG (llevError, "Unable to remove directory %s\n", path); - } - } + { + /* Don't remove '.' or '..' In theory we should do a better + * check for .., but the directories we are removing are fairly + * limited and should not have dot files in them. + */ + if (de->d_name[0] == '.') + continue; + + /* Linux actually has a type field in the dirent structure, + * but that is not portable - stat should be portable + */ + status = stat (de->d_name, &statbuf); + if ((status != -1) && (S_ISDIR (statbuf.st_mode))) + { + sprintf (buf, "%s/%s", path, de->d_name); + remove_directory (buf); + continue; + } + sprintf (buf, "%s/%s", path, de->d_name); + if (unlink (buf)) + { + LOG (llevError, "Unable to remove directory %s\n", path); + } + } closedir (dirp); } if (rmdir (path)) @@ -163,11 +166,11 @@ #if defined(sgi) -#include -#include -#include +# include +# include +# include -#define popen fixed_popen +# define popen fixed_popen FILE * popen_local (const char *command, const char *type) @@ -175,48 +178,44 @@ int fd[2]; int pd; FILE *ret; + if (!strcmp (type, "r")) - { - pd = STDOUT_FILENO; - } + pd = STDOUT_FILENO; else if (!strcmp (type, "w")) - { - pd = STDIN_FILENO; - } + pd = STDIN_FILENO; else - { - return NULL; - } + return NULL; + if (pipe (fd) != -1) { switch (fork ()) - { - case -1: - close (fd[0]); - close (fd[1]); - break; - case 0: - close (fd[0]); - if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) - { - if (fd[1] != pd) - { - close (fd[1]); - } - execl ("/bin/sh", "sh", "-c", command, NULL); - close (pd); - } - exit (1); - break; - default: - close (fd[1]); - if (ret = fdopen (fd[0], type)) - { - return ret; - } - close (fd[0]); - break; - } + { + case -1: + close (fd[0]); + close (fd[1]); + break; + case 0: + close (fd[0]); + if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) + { + if (fd[1] != pd) + { + close (fd[1]); + } + execl ("/bin/sh", "sh", "-c", command, NULL); + close (pd); + } + _exit (1); + break; + default: + close (fd[1]); + if (ret = fdopen (fd[0], type)) + { + return ret; + } + close (fd[0]); + break; + } } return NULL; } @@ -228,8 +227,6 @@ * String related function ****************************************************************************/ - - /* * A replacement of strdup(), since it's not defined at some * unix variants. @@ -238,6 +235,7 @@ strdup_local (const char *str) { char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); + strcpy (c, str); return c; } @@ -259,7 +257,7 @@ c1 = tolower (*s1); c2 = tolower (*s2); if (c1 != c2) - return (c1 - c2); + return (c1 - c2); s1++; s2++; n--; @@ -281,7 +279,7 @@ c1 = tolower (*s1); c2 = tolower (*s2); if (c1 != c2) - return (c1 - c2); + return (c1 - c2); s1++; s2++; } @@ -302,14 +300,14 @@ c = tolower (c); len = strlen (find); do - { - do - { - if ((sc = *s++) == 0) - return NULL; - } - while (tolower (sc) != c); - } + { + do + { + if ((sc = *s++) == 0) + return NULL; + } + while (tolower (sc) != c); + } while (strncasecmp (s, find, len) != 0); s--; } @@ -349,6 +347,7 @@ isqrt (int n) { int result, sum, prev; + result = 0; prev = sum = 1; while (sum <= n) @@ -369,8 +368,8 @@ char * ltostr10 (signed long n) { - static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 - character for the trailing nul character */ + static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 + character for the trailing nul character */ snprintf (buf, sizeof (buf), "%ld", n); return buf; } @@ -379,6 +378,7 @@ doubletostr10 (double v) { static char tbuf[200]; + sprintf (tbuf, "%f", v); return tbuf; } @@ -402,7 +402,7 @@ */ void -close_and_delete (FILE *fp, int compressed) +close_and_delete (FILE * fp, int compressed) { fclose (fp); } @@ -425,13 +425,13 @@ { *cp = '\0'; if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) - { - if (mkdir (buf, SAVE_DIR_MODE)) - { - LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); - return; - } - } + { + if (mkdir (buf, SAVE_DIR_MODE)) + { + LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); + return; + } + } *cp = '/'; } }