You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
4.8 KiB
196 lines
4.8 KiB
/* $Id: sysfunc.c,v 1.17 2003/07/15 11:43:09 stpohle Exp $
|
|
sysfunc.c - this file hold some routines for the system functions..
|
|
like d_delay
|
|
*/
|
|
|
|
#include "bomberclone.h"
|
|
#include "sysfunc.h"
|
|
|
|
/* i have no banned usleep completly, even usleep would
|
|
bring the cpu usage more down. but i found out that
|
|
usleep is not as excact as select is. */
|
|
void
|
|
s_delay (int ms)
|
|
{
|
|
#ifdef _WIN32
|
|
Sleep (ms); /* i hope this works on windows.. on MSVC it
|
|
should but with MINGW i don't know yet */
|
|
#else
|
|
fd_set selectset;
|
|
struct timeval tval;
|
|
|
|
FD_ZERO (&selectset);
|
|
tval.tv_sec = 0;
|
|
tval.tv_usec = ms * 1000;
|
|
select (1, &selectset, NULL, NULL, &tval);
|
|
#endif
|
|
};
|
|
|
|
|
|
int
|
|
s_random (int maxnr)
|
|
{
|
|
#ifdef _WIN32
|
|
return ((rand () * maxnr) / RAND_MAX);
|
|
#else
|
|
int i;
|
|
|
|
i = (((rand () >> 16) * (maxnr + 1)) / (RAND_MAX >> 16));
|
|
if (i >= maxnr)
|
|
i = 0;
|
|
return i;
|
|
#endif
|
|
};
|
|
|
|
static char homedir[255];
|
|
|
|
char *
|
|
s_gethomedir ()
|
|
{
|
|
char *hd;
|
|
|
|
if ((hd = getenv ("HOME")) == NULL) {
|
|
/* Homedir konnte nicht ermittelt werden. */
|
|
homedir[0] = 0;
|
|
d_printf ("Variable HOME could not be found\n");
|
|
}
|
|
else {
|
|
strncpy (homedir, hd, 253);
|
|
homedir[strlen (homedir) + 1] = 0;
|
|
#ifdef _WIN32
|
|
if (homedir[strlen (homedir) - 1] != '\\')
|
|
homedir[strlen (homedir)] = '\\';
|
|
#else
|
|
if (homedir[strlen (homedir) - 1] != '/')
|
|
homedir[strlen (homedir)] = '/';
|
|
#endif
|
|
}
|
|
return homedir;
|
|
};
|
|
|
|
static _direntry direntrys[MAX_DIRENTRYS];
|
|
|
|
_direntry *
|
|
s_getdir (char *path)
|
|
{
|
|
int entrynr = 0;
|
|
|
|
#ifdef _WIN32
|
|
WIN32_FIND_DATA fdata;
|
|
HANDLE fhandle;
|
|
struct stat fstat;
|
|
char filename[LEN_PATHFILENAME];
|
|
|
|
sprintf (filename, "%s\\*.*", path);
|
|
d_printf ("Reading Dir [%s]\n", filename);
|
|
if ((fhandle = FindFirstFile (filename, &fdata)) != INVALID_HANDLE_VALUE) {
|
|
do {
|
|
d_printf (" Got Somthing [%s]\n",fdata.cFileName);
|
|
|
|
direntrys[entrynr].next = NULL;
|
|
strncpy (direntrys[entrynr].name, fdata.cFileName, LEN_FILENAME - 1);
|
|
if (strlen (fdata.cFileName) >= LEN_FILENAME)
|
|
direntrys[entrynr].name[LEN_FILENAME - 1] = 0;
|
|
sprintf (filename, "%s\\%s", path, direntrys[entrynr].name);
|
|
stat (filename, &fstat);
|
|
if (S_ISREG (fstat.st_mode)) {
|
|
direntrys[entrynr].flags = DF_file;
|
|
direntrys[entrynr].next = &direntrys[entrynr + 1];
|
|
entrynr++;
|
|
}
|
|
else if (S_ISDIR (fstat.st_mode)) {
|
|
direntrys[entrynr].flags = DF_dir;
|
|
direntrys[entrynr].next = &direntrys[entrynr + 1];
|
|
entrynr++;
|
|
}
|
|
} while (FindNextFile (fhandle, &fdata) && entrynr < MAX_DIRENTRYS);
|
|
|
|
FindClose (fhandle);
|
|
}
|
|
|
|
#else
|
|
DIR *dp;
|
|
struct dirent *ep;
|
|
struct stat fstat;
|
|
char filename[LEN_PATHFILENAME];
|
|
|
|
dp = opendir (path);
|
|
if (dp != NULL) {
|
|
while ((ep = readdir (dp)) != NULL && entrynr < MAX_DIRENTRYS) {
|
|
direntrys[entrynr].next = NULL;
|
|
strncpy (direntrys[entrynr].name, ep->d_name, LEN_FILENAME - 1);
|
|
if (strlen (ep->d_name) >= LEN_FILENAME)
|
|
direntrys[entrynr].name[LEN_FILENAME - 1] = 0;
|
|
|
|
sprintf (filename, "%s/%s", path, direntrys[entrynr].name);
|
|
stat (filename, &fstat);
|
|
if (S_ISREG (fstat.st_mode)) {
|
|
direntrys[entrynr].flags = DF_file;
|
|
direntrys[entrynr].next = &direntrys[entrynr + 1];
|
|
entrynr++;
|
|
}
|
|
else if (S_ISDIR (fstat.st_mode)) {
|
|
direntrys[entrynr].flags = DF_dir;
|
|
direntrys[entrynr].next = &direntrys[entrynr + 1];
|
|
entrynr++;
|
|
}
|
|
}
|
|
closedir (dp);
|
|
}
|
|
#endif
|
|
d_printf ("Readin %d Entrys in the Directory\n", entrynr);
|
|
if (entrynr == 0)
|
|
return NULL;
|
|
direntrys[entrynr - 1].next = NULL;
|
|
|
|
return &direntrys[0];
|
|
};
|
|
|
|
_direntry *
|
|
s_dirfilter (_direntry * dirstart, signed char dirflags)
|
|
{
|
|
_direntry *newstart = NULL,
|
|
*pos = NULL,
|
|
*old = NULL;
|
|
|
|
for (pos = dirstart; pos != NULL; pos = pos->next)
|
|
if (pos->name[0] != '.' && (pos->flags & dirflags) != 0) {
|
|
if (newstart == NULL) {
|
|
newstart = pos;
|
|
old = pos;
|
|
}
|
|
else {
|
|
old->next = pos;
|
|
old = pos;
|
|
}
|
|
}
|
|
|
|
if (old != NULL)
|
|
old->next = NULL;
|
|
|
|
return newstart;
|
|
};
|
|
|
|
|
|
/* count the bits ... for directions and so on */
|
|
int s_countbits (int bits, int nr) {
|
|
int i, r = 0;
|
|
|
|
for (i = nr-1; i >= 0; i--)
|
|
if ((bits & (1 << i)) != 0)
|
|
r++;
|
|
|
|
return r;
|
|
}
|
|
|
|
|
|
// Return only the file name
|
|
char* getfilename(char* path)
|
|
{
|
|
int i;
|
|
for(i=strlen(path);i>=0;i--)
|
|
if(path[i] == '\\' || path[i] == '/')
|
|
return path+i+1;
|
|
return path;
|
|
}
|