File: repair.c

package info (click to toggle)
btoa 5.2.1-5
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 80 kB
  • ctags: 57
  • sloc: ansic: 909; makefile: 65
file content (338 lines) | stat: -rw-r--r-- 8,369 bytes parent folder | download | duplicates (3)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* repair.c */

/* Written by Stefan Parmark. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "btoa.h"

/* File names. */
const BYTE *diagnosisname   = "btoa.dia";
const BYTE *repairname      = "btoa.rep";
const BYTE *repairedname    = "btoa.rdy";

/* File headers. */
const BYTE *diagnosisheader = "xdiagnosis\n";
const BYTE *repairheader    = "xrepair\n";


/* Produce diagnosis file from diagnoses records created by atob(). */
/* It contains the lines immediately before and after the error     */
/* sequence.                                                        */
void producediagnosis(diagnosislist, infile)
register struct Diagnosis *diagnosislist;
register FILE *infile;
{
  register FILE *diagnosisfile;
  LONG startpos, endpos;
  register LONG currentpos;
/*  extern BYTE *diagnosisname, *diagnosisheader; */
  extern BYTE buffer[BUFSIZE];

  currentpos = ftell(infile);

  if ((diagnosisfile = fopen_write(diagnosisname)) != NULL)
  {
    fprintf(stderr, "btoa: Diagnosis output to '%s'.\n", diagnosisname);

    fputs(diagnosisheader, diagnosisfile);
    do
    {
      /* Extract startpos & endpos from diagnosislist. */
      outdiagnosislist(diagnosislist, &startpos, &endpos);

      if (startpos != -1)
      {
        /* Print line before error. */
        fseek(infile, startpos, 0);
        fgets(buffer, BUFSIZE, infile);
        fputs(buffer, diagnosisfile);

        /* Print line after error. */
        fseek(infile, endpos, 0);
        fgets(buffer, BUFSIZE, infile);
        fputs(buffer, diagnosisfile);
      }
    }
    while (startpos != -1);
    fputs(diagnosisheader, diagnosisfile);

    fclose(diagnosisfile);
  }

  /* Move file pointer to where it was when we entered. */
  fseek(infile, currentpos, 0);
}


/* Insert two file positions into diagnosislist. */
void intodiagnosislist(diagnosislist, startpos, endpos)
register struct Diagnosis *diagnosislist;
register LONG startpos, endpos;
{
  register struct Diagnosis *diagnosisitem, *lastitem;

  diagnosisitem = (struct Diagnosis *)malloc(sizeof(struct Diagnosis));
  diagnosisitem->startpos = startpos;
  diagnosisitem->endpos = endpos;
  diagnosisitem->next = NULL;

  if ((lastitem = diagnosislist->last) == NULL)  /* List is empty */
    diagnosislist->next = diagnosislist->last = diagnosisitem;
  else
  {
    if (lastitem->endpos >= startpos)
    {
      lastitem->endpos = endpos;
      free((BYTE *) diagnosisitem);
    }
    else
    {
      lastitem->next = diagnosisitem;
      diagnosislist->last = diagnosisitem;
    }
  }
}


/* Extract two file positions from diagnosislist. */
void outdiagnosislist(diagnosislist, startpos, endpos)
register struct Diagnosis *diagnosislist;
LONG *startpos, *endpos;
{
  register struct Diagnosis *diagnosisitem;

  if ((diagnosisitem = diagnosislist->next) == NULL)  /* List is empty */
    *startpos = *endpos = -1;
  else
  {
    *startpos = diagnosisitem->startpos;
    *endpos = diagnosisitem->endpos;

    diagnosislist->next = diagnosisitem->next;
    free((BYTE *)diagnosisitem);
    if (diagnosislist->next == NULL)
      diagnosislist->last = NULL;
  }
}


/* Copy infile to outfile until searchstring is found. If outfile */
/* is NULL nothing will be written.                               */
BYTE copyfile(infile, outfile, searchstring)
register FILE *infile, *outfile;
register BYTE *searchstring;
{
  register BYTE stop, error;
  static BYTE copybuffer[BUFSIZE];

  stop = error = FALSE;
  while (!(stop || error))
    if (readbuffer(copybuffer, "archive", infile))
      error = TRUE;
    else
    {
      if (outfile != NULL)
        fputs(copybuffer, outfile);
      if (strcmp(copybuffer, searchstring) == 0)
        stop = TRUE;
    }

  return(error);
}


/* Read a line from infile into buffer. Returns TRUE if */
/* end-of-file has been reached.                        */
BYTE readbuffer(buffer, errormsg, infile)
register BYTE *buffer, *errormsg;
register FILE *infile;
{
  register BYTE error;

  error = FALSE;
  if (fgets(buffer, BUFSIZE, infile) == NULL)
  {
    fprintf(stderr, "btoa: Unexpected end of %s file.\n", errormsg);
    error = TRUE;
  }

  return(error);
}


