platform_unix.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 22 Apr 2005 09:59:45 +0000
changeset 78 55c90060fbc1
parent 77 809774c1e78d
child 81 50f6052737ce
permissions -rw-r--r--
Some scary fixes that came up at the last moment for ArmyOps 2.3.0's patch.
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>
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
    13
#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
    14
8f4e25f1ceb5 Gregory McLean pointed out that pthreads are needed; start to mark out the
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
    15
#if USE_PTHREAD
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#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
    17
#endif
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include "platform.h"
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include "ui.h"
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
int file_exists(const char *fname)
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
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
    return(stat(fname, &statbuf) == 0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
} /* file_exists */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
int file_is_directory(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    if (stat(fname, &statbuf) == 0)
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
        if (S_ISDIR(statbuf.st_mode))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    } /* if */
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
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
} /* file_is_directory */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
int file_is_symlink(const char *fname)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    struct stat statbuf;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    if (lstat(fname, &statbuf) == 0)
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
        if (S_ISLNK(statbuf.st_mode))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
            retval = 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    } /* if */
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
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
} /* file_is_symlink */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
/* enumerate contents of (base) directory. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
file_list *make_filelist(const char *base)
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
    file_list *retval = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    file_list *l = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    file_list *prev = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    DIR *dir;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    struct dirent *ent;
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
    errno = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    dir = opendir(base);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    if (dir == NULL)
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
        _fatal("Error: could not read dir %s: %s.", base, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    while (1)
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
        ent = readdir(dir);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        if (ent == NULL)   /* we're done. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        if (strcmp(ent->d_name, ".") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
            continue;
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
        if (strcmp(ent->d_name, "..") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
            continue;
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
         * !!! FIXME: This is a workaround until symlinks are really
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
         * !!! FIXME:  supported...just pretend they don't exist for now.  :(
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
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
            char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
            snprintf(buf, sizeof (buf), "%s/%s", base, ent->d_name);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
            if (file_is_symlink(buf))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
                continue;
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        l = (file_list *) malloc(sizeof (file_list));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        if (l == NULL)
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
            _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        } /* if */
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->fname = (char *) malloc(strlen(ent->d_name) + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        if (l->fname == 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
            free(l);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
            _fatal("Error: out of memory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        strcpy(l->fname, ent->d_name);
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
        if (retval == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
            retval = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
            prev->next = l;
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
        prev = l;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
        l->next = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    } /* while */
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
    closedir(dir);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
} /* make_filelist */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
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
int get_file_size(const char *fname, long *fsize)
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
    struct stat statbuf;
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
    assert(fsize != NULL);
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
    if (stat(fname, &statbuf) == -1)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
        _fatal("Error: failed to get filesize of [%s]: %s.",
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
                fname, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    } /* if */
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
    *fsize = statbuf.st_size;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
} /* get_file_size */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
char *get_realpath(const char *path)
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
    char resolved_path[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    char *retval = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    errno = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    if (!realpath(path, resolved_path))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
        _fatal("Can't determine full path of [%s]: %s.",
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
                path, strerror(errno));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    retval = malloc(strlen(resolved_path) + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    if (retval == NULL)
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
        _fatal("Error: out of memory.");
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
    strcpy(retval, resolved_path);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
} /* get_realpath */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
#ifdef PLATFORM_MACOSX
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
#include <ApplicationServices/ApplicationServices.h>
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
static char *parse_xml(char *ptr, char **tag, char **val)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    char *ptr2;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    while ( (ptr = strchr(ptr, '<')) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    {
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   180
        ptr++;  /* skip past '<' to start of tag.*/
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   181
        if (*ptr == '/') continue;  /* prior endtag? */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   182
        if (*ptr == '!') continue;  /* initial crap? */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   183
        if (*ptr == '?') continue;  /* initial crap? */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        *tag = ptr;
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   186
        *(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
   187
        ptr2 = strchr(ptr, ' '); /* look for a space (tag attributes?) */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
        if ( (ptr = strchr(ptr, '>')) == NULL ) return(NULL);
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   189
        if ((ptr2) && (ptr2 < ptr)) *ptr2 = '\0';  /* chop out attributes. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   190
        *ptr = '\0';  /* chop off '<' at end of tag. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   191
        *val = ptr + 1;  /* (*val) == start of children of this tag. */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   192
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   193
        /* Catch <tagname/> tags... */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   194
        ptr2 = ptr - 1;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   195
        while ( (ptr2 != *tag) && (isspace(*ptr2)) )
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   196
            ptr2--;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   197
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   198
        if ((ptr2 != *tag) && (*ptr2 == '/'))  /* it's a <tag/> ... skip it. */
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
            ptr = *val;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   201
            continue;
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   202
        } /* if */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   203
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   204
        /* look for endtag... */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   205
        ptr++;
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
        while ( (ptr = strstr(ptr, (*tag)-1)) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
        {
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   208
            if (*(ptr-1) != '<') { ptr++; continue; }  /* false positive */
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   209
            *(ptr-1) = '\0';  /* null-terminate tag's children. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
            break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
41
7edecb332efb Tweaked xml parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
   213
        /* return everything after this tag's children. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        return((ptr == NULL) ? NULL : ptr + 1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    } /* while */
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
    return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
} /* parse_xml */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
static char *find_info_plist_version(char *ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    int have_key = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    char *tag;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    char *val;
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
    while ( (ptr = parse_xml(ptr, &tag, &val)) != NULL )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
        if (have_key)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
            have_key = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
            if (strcasecmp(tag, "string") == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
                return(val);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
        } /* if */
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 ((strcasecmp(tag, "plist") == 0) || (strcasecmp(tag, "dict") == 0))
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
            ptr = val;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
            continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
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
   242
        /* 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
   243
         *  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
   244
         *  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
   245
         */
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   246
        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
   247
        {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   248
            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
   249
                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
   250
            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
   251
                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
   252
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    return(NULL);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
} /* find_info_plist_version */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
9
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   259
/* !!! FIXME: Code duplication */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   260
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
   261
{
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   262
    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
   263
    char *tag;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   264
    char *val;
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
    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
   267
    {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   268
        if (have_key)
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   269
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   270
            have_key = 0;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   271
            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
   272
                return(val);
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   273
        } /* if */
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 ((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
   276
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   277
            ptr = val;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   278
            continue;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   279
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   280
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   281
        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
   282
        {
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   283
            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
   284
                have_key = 1;
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   285
        } /* if */
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   286
    } /* while */
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
    return(NULL);
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   289
} /* 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
   290
4a96fabb7b79 Missed a bit when merging this file. Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   291
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
   292
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
   293
                                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
   294
                                const char *newversion)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
{
45
273952a3dc2a Changed some C++ comments to old C form.
Ryan C. Gordon <icculus@icculus.org>
parents: 44
diff changeset
   296
    const char *fname = "Contents/Info.plist";  /* already chdir'd for this. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    char *mem = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    long fsize;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    FILE *io = NULL;
63
10011b12af93 Fixed confusing error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 59
diff changeset
   302
    int knowver = 0;
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    if ( !get_file_size(fname, &fsize) ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
    if ( (mem = malloc(fsize + 1)) == NULL ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    if ( (io = fopen(fname, "r")) == NULL ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto parse_info_plist_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    mem[fsize] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
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
   312
    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
   313
    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
   314
    {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   315
        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
   316
                               " 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
   317
                               " 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
   318
        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
   319
        {
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
            _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
   321
            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
   322
            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
   323
        } /* 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
   324
    } /* 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
   325
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   326
    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
   327
    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
   328
    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
   329
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    ptr = find_info_plist_version(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    {
63
10011b12af93 Fixed confusing error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 59
diff changeset
   333
        knowver = 1;
76
38a9ee7c1d44 Version detection code was total pants.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
   334
        retval = version_ok(ptr, version, newversion);
57
26c5e31040e5 Make the version_ok() hack a little more...uh, big.
Ryan C. Gordon <icculus@icculus.org>
parents: 48
diff changeset
   335
        if (retval == -1)
26c5e31040e5 Make the version_ok() hack a little more...uh, big.
Ryan C. Gordon <icculus@icculus.org>
parents: 48
diff changeset
   336
            _fatal("You seem to be all patched up already!");
26c5e31040e5 Make the version_ok() hack a little more...uh, big.
Ryan C. Gordon <icculus@icculus.org>
parents: 48
diff changeset
   337
        else if (retval == 0)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        {
57
26c5e31040e5 Make the version_ok() hack a little more...uh, big.
Ryan C. Gordon <icculus@icculus.org>
parents: 48
diff changeset
   339
            _fatal("This patch applies to version '%s', but you have '%s'.",
26c5e31040e5 Make the version_ok() hack a little more...uh, big.
Ryan C. Gordon <icculus@icculus.org>
parents: 48
diff changeset
   340
                    version, ptr);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
        } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    } /* if */
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
parse_info_plist_bailed:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    free(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    if (io != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
63
10011b12af93 Fixed confusing error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 59
diff changeset
   349
    if (!knowver) _fatal("Can't determine product's installed version.");
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
} /* parse_info_dot_plist */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
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
int update_version(const char *ver)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
{
45
273952a3dc2a Changed some C++ comments to old C form.
Ryan C. Gordon <icculus@icculus.org>
parents: 44
diff changeset
   356
    const char *fname = "Contents/Info.plist";  /* already chdir'd for this. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    char *mem = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    long fsize;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    int retval = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    long writestart;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    long writeend;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    FILE *io = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    if ( !get_file_size(fname, &fsize) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    if ( (mem = malloc(fsize + 1)) == NULL ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    if ( (io = fopen(fname, "r+")) == NULL ) 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
    mem[fsize] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    ptr = find_info_plist_version(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    if (ptr == NULL) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    writestart = (long) (ptr - mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    writeend = writestart + strlen(ptr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    ptr = mem + writeend;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    if ( (fseek(io, 0, SEEK_SET) == -1) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    if ( (fread(mem, fsize, 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    if ( (fseek(io, writestart, SEEK_SET) == -1) ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    if ( (fwrite(ver, strlen(ver), 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    if ( (fwrite(ptr, strlen(ptr), 1, io)) != 1 ) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    for (fsize = (writeend - writestart); fsize > 0; fsize--)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
        if (fwrite(" ", 1, 1, io) != 1) goto update_version_bailed;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    retval = 1;
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
update_version_bailed:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    free(mem);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    if (io != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
        fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    if (!retval) _fatal("Can't update product's installed version.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    return(retval);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
} /* update_version */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
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
   396
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
   397
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
   398
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
   399
                        const char *version, const char *newversion)
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
    char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
    Boolean b;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
    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
   404
    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
   405
    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
   406
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 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
   408
    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
   409
    {
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
        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
   411
        CFStringRef id = CFStringCreateWithBytes(NULL, str, strlen(str),
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
                                                kCFStringEncodingISOLatin1, 0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
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
   414
        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
   415
        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
   416
        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
   417
        {
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
   418
            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
   419
            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
   420
            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
   421
            {
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   422
                _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
   423
                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
   424
            } /* 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
   425
            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
   426
        } /* 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
   427
        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
   428
        {
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   429
            _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
   430
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    } /* 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
   432
fb920893c233 Always prompt the user to find their installation if product identifier isn't
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   433
    if (!found) /* No identifier, or LaunchServices couldn't find it. */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    {
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
   435
        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
   436
        {
013c7889f09e Now looks at multiple XML tags to determine product version in MacOS's
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   437
            _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
   438
            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
   439
        } /* if */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    _log("I think the product is installed at [%s].", buf);
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
    if (chdir(buf) != 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
        _fatal("Failed to change to product's installation directory.");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
44
8ddfda0b6aaa A collection of hacks for broken installations, like Neverwinter Nights.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   450
    if (hasident)
8ddfda0b6aaa A collection of hacks for broken installations, like Neverwinter Nights.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   451
        return(parse_info_dot_plist(str, version, newversion));
8ddfda0b6aaa A collection of hacks for broken installations, like Neverwinter Nights.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   452
8ddfda0b6aaa A collection of hacks for broken installations, like Neverwinter Nights.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   453
    return(1);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
} /* chdir_by_identifier */
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 show_and_install_readme(const char *fname, const char *text)
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
    FILE *io;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    char *cmd;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    const char *envr = getenv("HOME");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    if (!envr)
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
        _fatal("HOME environment var not set?");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    cmd = alloca(strlen(fname) + strlen(envr) + 30);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    strcpy(cmd, "open ");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
    strcat(cmd, envr);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    if (cmd[strlen(cmd)-1] != '/')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
        strcat(cmd, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
    strcat(cmd, "Desktop/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    strcat(cmd, fname);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
    io = fopen(cmd + 5, "w");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    if (!io)
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
        _fatal("Failed to open [%s] for writing.", cmd+5);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    /* !!! FIXME: error checking! */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
    fputs(text, io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
    fclose(io);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    system(cmd);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
} /* show_and_install_readme */
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
   489
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   490
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   491
#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
   492
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   493
int update_version(const char *ver)
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   494
{
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   495
    /*
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   496
     * !!! 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
   497
     * !!! 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
   498
     */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   499
    _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
   500
    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
   501
} /* show_and_install_readme */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   502
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   503
int show_and_install_readme(const char *fname, const char *text)
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   504
{
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   505
    /*
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   506
     * !!! FIXME: Can just dump to stdout? This should really be in the
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   507
     * !!! FIXME:  UI modules...
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   508
     */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   509
    _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
   510
    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
   511
} /* show_and_install_readme */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   512
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   513
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   514
int chdir_by_identifier(const char *name, const char *str,
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   515
                        const char *version, const char *newversion)
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   516
{
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   517
    /*
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   518
     * !!! FIXME: need some way to find the program automatically...
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   519
     * !!! FIXME:  maybe just prompt the user? Oh well.
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   520
     */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   521
    _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
   522
    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
   523
} /* chdir_by_identifier */
118c760d8e55 More Non-Mac Unix tweaks...now compiles, but needs more work to actually
Ryan C. Gordon <icculus@icculus.org>
parents: 65
diff changeset
   524
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
int calc_tmp_filenames(char **tmp1, char **tmp2)
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 char _tmp1[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
    static char _tmp2[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
    pid_t pid = getpid();
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    snprintf(_tmp1, sizeof (_tmp1), "/tmp/mojopatch.tmp1.%d", (int) pid);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    snprintf(_tmp2, sizeof (_tmp2), "/tmp/mojopatch.tmp2.%d", (int) pid);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
    *tmp1 = _tmp1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
    *tmp2 = _tmp2;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    return(1);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
} /* calc_tmp_filenames */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
static char *basedir = NULL;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
static volatile int thread_alive = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
static void *spawn_thread(void *arg)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    static int rc;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
    rc = system((char *) arg);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    thread_alive = 0;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    return(&rc);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
} /* spawn_thread */
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
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
int spawn_xdelta(const char *cmdline)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
    const char *binname = "xdelta";
78
55c90060fbc1 Some scary fixes that came up at the last moment for ArmyOps 2.3.0's patch.
Ryan C. Gordon <icculus@icculus.org>
parents: 77
diff changeset
   556
    char *cmd = alloca(strlen(cmdline) + strlen(basedir) + strlen(binname) + 5);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
    if (!cmd)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        return(0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
78
55c90060fbc1 Some scary fixes that came up at the last moment for ArmyOps 2.3.0's patch.
Ryan C. Gordon <icculus@icculus.org>
parents: 77
diff changeset
   560
    sprintf(cmd, "\"%s/%s\" %s", basedir, binname, cmdline);
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   561
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   562
#if !USE_PTHREAD
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   563
    int rc = 0;
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   564
    pid_t pid = fork();
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   565
    if (pid == -1)
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   566
    {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   567
        int e = errno;
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   568
        _fatal("fork() failed: %d (%s)", e, strerror(e));
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   569
        return(0);
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   570
    } /* if */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   571
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   572
    else if (pid == 0)   // child process.
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   573
    {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   574
        rc = spawn_thread(cmd);
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   575
        exit(1);  /* !!! FIXME    *((int *) rc) == 0 ); */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   576
    } /* else if */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   577
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   578
    else
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   579
    {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   580
        while (waitpid(pid, &rc, WNOHANG) == 0)
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   581
        {
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   582
            ui_pump();
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   583
            usleep(10000);
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   584
        } /* while */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   585
        return(1);
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   586
    } /* else */
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   587
#else
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   588
    pthread_t thr;
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   589
    void *rc;
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   590
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
    thread_alive = 1;
65
35c52890a14f Added a fork() replacement for pthread code...
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
   592
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    if (pthread_create(&thr, NULL, spawn_thread, cmd) != 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
        return(0);
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
    while (thread_alive)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
        ui_pump();
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
        usleep(10000);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
    } /* while */
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
    pthread_join(thr, &rc);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
    return(1);  /* !!! FIXME    *((int *) rc) == 0 ); */
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
   604
#endif
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
} /* spawn_xdelta */
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
static void find_basedir(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
    const char *argv0 = argv[0];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
    char buf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
    char realbuf[MAXPATHLEN];
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
    if ((argv0 != NULL) && (strchr(argv0, '/') != NULL)) /* path specifed? */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
        strncpy(buf, argv0, sizeof (buf));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
    else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
        char *ptr;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
        char *envr = getenv("PATH");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
        if (!envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
            return;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
        while (*envr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
        {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
            ptr = strchr(envr, ':');
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
            if (!ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
                strcpy(buf, envr);
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
                memcpy(buf, envr, (size_t) (ptr - envr));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
                buf[(size_t) (ptr - envr)] = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
            } /* else */
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
            envr = ptr + 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
            if (*buf == '\0')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
                continue;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
            strcat(buf, "/");
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
            strcat(buf, argv0);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
            if (access(buf, X_OK) == 0)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
                break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
            if (!ptr)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
            {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
                strcpy(buf, ".");  /* oh well. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
                break;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
            } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
        } /* while */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
    } /* else */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
    buf[sizeof (buf) - 1] = '\0';  /* null terminate, just in case. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    if (realpath(buf, realbuf) == NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
        return;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
    char *ptr = strrchr(realbuf, '/');  /* chop off binary name. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
    if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
        *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
    if (realbuf[strlen(realbuf)-1] != '/')
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
        strcat(realbuf, "/");
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
    basedir = malloc(strlen(realbuf + 1));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
    strcpy(basedir, realbuf);
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
#if PLATFORM_MACOSX
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    /* Chop off process serial number arg that the Finder adds... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
    if ( (*argc >= 2) && (strncmp(argv[1], "-psn_", 5) == 0) )
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
    {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
        *argc = *argc - 1;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
        argv[1] = NULL;
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
        /* 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
   675
        /* !!! 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
   676
        ptr = strstr(realbuf, "/Contents/MacOS/");
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
        if (ptr != NULL)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
        {
77
809774c1e78d Fixed xdelta spawning on MacOS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 76
diff changeset
   679
            ptr++;  /* keep a '/' at the end of the string... */
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
            *ptr = '\0';
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
            chdir(realbuf);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
        } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
    } /* if */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
} /* find_basedir */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
int main(int argc, char **argv)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
    int retval;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
    find_basedir(&argc, argv);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
    retval = mojopatch_main(argc, argv);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
    free(basedir);
36
9e60553d82bc Better fix for converting to Unix process exit codes.
Ryan C. Gordon <icculus@icculus.org>
parents: 27
diff changeset
   694
    return((retval == PATCHSUCCESS) ? 0 : 1);
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
} /* unixmain */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
/* end of platform_unix.c ... */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698