fragglet / lhasa

Free Software LHA implementation
http://fragglet.github.io/lhasa/
ISC License
80 stars 15 forks source link

MinGW compilation #4

Closed roytam1 closed 12 years ago

roytam1 commented 12 years ago

I'd just hacked around it(especially the file-related APIs). PMA archive and -lh0- works in windows.

diff --git a/configure.ac b/configure.ac
index 9855a61..85add98 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,6 +6,7 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 AM_PROG_CC_C_O

 AC_PROG_CXX
+AC_PROG_RANLIB
 AC_PROG_LIBTOOL
 AC_PROG_INSTALL
 AC_PROG_MAKE_SET
diff --git a/lib/lha_input_stream.c b/lib/lha_input_stream.c
index d0d4de4..45502b6 100644
--- a/lib/lha_input_stream.c
+++ b/lib/lha_input_stream.c
@@ -290,13 +290,14 @@ int lha_input_stream_skip(LHAInputStream *stream, size_t bytes)
 static int file_source_read(void *handle, void *buf, size_t buf_len)
 {
    size_t bytes_read;
+   FILE *fh = (FILE *)handle;

-   bytes_read = fread(buf, 1, buf_len, handle);
+   bytes_read = fread(buf, 1, buf_len, fh);

    // If an error occurs, zero is returned; however, it may also
    // indicate end of file.

-   if (bytes_read == 0 && !feof(handle)) {
+   if (bytes_read == 0 && !feof(fh)) {
        return -1;
    }

diff --git a/lib/lha_reader.c b/lib/lha_reader.c
index 7a532f8..12070c6 100644
--- a/lib/lha_reader.c
+++ b/lib/lha_reader.c
@@ -356,12 +356,14 @@ static FILE *open_output_file_unix(LHAReader *reader, char *filename)
    // Set owner and group.

    if (reader->curr_file->extra_flags & LHA_FILE_UNIX_UID_GID) {
+#ifndef __MINGW32__
        if (fchown(fileno, reader->curr_file->unix_uid,
                   reader->curr_file->unix_gid)) {
            close(fileno);
            remove(filename);
            return NULL;
        }
+#endif
    }

    // Set file permissions.
@@ -370,11 +372,13 @@ static FILE *open_output_file_unix(LHAReader *reader, char *filename)
    // to the wrong group.

    if (reader->curr_file->extra_flags & LHA_FILE_UNIX_PERMS) {
+#ifndef __MINGW32__
        if (fchmod(fileno, reader->curr_file->unix_perms)) {
            close(fileno);
            remove(filename);
            return NULL;
        }
+#endif
    }

    // Create stdc FILE handle.
@@ -437,7 +441,11 @@ static int create_directory_unix(LHAFileHeader *header, char *path)
        mode = 0777;
    }

+#ifdef __MINGW32__
+   return mkdir(path) == 0;
+#else
    return mkdir(path, mode) == 0;
+#endif
 }

 // Set timestamp for the specified file / directory.
@@ -467,9 +475,11 @@ static int set_directory_metadata(LHAFileHeader *header, char *path)
    // Set owner and group:

    if (header->extra_flags & LHA_FILE_UNIX_UID_GID) {
+#ifndef __MINGW32__
        if (chown(path, header->unix_uid, header->unix_gid)) {
            return 0;
        }
+#endif
    }

    // Set permissions on directory:
fragglet commented 12 years ago

Hi,

Thanks for the patch! Would you mind make a couple of alterations?

Using MINGW32 means that it will only work with that compiler (and not with eg. MSVC). According to this, _WIN32 looks like it would be a better choice:

http://sourceforge.net/apps/mediawiki/predef/index.php?title=Operating_Systems#Windows

Is the change to lha_input_stream.c really necessary? ANSI C lets you cast (void *) to any pointer type, so I'm not sure why the intermediate variable is necessary.

roytam1 commented 12 years ago

Using MINGW32 means that it will only work with that compiler (and not with eg. MSVC). According to this, _WIN32 looks like it would be a better choice:

http://sourceforge.net/apps/mediawiki/predef/index.php?title=Operating_Systems#Windows

I didn't test on MSVC.

Is the change to lha_input_stream.c really necessary? ANSI C lets you cast (void *) to any pointer type, so I'm not sure why the intermediate variable is necessary.

I'm not sure if MinGW's feof() accept such casting as MinGW's feof() dereferences the pointer.

fragglet commented 12 years ago

I didn't test on MSVC.

I appreciate that; just saying: there are a lot of different compilers available on Windows. _WIN32 seems like a better choice if MingW32 supports it, which it looks like it does.

I'm not sure if MinGW's feof() accept such casting as MinGW's feof() dereferences the pointer.

Ah, I see. Fair enough.

fragglet commented 12 years ago

I fixed up your patch and committed it. Thanks!