~ubuntu-branches/ubuntu/hoary/cdrtools/hoary

« back to all changes in this revision

Viewing changes to mkisofs/diag/dump.c

  • Committer: Bazaar Package Importer
  • Author(s): Eduard Bloch
  • Date: 2002-04-09 10:03:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020409100306-t4hagiv7gm0fhggv
Tags: upstream-1.10
ImportĀ upstreamĀ versionĀ 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)dump.c   1.11 00/12/09 joerg */
 
2
#ifndef lint
 
3
static  char sccsid[] =
 
4
        "@(#)dump.c     1.11 00/12/09 joerg";
 
5
#endif
 
6
/*
 
7
 * File dump.c - dump a file/device both in hex and in ASCII.
 
8
 
 
9
   Written by Eric Youngdale (1993).
 
10
 
 
11
   Copyright 1993 Yggdrasil Computing, Incorporated
 
12
 
 
13
   This program is free software; you can redistribute it and/or modify
 
14
   it under the terms of the GNU General Public License as published by
 
15
   the Free Software Foundation; either version 2, or (at your option)
 
16
   any later version.
 
17
 
 
18
   This program is distributed in the hope that it will be useful,
 
19
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
   GNU General Public License for more details.
 
22
 
 
23
   You should have received a copy of the GNU General Public License
 
24
   along with this program; if not, write to the Free Software
 
25
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
26
 
 
27
#include <mconfig.h>
 
28
#include <stdxlib.h>
 
29
#include <unixstd.h>
 
30
#include <strdefs.h>
 
31
#include <utypes.h>
 
32
 
 
33
#include <stdio.h>
 
34
#include <standard.h>
 
35
#ifdef HAVE_TERMIOS_H
 
36
#include <termios.h>
 
37
#include <sys/ioctl.h>
 
38
#else
 
39
#include <termio.h>
 
40
#endif
 
41
#include <signal.h>
 
42
#include <schily.h>
 
43
 
 
44
FILE *  infile;
 
45
off_t   file_addr;
 
46
unsigned char buffer[256];
 
47
unsigned char search[64];
 
48
 
 
49
#define PAGE 256
 
50
 
 
51
#ifdef HAVE_TERMIOS_H
 
52
struct termios savetty;
 
53
struct termios newtty;
 
54
#else
 
55
struct termio savetty;
 
56
struct termio newtty;
 
57
#endif
 
58
 
 
59
void    reset_tty       __PR((void));
 
60
void    set_tty         __PR((void));
 
61
void    onsusp          __PR((int sig));
 
62
void    crsr2           __PR((int row, int col));
 
63
void    showblock       __PR((int flag));
 
64
int     getbyte         __PR((void));
 
65
void    usage           __PR((int excode));
 
66
int     main            __PR((int argc, char *argv[]));
 
67
 
 
68
void
 
69
reset_tty(){
 
70
#ifdef HAVE_TERMIOS_H
 
71
  if(tcsetattr(0, TCSANOW, &savetty) == -1)
 
72
#else
 
73
  if(ioctl(0, TCSETAF, &savetty)==-1)
 
74
#endif
 
75
    {
 
76
#ifdef  USE_LIBSCHILY
 
77
      comerr("Cannot put tty into normal mode\n");
 
78
#else
 
79
      printf("Cannot put tty into normal mode\n");
 
80
      exit(1);
 
81
#endif
 
82
    }
 
83
}
 
84
 
 
85
void
 
86
set_tty()
 
87
{
 
88
#ifdef HAVE_TERMIOS_H
 
89
  if(tcsetattr(0, TCSANOW, &newtty) == -1)
 
90
#else
 
91
  if(ioctl(0, TCSETAF, &newtty)==-1)
 
92
#endif
 
93
    {
 
94
#ifdef  USE_LIBSCHILY
 
95
      comerr("Cannot put tty into raw mode\n");
 
96
#else
 
97
      printf("Cannot put tty into raw mode\n");
 
98
      exit(1);
 
99
#endif
 
100
    }
 
101
}
 
102
 
 
103
 
 
104
/* Come here when we get a suspend signal from the terminal */
 
105
 
 
106
void
 
107
onsusp(sig)
 
108
        int     sig;
 
109
{
 
110
#ifdef  SIGTTOU
 
111
    /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
 
112
    signal(SIGTTOU, SIG_IGN);
 
113
#endif
 
114
    reset_tty ();
 
115
    fflush (stdout);
 
116
#ifdef  SIGTTOU
 
117
    signal(SIGTTOU, SIG_DFL);
 
118
    /* Send the TSTP signal to suspend our process group */
 
119
    signal(SIGTSTP, SIG_DFL);
 
120
/*    sigsetmask(0);*/
 
121
    kill (0, SIGTSTP);
 
122
    /* Pause for station break */
 
123
 
 
124
    /* We're back */
 
125
    signal (SIGTSTP, onsusp);
 
126
#endif
 
127
    set_tty ();
 
128
}
 
129
 
 
130
 
 
131
void
 
132
crsr2(row, col)
 
133
        int     row;
 
134
        int     col;
 
135
{
 
136
  printf("\033[%d;%dH",row,col);
 
137
}
 
138
 
 
139
void
 
140
showblock(flag)
 
141
        int     flag;
 
