File: diload.c

package info (click to toggle)
diablo 1.16.test2-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 1,504 kB
  • ctags: 1,603
  • sloc: ansic: 17,654; perl: 2,054; sh: 260; csh: 118; makefile: 73
file content (167 lines) | stat: -rw-r--r-- 3,451 bytes parent folder | download
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

/*
 * DILOAD.C	
 *
 *	Add records to the history database given information on stdin
 *	in the 'didump' format.  Generally used to recover or transfer
 *	a history file between two machines.
 *
 *	To recover a blown up history file:
 *
 *		rm -f dhistory.new
 *		didump dhistory | diload -f dhistory.new
 *		if ( $status == 0) then
 *		    mv -f dhistory.new dhistory
 *		endif
 *
 *	To transfer between machines:
 *
 *		rm -f dhistory.new
 *		rsh remote -n "/news/dbin/didump /news/dhistory" | \
 *		    diload -f dhistory.new
 *		if ( $status == 0) then
 *		    mv -f dhistory.new dhistory
 *		endif
 *
 * (c)Copyright 1997, Matthew Dillon, All Rights Reserved.  Refer to
 *    the COPYRIGHT file in the base directory of this distribution 
 *    for specific rights granted.
 */

#include "defs.h"

int HistoryEntryValid(History *h);

int
main(int ac, char **av)
{
    int r = 1;
    int count = 0;
    int failed = 0;
    char buf[1024];
    int flags = 0;
    int uflag = 0;
    int verbose = 0;
    char *fileName = NULL;

    LoadDiabloConfig();

    {
	int i;

	for (i = 1; i < ac; ++i) {
	    char *ptr = av[i];

	    if (*ptr != '-') {
		if (fileName != NULL) {
		    fprintf(stderr, "Unexpected argument\n");
		    exit(1);
		}
		fileName = ptr;
		continue;
	    }
	    ptr += 2;
	    switch(ptr[-1]) {
	    case 'h':
		NewHSize = strtol(((*ptr) ? ptr : av[++i]), &ptr, 0);
		if (*ptr == 'k' || *ptr == 'K')
		    NewHSize *= 1024;
		if (*ptr == 'm' || *ptr == 'M')
		    NewHSize *= 1024 * 1024;
		if ((NewHSize ^ (NewHSize - 1)) != (NewHSize << 1) - 1) {
		    fprintf(stderr, "specified history size is not a power of 2\n");
		    exit(1);
		}
		break;
	    case 'v':
		verbose = 1;
		break;
	    case 'f':
		flags |= HGF_FAST | HGF_NOSEARCH;
		break;
	    case 'u':
		uflag = 1;
		break;
	    }
	}
    }
    if (flags & HGF_FAST) {
	struct stat st;

	if (fileName == NULL) {
	    fprintf(stderr, "You cannot run fastmode without specifying a filename!\n");
	    exit(1);
	}
	if (stat(fileName, &st) == 0 && uflag == 0) {
	    fprintf(stderr, "-f history files may not previously exist unless you also specify -u\n");
	    fprintf(stderr, "WARNING! -f -u is NOT suggested!\n");
	    exit(1);
	}
	if (uflag)
	    flags &= ~HGF_NOSEARCH;
    }

    HistoryOpen(fileName, flags);

    while (fgets(buf, sizeof(buf), stdin) != NULL) {
	History h = { 0 };
	int n;
	int iter;
	int exp;

	if (buf[0] == '.') {
	    r = 0;
	    break;
	}

	n = sscanf(buf, "DUMP %x.%x.%x gm=%d ex=%d boff=%d bsize=%d",
	    &h.hv.h1,
	    &h.hv.h2,
	    &iter,
	    &h.gmt,
	    &exp,
	    &h.boffset,		/* 0 if old style dump */
	    &h.bsize		/* 0 if old style dump */
	);
	if (n >= 5) {
	    int r = 0;

	    h.iter = iter;
	    h.exp = exp;
	    if (HistoryEntryValid(&h) < 0)
		++failed;
	    else if ((r = HistoryAdd(NULL, &h)) == 0)
		++count;
	    else if (r != RCTRYAGAIN)
		++failed;
	    else {
		fprintf(stderr, "HistoryAdd: write failed!\n");
		exit(1);
	    }
	} else {
	    fprintf(stderr, "Format error: %s", buf);
	    break;
	}
	if (verbose && ((count + failed) & 1023) == 0) {
	    printf("%d/%d\r", count, count + failed);
	    fflush(stdout);
	}
    }
    printf("diload: %d/%d entries loaded\n", count, count + failed);
    r = HistoryClose();
    if (r == RCOK)
	return(0);
    else
	return(1);
}

int
HistoryEntryValid(History *h)
{
    if (h->gmt == 0)
	return(-1);
    if (h->hv.h1 == 0 && h->hv.h2 == 0)
	return(-1);
    return(0);
}