Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / lib / datapid.c
blobc047792e090e526b89f278d6ce63687e24d7cbae
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32 /************
34 64 bits in a Cray word
36 12345678901234567890123456789012
37 1234567890123456789012345678901234567890123456789012345678901234
38 ________________________________________________________________
39 < pid >< word-offset in file (same #) >< pid >
41 1234567890123456789012345678901234567890123456789012345678901234
42 ________________________________________________________________
43 < pid >< offset in file of this word >< pid >
46 8 bits to a bytes == character
47 NBPW 8
48 ************/
50 #include <stdio.h>
51 #include <sys/param.h>
52 #ifdef UNIT_TEST
53 #include <unistd.h>
54 #include <stdlib.h>
55 #endif
57 static char Errmsg[80];
59 #define LOWER16BITS(X) (X & 0177777)
60 #define LOWER32BITS(X) (X & 0xffffffff)
62 /***
63 #define HIGHBITS(WRD, bits) ( (-1 << (64-bits)) & WRD)
64 #define LOWBITS(WRD, bits) ( (-1 >> (64-bits)) & WRD)
65 ****/
67 #define NBPBYTE 8 /* number bits per byte */
69 #ifndef DEBUG
70 #define DEBUG 0
71 #endif
73 /***********************************************************************
76 * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 bytes
77 * 1234567890123456789012345678901234567890123456789012345678901234 bits
78 * ________________________________________________________________ 1 word
79 * < pid >< offset in file of this word >< pid >
81 * the words are put together where offset zero is the start.
82 * thus, offset 16 is the start of the second full word
83 * Thus, offset 8 is in middle of word 1
84 ***********************************************************************/
85 int
86 datapidgen(pid, buffer, bsize, offset)
87 int pid;
88 char *buffer;
89 int bsize;
90 int offset;
92 #if CRAY
94 int cnt;
95 int tmp;
96 char *chr;
97 long *wptr;
98 long word;
99 int woff; /* file offset for the word */
100 int boff; /* buffer offset or index */
101 int num_full_words;
103 num_full_words = bsize/NBPW;
104 boff = 0;
106 if ( cnt=(offset % NBPW) ) { /* partial word */
108 woff = offset - cnt;
109 #if DEBUG
110 printf("partial at beginning, cnt = %d, woff = %d\n", cnt, woff);
111 #endif
113 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
115 chr = (char *)&word;
117 for (tmp=0; tmp<cnt; tmp++) { /* skip unused bytes */
118 chr++;
121 for (; boff<(NBPW-cnt) && boff<bsize; boff++, chr++) {
122 buffer[boff] = *chr;
127 * full words
130 num_full_words = (bsize-boff)/NBPW;
132 woff = offset+boff;
134 for (cnt=0; cnt<num_full_words; woff += NBPW, cnt++ ) {
136 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
138 chr = (char *)&word;
139 for(tmp=0; tmp<NBPW; tmp++, chr++) {
140 buffer[boff++] = *chr;
142 /****** Only if wptr is a word ellined
143 wptr = (long *)&buffer[boff];
144 *wptr = word;
145 boff += NBPW;
146 *****/
151 * partial word at end of buffer
154 if ( cnt=((bsize-boff) % NBPW) ) {
155 #if DEBUG
156 printf("partial at end\n");
157 #endif
158 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
160 chr = (char *)&word;
162 for (tmp=0; tmp<cnt && boff<bsize; tmp++, chr++) {
163 buffer[boff++] = *chr;
167 return bsize;
169 #else
170 return -1; /* not support on non-64 bits word machines */
172 #endif
176 /***********************************************************************
179 ***********************************************************************/
181 datapidchk(pid, buffer, bsize, offset, errmsg)
182 int pid;
183 char *buffer;
184 int bsize;
185 int offset;
186 char **errmsg;
188 #if CRAY
190 int cnt;
191 int tmp;
192 char *chr;
193 long *wptr;
194 long word;
195 int woff; /* file offset for the word */
196 int boff; /* buffer offset or index */
197 int num_full_words;
200 if ( errmsg != NULL ) {
201 *errmsg = Errmsg;
205 num_full_words = bsize/NBPW;
206 boff = 0;
208 if ( cnt=(offset % NBPW) ) { /* partial word */
209 woff = offset - cnt;
210 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
212 chr = (char *)&word;
214 for (tmp=0; tmp<cnt; tmp++) { /* skip unused bytes */
215 chr++;
218 for (; boff<(NBPW-cnt) && boff<bsize; boff++, chr++) {
219 if (buffer[boff] != *chr) {
220 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
221 offset+boff, *chr, buffer[boff]);
222 return offset+boff;
228 * full words
231 num_full_words = (bsize-boff)/NBPW;
233 woff = offset+boff;
235 for (cnt=0; cnt<num_full_words; woff += NBPW, cnt++ ) {
236 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
238 chr = (char *)&word;
239 for(tmp=0; tmp<NBPW; tmp++, boff++, chr++) {
240 if ( buffer[boff] != *chr ) {
241 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
242 woff, *chr, buffer[boff]);
243 return woff;
247 /****** only if a word elined
248 wptr = (long *)&buffer[boff];
249 if ( *wptr != word ) {
250 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
251 woff, word, *wptr);
252 return woff;
254 boff += NBPW;
255 ******/
259 * partial word at end of buffer
262 if ( cnt=((bsize-boff) % NBPW) ) {
263 #if DEBUG
264 printf("partial at end\n");
265 #endif
266 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
268 chr = (char *)&word;
271 for (tmp=0; tmp<cnt && boff<bsize; boff++, tmp++, chr++) {
272 if ( buffer[boff] != *chr ) {
273 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
274 offset+boff, *chr, buffer[boff]);
275 return offset+boff;
280 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
281 return -1; /* buffer is ok */
283 #else
285 if ( errmsg != NULL ) {
286 *errmsg = Errmsg;
288 sprintf(Errmsg, "Not supported on this OS.");
289 return 0;
291 #endif
294 } /* end of datapidchk */
296 #if UNIT_TEST
298 /***********************************************************************
299 * main for doing unit testing
300 ***********************************************************************/
302 main(ac, ag)
303 int ac;
304 char **ag;
307 int size=1234;
308 char *buffer;
309 int ret;
310 char *errmsg;
312 if ((buffer=(char *)malloc(size)) == NULL ) {
313 perror("malloc");
314 exit(2);
318 datapidgen(-1, buffer, size, 3);
320 /***
321 fwrite(buffer, size, 1, stdout);
322 fwrite("\n", 1, 1, stdout);
323 ****/
325 printf("datapidgen(-1, buffer, size, 3)\n");
327 ret=datapidchk(-1, buffer, size, 3, &errmsg);
328 printf("datapidchk(-1, buffer, %d, 3, &errmsg) returned %d %s\n",
329 size, ret, errmsg);
330 ret=datapidchk(-1, &buffer[1], size-1, 4, &errmsg);
331 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
332 size-1, ret, errmsg);
334 buffer[25]= 0x0;
335 buffer[26]= 0x0;
336 buffer[27]= 0x0;
337 buffer[28]= 0x0;
338 printf("changing char 25-28\n");
340 ret=datapidchk(-1, &buffer[1], size-1, 4, &errmsg);
341 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
342 size-1, ret, errmsg);
344 printf("------------------------------------------\n");
346 datapidgen(getpid(), buffer, size, 5);
348 /*******
349 fwrite(buffer, size, 1, stdout);
350 fwrite("\n", 1, 1, stdout);
351 ******/
353 printf("\ndatapidgen(getpid(), buffer, size, 5)\n");
355 ret=datapidchk(getpid(), buffer, size, 5, &errmsg);
356 printf("datapidchk(getpid(), buffer, %d, 5, &errmsg) returned %d %s\n",
357 size, ret, errmsg);
359 ret=datapidchk(getpid(), &buffer[1], size-1, 6, &errmsg);
360 printf("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
361 size-1, ret, errmsg);
363 buffer[25]= 0x0;
364 printf("changing char 25\n");
366 ret=datapidchk(getpid(), &buffer[1], size-1, 6, &errmsg);
367 printf("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
368 size-1, ret, errmsg);
370 exit(0);
373 #endif