142
{
 
143
  unsigned int k;
 
144
  int i, j;
 
145
  lseek(fileno(infile), file_addr, SEEK_SET);
 
146
  read(fileno(infile), buffer, sizeof(buffer));
 
147
  if(flag) {
 
148
    for(i=0;i<16;i++){
 
149
      crsr2(i+3,1);
 
150
        if (sizeof(file_addr) > sizeof(long)) {
 
151
                printf("%16.16llx ", (Llong)file_addr+(i<<4));
 
152
        } else {
 
153
                printf("%8.8lx ", (long)file_addr+(i<<4));
 
154
        }
 
155
      for(j=15;j>=0;j--){
 
156
        printf("%2.2x",buffer[(i<<4)+j]);
 
157
        if(!(j & 0x3)) printf(" ");
 
158
      };
 
159
      for(j=0;j< 16;j++){
 
160
        k = buffer[(i << 4) + j];
 
161
        if(k >= ' ' && k < 0x80) printf("%c",k);
 
162
        else printf(".");
 
163
      };
 
164
    }
 
165
  };
 
166
  crsr2(20,1);
 
167
        if (sizeof(file_addr) > sizeof(long)) {
 
168
                printf(" Zone, zone offset: %14llx %12.12llx  ",
 
169
                        (Llong)file_addr>>11, (Llong)file_addr & 0x7ff);
 
170
        } else {
 
171
                printf(" Zone, zone offset: %6lx %4.4lx  ",
 
172
                        (long)(file_addr>>11), (long)(file_addr & 0x7ff));
 
173
        }
 
174
  fflush(stdout);
 
175
}
 
176
 
 
177
int
 
178
getbyte()
 
179
{
 
180
  char c1;
 
181
  c1 = buffer[file_addr & (PAGE-1)];
 
182
  file_addr++;
 
183
  if ((file_addr & (PAGE-1)) == 0) showblock(0);
 
184
  return c1;
 
185
}
 
186
 
 
187
void
 
188
usage(excode)
 
189
        int     excode;
 
190
{
 
191
        errmsgno(EX_BAD, "Usage: %s [options] image\n",
 
192
                get_progname());
 
193
 
 
194
        error("Options:\n");
 
195
        exit(excode);
 
196
}
 
197
 
 
198
int
 
199
main(argc, argv)
 
200
        int     argc;
 
201
        char    *argv[];
 
202
{
 
203
  char c;
 
204
  int i,j;
 
205
 
 
206
        save_args(argc, argv);
 
207
 
 
208
  if(argc < 2)
 
209
        usage(EX_BAD);
 
210
  infile = fopen(argv[1],"rb");
 
211
  if (infile == NULL) {
 
212
#ifdef  USE_LIBSCHILY
 
213
        comerr("Cannot open '%s'.\n", argv[1]);
 
214
#else
 
215
        printf("Cannot open '%s'.\n", argv[1]);
 
216
        exit(1);
 
217
#endif
 
218
  }
 
219
  for(i=0;i<30;i++) printf("\n");
 
220
  file_addr = (off_t)0;
 
221
/* Now setup the keyboard for single character input. */
 
222
#ifdef HAVE_TERMIOS_H
 
223
  if(tcgetattr(0, &savetty) == -1)
 
224
#else
 
225
        if(ioctl(0, TCGETA, &savetty) == -1)
 
226
#endif
 
227
          {
 
228
#ifdef  USE_LIBSCHILY
 
229
            comerr("Stdin must be a tty\n");
 
230
#else
 
231
            printf("Stdin must be a tty\n");
 
232
            exit(1);
 
233
#endif
 
234
          }
 
235
        newtty=savetty;
 
236
        newtty.c_lflag&=~ICANON;
 
237
        newtty.c_lflag&=~ECHO;
 
238
        newtty.c_cc[VMIN]=1;
 
239
        set_tty();
 
240
#ifdef  SIGTSTP
 
241
        signal(SIGTSTP, onsusp);
 
242
#endif
 
243
 
 
244
  do{
 
245
    if(file_addr < (off_t)0) file_addr = (off_t)0;
 
246
    showblock(1);
 
247
    read (0, &c, 1);
 
248
    if (c == 'a') file_addr -= PAGE;
 
249
    if (c == 'b') file_addr += PAGE;
 
250
    if (c == 'g') {
 
251
      crsr2(20,1);
 
252
      printf("Enter new starting block (in hex):");
 
253
        if (sizeof(file_addr) > sizeof(long)) {
 
254
                Llong   ll;
 
255
                scanf("%llx",&ll);
 
256
                file_addr = (off_t)ll;
 
257
        } else {
 
258
                long    l;
 
259
                scanf("%lx",&l);
 
260
                file_addr = (off_t)l;
 
261
        }
 
262
      file_addr = file_addr << 11;
 
263
      crsr2(20,1);
 
264
      printf("                                     ");
 
265
    };
 
266
    if (c == 'f') {
 
267
      crsr2(20,1);
 
268
      printf("Enter new search string:");
 
269
      fgets((char *)search,sizeof(search),stdin);
 
270
      while(search[strlen((char *)search)-1] == '\n') search[strlen((char *)search)-1] = 0;
 
271
      crsr2(20,1);
 
272
      printf("                                     ");
 
273
    };
 
274
    if (c == '+') {
 
275
      while(1==1){
 
276
        while(1==1){
 
277
          c = getbyte();
 
278
          if (c == search[0]) break;
 
279
        };
 
280
        for (j=1;j<strlen((char *)search);j++) 
 
281
          if(search[j] != getbyte()) break;
 
282
        if(j==strlen((char *)search)) break;
 
283
      };
 
284
      file_addr &= ~(PAGE-1);
 
285
      showblock(1);
 
286
    };
 
287
    if (c == 'q') break;
 
288
  } while(1==1);
 
289
  reset_tty();
 
290
  fclose(infile);
 
291
  return (0);
 
292
}