~ubuntu-branches/debian/stretch/jfsutils/stretch

« back to all changes in this revision

Viewing changes to xpeek/display.c

  • Committer: Bazaar Package Importer
  • Author(s): Christopher L Cheney
  • Date: 2002-02-10 01:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20020210010000-v63g4150dcpnehzq
Tags: upstream-1.0.14
ImportĀ upstreamĀ versionĀ 1.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *   Copyright (c) International Business Machines  Corp., 2000
 
4
 *
 
5
 *   This program is free software;  you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU General Public License as published by
 
7
 *   the Free Software Foundation; either version 2 of the License, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   This program is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 
13
 *   the GNU General Public License for more details.
 
14
 *
 
15
 *   You should have received a copy of the GNU General Public License
 
16
 *   along with this program;  if not, write to the Free Software
 
17
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 */
 
19
/* display
 
20
    Displays data in a variety of formats
 
21
*/
 
22
 
 
23
#include "xpeek.h"
 
24
 
 
25
#include <jfs_xtree.h>
 
26
 
 
27
/* libfs includes */
 
28
#include <devices.h>
 
29
 
 
30
static void display_hex(char*, uint32_t, uint32_t);
 
31
 
 
32
/* endian routines */
 
33
#include <jfs_filsys.h>
 
34
extern uint32_t type_jfs;
 
35
extern void ujfs_swap_iag_t( iag_t  * );
 
36
extern void ujfs_swap_dinode( dinode_t *, int32_t, uint32_t );
 
37
extern void ujfs_swap_superblock( struct superblock * );
 
38
 
 
39
 
 
40
void display()
 
41
{
 
42
  int64_t   addr;
 
43
  int64_t   block;
 
44
  char      *buffer;
 
45
  char      cmdline[512];
 
46
  uint32_t  data_size;
 
47
  int       format = 'a';
 
48
  int64_t   len;
 
49
  uint32_t  length = 0;
 
50
  uint32_t  offset = 0;
 
51
  char      *token;
 
52
 
 
53
  token = strtok(0, "   \n");
 
54
  if (token == 0) {
 
55
    fputs("display: Please enter: block [offset [format [count]]]\ndisplay> ",stdout);
 
56
    fgets(cmdline, 512, stdin);
 
57
    token = strtok(cmdline, "   \n");
 
58
    if (token == 0)
 
59
      return;
 
60
  }
 
61
  errno = 0;
 
62
  block = strtoull(token, 0, 0);
 
63
  if (block == 0 && errno) {
 
64
    fputs("display: invalid block\n\n", stderr);
 
65
    return;
 
66
  }
 
67
  if ( (token = strtok(0, "     \n")) ) {
 
68
    offset = strtoul(token, 0, 16);
 
69
    if (offset == 0 && errno) {
 
70
      fputs("display: invalid offset\n\n", stderr);
 
71
      return;
 
72
    }
 
73
  }
 
74
  if ( (token = strtok(0, "     \n")) )
 
75
    format = token[0];
 
76
 
 
77
  if ( (token = strtok(0, "     \n")) ) {
 
78
    length = strtoul(token, 0, 0);
 
79
    if (length == 0 && errno) {
 
80
      fputs("display: invalid length\n\n", stderr);
 
81
      return;
 
82
    }
 
83
  }
 
84
 
 
85
  if (strtok(0, "       \n")) {
 
86
    fputs("display: Too many arguments\n\n", stderr);
 
87
    return;
 
88
  }
 
89
 
 
90
  switch (format) {
 
91
    case 'a':
 
92
      data_size=1;
 
93
      if (length == 0)
 
94
        length = bsize;
 
95
      break;
 
96
    case 'i':
 
97
      data_size=sizeof(struct dinode);
 
98
      if (length == 0)
 
99
        length = 1;
 
100
      break;
 
101
    case 'I':
 
102
      data_size=sizeof(iag_t);
 
103
      if (length == 0)
 
104
        length = 1;
 
105
      break;
 
106
    case 's':
 
107
      data_size=sizeof(struct superblock);
 
108
      if (length == 0)
 
109
        length = 1;
 
110
      break;
 
111
    case 'x':
 
112
      data_size=4;
 
113
      if (length == 0)
 
114
        length = bsize/4;
 
115
      break;
 
116
    case 'X':
 
117
      data_size=sizeof(xad_t);
 
118
      if (length == 0)
 
119
        length = 1;
 
120
      break;
 
121
    default:
 
122
      fputs("display:  invalid format\n\n", stderr);
 
123
      return;
 
124
  }
 
125
 
 
126
  addr = block << l2bsize;
 
127
  len = ((length*data_size)+offset+bsize-1)&(~(bsize-1));
 
128
  buffer = malloc(len);
 
129
  if (buffer == 0) {
 
130
    fputs("display: error calling malloc\n\n", stderr);
 
131
    return;
 
132
  }
 
133
 
 
134
  if (ujfs_rw_diskblocks(fd, addr, len, buffer, GET)) {
 
135
    fputs("display: ujfs_rw_diskblocks failed\n\n", stderr);
 
136
    free(buffer);
 
137
    return;
 
138
  }
 
139
 
 
140
  printf("Block: %lld     Real Address 0x%llx\n", (long long)block, (long long)addr);
 
141
  switch (format) {
 
142
    case 'a':
 
143
    case 'x':
 
144
      display_hex(&buffer[offset], length, offset);
 
145
      break;
 
146
    case 'i':
 
147
      {
 
148
        int i;
 
149
        struct dinode *inode = (struct dinode*)&buffer[offset];
 
150
        for (i=0; i < length; i++, inode++) {
 
151
          /* swap if on big endian machine */
 
152
          if (type_jfs & JFS_SWAP_BYTES) {
 
153
            ujfs_swap_dinode( inode, GET, type_jfs ); 
 
154
          }
 
155
          display_inode(inode);
 
156
          if (more())
 
157
            return;
 
158
        }
 
159
      }
 
160
      break;
 
161
    case 'I':
 
162
      {
 
163
        int i;
 
164
        iag_t *iag = (iag_t *)&buffer[offset];
 
165
        for (i=0; i < length; i++, iag++) {
 
166
          /* swap if on big endian machine */
 
167
          if (type_jfs & JFS_SWAP_BYTES) {
 
168
            ujfs_swap_iag_t( iag ); 
 
169
          }
 
170
          display_iag(iag);
 
171
          if (more())
 
172
            return;
 
173
        }
 
174
      }
 
175
      break;
 
176
 
 
177
    case 's':
 
178
      /* swap if on big endian machine */
 
179
      if (type_jfs & JFS_SWAP_BYTES) {
 
180
        ujfs_swap_superblock( (struct superblock *)&buffer[offset] ); 
 
181
      }
 
182
      if (display_super((struct superblock *)&buffer[offset]) == XPEEK_CHANGED) {
 
183
        /* swap if on big endian machine */
 
184
        if (type_jfs & JFS_SWAP_BYTES) {
 
185
          ujfs_swap_superblock( (struct superblock *)&buffer[offset] ); 
 
186
        }
 
187
        if (ujfs_rw_diskblocks(fd, addr, len, buffer, PUT))
 
188
          fputs("Display:  Error writing superblock!\n",
 
189
                stderr);
 
190
      }
 
191
 
 
192
      break;
 
193
    default:
 
194
      fputs("display:  specified format not yet supported\n\n", stderr);
 
195
      break;
 
196
  }
 
197
 
 
198
  free (buffer);
 
199
  return;
 
200
}
 
