-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
221 lines (188 loc) · 3.74 KB
/
misc.c
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
#include "Header.h" /* for ABEND_EXIT */
#include "Symbols.h" /* needed by Messages.h */
#include "List.h" /* needed by Clause.h */
#include "Clause.h" /* needed by Messages.h */
#include "Messages.h" /* for broadcast_string and HALT_MSG */
/*************
*
* abend
*
*************/
void abend(char *str)
{
output_stats(Peer_fp); /* Peers-mcd, November 1996 */
fprintf(Peer_fp, "\n\n********** ABNORMAL END **********\n");
fprintf(Peer_fp, "********** %s\n", str);
fprintf(Peer_fp, "\nTHE END\n");
fprintf(stderr, "\n\n\007********** ABNORMAL END **********\n");
fprintf(stderr, "********** %s\n", str);
broadcast_string("", 0, HALT_MSG); /* Upon abnormal end, all peers halt. */
MPI_Finalize();
exit(ABEND_EXIT);
} /* abend */
/*************
*
* int initial_str(s, t) -- Is s an initial substring of t?
*
*************/
int initial_str(char *s, char *t)
{
for ( ; *s == *t; s++, t++)
if (*s == '\0') return(1);
return(*s == '\0');
} /* initial_str */
/*************
*
* int str_int(string, int_ptr) -- Translate a string to an integer.
*
* String has optional '+' or '-' as first character.
* Return(1) iff success.
*
*************/
int str_int(char *s, int *np)
{
int i, sign, n;
i = 0;
sign = 1;
if (s[0] == '+' || s[0] == '-') {
if (s[0] == '-')
sign = -1;
i = 1;
}
if (s[i] == '\0')
return(0);
else {
n = 0;
for( ; s[i] >= '0' && s[i] <= '9'; i++)
n = n * 10 + s[i] - '0';
*np = n * sign;
return(s[i] == '\0');
}
} /* str_int */
/*************
*
* int_str(int, str) -- translate an integer to a string
*
*************/
void int_str(int i, char *s)
{
int j, sign;
if ((sign = i) < 0)
i = -i;
j = 0;
if (i == 0)
s[j++] = '0';
else {
while (i > 0) {
s[j++] = i % 10 + '0';
i = i / 10;
}
}
if (sign < 0)
s[j++] = '-';
s[j] = '\0';
reverse(s);
} /* int_str */
/*************
*
* int str_long(string, long_ptr) -- Translate a string to a long.
*
* String has optional '+' or '-' as first character.
* Return(1) iff success.
*
*************/
int str_long(char *s, long int *np)
{
int i, sign;
long n;
i = 0;
sign = 1;
if (s[0] == '+' || s[0] == '-') {
if (s[0] == '-')
sign = -1;
i = 1;
}
if (s[i] == '\0')
return(0);
else {
n = 0;
for( ; s[i] >= '0' && s[i] <= '9'; i++)
n = n * 10 + s[i] - '0';
*np = n * sign;
return(s[i] == '\0');
}
} /* str_long */
/*************
*
* long_str(int, str) -- translate a long to a string
*
*************/
void long_str(long int i, char *s)
{
int j;
long sign;
if ((sign = i) < 0)
i = -i;
j = 0;
if (i == 0)
s[j++] = '0';
else {
while (i > 0) {
s[j++] = i % 10 + '0';
i = i / 10;
}
}
if (sign < 0)
s[j++] = '-';
s[j] = '\0';
reverse(s);
} /* long_str */
/*************
*
* cat_str(s1, s2, s3)
*
*************/
void cat_str(char *s1, char *s2, char *s3)
{
int i, j;
for (i = 0; s1[i] != '\0'; i++)
s3[i] = s1[i];
for (j = 0; s2[j] != '\0'; j++, i++)
s3[i] = s2[j];
s3[i] = '\0';
} /* cat_str */
/*************
*
* str_copy(s, t) -- Copy a string.
*
*************/
void str_copy(s, t)
char *s;
char *t;
{
while (*t++ = *s++);
} /* str_copy */
/*************
*
* int str_ident(s, t) -- Identity of strings
*
*************/
int str_ident(char *s, char *t)
{
return(strcmp(s, t) == 0);
} /* str_ident */
/*************
*
* reverse(s) -- reverse a string
*
*************/
void reverse(char *s)
{
int i, j;
char temp;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
} /* reverse */