Skip to content

Commit

Permalink
support for msvc (2010 tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghazel committed Mar 27, 2012
1 parent 356c969 commit a8d1044
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CDEBUGFLAGS = -Os -g -Wall -fno-strict-aliasing
# On mingw, you need

# EXE=.exe
# LDLIBS = -lwsock32
# LDLIBS = -lws2_32

FILE_DEFINES = -DLOCAL_ROOT=\"$(LOCAL_ROOT)/\" \
-DDISK_CACHE_ROOT=\"$(DISK_CACHE_ROOT)/\"
Expand Down
149 changes: 149 additions & 0 deletions dirent_compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
Implementation of POSIX directory browsing functions and types for Win32.
Author: Kevlin Henney ([email protected], [email protected])
History: Created March 1997. Updated June 2003.
Rights: See end of file.
*/

#ifdef WIN32

#include "dirent_compat.h"
#include <errno.h>
#include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C"
{
#endif

struct DIR
{
long handle; /* -1 for failed rewind */
struct _finddata_t info;
struct dirent result; /* d_name null iff first time */
char *name; /* null-terminated char string */
};

DIR *opendir(const char *name)
{
DIR *dir = 0;

if(name && name[0])
{
size_t base_length = strlen(name);
const char *all = /* search pattern must end with suitable wildcard */
strchr("/\\", name[base_length - 1]) ? "*" : "/*";

if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
(dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
{
strcat(strcpy(dir->name, name), all);

if((dir->handle = (long) _findfirst(dir->name, &dir->info)) != -1)
{
dir->result.d_name = 0;
}
else /* rollback */
{
free(dir->name);
free(dir);
dir = 0;
}
}
else /* rollback */
{
free(dir);
dir = 0;
errno = ENOMEM;
}
}
else
{
errno = EINVAL;
}

return dir;
}

int closedir(DIR *dir)
{
int result = -1;

if(dir)
{
if(dir->handle != -1)
{
result = _findclose(dir->handle);
}

free(dir->name);
free(dir);
}

if(result == -1) /* map all errors to EBADF */
{
errno = EBADF;
}

return result;
}

struct dirent *readdir(DIR *dir)
{
struct dirent *result = 0;

if(dir && dir->handle != -1)
{
if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
{
result = &dir->result;
result->d_name = dir->info.name;
}
}
else
{
errno = EBADF;
}

return result;
}

void rewinddir(DIR *dir)
{
if(dir && dir->handle != -1)
{
_findclose(dir->handle);
dir->handle = (long) _findfirst(dir->name, &dir->info);
dir->result.d_name = 0;
}
else
{
errno = EBADF;
}
}

#ifdef __cplusplus
}
#endif

#endif

/*
Copyright Kevlin Henney, 1997, 2003. All rights reserved.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose is hereby granted without fee, provided
that this copyright and permissions notice appear in all copies and
derivatives.
This software is supplied "as is" without express or implied warranty.
But that said, if there are any problems please get in touch.
*/
50 changes: 50 additions & 0 deletions dirent_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef DIRENT_INCLUDED
#define DIRENT_INCLUDED

/*
Declaration of POSIX directory browsing functions and types for Win32.
Author: Kevlin Henney ([email protected], [email protected])
History: Created March 1997. Updated June 2003.
Rights: See end of file.
*/

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct DIR DIR;

struct dirent
{
char *d_name;
};

DIR *opendir(const char *);
int closedir(DIR *);
struct dirent *readdir(DIR *);
void rewinddir(DIR *);

/*
Copyright Kevlin Henney, 1997, 2003. All rights reserved.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose is hereby granted without fee, provided
that this copyright and permissions notice appear in all copies and
derivatives.
This software is supplied "as is" without express or implied warranty.
But that said, if there are any problems please get in touch.
*/

#ifdef __cplusplus
}
#endif

#endif
6 changes: 5 additions & 1 deletion diskcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ urlDirname(char *buf, int n, const char *url, int len)
return -1;

if(diskCacheRoot == NULL ||
diskCacheRoot->length <= 0 || diskCacheRoot->string[0] != '/')
diskCacheRoot->length <= 0
#ifndef WIN32
|| diskCacheRoot->string[0] != '/'
#endif
)
return -1;

if(n <= diskCacheRoot->length)
Expand Down
2 changes: 2 additions & 0 deletions event.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ typedef struct _Condition {

void initEvents(void);
void uninitEvents(void);
#ifdef HAVE_FORK
void interestingSignals(sigset_t *ss);
#endif

TimeEventHandlerPtr scheduleTimeEvent(int seconds,
int (*handler)(TimeEventHandlerPtr),
Expand Down
16 changes: 10 additions & 6 deletions fts_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ THE SOFTWARE.

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#ifndef _WIN32
#include <unistd.h>
#include <dirent.h>
#else
#include "dirent_compat.h"
#endif
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -281,13 +285,13 @@ fts_read(FTS *fts)
name = dirent->d_name;

again2:
rc = stat(name, &fts->stat);
rc = stat(name, &fts->ftstat);
if(rc < 0) {
fts->ftsent.fts_info = FTS_NS;
goto error2;
}

if(S_ISDIR(fts->stat.st_mode)) {
if(S_ISDIR(fts->ftstat.st_mode)) {
char *newcwd;
DIR *dir;

Expand Down Expand Up @@ -318,11 +322,11 @@ fts_read(FTS *fts)
fts->depth++;
fts->dir[fts->depth] = dir;
goto done;
} else if(S_ISREG(fts->stat.st_mode)) {
} else if(S_ISREG(fts->ftstat.st_mode)) {
fts->ftsent.fts_info = FTS_F;
goto done;
#ifdef S_ISLNK
} else if(S_ISLNK(fts->stat.st_mode)) {
} else if(S_ISLNK(fts->ftstat.st_mode)) {
int rc;
rc = readlink(name, buf, 1024);
if(rc < 0)
Expand Down Expand Up @@ -350,7 +354,7 @@ fts_read(FTS *fts)
fts->ftsent.fts_path = mkfilename(fts->cwd, name);
if(fts->ftsent.fts_path == NULL) goto error;
fts->ftsent.fts_accpath = name;
fts->ftsent.fts_statp = &fts->stat;
fts->ftsent.fts_statp = &fts->ftstat;
return &fts->ftsent;

error:
Expand Down
2 changes: 1 addition & 1 deletion fts_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct _FTS {
DIR *dir[FTS_MAX_DEPTH];
char *cwd0, *cwd;
struct _FTSENT ftsent;
struct stat stat;
struct stat ftstat;
char *dname;
};

Expand Down
1 change: 0 additions & 1 deletion log.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void really_do_log_error_v(int type, int e, const char *f, va_list args)
ATTRIBUTE ((format (printf, 3, 0)));
const char *scrub(const char *message);


#ifdef __GNUC__
#define DO_BACKTRACE() \
do { \
Expand Down
14 changes: 14 additions & 0 deletions mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ static int dummy ATTRIBUTE((unused));
/* Windows needs this header file for the implementation of inet_aton() */
#include <ctype.h>

/* _snprintf and friends have broken NULL termination semantics */
int win32_snprintf(char* dest, size_t count, const char* format, ...)
{
int r;
va_list args;
va_start(args, format);
r = _vsnprintf(dest, count, format, args);
va_end(args);
if (count > 0) {
dest[count-1] = '\0';
}
return r;
}

/*
* Check whether "cp" is a valid ascii representation of an Internet address
* and convert to a binary address. Returns 1 if the address is valid, 0 if
Expand Down
Loading

0 comments on commit a8d1044

Please sign in to comment.