18
extern void free(void *ptr);
20
#include "msgtypesc.h"
25
* Process node0 has a file (assumed unopened) named fname.
26
* This file will be copied to all other processes which must
27
* simultaneously invoke pfilecopy. Since the processes may be
28
* using the same directory one probably ought to make sure
29
* that each process uses a different name in the call.
33
* on node 0 pfilecopy(99, 0, 'argosin')
34
* on node 1 pfilecopy(99, 0, 'argosin_001')
35
* on node 2 pfilecopy(99, 0, 'argosin_002')
37
void tcgi_pfilecopy(Integer *type, Integer *node0, char *filename)
41
Integer length, nread=32768, len_nread=sizeof(Integer);
42
Integer typenr = (*type & 32767) | MSGINT; /* Force user type integer */
43
Integer typebuf =(*type & 32767) | MSGCHR;
45
if (!(buffer = malloc((unsigned) nread))) {
46
Error("pfilecopy: failed to allocate the I/O buffer",nread);
49
if (*node0 == NODEID_()) {
51
/* I have the original file ... open and check its size */
53
if ((file = fopen(filename,"r")) == (FILE *) NULL) {
54
(void) fprintf(stderr, "me=" FMT_INT ", filename = %s.\n",
56
Error("pfilecopy: node0 failed to open original file", *node0);
59
/* Quick sanity check on the length */
61
(void) fseek(file, 0L, (int) 2); /* Seek to end of file */
62
length = ftell(file); /* Find the length of file */
63
(void) fseek(file, 0L, (int) 0); /* Seek to beginning of file */
64
if ( (length<0) || (length>1e12) ) {
65
Error("pfilecopy: the file length is -ve or very big", length);
68
/* Send the file in chunks of nread bytes */
71
nread = fread(buffer, 1, (int) nread, file);
72
BRDCST_(&typenr, (char *) &nread, &len_nread, node0);
75
BRDCST_(&typebuf, buffer, &nread, node0);
82
/* Open the file for the duplicate */
84
if ((file = fopen(filename,"w+")) == (FILE *) NULL) {
85
(void) fprintf(stderr,"me=" FMT_INT ", filename = %s.\n",
87
Error("pfilecopy: failed to open duplicate file", *node0);
90
/* Receive data and write to file */
93
BRDCST_(&typenr, (char *) &nread, &len_nread, node0);
96
BRDCST_(&typebuf, buffer, &nread, node0);
98
if (nread != (Integer)fwrite(buffer, 1, (int) nread, file)) {
99
Error("pfilecopy: error data to duplicate file", nread);
105
/* Tidy up the stuff we have been using */
112
/** The original C interface to PFCOPY_. */
113
void PFILECOPY_(Integer *type, Integer *node0, char *filename)
115
tcgi_pfilecopy(type, node0, filename);
118
/** Fortran wrapper around pfilecopy */
119
void PFCOPY_(Integer *type, Integer *node0, char *fname, int len)
125
(void) printf("me=%d, type=%d, node0=%d, fname=%x, fname=%.8s, len=%d\n",
126
NODEID_(), *type, *node0, fname, fname, len);
129
/* Strip trailing blanks off the file name */
131
while ((len > 0) && (fname[len-1] == ' ')) {
135
Error("pfcopy_: file name length is toast", (Integer) len);
138
/* Generate a NULL terminated string */
140
filename = malloc( (unsigned) (len+1) );
142
(void) bcopy(fname, filename, len);
143
filename[len] = '\0';
146
Error("PFCOPY_: failed to malloc space for filename", (Integer) len);
149
/* Now call the C routine to do the work */
151
tcgi_pfilecopy(type, node0, filename);
153
(void) free(filename);