platform_unix.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 28 May 2004 12:56:49 +0000
changeset 40 bac807d1f829
parent 36 9e60553d82bc
child 41 7edecb332efb
permissions -rw-r--r--
Skip ahead in the patchfile if a given product is already patched.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <stdio.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <string.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <stdlib.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include <unistd.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include <dirent.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
#include <sys/stat.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include <sys/types.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include <fcntl.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <sys/param.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <errno.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <assert.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <pthread.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include "platform.h"
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include "ui.h"
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
int file_exists(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    return(stat(fname, &statbuf) == 0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
} /* file_exists */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
int file_is_directory(const char *fname)
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
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    int retval = 0;
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
    if (stat(fname, &statbuf) == 0)
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
        if (S_ISDIR(statbuf.st_mode))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    } /* if */
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
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
} /* file_is_directory */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
int file_is_symlink(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    if (lstat(fname, &statbuf) == 0)
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
        if (S_ISLNK(statbuf.st_mode))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    } /* if */
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
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
} /* file_is_symlink */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
/* enumerate contents of (base) directory. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
file_list *make_filelist(const char *base)
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
    file_list *retval = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    file_list *l = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    file_list *prev = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    DIR *dir;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    struct dirent *ent;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    errno = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    dir = opendir(base);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    if (dir == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        _fatal("Error: could not read dir %s: %s.", base, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    while (1)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        ent = readdir(dir);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        if (ent == NULL)   /* we're done. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
        if (strcmp(ent->d_name, ".") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
        if (strcmp(ent->d_name, "..") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
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
         * !!! FIXME: This is a workaround until symlinks are really
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
         * !!! FIXME:  supported...just pretend they don't exist for now.  :(
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
         */
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
            char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
            snprintf(buf, sizeof (buf), "%s/%s", base, ent->d_name);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
            if (file_is_symlink(buf))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
                continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        }
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
        l = (file_list *) malloc(sizeof (file_list));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
        if (l == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
            _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
        l->fname = (char *) malloc(strlen(ent->d_name) + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        if (l->fname == NULL)
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
            free(l);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
            _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        strcpy(l->fname, ent->d_name);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
        if (retval == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
            retval = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
            prev->next = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
        prev = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        l->next = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    } /* while */
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
    closedir(dir);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
} /* make_filelist */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
int get_file_size(const char *fname, long *fsize)
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
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    assert(fsize != NULL);
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
    if (stat(fname, &statbuf) == -1)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        _fatal("Error: failed to get filesize of [%s]: %s.",
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
                fname, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    } /* if */
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
    *fsize = statbuf.st_size;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
} /* get_file_size */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
char *get_realpath(const char *path)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    char resolved_path[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    char *retval = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    errno = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    if (!realpath(path, resolved_path))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
        _fatal("Can't determine full path of [%s]: %s.",
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
                path, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    } /* if */
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
    retval = malloc(strlen(resolved_path) + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    if (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
        _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    strcpy(retval, resolved_path);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
} /* get_realpath */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
#ifdef PLATFORM_MACOSX
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
#include <ApplicationServices/ApplicationServices.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
static char *parse_xml(char *ptr, char **tag, char **val)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    char *ptr2;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    while ( (ptr = strchr(ptr, '<')) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
        ptr++;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        if (*ptr == '/') continue;  // prior endtag.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
        if (*ptr == '!') continue;  // initial crap.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        if (*ptr == '?') continue;  // initial crap.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        *tag = ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        *(ptr-1) = '/';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        ptr2 = strchr(ptr, ' ');
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
        if ( (ptr = strchr(ptr, '>')) == NULL ) return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        if ((ptr2) && (ptr2 < ptr)) *ptr2 = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
        *val = ++ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
        while ( (ptr = strstr(ptr, (*tag)-1)) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
            if (*(ptr-1) != '<') { ptr++; continue; }
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
            *(ptr-1) = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
        } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        return((ptr == NULL) ? NULL : ptr + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
} /* parse_xml */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
static char *find_info_plist_version(char *ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    int have_key = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    char *tag;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    char *val;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    while ( (ptr = parse_xml(ptr, &tag, &val)) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
        if (have_key)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
            have_key = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
            if (strcasecmp(tag, "string") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
                return(val);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
        if ((strcasecmp(tag, "plist") == 0) || (strcasecmp(tag, "dict") == 0))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
            ptr = val;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
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
   223
        /* 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
   224
         *  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
   225
         *  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
   226
         */
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   227
        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
   228
        {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   229
            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
   230
                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
   231
            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
   232
                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
   233
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    } /* while */
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
    return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
} /* find_info_plist_version */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
9
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   240
/* !!! FIXME: Code duplication */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   241
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
   242
{
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   243
    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
   244
    char *tag;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   245
    char *val;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   246
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   247
    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
   248
    {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   249
        if (have_key)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   250
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   251
            have_key = 0;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   252
            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
   253
                return(val);
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   254
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   255
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   256
        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
   257
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   258
            ptr = val;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   259
            continue;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   260
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   261
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   262
        /* You should only use CFBundleShortVersionString, for various
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   263
         *  reasons not worth explaining here. CFBundleVersion is here
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   264
         *  for older products that need to update to the other tag.
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   265
         */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   266
        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
   267
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   268
            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
   269
                have_key = 1;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   270
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   271
    } /* while */
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
    return(NULL);
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   274
} /* find_info_plist_version */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   275
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   276
27
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   277
static int parse_info_dot_plist(const char *ident,
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   278
                                const char *version,
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   279
                                const char *newversion)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    const char *fname = "Contents/Info.plist";  // already chdir'd for this.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    char *mem = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    long fsize;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    FILE *io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    if ( !get_file_size(fname, &fsize) ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    if ( (mem = malloc(fsize + 1)) == NULL ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
    if ( (io = fopen(fname, "r")) == NULL ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    mem[fsize] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
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
   296
    ptr = find_info_plist_bundle_id(mem);
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   297
    if ((ptr == NULL) || (strcasecmp(ptr, ident) != 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
   298
    {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   299
        int yes = ui_prompt_ny("We don't think we're looking at the right directory!"
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   300
                               " Are you SURE this is the right place?"
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   301
                               " If you aren't sure, clicking 'Yes' can destroy unrelated files!");
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   302
        if (!yes)
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   303
        {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   304
            _fatal("Stopping at user's request.");
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   305
            free(mem);
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   306
            return(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
   307
        } /* 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
   308
    } /* 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
   309
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   310
    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
   311
    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
   312
    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
   313
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    ptr = find_info_plist_version(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    {
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
   317
        if (version_ok(ptr, version))
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
        {
40
bac807d1f829 Skip ahead in the patchfile if a given product is already patched.
Ryan C. Gordon <icculus@icculus.org>
parents: 36
diff changeset
   321
            if (strcmp(ptr, newversion) == 0)
bac807d1f829 Skip ahead in the patchfile if a given product is already patched.
Ryan C. Gordon <icculus@icculus.org>
parents: 36
diff changeset
   322
            {
27
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   323
                _fatal("You seem to be all patched up already!");
40
bac807d1f829 Skip ahead in the patchfile if a given product is already patched.
Ryan C. Gordon <icculus@icculus.org>
parents: 36
diff changeset
   324
                retval = -1;
bac807d1f829 Skip ahead in the patchfile if a given product is already patched.
Ryan C. Gordon <icculus@icculus.org>
parents: 36
diff changeset
   325
            } /* if */
27
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   326
            else
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   327
            {
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   328
                _fatal("This patch applies to version '%s', but you have '%s'.",
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   329
                        version, ptr);
40
bac807d1f829 Skip ahead in the patchfile if a given product is already patched.
Ryan C. Gordon <icculus@icculus.org>
parents: 36
diff changeset
   330
                retval = 0;
27
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   331
            } /* else */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
parse_info_plist_bailed:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    free(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    if (io != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
40
bac807d1f829 Skip ahead in the patchfile if a given product is already patched.
Ryan C. Gordon <icculus@icculus.org>
parents: 36
diff changeset
   340
    if (retval == 0) _fatal("Can't determine product's installed version.");
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
} /* parse_info_dot_plist */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
int update_version(const char *ver)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    const char *fname = "Contents/Info.plist";  // already chdir'd for this.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    char *mem = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    long fsize;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    long writestart;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    long writeend;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    FILE *io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    if ( !get_file_size(fname, &fsize) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    if ( (mem = malloc(fsize + 1)) == NULL ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    if ( (io = fopen(fname, "r+")) == NULL ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    mem[fsize] = '\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
    ptr = find_info_plist_version(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    if (ptr == NULL) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    writestart = (long) (ptr - mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    writeend = writestart + strlen(ptr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    ptr = mem + writeend;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    if ( (fseek(io, 0, SEEK_SET) == -1) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    if ( (fseek(io, writestart, SEEK_SET) == -1) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    if ( (fwrite(ver, strlen(ver), 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    if ( (fwrite(ptr, strlen(ptr), 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    for (fsize = (writeend - writestart); fsize > 0; fsize--)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
        if (fwrite(" ", 1, 1, io) != 1) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
update_version_bailed:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    free(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    if (io != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    if (!retval) _fatal("Can't update product's installed version.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
} /* update_version */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
19
f556da1872f4 When trying to locate a product, pass in proper name, so UI can refer to it.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   387
int manually_locate_product(const char *name, char *buf, size_t bufsize);
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
   388
27
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   389
int chdir_by_identifier(const char *name, const char *str,
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   390
                        const char *version, const char *newversion)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    Boolean b;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    OSStatus rc;
25
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   395
    int found = 0;
26
d459ca2ce76e Don't parse Info.plist if there wasn't a product identifier specified.
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
   396
    int hasident = ((str != NULL) && (*str));
25
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   397
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   398
    /* if an identifier is specified, ask LaunchServices to find product... */
26
d459ca2ce76e Don't parse Info.plist if there wasn't a product identifier specified.
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
   399
    if (hasident)
25
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   400
    {
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   401
        CFURLRef url = NULL;
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   402
        CFStringRef id = CFStringCreateWithBytes(NULL, str, strlen(str),
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
                                                kCFStringEncodingISOLatin1, 0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
25
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   405
        rc = LSFindApplicationForInfo(kLSUnknownCreator, id, NULL, NULL, &url);
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   406
        CFRelease(id);
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   407
        if (rc == noErr)
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
   408
        {
25
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   409
            b = CFURLGetFileSystemRepresentation(url, TRUE, buf, sizeof (buf));
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   410
            CFRelease(url);
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   411
            if (!b)
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   412
            {
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   413
                _fatal("Internal error.");
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   414
                return(0);
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   415
            } /* if */
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   416
            found = 1;
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   417
        } /* if */
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   418
        else
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   419
        {
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   420
            _log("Couldn't find product. Perhaps it isn't installed?");
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
   421
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    } /* if */
25
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   423
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   424
    if (!found) /* No identifier, or LaunchServices couldn't find it. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    {
19
f556da1872f4 When trying to locate a product, pass in proper name, so UI can refer to it.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   426
        if (!manually_locate_product(name, buf, sizeof (buf)))
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
   427
        {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   428
            _fatal("We can't patch the product if we can't find it!");
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   429
            return(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
   430
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    _log("I think the product is installed at [%s].", buf);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    if (chdir(buf) != 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
        _fatal("Failed to change to product's installation directory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
27
569fe0e4038e Print a friendlier message if product appears to be patched to patchfile's
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   441
    return(hasident ? parse_info_dot_plist(str, version, newversion) : 1);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
} /* chdir_by_identifier */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
int show_and_install_readme(const char *fname, const char *text)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
    FILE *io;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    char *cmd;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
    const char *envr = getenv("HOME");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
    if (!envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
        _fatal("HOME environment var not set?");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
    } /* if */
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
    cmd = alloca(strlen(fname) + strlen(envr) + 30);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    strcpy(cmd, "open ");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    strcat(cmd, envr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    if (cmd[strlen(cmd)-1] != '/')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
        strcat(cmd, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    strcat(cmd, "Desktop/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    strcat(cmd, fname);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    io = fopen(cmd + 5, "w");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    if (!io)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
        _fatal("Failed to open [%s] for writing.", cmd+5);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    /* !!! FIXME: error checking! */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    fputs(text, io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
    fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    system(cmd);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
} /* show_and_install_readme */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
int calc_tmp_filenames(char **tmp1, char **tmp2)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
    static char _tmp1[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    static char _tmp2[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
    pid_t pid = getpid();
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
    snprintf(_tmp1, sizeof (_tmp1), "/tmp/mojopatch.tmp1.%d", (int) pid);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    snprintf(_tmp2, sizeof (_tmp2), "/tmp/mojopatch.tmp2.%d", (int) pid);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
    *tmp1 = _tmp1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    *tmp2 = _tmp2;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
} /* calc_tmp_filenames */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
static char *basedir = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
static volatile int thread_alive = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
static void *spawn_thread(void *arg)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
    static int rc;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
    rc = system((char *) arg);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
    thread_alive = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
    return(&rc);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
} /* spawn_thread */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
int spawn_xdelta(const char *cmdline)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    pthread_t thr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
    void *rc;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
    const char *binname = "xdelta";
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
    char *cmd = alloca(strlen(cmdline) + strlen(basedir) + strlen(binname) + 2);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    if (!cmd)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
    sprintf(cmd, "\"%s%s\" %s", basedir, binname, cmdline);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    thread_alive = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
    if (pthread_create(&thr, NULL, spawn_thread, cmd) != 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    while (thread_alive)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
        ui_pump();
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
        usleep(10000);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
    pthread_join(thr, &rc);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    return(1);  /* !!! FIXME    *((int *) rc) == 0 ); */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
} /* spawn_xdelta */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
static void find_basedir(int *argc, char **argv)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
    const char *argv0 = argv[0];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    char realbuf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
    if ((argv0 != NULL) && (strchr(argv0, '/') != NULL)) /* path specifed? */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
        strncpy(buf, argv0, sizeof (buf));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
        char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
        char *envr = getenv("PATH");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
        if (!envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
            return;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
        while (*envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
            ptr = strchr(envr, ':');
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
            if (!ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
                strcpy(buf, envr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
            else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
            {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
                memcpy(buf, envr, (size_t) (ptr - envr));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
                buf[(size_t) (ptr - envr)] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
            } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
            envr = ptr + 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
            if (*buf == '\0')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
                continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
            strcat(buf, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
            strcat(buf, argv0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
            if (access(buf, X_OK) == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
                break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
            if (!ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
            {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
                strcpy(buf, ".");  /* oh well. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
                break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
            } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
        } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
    } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
    buf[sizeof (buf) - 1] = '\0';  /* null terminate, just in case. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
    if (realpath(buf, realbuf) == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
        return;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
    char *ptr = strrchr(realbuf, '/');  /* chop off binary name. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
    if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
        *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
    if (realbuf[strlen(realbuf)-1] != '/')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
        strcat(realbuf, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
    basedir = malloc(strlen(realbuf + 1));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    strcpy(basedir, realbuf);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
#if PLATFORM_MACOSX
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
    /* Chop off process serial number arg that the Finder adds... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
    if ( (*argc >= 2) && (strncmp(argv[1], "-psn_", 5) == 0) )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
        *argc = *argc - 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
        argv[1] = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
        /* Now that we know where xdelta will be, chdir out of AppBundle... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
        ptr = strstr(realbuf, "/Contents/MacOS");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
        if (ptr != NULL)
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
            *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
            chdir(realbuf);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
} /* find_basedir */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
int main(int argc, char **argv)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
    int retval;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
    find_basedir(&argc, argv);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
    retval = mojopatch_main(argc, argv);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
    free(basedir);
36
9e60553d82bc Better fix for converting to Unix process exit codes.
Ryan C. Gordon <icculus@icculus.org>
parents: 27
diff changeset
   614
    return((retval == PATCHSUCCESS) ? 0 : 1);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
} /* unixmain */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
/* end of platform_unix.c ... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618