201
 
 
202
/*
 
203
 *      display_hex: display region in hex/ascii
 
204
 */
 
205
static void display_hex( char      *addr,
 
206
                         uint32_t  length,
 
207
                         uint32_t  offset)
 
208
{
 
209
  uint8_t  hextext[37];
 
210
  uint8_t  asciitxt[17];
 
211
  uint8_t  *x = (uint8_t *)addr, x1, x2;
 
212
  int32_t  i, j, k, l;
 
213
 
 
214
  hextext[36] = '\0';
 
215
  asciitxt[16] = '\0';    /* null end of string */
 
216
 
 
217
  l = 0;
 
218
 
 
219
  for (i=1; i<=((length+15)/16); i++) {
 
220
    if (i>1 && ((i-1)%16) == 0)
 
221
      if (more())
 
222
        break;
 
223
 
 
224
      /* print address/offset */
 
225
    printf("%08x: ", offset+l);
 
226
 
 
227
    /* print 16 bytes per line */
 
228
    for (j=0, k = 0; j<16; j++, x++, l++) {
 
229
      if ((j%4) == 0)
 
230
        hextext[k++] = ' ';
 
231
      if (l < length) {
 
232
        hextext[k++] = ((x1 = ((*x & 0xf0) >> 4)) < 10)
 
233
                       ? ('0' + x1) : ('A' + x1 - 10);
 
234
        hextext[k++] = ((x2 = (*x & 0x0f)) < 10)
 
235
                       ? ('0' + x2) : ('A' + x2 - 10);
 
236
        asciitxt[j] = ((*x < 0x20) ||
 
237
                       (*x >= 0x7f)) ? '.' : *x;
 
238
      } else {  /* byte not in range */
 
239
        hextext[k++] = ' ';
 
240
        hextext[k++] = ' ';
 
241
        asciitxt[j] = '.';
 
242
      }
 
243
    }
 
244
    printf("%s   |%s|\n", hextext, asciitxt);
 
245
  }
 
246
}
 
247