/* * File util.c - file and directory i/o * * $Id: util.c,v 1.7 2004/07/07 16:43:13 iskywalker Exp $ * * Program XBLAST * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net) * * 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; or (at your option) * any later version * * This program is distributed in the hope that it will be entertaining, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILTY 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 */ #include "util.h" #ifdef WINMS32 #include #include #include #endif #include "str_util.h" #include "xblast.h" /* * local macros */ #define NUM_XBLAST_PATH 4 #ifndef XBLASTDIR #define XBLASTDIR NULL #endif /* * local variables */ static int initPath = 0; static char *pathList[NUM_XBLAST_PATH] = { NULL, NULL, ".", "/", }; static char zeile [1024]; /* * set paths for loading and saving */ static void InitPaths (void) { static char userPath[1024]; char tmp[1024]; char *home; /* set private xblast path */ home = getenv ("HOME"); if (NULL == home) { strcpy (userPath, "./user"); } else { sprintf (userPath, "%s/.xblast_tnt", home); } #ifdef WINMS32 mkdir (userPath); /* config subdirs */ sprintf (tmp, "%s/config", userPath); mkdir (tmp); sprintf (tmp, "%s/demo", userPath); mkdir (tmp); sprintf (tmp, "%s/central", userPath); mkdir (tmp); #else mkdir (userPath, 0755); /* config subdirs */ sprintf (tmp, "%s/config", userPath); mkdir (tmp, 0700); sprintf (tmp, "%s/demo", userPath); mkdir (tmp, 0700); sprintf (tmp, "%s/central", userPath); mkdir (tmp, 0700); #endif /* set user path as first path to load from */ pathList[0] = userPath; /* check environment for xblast search path */ pathList[1] = getenv ("XBLASTDIR"); /* --- */ initPath = 1; } /* InitPaths */ /* * find and open an xblast data file. */ FILE * FileOpen (const char *path, const char *name, const char *ext, const char *mode) { FILE *fp; int i; /* sanity check */ assert (NULL != path); assert (NULL != name); assert (NULL != ext); assert (NULL != mode); /* setup paths for loading */ if (! initPath) { InitPaths (); } /* try to open file, in directories given in path list */ for (i = 0; i < NUM_XBLAST_PATH; i++) { if (NULL != pathList[i]) { /* create full path to file */ sprintf (zeile, "%s/%s/%s.%s", pathList[i], path, name, ext); /* try to open it */ if (NULL != (fp = fopen (zeile, mode) ) ) { /* file opened succesfully, use fclose to close it */ return fp; } } } /* sorry file opening failed */ fprintf (stderr, "failed to open file \"%s/%s.%s\".\n", path, name, ext); return NULL; } /* FileOpen */ /* * */ static void AddToFileList (XBDir **pList, const char *name, size_t len, time_t mtime) { XBDir *ptr; XBDir *next; XBDir *prev = NULL; int cmp; char tmp[256]; assert (pList != NULL); assert (name != NULL); assert (len < 256 - 1); prev = NULL; strncpy (tmp, name, len); tmp[len] = 0; for (ptr = *pList; ptr != NULL; ptr = ptr->next) { cmp = strcmp (ptr->name, tmp); if (cmp == 0) { /* element already exists */ return; } else if (cmp > 0) { break; } prev = ptr; } /* create new element */ ptr = calloc (1, sizeof (XBDir)); assert (ptr != NULL); ptr->name = DupStringNum (name, len); ptr->mtime = mtime; /* insert element after prev */ if (prev == NULL) { next = *pList; *pList = ptr; } else { next = prev->next; prev->next = ptr; } ptr->next = next; } /* AddToFileList */ /* * */ XBDir * CreateFileList (const char *path, const char *ext) { int i; XBDir *list = NULL; #ifdef WINMS32 WIN32_FIND_DATA find_data; HANDLE dp = NULL; size_t lenExt; size_t lenName; struct _stat buf; char *pFile; static char dirName[1024]; static char fileName[1024]; char dirToOpen[256]; XBBool testFile = XBTrue; #else DIR *dp; struct dirent *dirp; size_t lenExt; size_t lenName; struct stat buf; char *pFile; static char dirName[1024]; static char fileName[1024]; #endif /* sanity check */ assert (NULL != path); assert (NULL != ext); assert ('.' != ext[0]); //assert ('.' != path[0]); // assert ('/' != path[0]); /* set load paths if needed */ if (! initPath) { InitPaths (); } /* search all directories */ lenExt = strlen (ext); for (i=0; i