/*
* This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
*
* Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
* Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992,2007 Frank Tore Johansen
*
* Crossfire TRT 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 3 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, see .
*
* The authors can be reached via e-mail to
*/
/* This file contains various functions that are not really unique for
* crossfire, but rather provides what should be standard functions
* for systems that do not have them. In this way, most of the
* nasty system dependent stuff is contained here, with the program
* calling these functions.
*/
/* Need to pull in the HAVE_... values somehow */
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* Has to be after above includes so we don't redefine some values */
#include "global.h"
/* This function removes everything in the directory. */
void
remove_directory (const char *path)
{
DIR *dirp;
char buf[MAX_BUF];
struct stat statbuf;
int status;
if ((dirp = opendir (path)) != NULL)
{
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);
}
}
closedir (dirp);
}
if (rmdir (path))
{
LOG (llevError, "Unable to remove directory %s\n", path);
}
}
#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
#define MBASE ('z' - 'a' + 1 + 10)
char *
strcasestr_local (const char *s, const char *find)
{
char c, sc;
size_t len;
if ((c = *find++) != 0)
{
c = tolower (c);
len = strlen (find);
do
{
do
{
if ((sc = *s++) == 0)
return NULL;
}
while (tolower (sc) != c);
}
while (strncasecmp (s, find, len) != 0);
s--;
}
return (char *) s;
}
/*
* returns a char-pointer to a static array, in which a representation
* of the decimal number given will be stored.
*/
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 */
snprintf (buf, sizeof (buf), "%ld", n);
return buf;
}
char *
doubletostr10 (double v)
{
static char tbuf[200];
sprintf (tbuf, "%f", v);
return tbuf;
}
/**
* open_and_uncompress() first searches for the original filename. If it exist,
* then it opens it and returns the file-pointer.
*/
FILE *
open_and_uncompress (const char *name, int flag, int *compressed)
{
*compressed = 0;
return fopen (name, "r");
}
/*
* See open_and_uncompress().
*/
void
close_and_delete (FILE * fp, int compressed)
{
fclose (fp);
}
/*
* If any directories in the given path doesn't exist, they are created.
*/
void
make_path_to_file (char *filename)
{
char buf[MAX_BUF], *cp = buf;
struct stat statbuf;
if (!filename || !*filename)
return;
assign (buf, filename);
while ((cp = strchr (cp + 1, (int) '/')))
{
*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;
}
}
*cp = '/';
}
}