void guiSave( void ) { int i = ( guiMenu ? IupGetInt ( guiMenu, "MEDIT_I" ) : guiMenu_Org ); char path[ PATH_MAX ] = {0}; char opath[ PATH_MAX ] = {0}; if ( !appMethods.OnDefPath || !appMethods.OnDefExt ) { return; } copystri ( path, ipGetUsrDir(), PATH_MAX ); appendstr ( path, DIR_SEP ".medit" DIR_SEP "data", PATH_MAX ); copystri ( opath, path, PATH_MAX ); appMethods.OnDefPath ( opath ); appMethods.OnApply(); appMethods.OnDefPath ( path ); // Rename Directory if ( _access( opath, 0 ) == 0 && istrcmp( opath, path ).i ) { rename( opath, path ); } if ( i >= guiMenu_Hacks ) { appendstr ( opath, "*.", PATH_MAX ); appendstr ( path, "*.", PATH_MAX ); } else { appendstr ( opath, ".", PATH_MAX ); appendstr ( path, ".", PATH_MAX ); } appMethods.OnDefExt ( path ); appMethods.OnDefExt ( opath ); // Rename File if ( i < guiMenu_Hacks && _access( opath, 0 ) == 0 && istrcmp( opath, path ).i ) { rename( opath, path ); } if ( i >= guiMenu_Hacks ) { if ( IupGetFile ( path ) == -1 ) { return; } } if ( _access( path, 0 ) != 0 ) { _creat( path, _S_IREAD | _S_IWRITE ); } appMethods.OnSave ( path ); }
void start(void) { int fd,x,y; char cr=0x0D, lf=0x0A; if((fd =_open("c:\\temp\\log.txt",O_WRONLY))<0) // open the file if((fd =_creat("c:\\temp\\log.txt",_A_NORMAL))<0) // try to make new write_string(1,1,"OOPS--write error1",0x8F); lseek (fd,0,SEEK_END); // jump to end of file for(y=0;y<25;y++) // grab lines 0 to 24 { for(x=0;x<80;x++) // grab rows 0 through 79 if(_write(fd,(char far *)(VIDMEM+160*y+2*x),1)==-1) write_string(1,2,"OOPS--write error2",0x8F); if(_write (fd,&cr,1)==-1) // put a cr and lf at end write_string(1,3,"OOPS--write error3",0x8F); // of line if(_write (fd,&lf,1)==-1) write_string(1,4,"OOPS--write error4",0x8F); } if(_write (fd,"---<END OF SCREEN>---",21)==-1) // show end of screen write_string(1,5,"OOPS--write error5",0x8F); if(_write (fd,&cr,1)==-1) // put a cr and lf at end write_string(1,6,"OOPS--write error6",0x8F); // of line if(_write (fd,&lf,1)==-1) write_string(1,7,"OOPS--write error7",0x8F); _close(fd); // close the file }
main() { int id,posrel; TIPO libro,v; char c; id = _creat ("LIBRO",32); close (id); id = _open ("LIBRO",O_WRONLY); c = 's'; while (c == 's') { printf ("De los datos del libro\n"); leer (&libro); grabar (id,libro); printf ("Desea grabar mas libros? (s/n)\n"); c = getche(); printf ("\n"); } close (id); id = _open ("LIBRO",O_RDONLY); while (lea_reg (id,v) != 0) printf ("%d %s %f\n",v.cod,v.N,v.precio); getch(); close (id); if ( _open ("LIBRO",O_RDWR) == -1 ) { printf ("Error en apertura\n"); exit (1); } printf ("Lea posicion relativa para el libro a modificar\n"); posrel = lea_entero(); while (posrel != 9999) { ubicar (id,posrel); /* Ubica el registro a modificar */ lea_reg (id,v); /* Lee el registro y avanza el apuntador */ modificar (&v); /* Modifica el registro */ ubicar (id,posrel); /* Reubica el apuntador */ grabar (id,v); /* Graba el registro modificado */ printf ("Lea posicion relativa para el libro a modificar\n"); posrel = lea_entero(); } close (id); id = _open ("LIBRO",O_RDONLY); while (lea_reg (id,v) != 0) printf ("%d %s %f\n",v.cod,v.N,v.precio); getch(); close (id); }
bool createFile(const char* path, bool createpath) { (void)createpath; // TODO: dir/path extraction from file path /*if (createpath && !dirExists()) { createDir(); }*/ int fd=_creat(path, _S_IREAD|_S_IWRITE); if (fd!=-1) { _close(fd); return true; } return false; }
int openfiles(char *s) { int ofl,oflg; int i; strcpy(infile,s); strcpy(listfile,s); strcpy(outfile,s); //strcpy(outfileG,s); _splitpath(s,NULL,NULL,nmspace[0],NULL); makename(infile,".fpp"); makename(listfile,".lis"); makename(outfile,".s"); if( (input = fopen(infile,"r")) == 0) { printf(" cant open %s\n",infile); return 0; } ofl = _creat(outfile,-1); if( ofl < 0 ) { printf(" cant create %s\n",outfile); fclose(input); return 0; } //oflg = _creat(outfileG,-1); //if( oflg < 0 ) // { // printf(" cant create %s\n",outfileG); // fclose(input); // return 0; // } if( (output = _fdopen(ofl,"w")) == 0) { printf(" cant open %s\n",outfile); fclose(input); return 0; } //if( (outputG = _fdopen(oflg,"w")) == 0) { // printf(" cant open %s\n",outfileG); // fclose(input); // fclose(output); // return 0; // } if( (list = fopen(listfile,"w")) == 0) { printf(" cant open %s\n",listfile); fclose(input); fclose(output); //fclose(outputG); return 0; } return 1; }
static int tmpfil() { static char namebuf[] = "/usr/tmp/plf.xxxxx"; int i; char *p,*q; i = _getpid(); p = namebuf; q = p + 13; do *q++ = (i & 07) + '0'; while (i >>= 3); *q = '\0'; if ((i = _creat(p,0644)) < 0) if ((i = _creat(p += 4,0644)) < 0) if ((i = _creat(p += 5,0644)) < 0) goto error; if (_close(i) != 0) goto error; if ((i = _open(p,2)) < 0) goto error; if (_unlink(p) != 0) error: _trp(EREWR); return(i); }
void copy() { Window *wnd; int progr=0; int fd1,fd2; long len; int cnt1,cnt2; char buffer[0x100]; Create(&wnd,MIDDLE,MIDDLE,max(strlen(infile)+27,strlen(outfile))+4,7,p); Add(&frame,FULL,"ÉÍ»ººÈͼ",FRAME); Add(&string,MIDDLE,0," Copy ",NORMAL); Add(&varstr,MIDDLE,1,"Copying the file `%s' to",infile,NORMAL); Add(&varstr,MIDDLE,2,"the file `%s'",outfile,NORMAL); Add(&varint,RIGHT-2,UH-2,"%3d%%",&progr,NORMAL); Add(&progress,2,UH-3,UW-4,&progr,NORMAL); Display(wnd); fd1=_open(infile,O_RDONLY); if(fd1==-1) Error(infile); fd2=_creat(outfile,0); len=filelength(fd1); if(len==0) { progr=100; Signal(&progr); _close(fd1); _close(fd2); return; } do { cnt1=_read(fd1,buffer,sizeof(buffer)); if(cnt1==-1) Error(infile); if(cnt1==0) break; cnt2=_write(fd2,buffer,cnt1); if(cnt2<cnt1) Error(outfile); progr=(int)((tell(fd1)*100+len/2)/len); Signal(&progr); } while(TRUE); Destroy(wnd); }
APosixStatus path_empty_file( const char* path ) { #ifdef _WIN32 int fd = _creat( path, S_IWRITE ); #else /* on Unix, only allow the owner to read/write, since the file * * may contain some personal data we don't want to see exposed */ int fd = creat(path, S_IRUSR | S_IWUSR); #endif if (fd >= 0) { close(fd); return 0; } return -1; }
////////// // // Called to open the file using custom share settings, or explicitly shared, // or explicitly exclusive. // // tnType -- // ////// s32 iDisk_open(cs8* tcPathname, s32 tnType, s32 tnShare, bool tlCreateIfCannotOpen) { s32 lnFh; ////////// // Attempt to open if valid ////// if (tcPathname) { // Try to open existing lnFh = _sopen(tcPathname, tnType, tnShare); if (lnFh == -1) { // Error opening switch (errno) { default: case EACCES: // The path is a directory, or the file is read-only, but an "open-for-write" was specified case EEXIST: // _O_CREAT and _O_EXCL flags were specified, but filename already exists case EINVAL: // Invalid type or share argument case EMFILE: // No more file descriptors are available // All valid errors, indicate failure break; case ENOENT: // File or path was not found if (tlCreateIfCannotOpen) { // Try to create lnFh = _creat(tcPathname, _S_IREAD | _S_IWRITE); // Right now, it's either open or not } break; } } // Indicate our (potentially new) status return(lnFh); } ////////// // If we get here, could not open (or possibly create) ////// return(-1); }
void main() { int id; char *p; if ( (id = _creat ("prueba",FA_ARCH) ) == -1) { p = _strerror ("Error _creat()"); PRS (p); getch(); exit(1); } if ( _close (id) == -1) { p = strerror (errno); PRS (p); getch(); exit(1); } getch(); }
int getplid (char *nam) { int fd7, high = 999, no; char *p, *p2; char name[80]; if (havepid != -1) return (havepid); /* already did it */ lflush (); /* flush any pending I/O */ sprintf (name, "%s\n", nam); /* append a \n to name */ if (lopen (playerids) < 0) /* no file, make it */ { if ((fd7 = _creat (playerids, _S_IWRITE)) < 0) return (-1); /* can't make it */ _close (fd7); goto addone; /* now append new playerid record to file */ } for (;;) /* now search for the name in the player id file */ { p = lgetl (); if (p == NULL) break; /* EOF? */ no = atoi (p); /* the id # */ p2 = lgetl (); if (p2 == NULL) break; /* EOF? */ if (no > high) high = no; /* accumulate highest id # */ if (strcmp (p2, name) == 0) /* we found him */ { return (no); /* his id number */ } } lrclose (); /* if we get here, we didn't find him in the file -- put him there */ addone: if (lappend (playerids) < 0) return (-1); /* can't open file for append */ lprintf ("%d\n%s", (int) ++high, name); /* new id # and name */ lwclose (); lcreat ((char *) 0); /* re-open terminal channel */ return (high); }
void main() { struct X dato; int i,id; time_t t1,t2; double d; void error(); for (i = 1; i < 80; i++) { dato.numero [i] = 56723L; dato.x [i] = 3.4567; } id = _creat ("a:prueba",FA_ARCH); if (id == -1) error(); _close (id); id = _open ("a:prueba",O_WRONLY); if (id == -1) error(); strcpy (dato.a,"prueba"); for (i = 1; i < 100; i++) if (_write(id, (char *)&dato,LON) != LON) error(); close (id); if (id == -1) error(); id = _open ("a:prueba",O_RDWR); if (id == -1) error(); i = time (&t1); /* Hora antes del lseek() */ if (lseek (id, (long)60 * LON,SEEK_SET) == -1) error(); if (_read (id,(char *)&dato,LON) != LON) error(); dato.numero [0] = 0; if (lseek (id, (long)60 * LON,SEEK_SET) == -1) error(); if (_write (id,(char *)&dato,LON) != LON) error(); i = time (&t2); /* Hora despues del lseek */ d = difftime (t2,t1); printf ("Tiempo gastado por la actualizacion = %10.7lf segundos\n",d); _close (id); getch(); }
void guiLoad ( uchar useDlg ) { // FIXME: i = 0 when attempt to load hacks int i = ( guiMenu ? IupGetInt ( guiMenu, "MEDIT_I" ) : guiMenu_Org ); char path[ PATH_MAX ] = {0}; if ( !appMethods.OnDefPath || !appMethods.OnDefExt ) { return; } copystri ( path, ipGetUsrDir(), PATH_MAX ); appendstr ( path, DIR_SEP ".medit" DIR_SEP "data", PATH_MAX ); appMethods.OnDefPath ( path ); if ( useDlg || i >= guiMenu_Hacks ) { appendstr ( path, "*.", PATH_MAX ); } else { appendstr ( path, ".", PATH_MAX ); } appMethods.OnDefExt ( path ); if ( useDlg || i >= guiMenu_Hacks ) { if ( IupGetFile ( path ) == -1 ) { return; } } if ( _access( path, 0 ) != 0 ) { _creat( path, _S_IREAD | _S_IWRITE ); } appMethods.OnLoad ( path ); appMethods.OnReset(); }
static int WriteDATFile (char *filename, struct IniParserStruct *ParsedData) { char buffer[6]; int handle, result; if ((handle = _creat (filename, FA_ARCH)) == -1) return FALSE; buffer[0] = '!'; /* My initials IL. */ buffer[1] = 'œ'; buffer[2] = ITEMCOUNT; /* 12 items. */ /* Mode: 256 posibilities. */ buffer[3] = (char) ParsedData->mode; /* The items. */ buffer[4] = (ParsedData->UseEMS) ? 1 : 0; buffer[4] = buffer[4] + ((ParsedData->UseXMS) ? 2 : 0); buffer[4] = buffer[4] + ((ParsedData->UseSWAP) ? 4 : 0); buffer[4] = buffer[4] + ((ParsedData->audible) ? 8 : 0); buffer[4] = buffer[4] + ((ParsedData->verify) ? 16 : 0); buffer[4] = buffer[4] + ((ParsedData->informative) ? 32 : 0); buffer[4] = buffer[4] + ((ParsedData->overwrite) ? 64 : 0); buffer[4] = buffer[4] + ((ParsedData->autoexit) ? 128 : 0); buffer[5] = (ParsedData->askdisk) ? 1 : 0; buffer[5] = buffer[5] + ((ParsedData->speed == FULL) ? 2 : 0); buffer[5] = buffer[5] + ((ParsedData->asktdisk) ? 4 : 0); buffer[5] = buffer[5] + ((ParsedData->serialnumber == UPDATE) ? 8 : 0); result = write (handle, buffer, 6) != -1; close (handle); if (!result) remove (filename); /* Make sure that there is no problem with synchronism. */ return result; }
long File::createWithCase( char* fName ) { gosASSERT( !isOpen() ); //------------------------------------------------------------- long fNameLength = strlen(fName); fileName = (char *)systemHeap->Malloc(fNameLength+1); gosASSERT(fileName != NULL); strncpy(fileName,fName,fNameLength+1); fileMode = CREATE; _fmode = _O_BINARY; handle = _creat(fileName,_S_IWRITE); if (handle == INVALID_HANDLE_VALUE) { lastError = errno; return lastError; } return 0; }
main() { void lea_datos(A *); void error(void); A b,c; int id; if((id = _creat("Objeto",FA_ARCH)) == -1) error(); if(_close (id) == -1) error(); if((id = _open ("Objeto",O_WRONLY)) == -1) error(); lea_datos(&b); while (b.F() != 999) { _write (id, (char *)&b,sizeof (A) ); lea_datos(&b); } _close (id); // Procesos Adicionales if((id = _open ("Objeto",O_RDONLY)) == -1) error(); while ( !eof(id) ) { _read (id, (char *)&c,sizeof (A) ); cout << c.F() << endl; } if(_close (id) == -1) error(); }
int main(int argc, char* argv[]) { int h_bmp1, h_bmp2, h_bmp3; errno_t err_file1 = _sopen_s(&h_bmp1,"man.bmp", _O_RDONLY, _SH_DENYNO,_S_IREAD | _S_IWRITE); errno_t err_file2 = _sopen_s(&h_bmp2,"background.bmp", _O_RDONLY, _SH_DENYNO,_S_IREAD | _S_IWRITE); h_bmp3 = _creat( "res.bmp", _S_IREAD | _S_IWRITE ); if( err_file1||err_file2||(h_bmp3==-1)) exit( 1 ); unsigned char * buf_bmp1, * buf_bmp2, * man, * background; unsigned long len_bmp1, len_bmp2; len_bmp1 = _filelength(h_bmp1); len_bmp2 = _filelength(h_bmp2); man = buf_bmp1 = new unsigned char[len_bmp1]; background = buf_bmp2 = new unsigned char[len_bmp2]; buf_bmp1=background;// _read(h_bmp1, man, len_bmp1); _read(h_bmp2, background, len_bmp2); BITMAPFILEHEADER bmp_fh1, bmp_fh2; BITMAPINFO bmp_inf1, bmp_inf2; bmp_fh1 = *((BITMAPFILEHEADER*)man); bmp_fh2 = *((BITMAPFILEHEADER*)background); man = man + sizeof(BITMAPFILEHEADER); background = background + sizeof(BITMAPFILEHEADER); bmp_inf1 = *((BITMAPINFO*)man); bmp_inf2 = *((BITMAPINFO*)background); man = man + sizeof(BITMAPINFO); background = background + sizeof(BITMAPINFO); mmx(man,background, bmp_inf1.bmiHeader.biSizeImage); _write(h_bmp3,buf_bmp1/*man*/, len_bmp1); _close(h_bmp1); _close(h_bmp2); _close(h_bmp3); return 0; }
/** * Interrupt 80h. Handles the system calls. * * @param regs Pointer to struct containing micro's registers. */ void int80(registers* regs) { switch (regs->eax) { case _SYS_READ: regs->eax = _read((unsigned int)regs->ebx, (char*)translate(regs->ecx), (size_t)regs->edx); break; case _SYS_WRITE: regs->eax = _write((unsigned int)regs->ebx, (const char*)translate(regs->ecx), (size_t)regs->edx); break; case _SYS_TIME: regs->eax = _time((time_t*)translate(regs->ebx)); break; case _SYS_IOCTL: regs->eax = _ioctl(regs->ebx, regs->ecx, (void*)translate(regs->edx)); break; case _SYS_TICKS: regs->eax = _getTicksSinceStart(); break; case _SYS_YIELD: // This just makes sure we call the scheduler again, for now break; case _SYS_EXIT: _exit(); break; case _SYS_GETPID: regs->eax = _getpid(); break; case _SYS_GETPPID: regs->eax = _getppid(); break; case _SYS_RUN: regs->eax = _run((EntryPoint) translate(regs->ebx), (char*) translate(regs->ecx), regs->edx); break; case _SYS_WAIT: regs->eax = _wait(); break; case _SYS_KILL: _kill((pid_t) regs->ebx); break; case _SYS_PINFO: regs->eax = _pinfo((struct ProcessInfo*)translate(regs->ebx), (size_t)regs->ecx); break; case _SYS_SLEEP: _sleep(regs->ebx); break; case _SYS_NICE: regs->eax = _nice(regs->ebx); break; case _SYS_RENICE: regs->eax = _renice(regs->ebx, regs->ecx); break; case _SYS_CLOSE: regs->eax = _close(regs->ebx); break; case _SYS_OPEN: regs->eax = _open((char*)translate(regs->ebx), regs->ecx, regs->edx); break; case _SYS_CREAT: regs->eax = _creat((char*)translate(regs->ebx), regs->ecx); break; case _SYS_MKDIR: regs->eax = _mkdir((const char*)translate(regs->ebx), regs->ecx); break; case _SYS_RMDIR: regs->eax = _rmdir((const char*)translate(regs->ebx)); break; case _SYS_UNLINK: regs->eax = _unlink((const char*)translate(regs->ebx)); break; case _SYS_RENAME: regs->eax = _rename((const char*)translate(regs->ebx), (const char*)translate(regs->ecx)); break; case _SYS_CHDIR: regs->eax = _chdir((const char*)translate(regs->ebx)); break; case _SYS_GETCWD: regs->eax = _getcwd((char*)translate(regs->ebx), (size_t)regs->ecx); break; case _SYS_READDIR: regs->eax = _readdir(regs->ebx, (struct fs_DirectoryEntry*)translate(regs->ecx), regs->edx); break; case _SYS_SETPPERSONA: _setProcessPersona(regs->ebx, regs->ecx, regs->edx); break; case _SYS_GETPPERSONA: _getProcessPersona(regs->ebx, (int*)translate(regs->ecx), (int*) translate(regs->edx)); break; case _SYS_SYMLINK: regs->eax = _symlink((const char *)translate(regs->ebx), (const char *)translate(regs->ecx)); break; case _SYS_MKFIFO: regs->eax = _mkfifo((const char*)translate(regs->ebx)); break; case _SYS_CHMOD: regs->eax = _chmod(regs->ebx, (const char*)translate(regs->ecx)); break; case _SYS_STAT: regs->eax = _stat((const char*)translate(regs->ebx), (struct stat*)translate(regs->ecx)); break; case _SYS_CHOWN: regs->eax = _chown((const char*)translate(regs->ebx)); break; case _SYS_LOG: _loglevel(regs->ebx); break; case _SYS_STACKSIZE: regs->eax = _stacksize(); } }
FILE * freopen(const char *name, const char *mode, FILE *stream) { register int i; struct stat st; int rwmode = 0, rwflags = 0; int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF); (void) fflush(stream); /* ignore errors */ (void) _close(fileno(stream)); switch(*mode++) { case 'r': flags |= _IOREAD; rwmode = O_RDONLY; break; case 'w': flags |= _IOWRITE; rwmode = O_WRONLY; rwflags = O_CREAT | O_TRUNC; break; case 'a': flags |= _IOWRITE | _IOAPPEND; rwmode = O_WRONLY; rwflags |= O_APPEND | O_CREAT; break; default: goto loser; } while (*mode) { switch(*mode++) { case 'b': continue; case '+': rwmode = O_RDWR; flags |= _IOREAD | _IOWRITE; continue; /* The sequence may be followed by aditional characters */ default: break; } break; } if ((rwflags & O_TRUNC) || (((fd = _open(name, rwmode)) < 0) && (rwflags & O_CREAT))) { if (((fd = _creat(name, PMODE)) < 0) && flags | _IOREAD) { (void) _close(fd); fd = _open(name, rwmode); } } if (fd < 0) { goto loser; } if ( fstat( fd, &st ) == 0 ) { if ( S_ISFIFO(st.st_mode) ) flags |= _IOFIFO; } else { goto loser; } stream->_count = 0; stream->_fd = fd; stream->_flags = flags; return stream; loser: for( i = 0; i < FOPEN_MAX; i++) { if (stream == __iotab[i]) { __iotab[i] = 0; break; } } if (stream != stdin && stream != stdout && stream != stderr) free((void *)stream); return (FILE *)NULL; }
FILE * fopen(const char *name, const char *mode) { register int i; int rwmode = 0, rwflags = 0; FILE *stream; struct stat st; int fd, flags = 0; for (i = 0; __iotab[i] != 0 ; i++) if ( i >= FOPEN_MAX-1 ) return (FILE *)NULL; switch(*mode++) { case 'r': flags |= _IOREAD | _IOREADING; rwmode = O_RDONLY; break; case 'w': flags |= _IOWRITE | _IOWRITING; rwmode = O_WRONLY; rwflags = O_CREAT | O_TRUNC; break; case 'a': flags |= _IOWRITE | _IOWRITING | _IOAPPEND; rwmode = O_WRONLY; rwflags |= O_APPEND | O_CREAT; break; default: return (FILE *)NULL; } while (*mode) { switch(*mode++) { case 'b': continue; case '+': rwmode = O_RDWR; flags |= _IOREAD | _IOWRITE; continue; /* The sequence may be followed by additional characters */ default: break; } break; } /* Perform a creat() when the file should be truncated or when * the file is opened for writing and the open() failed. */ if ((rwflags & O_TRUNC) || (((fd = _open(name, rwmode)) < 0) && (rwflags & O_CREAT))) { if (((fd = _creat(name, PMODE)) > 0) && flags | _IOREAD) { (void) _close(fd); fd = _open(name, rwmode); } } if (fd < 0) return (FILE *)NULL; if ( fstat( fd, &st ) < 0 ) { _close(fd); return (FILE *)NULL; } if ( st.st_mode & S_IFIFO ) flags |= _IOFIFO; if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) { _close(fd); return (FILE *)NULL; } if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE)) flags &= ~(_IOREADING | _IOWRITING); stream->_count = 0; stream->_fd = fd; stream->_flags = flags; stream->_buf = NULL; __iotab[i] = stream; return stream; }
int _zip_mkstemp(char *path) { #ifdef _WIN32 int ret; ret = _creat(_mktemp(path), _S_IREAD|_S_IWRITE); if (ret == -1) { return 0; } else { return ret; } #else int fd; char *start, *trv; struct stat sbuf; pid_t pid; /* To guarantee multiple calls generate unique names even if the file is not created. 676 different possibilities with 7 or more X's, 26 with 6 or less. */ static char xtra[2] = "aa"; int xcnt = 0; pid = getpid(); /* Move to end of path and count trailing X's. */ for (trv = path; *trv; ++trv) if (*trv == 'X') xcnt++; else xcnt = 0; /* Use at least one from xtra. Use 2 if more than 6 X's. */ if (*(trv - 1) == 'X') *--trv = xtra[0]; if (xcnt > 6 && *(trv - 1) == 'X') *--trv = xtra[1]; /* Set remaining X's to pid digits with 0's to the left. */ while (*--trv == 'X') { *trv = (pid % 10) + '0'; pid /= 10; } /* update xtra for next call. */ if (xtra[0] != 'z') xtra[0]++; else { xtra[0] = 'a'; if (xtra[1] != 'z') xtra[1]++; else xtra[1] = 'a'; } /* * check the target directory; if you have six X's and it * doesn't exist this runs for a *very* long time. */ for (start = trv + 1;; --trv) { if (trv <= path) break; if (*trv == '/') { *trv = '\0'; if (stat(path, &sbuf)) return (0); if (!S_ISDIR(sbuf.st_mode)) { errno = ENOTDIR; return (0); } *trv = '/'; break; } } for (;;) { if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0) return (fd); if (errno != EEXIST) return (0); /* tricky little algorithm for backward compatibility */ for (trv = start;;) { if (!*trv) return (0); if (*trv == 'z') *trv++ = 'a'; else { if (isdigit((unsigned char)*trv)) *trv = 'a'; else ++*trv; break; } } } /*NOTREACHED*/ #endif }
//--------------------------------------------------------------------------- long File::open (const char* fName, FileMode _mode, long numChild) { gosASSERT( !isOpen() ); //------------------------------------------------------------- long fNameLength = strlen(fName); fileName = (char *)systemHeap->Malloc(fNameLength+1); gosASSERT(fileName != NULL); strncpy(fileName,fName,fNameLength+1); fileMode = _mode; _fmode = _O_BINARY; _strlwr(fileName); if (fileMode == CREATE) { handle = _creat(fileName,_S_IWRITE); if (handle == INVALID_HANDLE_VALUE) { lastError = errno; return lastError; } } else { //---------------------------------------------------------------- //-- First, see if file is in normal place. Useful for patches!! handle = _open(fileName,_O_RDONLY); //------------------------------------------ //-- Next, see if file is in fastFile. if (handle == INVALID_HANDLE_VALUE) { lastError = errno; fastFile = FastFileFind(fileName,fastFileHandle); if (!fastFile) { //Not in main installed directory and not in fastfile. Look on CD. char actualPath[2048]; strcpy(actualPath,CDInstallPath); strcat(actualPath,fileName); handle = _open(actualPath,_O_RDONLY); if (handle == INVALID_HANDLE_VALUE) { bool openFailed = false; bool alreadyFullScreen = (Environment.fullScreen != 0); while (handle == INVALID_HANDLE_VALUE) { openFailed = true; //OK, check to see if the CD is actually present. // Do this by checking for tgl.fst on the CD Path. // If its there, the CD is present BUT the file is missing. // MANY files in MechCommander 2 are LEGALLY missing! // Tell it to the art staff. char testCDPath[2048]; strcpy(testCDPath,CDInstallPath); strcat(testCDPath,"tgl.fst"); DWORD findCD = fileExists(testCDPath); if (findCD == 1) //File exists. CD is in drive. Return 2 to indicate file not found. return 2; EnterWindowMode(); char data[2048]; sprintf(data,FileMissingString,fileName,CDMissingString); DWORD result1 = MessageBox(NULL,data,MissingTitleString,MB_OKCANCEL | MB_ICONWARNING); if (result1 == IDCANCEL) { ExitGameOS(); return (2); //File not found. Never returns though! } handle = _open(actualPath,_O_RDONLY); } if (openFailed && (Environment.fullScreen == 0) && alreadyFullScreen) EnterFullScreenMode(); } else { if (logFileTraffic && (handle != INVALID_HANDLE_VALUE)) { if (!fileTrafficLog) { createTrafficLog(); } char msg[300]; sprintf(msg,"CFHandle Length: %010d File: %s",fileSize(),fileName); fileTrafficLog->writeLine(msg); } setup(); //------------------------------------------------------------ // NEW FUNCTIONALITY!!! // // Each file may have a number of files open as children which // use the parent's handle for reads and writes. This would // allow us to open a packet file and read a packet as a fitIni // or allow us to write a packet as a fit ini and so forth. // // It also allows us to use the packet file extensions as tree // files to avoid the ten thousand file syndrome. // // There is now an open which takes a FilePtr and a size. maxChildren = numChild; childList = (FilePtr *)systemHeap->Malloc(sizeof(FilePtr) * maxChildren); if (!childList) { return(NO_RAM_FOR_CHILD_LIST); } numChildren = 0; for (long i=0;i<(long)maxChildren;i++) { childList[i] = NULL; } return (NO_ERR); } } if (logFileTraffic) { if (!fileTrafficLog) { createTrafficLog(); } char msg[300]; sprintf(msg,"FASTF Length: %010d File: %s",fileSize(),fileName); fileTrafficLog->writeLine(msg); } //--------------------------------------------------------------------- //-- FastFiles are all compressed. Must read in entire chunk into RAM //-- Then close fastfile!!!!! inRAM = TRUE; fileImage = (unsigned char *)malloc(fileSize()); if (fileImage) { fastFile->readFast(fastFileHandle,fileImage,fileSize()); physicalLength = getLength(); //------------------------------------ //-- Image is in RAM. Shut the file. //fastFile->closeFast(fastFileHandle); //fastFile = NULL; //fastFileHandle = -1; logicalPosition = 0; } return NO_ERR; } else { if (logFileTraffic && (handle != INVALID_HANDLE_VALUE)) { if (!fileTrafficLog) { createTrafficLog(); } char msg[300]; sprintf(msg,"CFHandle Length: %010d File: %s",fileSize(),fileName); fileTrafficLog->writeLine(msg); } setup(); //------------------------------------------------------------ // NEW FUNCTIONALITY!!! // // Each file may have a number of files open as children which // use the parent's handle for reads and writes. This would // allow us to open a packet file and read a packet as a fitIni // or allow us to write a packet as a fit ini and so forth. // // It also allows us to use the packet file extensions as tree // files to avoid the ten thousand file syndrome. // // There is now an open which takes a FilePtr and a size. maxChildren = numChild; childList = (FilePtr *)systemHeap->Malloc(sizeof(FilePtr) * maxChildren); if (!childList) { return(NO_RAM_FOR_CHILD_LIST); } numChildren = 0; for (long i=0;i<(long)maxChildren;i++) { childList[i] = NULL; } return (NO_ERR); } } return(NO_ERR); }
void main(int argc, char *argv[]) { FILE *fp; char path[MAXPATH], pattern[MAXLINE], line[MAXLINE], out[MAXLINE]; char lineno[20], filename[MAXPATH], outfile[MAXPATH]; char fname[MAXPATH], fext[MAXPATH]; char * outExt; int linenum = 0; int fh; // Verify correct number of args if (argc != 3) { printf("Usage: find <pattern> <file>\n"); } else { /* Copy args */ strcpy(pattern, *++argv); strcpy(path, *++argv); /* Open File */ if ((fp = fopen(path, "r")) == NULL) { printf("Error - Could not open file: %s", filename); } else { /* Create a BoyerMoore search object based on pattern */ BoyerMoore myPattern(pattern); /* Split out the filename */ _splitpath(path, NULL, NULL, fname, fext); sprintf(filename, "%s%s", fname, fext); /* Open an output file - check the OutputExt environment variable for a file extension, default to ".match" if not set */ #pragma warning (suppress: 6412 6054) memcpy(outfile, filename, strlen(filename) + 1); if ((outExt = getenv("OutputExt")) != NULL) { strcat(outfile, outExt); } else { /* This is trouble. It's an easy mistake to make. It should be: strncat(outfile, ".match", MAXPATH - strlen(outfile) - 1) */ strncat(outfile, ".match", 6); } if ((fh = _creat(outfile, _S_IWRITE)) == -1) { printf("Could not open output file: %s\n", outfile); } else { sprintf(out, "*** File: %s\n", path); #pragma warning (suppress: 6054) _write(fh, out, (int) strlen(out)); /* Read a line of input and search it for <pattern> using Boyer-Moore Print the line if a match is found */ while (!feof(fp)) { if (fgets(line, MAXLINE, fp) != NULL) { ++linenum; if ( myPattern.Match(line) != NULL) { itoa(linenum, lineno, 10); sprintf(out, "%s [%s]: %s", filename, lineno, line); _write(fh, out, (int) strlen(out)); } } else { printf("Error reading file\n"); } } printf("Done - output in: %s\n", outfile); _close(fh); fclose(fp); } } } }