File: exam.c

package info (click to toggle)
pact 980714-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 13,096 kB
  • ctags: 26,034
  • sloc: ansic: 109,076; lisp: 9,645; csh: 7,147; fortran: 1,050; makefile: 136; lex: 95; sh: 32
file content (147 lines) | stat: -rw-r--r-- 3,463 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * EXAM.C - diagnostic to examine binary files
 *
 * Source Version: 9.0
 * Software Release #92-0043
 *
 */

#include "cpyright.h"

#include <signal.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

#ifdef THINK_C
#include <stdlib.h>
#endif

#define MAXLINE 255

extern long atol();

#ifndef SEEK_SET
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif

void
 main(int c, char **);

/*--------------------------------------------------------------------------*/

#ifndef max

/*--------------------------------------------------------------------------*/

/* MAX - the oft wanted maximum */

#define max(a, b) (a > b) ? a : b

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* MIN - companion to max */

#define min(a, b) (a < b) ? a : b

/*--------------------------------------------------------------------------*/

#endif

/*--------------------------------------------------------------------------*/

/* MAIN - control the exam process */

void main(c,v)
   int c;
   char *v[];
   {FILE *fp, *out;
    long i, start, end;
    char *name;

#ifdef THINK_C
    char s[MAXLINE], *t;

    printf("Examine: ");
    gets(s);
    name = strtok(s, " ");
    if ((fp = fopen(name, BINARY_MODE_RPLUS)) == NULL)
       {fprintf(stderr, "EXAM couldn't open file %s\r\n", name);
        exit(1);};

    strcat(name, ".lst");
    out = fopen(name, "w");

    fprintf(out, "File %s opened\n", name);

    if ((t = strtok(NULL, " ")) == NULL)
       {start = 0L;
        fseek(fp, 0L, SEEK_END);
        end   = ftell(fp) - 1;}
    else
       {start = atol(t);
        if ((t = strtok(NULL, " ")) == NULL)
           {fseek(fp, 0L, SEEK_END);
            end = ftell(fp) - 1;}
        else
           end = atol(t);};

#else

    out = STDOUT;

/* open the file */
    name = v[1];
    if ((fp = fopen(name, "rb")) == NULL)
       {fprintf(stderr, "EXAM couldn't open file %s\r\n", name);
        exit(1);};
    fprintf(out, "File %s opened\n", name);

    if (c >= 4)
       {start = atol(v[2]);
        end   = atol(v[3]);}
    else if (c == 3)
       {start = atol(v[2]);
        fseek(fp, 0L, SEEK_END);
        end   = ftell(fp) - 1;
        fseek(fp, 0L, SEEK_SET);}
    else if (c == 2)
       {start = 0L;
        fseek(fp, 0L, SEEK_END);
        end   = ftell(fp) - 1;
        fseek(fp, 0L, SEEK_SET);}
    else
       {printf("\nUsage: exam <filename> <start> <end>\n\n");
        exit(1);};
        
#endif

    signal(SIGINT, SIG_DFL);

    start = max(0L, start);
    fseek(fp, start, SEEK_SET);

    fprintf(out, "\n\nExamine file %s from byte %ld to byte %ld\n\n",
           name, start, end);
    fprintf(out, "   Byte   Char   Dec   Hex\n\n");
    for (i = start; i <= end; i++)
        {c = fgetc(fp);
         if (c == '\0')
            fprintf(out, "%8ld:   0     %3d    %2x\n", i, c, c);
         else if (c < ' ')
            fprintf(out, "%8ld:  ^%c     %3d    %2x\n", i, '@'+c, c, c);
         else if (c < '\177')
            fprintf(out, "%8ld:   %c     %3d    %2x\n", i, c, c, c);
         else
            fprintf(out, "%8ld:  --     %3d    %2x\n", i, c, c);};

    fclose(fp);
    fprintf(out, "\nFile closed - EXAM exiting\n");

    exit(0);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/