platform_unix.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 28 Mar 2008 06:56:37 +0000
changeset 108 0b8bcaef83e7
parent 100 85e5ea217e82
permissions -rw-r--r--
Renamed some files, cleaned up the lack of headers on source files.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
108
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     1
/**
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     2
 * MojoPatch; a tool for updating data in the field.
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     3
 *
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     5
 *
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     6
 *  This file written by Ryan C. Gordon.
0b8bcaef83e7 Renamed some files, cleaned up the lack of headers on source files.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
     7
 */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include <stdio.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <string.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdlib.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <unistd.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <dirent.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include <sys/stat.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <sys/types.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <fcntl.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <sys/param.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include <errno.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include <assert.h>
66
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
    20
#include <sys/wait.h>
64
8f4e25f1ceb5 Gregory McLean pointed out that pthreads are needed; start to mark out the
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
    21
8f4e25f1ceb5 Gregory McLean pointed out that pthreads are needed; start to mark out the
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
    22
#if USE_PTHREAD
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#include <pthread.h>
64
8f4e25f1ceb5 Gregory McLean pointed out that pthreads are needed; start to mark out the
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
    24
#endif
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
#include "platform.h"
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#include "ui.h"
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
int file_exists(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    return(stat(fname, &statbuf) == 0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
} /* file_exists */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
int file_is_directory(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    if (stat(fname, &statbuf) == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
        if (S_ISDIR(statbuf.st_mode))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
} /* file_is_directory */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
int file_is_symlink(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    if (lstat(fname, &statbuf) == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
        if (S_ISLNK(statbuf.st_mode))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
} /* file_is_symlink */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
/* enumerate contents of (base) directory. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
file_list *make_filelist(const char *base)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    file_list *retval = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    file_list *l = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    file_list *prev = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    DIR *dir;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    struct dirent *ent;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    errno = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    dir = opendir(base);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    if (dir == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        _fatal("Error: could not read dir %s: %s.", base, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    while (1)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
        ent = readdir(dir);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        if (ent == NULL)   /* we're done. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
        if (strcmp(ent->d_name, ".") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        if (strcmp(ent->d_name, "..") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        /*
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
         * !!! FIXME: This is a workaround until symlinks are really
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
         * !!! FIXME:  supported...just pretend they don't exist for now.  :(
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
         */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
            char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
            snprintf(buf, sizeof (buf), "%s/%s", base, ent->d_name);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
            if (file_is_symlink(buf))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
                continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        }
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
        l = (file_list *) malloc(sizeof (file_list));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        if (l == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
            _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        l->fname = (char *) malloc(strlen(ent->d_name) + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        if (l->fname == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
            free(l);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
            _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
        strcpy(l->fname, ent->d_name);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
        if (retval == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
            retval = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
            prev->next = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
        prev = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
        l->next = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    closedir(dir);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
} /* make_filelist */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
98
4912346d49c9 Cleaned up some datatype sizes and handle byte order explicitly...this allows
Ryan C. Gordon <icculus@icculus.org>
parents: 97
diff changeset
   136
int get_file_size(const char *fname, unsigned int *fsize)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    assert(fsize != NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    if (stat(fname, &statbuf) == -1)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        _fatal("Error: failed to get filesize of [%s]: %s.",
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
                fname, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    *fsize = statbuf.st_size;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
} /* get_file_size */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
char *get_realpath(const char *path)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    char resolved_path[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    char *retval = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    errno = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    if (!realpath(path, resolved_path))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        _fatal("Can't determine full path of [%s]: %s.",
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
                path, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    retval = malloc(strlen(resolved_path) + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    if (retval == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    strcpy(retval, resolved_path);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
} /* get_realpath */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   179
#if PLATFORM_MACOSX
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
#include <ApplicationServices/ApplicationServices.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
static char *parse_xml(char *ptr, char **tag, char **val)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    char *ptr2;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    while ( (ptr = strchr(ptr, '<')) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    {
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   187
        ptr++;  /* skip past '<' to start of tag.*/
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   188
        if (*ptr == '/') continue;  /* prior endtag? */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   189
        if (*ptr == '!') continue;  /* initial crap? */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   190
        if (*ptr == '?') continue;  /* initial crap? */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        *tag = ptr;
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   193
        *(ptr-1) = '/';  /* prepend a '/' so we can search for the endtag. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   194
        ptr2 = strchr(ptr, ' '); /* look for a space (tag attributes?) */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        if ( (ptr = strchr(ptr, '>')) == NULL ) return(NULL);
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   196
        if ((ptr2) && (ptr2 < ptr)) *ptr2 = '\0';  /* chop out attributes. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   197
        *ptr = '\0';  /* chop off '<' at end of tag. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   198
        *val = ptr + 1;  /* (*val) == start of children of this tag. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   199
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   200
        /* Catch <tagname/> tags... */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   201
        ptr2 = ptr - 1;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   202
        while ( (ptr2 != *tag) && (isspace(*ptr2)) )
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   203
            ptr2--;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   204
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   205
        if ((ptr2 != *tag) && (*ptr2 == '/'))  /* it's a <tag/> ... skip it. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   206
        {
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   207
            ptr = *val;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   208
            continue;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   209
        } /* if */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   210
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   211
        /* look for endtag... */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   212
        ptr++;
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        while ( (ptr = strstr(ptr, (*tag)-1)) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        {
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   215
            if (*(ptr-1) != '<') { ptr++; continue; }  /* false positive */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   216
            *(ptr-1) = '\0';  /* null-terminate tag's children. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   220
        /* return everything after this tag's children. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        return((ptr == NULL) ? NULL : ptr + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
} /* parse_xml */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
static char *find_info_plist_version(char *ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    int have_key = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    char *tag;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    char *val;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    while ( (ptr = parse_xml(ptr, &tag, &val)) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
        if (have_key)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
            have_key = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
            if (strcasecmp(tag, "string") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
                return(val);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
        if ((strcasecmp(tag, "plist") == 0) || (strcasecmp(tag, "dict") == 0))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
            ptr = val;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
8
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   249
        /* You should only use CFBundleShortVersionString, for various
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   250
         *  reasons not worth explaining here. CFBundleVersion is here
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   251
         *  for older products that need to update to the other tag.
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   252
         */
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   253
        if (strcasecmp(tag,"key") == 0)
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   254
        {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   255
            if (strcasecmp(val,"CFBundleVersion") == 0)
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   256
                have_key = 1;
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   257
            if (strcasecmp(val,"CFBundleShortVersionString") == 0)
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   258
                have_key = 1;
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   259
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
} /* find_info_plist_version */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
9
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   266
/* !!! FIXME: Code duplication */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   267
static char *find_info_plist_bundle_id(char *ptr)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   268
{
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   269
    int have_key = 0;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   270
    char *tag;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   271
    char *val;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   272
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   273
    while ( (ptr = parse_xml(ptr, &tag, &val)) != NULL )
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   274
    {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   275
        if (have_key)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   276
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   277
            have_key = 0;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   278
            if (strcasecmp(tag, "string") == 0)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   279
                return(val);
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   280
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   281
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   282
        if ((strcasecmp(tag, "plist") == 0) || (strcasecmp(tag, "dict") == 0))
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   283
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   284
            ptr = val;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   285
            continue;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   286
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   287
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   288
        if (strcasecmp(tag,"key") == 0)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   289
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   290
            if (strcasecmp(val,"CFBundleIdentifier") == 0)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   291
                have_key = 1;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   292
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   293
    } /* while */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   294
    
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   295
    return(NULL);
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   296
} /* find_info_plist_version */
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   297
#endif  /* PLATFORM_MACOSX */
9
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   298
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   299
83
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   300
/* you are already chdir()'d when this is called. */
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   301
int get_product_version(const char *ident, char *buf, size_t bufsize)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
{
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   303
#if PLATFORM_MACOSX
45
273952a3dc2a Changed some C++ comments to old C form.
Ryan C. Gordon <icculus@icculus.org>
parents: 44
diff changeset
   304
    const char *fname = "Contents/Info.plist";  /* already chdir'd for this. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
    char *mem = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    char *ptr;
98
4912346d49c9 Cleaned up some datatype sizes and handle byte order explicitly...this allows
Ryan C. Gordon <icculus@icculus.org>
parents: 97
diff changeset
   307
    unsigned int fsize;
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    FILE *io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    if ( !get_file_size(fname, &fsize) ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    if ( (mem = malloc(fsize + 1)) == NULL ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    if ( (io = fopen(fname, "r")) == NULL ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    mem[fsize] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   319
    if (ident != NULL)
8
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   320
    {
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   321
        ptr = find_info_plist_bundle_id(mem);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   322
        if ((ptr == NULL) || (strcasecmp(ptr, ident) != 0))
8
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   323
        {
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   324
            int yes = ui_prompt_ny("We don't think we're looking at the right directory!"
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   325
                                   " Are you SURE this is the right place?"
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   326
                                   " If you aren't sure, clicking 'Yes' can destroy unrelated files!");
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   327
            if (!yes)
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   328
            {
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   329
                _fatal("Stopping at user's request.");
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   330
                free(mem);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   331
                return(0);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   332
            } /* if */
8
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   333
        } /* if */
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   334
    } /* if */
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   335
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   336
    /* !!! FIXME: this is kinda a lame hack. */
8
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   337
    if ( (io = fopen(fname, "r")) == NULL ) goto parse_info_plist_bailed;
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   338
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto parse_info_plist_bailed;
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   339
    fclose(io);
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   340
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    ptr = find_info_plist_version(mem);
83
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   342
    if (ptr == NULL)
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   343
        return(0);
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   344
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   345
    strncpy(buf, ptr, bufsize);
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   346
    buf[bufsize-1] = '\0';
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   347
    retval = 1;
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
parse_info_plist_bailed:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    free(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    if (io != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
        fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    return(retval);
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   355
#else
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   356
    _fatal("Not implemented!");  /* !!! FIXME */
83
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   357
    *buf = '\0';
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   358
    return(0);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   359
#endif
83
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   360
} /* check_product_version */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   363
int locate_product_by_identifier(const char *str, char *buf, size_t bufsize)
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   364
{
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   365
#if PLATFORM_MACOSX
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   366
    /* Ask LaunchServices to find product by identifier... */
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   367
    OSStatus rc;
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   368
    CFURLRef url = NULL;
100
85e5ea217e82 One more compiler warning cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   369
    CFStringRef id = CFStringCreateWithBytes(NULL,
85e5ea217e82 One more compiler warning cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   370
                                             (const unsigned char *) str,
85e5ea217e82 One more compiler warning cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   371
                                             strlen(str),
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   372
                                             kCFStringEncodingISOLatin1, 0);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   373
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   374
    rc = LSFindApplicationForInfo(kLSUnknownCreator, id, NULL, NULL, &url);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   375
    CFRelease(id);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   376
    if (rc == noErr)
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   377
    {
100
85e5ea217e82 One more compiler warning cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   378
        int b = (int) CFURLGetFileSystemRepresentation(url, TRUE,
85e5ea217e82 One more compiler warning cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   379
                                                       (unsigned char *) buf,
85e5ea217e82 One more compiler warning cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   380
                                                       bufsize);
93
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   381
        if (b) b = 1;
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   382
        CFRelease(url);
93
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   383
        if ((b) && (strstr(buf, "/.Trash/")))
83
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   384
        {
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   385
            _fatal("It looks like your installation is in the Trash can."
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   386
                    " Please take it out of the trash first."
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   387
                    " If this is an old installation, please empty your"
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   388
                    " trash so we find the right one.");
93
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   389
            b = -1;
83
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   390
        } /* if */
2ab590824674 Moved a bunch of the versioning stuff into mojopatch.c and cleaned up patch
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   391
93
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   392
        return(b);
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   393
    } /* if */
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   394
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   395
    return(0);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   396
#else
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   397
    _fatal("Not implemented!");  /* !!! FIXME */
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   398
    return(0);
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   399
#endif
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   400
} /* locate_product_by_identifier */
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   401
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   402
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
int update_version(const char *ver)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
{
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   405
#if PLATFORM_MACOSX
45
273952a3dc2a Changed some C++ comments to old C form.
Ryan C. Gordon <icculus@icculus.org>
parents: 44
diff changeset
   406
    const char *fname = "Contents/Info.plist";  /* already chdir'd for this. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    char *mem = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    char *ptr;
98
4912346d49c9 Cleaned up some datatype sizes and handle byte order explicitly...this allows
Ryan C. Gordon <icculus@icculus.org>
parents: 97
diff changeset
   409
    unsigned int fsize;
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
    long writestart;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    long writeend;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    FILE *io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
    if ( !get_file_size(fname, &fsize) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
    if ( (mem = malloc(fsize + 1)) == NULL ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
    if ( (io = fopen(fname, "r+")) == NULL ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    mem[fsize] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    ptr = find_info_plist_version(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    if (ptr == NULL) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    writestart = (long) (ptr - mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    writeend = writestart + strlen(ptr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    ptr = mem + writeend;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    if ( (fseek(io, 0, SEEK_SET) == -1) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
    if ( (fseek(io, writestart, SEEK_SET) == -1) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
    if ( (fwrite(ver, strlen(ver), 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
    if ( (fwrite(ptr, strlen(ptr), 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    for (fsize = (writeend - writestart); fsize > 0; fsize--)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
        if (fwrite(" ", 1, 1, io) != 1) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
update_version_bailed:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
    free(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    if (io != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
        fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    if (!retval) _fatal("Can't update product's installed version.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    return(retval);
66
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   443
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   444
#else  /* Regular old POSIX-compliant Unix... */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   445
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   446
    /*
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   447
     * !!! FIXME: need some way to flag this install as updated...
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   448
     * !!! FIXME:  maybe just leave unimplemented?
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   449
     */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   450
    _fatal("Not implemented!");
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   451
    return(0);
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   452
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
#endif
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   454
} /* update_version */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
int calc_tmp_filenames(char **tmp1, char **tmp2)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    static char _tmp1[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    static char _tmp2[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    pid_t pid = getpid();
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    snprintf(_tmp1, sizeof (_tmp1), "/tmp/mojopatch.tmp1.%d", (int) pid);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    snprintf(_tmp2, sizeof (_tmp2), "/tmp/mojopatch.tmp2.%d", (int) pid);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    *tmp1 = _tmp1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    *tmp2 = _tmp2;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
} /* calc_tmp_filenames */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
static char *basedir = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
static volatile int thread_alive = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
static void *spawn_thread(void *arg)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    static int rc;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
    rc = system((char *) arg);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    thread_alive = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
    return(&rc);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
} /* spawn_thread */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   482
static SpawnResult spawn_binary(const char *cmd)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
{
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   484
    int *ptr = NULL;
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   485
    int rc = 127;
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   486
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   487
#if !USE_PTHREAD
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   488
    pid_t pid = fork();
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   489
    if (pid == -1)
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   490
    {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   491
        int e = errno;
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   492
        _fatal("fork() failed: %d (%s)", e, strerror(e));
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   493
        return(SPAWN_FAILED);
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   494
    } /* if */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   495
81
50f6052737ce Did a lot of ripping things up.
Ryan C. Gordon <icculus@icculus.org>
parents: 78
diff changeset
   496
    else if (pid == 0)   /* child process. */
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   497
    {
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   498
        ptr = (int *) spawn_thread((void *) cmd);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   499
        if (ptr) rc = *ptr;
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   500
        exit(rc != 0);
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   501
    } /* else if */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   502
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   503
    else
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   504
    {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   505
        while (waitpid(pid, &rc, WNOHANG) == 0)
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   506
        {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   507
            ui_pump();
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   508
            usleep(10000);
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   509
        } /* while */
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   510
        return((rc == 0) ? SPAWN_RETURNGOOD : SPAWN_RETURNBAD);
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   511
    } /* else */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   512
#else
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   513
    pthread_t thr;
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   514
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    thread_alive = 1;
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   516
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    if (pthread_create(&thr, NULL, spawn_thread, cmd) != 0)
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   518
        return(SPAWN_FAILED);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    while (thread_alive)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
        ui_pump();
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
        usleep(10000);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   526
    pthread_join(thr, (void **) &ptr);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   527
    if (ptr) rc = *ptr;
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   528
    return((rc == 0) ? SPAWN_RETURNGOOD : SPAWN_RETURNBAD);
64
8f4e25f1ceb5 Gregory McLean pointed out that pthreads are needed; start to mark out the
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
   529
#endif
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   530
} /* spawn_binary */
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   531
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   532
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   533
SpawnResult spawn_xdelta(const char *cmdline)
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   534
{
93
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   535
    SpawnResult retval;
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   536
    const char *binname = "xdelta";
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   537
    char *cmd = alloca(strlen(cmdline) + strlen(basedir) + strlen(binname) + 5);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   538
    if (!cmd)
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   539
        return(SPAWN_FAILED);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   540
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   541
    sprintf(cmd, "\"%s/%s\" %s", basedir, binname, cmdline);
93
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   542
    retval = spawn_binary(cmd);
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   543
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   544
    /* !!! FIXME: xdelta returns an unexpected value when making a delta. */
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   545
    /* !!! FIXME:  (we check the md5sums from all output anyhow. */
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   546
    if (retval == SPAWN_RETURNBAD) retval = SPAWN_RETURNGOOD;
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   547
443f73ca45e2 A couple of fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 91
diff changeset
   548
    return(retval);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
} /* spawn_xdelta */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
91
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   552
/* you are chdir()'d to the directory with the patchfile here. */
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   553
SpawnResult spawn_script(const char *scriptname, const char *dstdir)
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   554
{
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   555
    char *cmd = alloca(strlen(scriptname) + strlen(dstdir) + 32);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   556
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   557
    if (!file_exists(scriptname))
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   558
        return(SPAWN_FILENOTFOUND);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   559
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   560
    sprintf(cmd, "./%s '%s'", scriptname, dstdir);
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   561
    return(spawn_binary(cmd));
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   562
} /* spawn_script */
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   563
6db187bc77c7 Let scripts run before and after each patch, and upon successful execution of
Ryan C. Gordon <icculus@icculus.org>
parents: 90
diff changeset
   564
89
0befb9dfdee4 Removed getcwd() and use get_current_dir() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 83
diff changeset
   565
char *get_current_dir(char *buf, size_t bufsize)
0befb9dfdee4 Removed getcwd() and use get_current_dir() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 83
diff changeset
   566
{
90
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   567
    size_t buflen = 0;
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   568
    if (getcwd(buf, bufsize) == NULL)
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   569
        return(NULL);
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   570
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   571
    buflen = strlen(buf) + 1;
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   572
    if (buflen <= bufsize)
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   573
    {
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   574
        *buf = '\0';
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   575
        return(NULL);
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   576
    } /* if */
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   577
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   578
    if (buf[buflen - 2] != '/')
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   579
        strcat(buf, "/");
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   580
1b004ccf775b Minor get_current_dir() fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 89
diff changeset
   581
    return(buf);
89
0befb9dfdee4 Removed getcwd() and use get_current_dir() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 83
diff changeset
   582
} /* get_current_dir */
0befb9dfdee4 Removed getcwd() and use get_current_dir() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 83
diff changeset
   583
0befb9dfdee4 Removed getcwd() and use get_current_dir() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 83
diff changeset
   584
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
static void find_basedir(int *argc, char **argv)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    const char *argv0 = argv[0];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
    char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
    char realbuf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
    if ((argv0 != NULL) && (strchr(argv0, '/') != NULL)) /* path specifed? */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
        strncpy(buf, argv0, sizeof (buf));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
        char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
        char *envr = getenv("PATH");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
        if (!envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
            return;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
        while (*envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
            ptr = strchr(envr, ':');
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
            if (!ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
                strcpy(buf, envr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
            else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
            {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
                memcpy(buf, envr, (size_t) (ptr - envr));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
                buf[(size_t) (ptr - envr)] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
            } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
            envr = ptr + 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
            if (*buf == '\0')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
                continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
            strcat(buf, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
            strcat(buf, argv0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
            if (access(buf, X_OK) == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
                break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
            if (!ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
            {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
                strcpy(buf, ".");  /* oh well. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
                break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
            } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
        } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
    } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
    buf[sizeof (buf) - 1] = '\0';  /* null terminate, just in case. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
    if (realpath(buf, realbuf) == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
        return;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
    char *ptr = strrchr(realbuf, '/');  /* chop off binary name. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
    if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
        *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
    if (realbuf[strlen(realbuf)-1] != '/')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
        strcat(realbuf, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
97
d67550f68247 Argh, stupid C error.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   641
    basedir = malloc(strlen(realbuf) + 1);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
    strcpy(basedir, realbuf);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
#if PLATFORM_MACOSX
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
    /* Chop off process serial number arg that the Finder adds... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
    if ( (*argc >= 2) && (strncmp(argv[1], "-psn_", 5) == 0) )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
        *argc = *argc - 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
        argv[1] = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
        /* Now that we know where xdelta will be, chdir out of AppBundle... */
77
809774c1e78d Fixed xdelta spawning on MacOS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 76
diff changeset
   652
        /* !!! FIXME: Fails if there are more than one of these in the string... */
809774c1e78d Fixed xdelta spawning on MacOS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 76
diff changeset
   653
        ptr = strstr(realbuf, "/Contents/MacOS/");
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
        if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
        {
77
809774c1e78d Fixed xdelta spawning on MacOS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 76
diff changeset
   656
            ptr++;  /* keep a '/' at the end of the string... */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
            *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
            chdir(realbuf);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
} /* find_basedir */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
int main(int argc, char **argv)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
    int retval;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    find_basedir(&argc, argv);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
    retval = mojopatch_main(argc, argv);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
    free(basedir);
36
9e60553d82bc Better fix for converting to Unix process exit codes.
Ryan C. Gordon <icculus@icculus.org>
parents: 27
diff changeset
   671
    return((retval == PATCHSUCCESS) ? 0 : 1);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
} /* unixmain */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
/* end of platform_unix.c ... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675