diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 89b75927a..d4fd3192b 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,7 @@ Changes towards version 0.2: +- Key config is now saved in user dir too. + melee.cfg too again. - SvdB +- Added copyFile() - SvdB - Pure mode partial screen updates are now more efficient; fixed 'crossfades not finished' problem -Mika - Graduated colours for crew in shipyard, from Nic diff --git a/sc2/src/msvc++/UrQuanMasters.dsp b/sc2/src/msvc++/UrQuanMasters.dsp index d53f58a8b..a98b3ee80 100644 --- a/sc2/src/msvc++/UrQuanMasters.dsp +++ b/sc2/src/msvc++/UrQuanMasters.dsp @@ -137,6 +137,10 @@ SOURCE=..\sc2code\libs\file\dirs.c # End Source File # Begin Source File +SOURCE=..\sc2code\libs\file\files.c +# End Source File +# Begin Source File + SOURCE=..\sc2code\libs\file\filintrn.h # End Source File # Begin Source File diff --git a/sc2/src/sc2code/libs/file.h b/sc2/src/sc2code/libs/file.h index 9d42f6c67..5ecea6792 100644 --- a/sc2/src/sc2code/libs/file.h +++ b/sc2/src/sc2code/libs/file.h @@ -33,8 +33,18 @@ # define W_OK 2 # define R_OK 4 # include +# define open _open # define mkdir _mkdir +# define read _read # define rmdir _rmdir +# define ssize_t int +# define fstat _fstat +# define S_IRWXU (S_IREAD | S_IWRITE | S_IEXEC) +# define S_IRWXG 0 +# define S_IRWXO 0 +# define write _write +# define stat _stat +# define unlink _unlink #else # include #endif @@ -42,15 +52,22 @@ // for BOOLEAN #include "compiler.h" +// from temp.h void initTempDir (void); void unInitTempDir (void); char *tempFilePath (const char *filename); + + +// from dirs.h int mkdirhier (const char *path); const char *getHomeDir (void); int createDirectory (const char *dir, int mode); int expandPath (char *dest, size_t len, const char *src); + +// from files.h +int copyFile (const char *srcName, const char *newName); BOOLEAN fileExists (const char *name); -int getUserDataDir (char *dir, size_t len); + #endif /* _FILE_H */ diff --git a/sc2/src/sc2code/libs/file/Makeinfo b/sc2/src/sc2code/libs/file/Makeinfo index 4024c4b8e..6ab667de2 100644 --- a/sc2/src/sc2code/libs/file/Makeinfo +++ b/sc2/src/sc2code/libs/file/Makeinfo @@ -1 +1 @@ -uqm_CFILES="dirs.c temp.c" +uqm_CFILES="dirs.c files.c temp.c" diff --git a/sc2/src/sc2code/libs/file/dirs.c b/sc2/src/sc2code/libs/file/dirs.c index 028fa3b1a..b2bab5910 100644 --- a/sc2/src/sc2code/libs/file/dirs.c +++ b/sc2/src/sc2code/libs/file/dirs.c @@ -322,9 +322,3 @@ expandPath (char *dest, size_t len, const char *src) return 0; } -BOOLEAN -fileExists (const char *name) -{ - return access (name, F_OK) == 0; -} - diff --git a/sc2/src/sc2code/libs/file/files.c b/sc2/src/sc2code/libs/file/files.c new file mode 100644 index 000000000..a834abcec --- /dev/null +++ b/sc2/src/sc2code/libs/file/files.c @@ -0,0 +1,130 @@ +/* + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +// Contains code handling files + +#include +#include +#include +#include +#include +#include "port.h" +#include "config.h" +#include "types.h" +#include "filintrn.h" + +static int copyError(int srcFd, int dstFd, const char *unlinkPath); + +BOOLEAN +fileExists (const char *name) +{ + return access (name, F_OK) == 0; +} + +/* + * Copy a file with path srcName to a file with name newName. + * If the destination already exists, the operation fails. + * Links are followed. + * Special files (fifos, char devices, block devices, etc) will be + * read as long as there is data available and the destination will be + * a regular file with that data. + * The new file will have the same permissions as the old. + * If an error occurs during copying, an attempt will be made to + * remove the copy. + */ +int +copyFile (const char *srcName, const char *newName) +{ + int src, dst; + struct stat sb; +#define BUFSIZE 65536 + uint8 buf[BUFSIZE], *bufPtr; + ssize_t numInBuf, numWritten; + + src = open (srcName, O_RDONLY); + if (src == -1) + return -1; + + if (fstat (src, &sb) == -1) + return copyError (src, -1, NULL); + + dst = open (newName, O_WRONLY | O_CREAT | O_EXCL, + sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); + if (dst == -1) + return copyError (src, -1, NULL); + + while (1) { + numInBuf = read (src, buf, BUFSIZE); + if (numInBuf == -1) + { + if (errno == EINTR) + continue; + return copyError (src, dst, newName); + } + if (numInBuf == 0) + break; + + bufPtr = buf; + do { + numWritten = write (dst, bufPtr, numInBuf); + if (numWritten == -1) + { + if (errno == EINTR) + continue; + return copyError (src, dst, newName); + } + numInBuf -= numWritten; + bufPtr += numWritten; + } while (numInBuf > 0); + } + + close(src); + close(dst); + errno = 0; + return 0; +} + +/* + * Closes srcFd if it's not -1. + * Closes dstFd if it's not -1. + * Removes unlinkpath if it's not NULL. + * Always returns -1. + * errno is what was before the call. + */ +static int +copyError(int srcFd, int dstFd, const char *unlinkPath) +{ + int savedErrno; + + savedErrno = errno; + +#ifdef DEBUG + fprintf (stderr, "Error while copying: %s\n", strerror (errno)) +#endif + + if (srcFd >= 0) + close (srcFd); + + if (dstFd >= 0) + close (dstFd); + + if (unlinkPath != NULL) + unlink (unlinkPath); + + errno = savedErrno; + return -1; +} + diff --git a/sc2/src/sc2code/libs/misc.h b/sc2/src/sc2code/libs/misc.h index 90d9efda8..125f95985 100644 --- a/sc2/src/sc2code/libs/misc.h +++ b/sc2/src/sc2code/libs/misc.h @@ -22,19 +22,6 @@ #ifndef MISC_H #define MISC_H -#define Stream FILE -#define MEMTYPE_ANY 1 - -#define SqrtF16 sqrt -#define Atan2F16 atan2 -#define CosF16 cos -#define SinF16 sin - -int CD_get_drive(void); // Returns 0 -unsigned int CD_get_volsize(void); // Returns 0 - -void OpenMathFolio(void); - extern void *HMalloc (int size); extern void HFree (void *p); extern void *HCalloc (int size); @@ -42,4 +29,8 @@ extern void *HRealloc (void *p, int size); extern int TFB_DEBUG_HALT; +// from misc.c: +size_t vstrncpy (char *dest, size_t destLen, ...); + #endif + diff --git a/sc2/src/sc2code/melee.c b/sc2/src/sc2code/melee.c index 0b8445349..7eddec8d2 100644 --- a/sc2/src/sc2code/melee.c +++ b/sc2/src/sc2code/melee.c @@ -17,6 +17,7 @@ */ #include +#include #include "starcon.h" #include "libs/graphics/drawable.h" #include "melee.h" @@ -1853,8 +1854,11 @@ Melee (void) LoadMeleeInfo (&MenuState); { FILE *load_fp; + char configFile[PATH_MAX]; - load_fp = res_OpenResFile ("melee.cfg", "rb"); + assert (snprintf (configFile, PATH_MAX, "%smelee.cfg", + configDir) != -1); + load_fp = res_OpenResFile (configFile, "rb"); if (load_fp) { int status; @@ -1899,9 +1903,12 @@ Melee (void) { FILE *save_fp; BOOLEAN err; - + char configFile[PATH_MAX]; + err = FALSE; - save_fp = res_OpenResFile ("melee.cfg", "wb"); + assert (snprintf (configFile, PATH_MAX, "%smelee.cfg", + configDir) != -1); + save_fp = res_OpenResFile (configFile, "wb"); if (save_fp) { if (PutResFileChar (PlayerControl[0], save_fp) == -1) diff --git a/sc2/src/sc2code/setup.c b/sc2/src/sc2code/setup.c index 7e74ad6e1..478460d95 100644 --- a/sc2/src/sc2code/setup.c +++ b/sc2/src/sc2code/setup.c @@ -16,10 +16,15 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include +#include +#include #include "starcon.h" #include "coderes.h" #include "libs/threadlib.h" #include "libs/graphics/gfx_common.h" +#include "libs/file.h" +#include "options.h" //Added by Chris @@ -265,6 +270,31 @@ LoadKeyConfig (FILE *fp, DWORD length) return (NULL_HANDLE); } +static void +initKeyConfig() { + char userFile[PATH_MAX]; /* System-wide key config */ + FILE *fp; + + assert (snprintf (userFile, PATH_MAX, "%skeys.cfg", configDir) != -1); + // strlen (configDir) <= PATH_MAX - 13 + + if (fileExists (userFile)) { + fp = res_OpenResFile (userFile, "rt"); + } else { + if (copyFile ("starcon.key", userFile) == -1) { + fprintf(stderr, "Warning: Could not copy default key config " + "to %s: %s.\n", userFile, strerror (errno)); + } else + fprintf(stderr, "Copying default key config file to %s.\n", + userFile); + fp = res_OpenResFile ("starcon.key", "rt"); + } + LoadKeyConfig (fp, 0); + // second arg is file length, but is unused. + // It should be temporary anyhow. + res_CloseResFile (fp); +} + static void InitPlayerInput (void) { @@ -335,7 +365,8 @@ LoadKernel (int argc, char *argv[]) INIT_INSTANCES (); InstallResTypeVectors (KEY_CONFIG, LoadKeyConfig, NULL_PTR); - res_GetResource (JOYSTICK_KEYS); +// res_GetResource (JOYSTICK_KEYS); + initKeyConfig(); { COLORMAP ColorMapTab;