FILE *fopen_read(filename)
register BYTE *filename;
{
  register FILE *infile;

  if ((infile = fopen(filename, "r")) == NULL)
    fprintf(stderr, "btoa: Can't open '%s' for input.\n", filename);

  return(infile);
}


FILE *fopen_write(filename)
register BYTE *filename;
{
  register FILE *outfile;

  if ((outfile = fopen(filename, "w")) == NULL)
    fprintf(stderr, "btoa: Can't open '%s' for output.\n", filename);

  return(outfile);
}


/* Extract lines from original archive to fix the damaged one. */
BYTE producerepair(infile)
register FILE *infile;
{
  register FILE *repairfile, *diagnosisfile;
  register BYTE error, stop;
  static const BYTE *errormsg = "diagnosis";
/*  extern BYTE *diagnosisname, *diagnosisheader, *repairname, *repairheader,*/
  extern BYTE buffer[BUFSIZE];

  error = FALSE;
  diagnosisfile = repairfile = NULL;

  fprintf(stderr, "btoa: Repair output to '%s'.\n", repairname);
  if ((diagnosisfile = fopen_read(diagnosisname)) == NULL)
    error = TRUE;
  else if ((repairfile = fopen_write(repairname)) == NULL)
  {
    fclose(diagnosisfile);
    diagnosisfile = NULL;
    error = TRUE;
  }
  else
  {
    /* Read until header is found. This makes it possible to   */
    /* have junk before the header, such as an article header. */
    do
    {
      if (readbuffer(buffer, errormsg, diagnosisfile))
        error = TRUE;
    }
    while (!error && strcmp(buffer, diagnosisheader) != 0);
    fputs(repairheader, repairfile);
  }

  stop = FALSE;
  while (!(error || stop))
  {
    /* Loop until header is found again. */

    if (readbuffer(buffer, errormsg, diagnosisfile))
      error = TRUE;
    else if (strcmp(buffer, diagnosisheader) == 0)
      stop = TRUE;
    else
    {
      /* Read until line before error is found. */
      error = copyfile(infile, NULL, buffer);
      if (!error)
      {
        /* Print line before error. */
        fputs(buffer, repairfile);

        if (readbuffer(buffer, errormsg, diagnosisfile))
          error = TRUE;
        else
        {
          /* Print line after error */
          fputs(buffer, repairfile);
          /* Copy infile to repairfile until line after error */
          error = copyfile(infile, repairfile, buffer);
        }
      }
    }
  }

  if (!error)
    fputs(repairheader, repairfile);

  if (repairfile != NULL)
    fclose(repairfile);
  if (diagnosisfile != NULL)
    fclose(diagnosisfile);

  return(error);
}


/* Repair damaged archive from repair file. */
BYTE performrepair(infile)
register FILE *infile;
{
  register FILE *repairfile, *outfile;
  register BYTE error, stop;
  static const BYTE *errormsg = "repair";
/*  extern BYTE *repairname, *repairedname, *repairheader, buffer[BUFSIZE]; */
  extern BYTE  buffer[BUFSIZE];

  error = FALSE;
  repairfile = outfile = NULL;

  if ((repairfile = fopen_read(repairname)) == NULL)
    error = TRUE;
  else if ((outfile = fopen_write(repairedname)) == NULL)
  {
    fclose(repairfile);
    repairfile = NULL;
    error = TRUE;
  }
  else
  {
    fprintf(stderr, "btoa: Repaired archive written to '%s'.\n", repairedname);

    /* Read until header is found. */
    do
    {
      if (readbuffer(buffer, errormsg, repairfile))
        error = TRUE;
    }
    while (!error && strcmp(buffer, repairheader) != 0);
  }

  stop = FALSE;
  while (!(error || stop))
  {
    /* Loop until header is found. */

    if (readbuffer(buffer, errormsg, repairfile))
      error = TRUE;
    else if (strcmp(buffer, repairheader) == 0)
      stop = TRUE;
    else
    {
      /* Read and write until line before error. */
      error = copyfile(infile, outfile, buffer);
      if (!error)
        if (readbuffer(buffer, errormsg, repairfile))
          error = TRUE;
        else
        {
          /* Read and write until line after error. */
          error = copyfile(repairfile, outfile, buffer);
          /* Skip until line after error */
          copyfile(infile, NULL, buffer);
        }
    }
  }

  if (!error)  /* Write rest of archive. */
    while (fgets(buffer, BUFSIZE, infile) != NULL)
      fputs(buffer, outfile);

  if (outfile != NULL)
    fclose(outfile);
  if (repairfile != NULL)
    fclose(repairfile);

  return(error);
}