const char* ocdtmodestring(OCDT mode,int compact) { static char result[1+(NMODES*(MAXMODENAME+1))]; /* hack to avoid malloc */ int i; char* p = result; result[0] = '\0'; if(mode == 0) { if(compact) *p++ = '-'; else if(!occoncat(result,sizeof(result),1,"NONE")) return NULL; } else for(i=0;;i++) { char* ms = modestrings[i]; if(ms == NULL) break; if(!compact && i > 0) if(!occoncat(result,sizeof(result),1,",")) return NULL; if(fisset(mode,(1<<i))) { if(compact) *p++ = ms[0]; else if(!occoncat(result,sizeof(result),1,ms)) return NULL; } } /* pad compact list out to NMODES in length (+1 for null terminator) */ if(compact) { while((p-result) < NMODES) *p++ = ' '; *p = '\0'; } return result; }
int ocmktmp(const char* base, char** tmpnamep, int* fdp) { int fd; char* tmpname = NULL; mode_t oldmask; size_t tmpsize = strlen(base)+strlen("XXXXXX") + 1; tmpname = (char*)malloc(tmpsize); if(tmpname == NULL) return OC_ENOMEM; if(!occopycat(tmpname,tmpsize,1,base)) { free(tmpname); return OC_EOVERRUN; } #ifdef HAVE_MKSTEMP if(!occoncat(tmpname,tmpsize,1,"XXXXXX")) { free(tmpname); return OC_EOVERRUN; } /* Note Potential problem: old versions of this function leave the file in mode 0666 instead of 0600 */ oldmask= umask(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); fd = mkstemp(tmpname); umask(oldmask); /* restore */ #else /* !HAVE_MKSTEMP */ /* Need to simulate by using some kind of pseudo-random number */ { int rno = rand(); char spid[7]; if(rno < 0) rno = -rno; snprintf(spid,sizeof(spid),"%06d",rno); if(!occoncat(tmpname,tmpsize,1,spid)) { free(tmpname); return OC_EOVERRUN; } #if defined(_WIN32) || defined(_WIN64) fd=open(tmpname,O_RDWR|O_BINARY|O_CREAT|O_EXCL|FILE_ATTRIBUTE_TEMPORARY, _S_IREAD|_S_IWRITE); # else fd=open(tmpname,O_RDWR|O_CREAT|O_EXCL, S_IRWXU); # endif } #endif /* !HAVE_MKSTEMP */ if(fd < 0) { if(tmpname != NULL) free(tmpname); return OC_EOPEN; } if(tmpnamep) *tmpnamep = tmpname; else if(tmpname != NULL) {free(tmpname);} if(fdp) *fdp = fd; else if(fd >= 0) close(fd); return OC_NOERR; }
int createtempfile1(char* tmppath, char** tmpnamep) { int fd = 0; char* tmpname = NULL; size_t tmpsize = strlen(tmppath)+strlen("dataddsXXXXXX") + 1; tmpname = (char*)malloc(tmpsize); if(tmpname == NULL) return -1; if(!occopycat(tmpname,tmpsize,1,tmppath)) return OC_EOVERRUN; #ifdef HAVE_MKSTEMP if(!occoncat(tmpname,tmpsize,1,"dataddsXXXXXX")) return OC_EOVERRUN; /* Note Potential problem: old versions of this function leave the file in mode 0666 instead of 0600 */ umask(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); fd = mkstemp(tmpname); #else /* !HAVE_MKSTEMP */ /* Need to simulate by using some kind of pseudo-random number */ if(!occoncat(tmpname,tmpsize,1,"datadds")) return OC_EOVERRUN; { int rno = rand(); char spid[7]; if(rno < 0) rno = -rno; snprintf(spid,sizeof(spid),"%06d",rno); if(!occoncat(tmpname,tmpsize,1,spid)) return OC_EOVERRUN; # ifdef _WIN32 fd=open(tmpname,O_RDWR|O_BINARY|O_CREAT|O_EXCL|FILE_ATTRIBUTE_TEMPORARY, _S_IREAD|_S_IWRITE); # else fd=open(tmpname,O_RDWR|O_CREAT|O_EXCL, S_IRWXU); # endif } #endif /* !HAVE_MKSTEMP */ if(tmpname == NULL) return -1; if(tmpnamep) *tmpnamep = tmpname; else free(tmpname); return fd; }
static void dumpfield(int index, char* n8, int isxdr) { char line[1024]; char tmp[32]; union { unsigned int uv; int sv; char cv[4]; float fv; } form; union { char cv[8]; unsigned long long ll; double d; } dform; line[0] = '\0'; /* offset */ sprintf(tmp,"%6d",index); addfield(tmp,line,5); memcpy(form.cv,n8,4); /* straight hex*/ sprintf(tmp,"%08x",form.uv); addfield(tmp,line,8); if(isxdr) {swapinline32(&form.uv);} /* unsigned integer */ sprintf(tmp,"%12u",form.uv); addfield(tmp,line,12); /* signed integer */ sprintf(tmp,"%12d",form.sv); addfield(tmp,line,12); /* float */ sprintf(tmp,"%#g",form.fv); addfield(tmp,line,12); /* char[4] */ { /* use raw form (i.e. n8)*/ int i; tmp[0] = '\0'; for(i=0;i<4;i++) { char stmp[64]; unsigned int c = (n8[i] & 0xff); if(c < ' ' || c > 126) snprintf(stmp,sizeof(stmp),"\\%02x",c); else snprintf(stmp,sizeof(stmp),"%c",c); if(!occoncat(tmp,sizeof(tmp),1,stmp)) return; } } addfield(tmp,line,16); /* double */ memcpy(dform.cv,n8,2*XDRUNIT); if(isxdr) xxdrntohdouble(dform.cv,&dform.d); sprintf(tmp,"%#g",dform.d); addfield(tmp,line,12); fprintf(stdout,"%s\n",line); }