~ubuntu-branches/ubuntu/vivid/macutils/vivid

« back to all changes in this revision

Viewing changes to binhex/binhex.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Sharkey
  • Date: 2001-10-27 15:22:18 UTC
  • Revision ID: james.westby@ubuntu.com-20011027152218-oikhfgiaja3hjf5f
Tags: upstream-2.0b3
ImportĀ upstreamĀ versionĀ 2.0b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include "../fileio/machdr.h"
 
3
#include "../fileio/rdfile.h"
 
4
#include "../util/patchlevel.h"
 
5
 
 
6
extern char *malloc();
 
7
extern char *realloc();
 
8
extern char *strcat();
 
9
extern void exit();
 
10
extern void transname();
 
11
extern void do_indent();
 
12
extern void dofile();
 
13
 
 
14
#define LOCALOPT        "RilqVH"
 
15
 
 
16
static void usage();
 
17
 
 
18
static char options[128];
 
19
static char *dir_stack;
 
20
static int dir_ptr = -64;
 
21
static int dir_max;
 
22
int dorep = 1;
 
23
 
 
24
int main(argc, argv)
 
25
int argc;
 
26
char **argv;
 
27
{
 
28
    int c, i, j, n;
 
29
    extern int optind;
 
30
    extern char *optarg;
 
31
    int errflg;
 
32
    char text[32], ftype[5], fauth[5];
 
33
    int dir_skip = 0, write_it, query = 0, list = 0, info_only = 0;
 
34
    int indent = 0;
 
35
 
 
36
    (void)strcat(options, get_rdfileopt());
 
37
    (void)strcat(options, LOCALOPT);
 
38
    errflg = 0;
 
39
 
 
40
    while((c = getopt(argc, argv, options)) != EOF) {
 
41
        if(!rdfileopt((char)c)) {
 
42
            switch(c) {
 
43
            case 'R':
 
44
                dorep = 0;
 
45
                break;
 
46
            case 'l':
 
47
                list++;
 
48
                break;
 
49
            case 'q':
 
50
                query++;
 
51
                break;
 
52
            case 'i':
 
53
                info_only++;
 
54
                break;
 
55
            case '?':
 
56
                errflg++;
 
57
                break;
 
58
            case 'H':
 
59
                give_rdfileopt();
 
60
                (void)fprintf(stderr, "Binhex specific options:\n");
 
61
                (void)fprintf(stderr, "-r:\tdo not use run length encoding\n");
 
62
                (void)fprintf(stderr,
 
63
                        "-i:\tgive information only, do not write\n");
 
64
                (void)fprintf(stderr, "-l:\tgive listing\n");
 
65
                (void)fprintf(stderr,
 
66
                        "-q:\tquery for every file/folder before writing\n");
 
67
                (void)fprintf(stderr,
 
68
                        "-V:\tgive information about this version\n");
 
69
                (void)fprintf(stderr, "-H:\tthis message\n");
 
70
                (void)fprintf(stderr, "Default is silent writing\n");
 
71
                exit(0);
 
72
            case 'V':
 
73
                (void)fprintf(stderr, "Version %s, ", VERSION);
 
74
                (void)fprintf(stderr, "patchlevel %d", PATCHLEVEL);
 
75
                (void)fprintf(stderr, "%s.\n", get_minb());
 
76
                exit(0);
 
77
            }
 
78
        }
 
79
    }
 
80
    if(errflg) {
 
81
        usage();
 
82
        exit(1);
 
83
    }
 
84
 
 
85
    if(info_only || query) {
 
86
        list++;
 
87
    }
 
88
 
 
89
    setup(argc - optind, argv + optind);
 
90
    while((i = nextfile()) != ISATEND) {
 
91
        if(dir_skip) {
 
92
            if(i == ISDIR) {
 
93
                dir_skip++;
 
94
            } else if(i == ENDDIR) {
 
95
                dir_skip--;
 
96
            }
 
97
            continue;
 
98
        }
 
99
 
 
100
        write_it = 1;
 
101
        n = file_info[I_NAMEOFF] & 0x7f;
 
102
        transname(file_info + I_NAMEOFF + 1, text, n);
 
103
        if(i == ISFILE) {
 
104
            transname(file_info + I_TYPEOFF, ftype, 4);
 
105
            transname(file_info + I_AUTHOFF, fauth, 4);
 
106
        }
 
107
        if(list) {
 
108
            if(i == ISFILE) {
 
109
                do_indent(indent);
 
110
                (void)fprintf(stderr,
 
111
                    "name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
 
112
                    text, ftype, fauth, (long)data_size, (long)rsrc_size);
 
113
            } else if(i == ISDIR) {
 
114
                do_indent(indent);
 
115
                dir_ptr += 64;
 
116
                if(dir_ptr == dir_max) {
 
117
                    if(dir_max == 0) {
 
118
                        dir_stack = malloc(64);
 
119
                    } else {
 
120
                        dir_stack = realloc(dir_stack, (unsigned)dir_max + 64);
 
121
                    }
 
122
                    dir_max += 64;
 
123
                    if(dir_stack == NULL) {
 
124
                        (void)fprintf(stderr, "Insufficient memory\n");
 
125
                        exit(1);
 
126
                    }
 
127
                }
 
128
                for(j = 0; j <= n; j++) {
 
129
                    dir_stack[dir_ptr + j] = text[j];
 
130
                }
 
131
                (void)fprintf(stderr, "folder=\"%s\"", text);
 
132
                indent++;
 
133
            } else {
 
134
                indent--;
 
135
                do_indent(indent);
 
136
                (void)fprintf(stderr, "leaving folder \"%s\"",
 
137
                        dir_stack + dir_ptr);
 
138
                dir_ptr -= 64;
 
139
            }
 
140
            if(info_only) {
 
141
                write_it = 0;
 
142
            }
 
143
            if(query) {
 
144
                if(i != ENDDIR) {
 
145
                    write_it = do_query();
 
146
                } else {
 
147
                    (void)fputc('\n', stderr);
 
148
                }
 
149
                if(!write_it && i == ISDIR) {
 
150
                    dir_skip = 1;
 
151
                    indent--;
 
152
                    dir_ptr -= 64;
 
153
                }
 
154
            } else {
 
155
                (void)fputc('\n', stderr);
 
156
            }
 
157
        }
 
158
 
 
159
        if(write_it) {
 
160
            if(i == ISFILE) {
 
161
                dofile();
 
162
            }
 
163
        }
 
164
    }
 
165
    exit(0);
 
166
    /* NOTREACHED */
 
167
}
 
168
 
 
169
static void usage()
 
170
{
 
171
    (void)fprintf(stderr, "Usage: binhex [-%s] [files]\n", options);
 
172
    (void)fprintf(stderr, "Use \"binhex -H\" for help.\n");
 
173
}
 
174