📄 bacio.v1.3.c
字号:
return -666; } #ifdef VERBOSE printf("write file descriptor, datary = %d %d\n", *fdes, (int) datary); #endif count = (size_t) *no; jret = write(*fdes, (void *) datary, count); if (jret != *no) { #ifdef VERBOSE printf("did not write out the requested number of bytes\n"); printf("wrote %d bytes instead\n", jret); #endif *nactual = jret; *newpos = *start + jret; } else { #ifdef VERBOSE printf("wrote %d bytes \n", jret); #endif *nactual = jret; *newpos = *start + jret; } }/* Done with writing */ /* Close file if requested */ if (BACLOSE & *mode ) { jret = close(*fdes); if (jret != 0) { #ifdef VERBOSE printf("close failed! jret = %d\n",jret); #endif return -9; } }/* Done closing *//* Check that if we were reading or writing, that we actually got what *//* we expected, else return a -10. Return 0 (success) if we're here *//* and weren't reading or writing */ if ( (*mode & BAREAD || *mode & BAWRITE) && (*nactual != *no) ) { return -10; } else { return 0; }} #if defined _UNDERSCORE int banio_ (int * mode, int * start, int *newpos, int * size, int * no, int * nactual, int * fdes, const char *fname, char *datary, int namelen ) {#elif defined _DOUBLEUNDERSCORE int banio__ (int * mode, int * start, int *newpos, int * size, int * no, int * nactual, int * fdes, const char *fname, char *datary, int namelen ) {#else int banio (int * mode, int * start, int *newpos, int * size, int * no, int * nactual, int * fdes, const char *fname, char *datary, int namelen ) {#endif int i, j, jret, seekret; char *realname, *tempchar; int tcharval;/* Initialization(s) */ *nactual = 0;/* Check for illegal combinations of options */ if (( BAOPEN_RONLY & *mode) && ( (BAOPEN_WONLY & *mode) || (BAOPEN_WONLY_TRUNC & *mode) || (BAOPEN_WONLY_APPEND & *mode) ) ) { #ifdef VERBOSE printf("illegal -- trying to open both read only and write only\n"); #endif return -1; } if ( (BAREAD & *mode ) && (BAWRITE & *mode) ) { #ifdef VERBOSE printf("illegal -- trying to both read and write in the same call\n"); #endif return -2; }/* This section handles Fortran to C translation of strings so as to *//* be able to open the files Fortran is expecting to be opened. */ #ifdef CRAY90 namelen = _fcdlen(fcd_fname); fname = _fcdtocp(fcd_fname); #endif if ( (BAOPEN_RONLY & *mode) || (BAOPEN_WONLY & *mode) || (BAOPEN_WONLY_TRUNC & *mode) || (BAOPEN_WONLY_APPEND & *mode) || (BAOPEN_RW & *mode) ) { #ifdef VERBOSE printf("Will be opening a file %s %d\n", fname, namelen); fflush(stdout); printf("Strlen %d namelen %d\n", strlen(fname), namelen); fflush(stdout); #endif realname = (char *) malloc( namelen * sizeof(char) ) ; if (realname == NULL) { #ifdef VERBOSE printf("failed to mallocate realname %d = namelen\n", namelen); fflush(stdout); #endif return -3; } tempchar = (char *) malloc(sizeof(char) * 1 ) ; i = 0; j = 0; *tempchar = fname[i]; tcharval = *tempchar; while (i == j && i < namelen ) { fflush(stdout); if ( isgraph(tcharval) ) { realname[j] = fname[i]; j += 1; } i += 1; *tempchar = fname[i]; tcharval = *tempchar; } #ifdef VERBOSE printf("i,j = %d %d\n",i,j); fflush(stdout); #endif realname[j] = '\0'; } /* Open files with correct read/write and file permission. */ if (BAOPEN_RONLY & *mode) { #ifdef VERBOSE printf("open read only %s\n", realname); #endif *fdes = open(realname, O_RDONLY , S_IRWXU | S_IRWXG | S_IRWXO ); } else if (BAOPEN_WONLY & *mode ) { #ifdef VERBOSE printf("open write only %s\n", realname); #endif *fdes = open(realname, O_WRONLY | O_CREAT , S_IRWXU | S_IRWXG | S_IRWXO ); } else if (BAOPEN_WONLY_TRUNC & *mode ) { #ifdef VERBOSE printf("open write only with truncation %s\n", realname); #endif *fdes = open(realname, O_WRONLY | O_CREAT | O_TRUNC , S_IRWXU | S_IRWXG | S_IRWXO ); } else if (BAOPEN_WONLY_APPEND & *mode ) { #ifdef VERBOSE printf("open write only with append %s\n", realname); #endif *fdes = open(realname, O_WRONLY | O_CREAT | O_APPEND , S_IRWXU | S_IRWXG | S_IRWXO ); } else if (BAOPEN_RW & *mode) { #ifdef VERBOSE printf("open read-write %s\n", realname); #endif *fdes = open(realname, O_RDWR | O_CREAT , S_IRWXU | S_IRWXG | S_IRWXO ); } else { #ifdef VERBOSE printf("no openings\n"); #endif } if (*fdes < 0) { #ifdef VERBOSE printf("error in file descriptor! *fdes %d\n", *fdes); #endif return -4; } else { #ifdef VERBOSE printf("file descriptor = %d\n",*fdes ); #endif }/* Read data as requested */ if (BAREAD & *mode && ( (BAOPEN_WONLY & *mode) || (BAOPEN_WONLY_TRUNC & *mode) || (BAOPEN_WONLY_APPEND & *mode) ) ) { #ifdef VERBOSE printf("Error, trying to read while in write only mode!\n"); #endif return -5; } else if (BAREAD & *mode ) { /* Read in some data */ if (! (*mode & NOSEEK) ) { seekret = lseek(*fdes, *start, SEEK_SET); if (seekret == -1) { #ifdef VERBOSE printf("error in seeking to %d\n",*start); #endif return -6; } #ifdef VERBOSE else { printf("Seek successful, seek ret %d, start %d\n", seekret, *start); } #endif } jret = read(*fdes, datary, *no*(*size) ); if (jret != *no*(*size) ) { #ifdef VERBOSE printf("did not read in the requested number of items\n"); printf("read in %d items of %d \n",jret/(*size), *no); #endif *nactual = jret/(*size); *newpos = *start + jret; } #ifdef VERBOSE printf("read in %d items \n", jret/(*size)); #endif *nactual = jret/(*size); *newpos = *start + jret; }/* Done with reading */ /* See if we should be writing */ if ( BAWRITE & *mode && BAOPEN_RONLY & *mode ) { #ifdef VERBOSE printf("Trying to write on a read only file \n"); #endif return -7; } else if ( BAWRITE & *mode ) { if (! (*mode & NOSEEK) ) { seekret = lseek(*fdes, *start, SEEK_SET); if (seekret == -1) { #ifdef VERBOSE printf("error in seeking to %d\n",*start); #endif return -8; } #ifdef VERBOSE else { printf("Seek successful, seek ret %d, start %d\n", seekret, *start); } #endif } jret = write(*fdes, datary, *no*(*size)); if (jret != *no*(*size)) { #ifdef VERBOSE printf("did not write out the requested number of items\n"); printf("wrote %d items instead\n", jret/(*size) ); #endif *nactual = jret/(*size) ; *newpos = *start + jret; } else { #ifdef VERBOSE printf("wrote %d items \n", jret/(*size) ); #endif *nactual = jret/(*size) ; *newpos = *start + jret; } }/* Done with writing */ /* Close file if requested */ if (BACLOSE & *mode ) { jret = close(*fdes); if (jret != 0) { #ifdef VERBOSE printf("close failed! jret = %d\n",jret); #endif return -9; } }/* Done closing *//* Check that if we were reading or writing, that we actually got what *//* we expected, else return a -10. Return 0 (success) if we're here *//* and weren't reading or writing */ if ( (*mode & BAREAD || *mode & BAWRITE) && (*nactual != *no) ) { return -10; } else { return 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -