POSIX compliance for get_saved_games().

This commit is contained in:
warwick
2003-11-07 04:47:26 +00:00
parent 0834d51482
commit 2d9aba259d

View File

@@ -33,7 +33,9 @@ extern int errno;
#endif #endif
#if defined(UNIX) && defined(QT_GRAPHICS) #if defined(UNIX) && defined(QT_GRAPHICS)
#include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <stdlib.h>
#endif #endif
#if defined(UNIX) || defined(VMS) #if defined(UNIX) || defined(VMS)
@@ -144,6 +146,9 @@ extern char *sounddir;
extern int n_dgns; /* from dungeon.c */ extern int n_dgns; /* from dungeon.c */
#if defined(UNIX) && defined(QT_GRAPHICS)
STATIC_DCL int FDECL(strcmp_wrap, (const void *, const void *));
#endif
STATIC_DCL char *FDECL(set_bonesfile_name, (char *,d_level*)); STATIC_DCL char *FDECL(set_bonesfile_name, (char *,d_level*));
STATIC_DCL char *NDECL(set_bonestemp_name); STATIC_DCL char *NDECL(set_bonestemp_name);
#ifdef COMPRESS #ifdef COMPRESS
@@ -537,6 +542,16 @@ clearlocks()
#endif #endif
} }
#if defined(UNIX) && defined(QT_GRAPHICS)
STATIC_OVL int
strcmp_wrap(p, q)
const void *p;
const void *q;
{
return strncasecmp(*(char **) p, *(char **) q, 16);
}
#endif
#ifdef HOLD_LOCKFILE_OPEN #ifdef HOLD_LOCKFILE_OPEN
STATIC_OVL int STATIC_OVL int
open_levelfile_exclusively(name, lev, oflag) open_levelfile_exclusively(name, lev, oflag)
@@ -997,33 +1012,45 @@ char**
get_saved_games() get_saved_games()
{ {
#if defined(UNIX) && defined(QT_GRAPHICS) #if defined(UNIX) && defined(QT_GRAPHICS)
/* posixly correct version */
int myuid=getuid(); int myuid=getuid();
struct dirent **namelist; DIR *dir;
int n = scandir("save", &namelist, 0, alphasort);; if((dir=opendir("save"))) {
if ( n > 0 ) { int n;
int i,j=0; for(n=0; readdir(dir); n++)
char** result = (char**)alloc((n+1)*sizeof(char*)); /* at most */ ;
for (i=0; i<n; i++) { closedir(dir);
int uid; if(n>0) {
char name[64]; /* more than PL_NSIZ */ int i,j=0;
if ( sscanf( namelist[i]->d_name, "%d%63s", &uid, name ) == 2 ) { char **result;
if ( uid == myuid ) { if(!(dir=opendir("save")))
char filename[BUFSZ]; return 0;
char* r; result = (char**)alloc((n+1)*sizeof(char*)); /* at most */
Sprintf(filename,"save/%d%s",uid,name); for (i=0; i<n; i++) {
r = plname_from_file(filename); int uid;
if ( r ) char name[64]; /* more than PL_NSIZ */
result[j++] = r; struct dirent *entry=readdir(dir);
if(!entry)
break;
if ( sscanf( entry->d_name, "%d%63s", &uid, name ) == 2 ) {
if ( uid == myuid ) {
char filename[BUFSZ];
char* r;
Sprintf(filename,"save/%d%s",uid,name);
r = plname_from_file(filename);
if ( r )
result[j++] = r;
}
}
} }
} closedir(dir);
qsort(result, j, sizeof(char *), strcmp_wrap);
result[j++] = 0;
return result;
} }
result[j++] = 0;
return result;
} else
#endif
{
return 0;
} }
#endif
return 0;
} }
void void