static File* walkfile1(File *dir, char *elem) { File *fp; Filelist *fl; rlock(&dir->RWLock); if(strcmp(elem, "..") == 0){ fp = dir->parent; incref(&fp->Ref); runlock(&dir->RWLock); closefile(dir); return fp; } fp = nil; for(fl=dir->filelist; fl; fl=fl->link) if(fl->f && strcmp(fl->f->Dir.name, elem)==0){ fp = fl->f; incref(&fp->Ref); break; } runlock(&dir->RWLock); closefile(dir); return fp; }
/** * Main execution of the program. Function calls * to load data from file, calculation of maximum flow * and to save result answer to a file */ int main(int argc, char *argv[]) { /* Corrects terminal output buffer */ setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); /* Parse main arguments */ char input[ARGS_BUFFER_SIZE], output[ARGS_BUFFER_SIZE]; arg_parser(argc, argv, input, output); #ifdef MYDEBUG printf("input: %s output: %s\n", input, output); #endif /* Import input file */ FILE *inpfile = openfile(input, READ_MODE); /* Loads data from file */ Champ champ; load(inpfile, &champ); closefile(inpfile); /* output file that will store the results */ FILE *outfile = openfile(output, WRITE_MODE); /* Calculate the maximum flow on the graph */ maximum_flow(&champ, outfile); closefile(outfile); return EXIT_SUCCESS; }
static boolean dbopenverb (hdltreenode hparam1, tyvaluerecord *vreturned) { // // 2006-06-20 creedon: for Mac, extend filespec // // 4.1b5 dmb: added ability to access already-open root in Frontier // tyodbrecord odbrec; hdlodbrecord hodb; WindowPtr w; odbrec.fref = 0; if ( ! getfilespecvalue ( hparam1, 1, &odbrec.fs ) ) return (false); flnextparamislast = true; if (!getbooleanvalue (hparam1, 2, &odbrec.flreadonly)) return (false); w = shellfindfilewindow ( &odbrec.fs ); if (w != nil) { if (odberror (odbaccesswindow (w, &odbrec.odb))) return (false); // fref remains zero, so unwanted closefiles aren't a problem } else { if ( ! openfile ( &odbrec.fs, &odbrec.fref, odbrec.flreadonly)) return (false); if (odberror (odbopenfile (odbrec.fref, &odbrec.odb, odbrec.flreadonly))) { closefile (odbrec.fref); return (false); } } if (!newfilledhandle (&odbrec, sizeof (odbrec), (Handle *) &hodb)) { odbclosefile (odbrec.odb); closefile (odbrec.fref); return (false); } listlink ((hdllinkedlist) hodblist, (hdllinkedlist) hodb); return (setbooleanvalue (true, vreturned)); } // dbopenverb
/* * Read input file names from a file (file0-from option). */ static void read_fns_from_file0(const char *fn) { if (fn) { struct file0_reader f0r; FILE *f; f = fopen(fn, "r"); if (f == NULL) err(2, NULL); memset(&f0r, 0, sizeof(f0r)); f0r.f = f; while (!feof(f)) { char *line = read_file0_line(&f0r); if (line && *line) { ++argc_from_file0; if (argc_from_file0 < 1) argc_from_file0 = 1; argv_from_file0 = sort_realloc(argv_from_file0, argc_from_file0 * sizeof(char *)); if (argv_from_file0 == NULL) err(2, NULL); argv_from_file0[argc_from_file0 - 1] = sort_strdup(line); } } closefile(f, fn); } }
void Write_Displ( int* pid, int* stepno, int* nshg, int* numVars, double* array1 ) { //TO BE UPDATED FOR SYNCIO char fname[255]; char rfile[60]; int irstou; int magic_number = 362436; int* mptr = &magic_number; time_t timenow = time ( &timenow); double version=0.0; int isize, nitems; int iarray[10]; sprintf(rfile,"restart.%d.%d",*stepno,*pid+1); openfile(rfile,"append", &irstou); isize = (*nshg)*(*numVars); nitems = 3; iarray[ 0 ] = (*nshg); iarray[ 1 ] = (*numVars); iarray[ 2 ] = (*stepno); writeheader( &irstou, "displacement", (void*)iarray, &nitems, &isize, "double", phasta_iotype ); writedatablock( &irstou, "displacement", (void*)(array1), &isize, "double", phasta_iotype ); closefile( &irstou, "append" ); }
int main(int argc,char** argv) { int rv = 0; FILE *f = NULL; Initialize(); if ( argc > 1) { if( strcmp(argv[1],"--help")==0 || strcmp(argv[1],"-H")==0 ) { printf("Use: %s [rtf_filename]\n",argv[0]); rv = 0; } else if ( strcmp(argv[1],"--version")==0 || strcmp(argv[1],"-V")==0 ) { printf("rtf2html version 1.2\n"); rv = 0; } else { rv = openfile(argv[1], &f); if ( rv ) rv = RTF_FindCharset(f); if ( rv ) { rewind(f); rv = RTF_Parse(f); } if ( rv ) rv = closefile(f); } } else { printf("Use: %s [rtf_filename]\n",argv[0]); } return rv; }
/* * Loads url and starts playback. * On success returns 0, on failure -1. */ int player_play(const char *url) { if (state != STOPPED) player_stop(); if (openfile(url) < 0) return -1; if (openaudio() < 0) { closefile(); return -1; } flinit(); /* initialize the framelist */ /* * Create two threads: one for reading and decoding the url, * the other one for cleaning up after the track has finished or * after an abort signal. */ pthread_create(&decodetid, NULL, decodethread, NULL); pthread_create(&watchtid, NULL, watchthread, NULL); pthread_detach(watchtid); SDL_PauseAudio(0); /* start playback (1 = Pause, 0 = Play) */ state = PLAYING; return 0; }
static void io_writeto() { lua_Object f = lua_getparam(FIRSTARG); if (f == LUA_NOOBJECT) { closefile(FOUTPUT); setreturn(2, FOUTPUT); } else if (lua_tag(f) == gettag(IOTAG)) { int32 id = lua_getuserdata(f); LuaFile *current = getfile(id); if (!current->isOpen()) { pushresult(0); return; } setreturn(id, FOUTPUT); } else { Common::String s = Common::lastPathComponent(luaL_check_string(FIRSTARG), '\\'); LuaFile *current; Common::WriteStream *outFile = NULL; Common::SaveFileManager *saveFileMan = g_system->getSavefileManager(); outFile = saveFileMan->openForSaving(s); if (!outFile) { pushresult(0); return; } current = new LuaFile(); current->_out = outFile; current->_filename = s; setreturn(addfile(current), FOUTPUT); } }
static void fatal(char *string, char *arg) { char *fls; int num; (void) fprintf(stderr, "csplit: "); /* gettext dynamically replaces string */ (void) fprintf(stderr, gettext(string), arg); if (!keep) { if (outfile) { (void) fclose(outfile); for (fls = file; *fls != '\0'; fls++) continue; fls -= fiwidth; for (num = atoi(fls); num >= 0; num--) { (void) sprintf(fls, "%.*d", fiwidth, num); (void) unlink(file); } } } else if (outfile) closefile(); exit(1); }
static void io_readfrom() { lua_Object f = lua_getparam(FIRSTARG); if (f == LUA_NOOBJECT) { closefile(FINPUT); setreturn(1, FINPUT); } else if (lua_tag(f) == gettag(IOTAG)) { int32 id = lua_getuserdata(f); LuaFile *current = getfile(id); if (!current) { pushresult(0); return; } setreturn(id, FINPUT); } else { const char *s = luaL_check_string(FIRSTARG); LuaFile *current; Common::SeekableReadStream *inFile = NULL; Common::SaveFileManager *saveFileMan = g_system->getSavefileManager(); inFile = saveFileMan->openForLoading(s); if (!inFile) current = g_resourceloader->openNewStreamLuaFile(s); else { current = new LuaFile(); current->_in = inFile; current->_filename = s; } if (!current) { delete current; pushresult(0); } else { setreturn(addfile(current), FINPUT); } } }
static void fscreate(Req* r) { File* file; Query* q; char* name; char* uid; int mode; File* f; file = r->fid->file; name = r->ifcall.name; uid = r->fid->uid; mode = r->fid->file->dir.mode & 0x777 & r->ifcall.perm; mode |= (r->ifcall.perm & ~0x777); if(mode&DMDIR){ respond(r, "queries cannot be directories"); return; } if(f = createfile(file, name, uid, mode, nil)){ q = emalloc9p(sizeof *q); q->text = estrdup9p(""); q->expr = nil; f->aux = q; closefile(r->fid->file); r->fid->file = f; r->ofcall.qid = f->dir.qid; respond(r, nil); } else respond(r, "problem creating file"); }
const char* createDebuggerFile(const char* debugger, int argc, char* argv[]) { const char* dbgfilename = genIntermediateFilename(astr(debugger, ".commands")); FILE* dbgfile = openfile(dbgfilename); int i; if (strcmp(debugger, "gdb") == 0) { fprintf(dbgfile, "set args"); } else if (strcmp(debugger, "lldb") == 0) { fprintf(dbgfile, "settings set -- target.run-args"); } else { INT_FATAL(astr("createDebuggerFile doesn't know how to handle the given " "debugger: '", debugger, "'")); } for (i=1; i<argc; i++) { if (strcmp(argv[i], astr("--", debugger)) != 0) { fprintf(dbgfile, " %s", argv[i]); } } fprintf(dbgfile, "\n"); closefile(dbgfile); mysystem(astr("cat ", CHPL_HOME, "/compiler/etc/", debugger, ".commands >> ", dbgfilename), astr("appending ", debugger, " commands"), false); return dbgfilename; }
/* * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file * @param path the path of the file to open and write * @param buf the starting address of the data to write into file * @param sz how many bytes to write at most * @return the byte we've written, or Linux specific error code */ static int storetofile(char *path, u8 __user *buf, u32 sz) { int ret = 0; mm_segment_t oldfs; struct file *fp; if (path && buf) { ret = openfile(&fp, path, O_CREAT|O_WRONLY, 0666); if (0 == ret) { DBG_88E("%s openfile path:%s fp =%p\n", __func__, path, fp); oldfs = get_fs(); set_fs(get_ds()); ret = writefile(fp, buf, sz); set_fs(oldfs); closefile(fp); DBG_88E("%s writefile, ret:%d\n", __func__, ret); } else { DBG_88E("%s openfile path:%s Fail, ret:%d\n", __func__, path, ret); } } else { DBG_88E("%s NULL pointer\n", __func__); ret = -EINVAL; } return ret; }
Interface::~Interface() { //将修改后的信息保存进文件中,关闭文件 ofstream closefile("database.txt",ios::out); closefile << "账号 " <<"密码"<< endl; data->inOrder(closefile); closefile.close(); }
// fname indica la ruta y denominación del archivo void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn) { FILE *f; int row, col; register struct ent **pp; int pid; info("Writing file \"%s\"...", fname); if ((f = openfile(fname, &pid, NULL)) == (FILE *)0) { error ("Can't create file \"%s\"", fname); return; } struct ent * ent = go_end(); if (rn > ent->row) rn = ent->row; //if (cn > ent->col) cn = ent->col; for (row = r0; row <= rn; row++) { for (pp = ATBL(tbl, row, col = c0); col <= cn; col++, pp++) { if (*pp) { char * s; if ((*pp)->flags & is_valid) { if ((*pp)->cellerror) { (void) fprintf (f, "%*s", fwidth[col], ((*pp)->cellerror == CELLERROR ? "ERROR" : "INVALID")); } else if ((*pp)->format) { char field[FBUFLEN]; if (*((*pp)->format) == 'd') { // formato fecha time_t v = (time_t) ((*pp)->v); strftime(field, sizeof(field), ((*pp)->format)+1, localtime(&v)); } else { // formato numerico format((*pp)->format, precision[col], (*pp)->v, field, sizeof(field)); } ltrim(field, ' '); unspecial(f, field, coldelim); } else { //eng number format char field[FBUFLEN] = ""; (void) engformat(realfmt[col], fwidth[col], precision[col], (*pp)->v, field, sizeof(field)); ltrim(field, ' '); unspecial(f, field, coldelim); } } if ((s = (*pp)->label)) { ltrim(s, ' '); unspecial(f, s, coldelim); } } if (col < cn) (void) fprintf(f,"%c", coldelim); } (void) fprintf(f,"\n"); } closefile(f, pid, 0); if (! pid) { info("File \"%s\" written", fname); } }
/* the symbol table is loaded from 'nm' output All platforms have their own 'nm' output format, but luckily some support POSIX format (although often poorly supported). POSIX.2 format is <symbol> <type> <address> [<size>] The symbol apperently may be empty on some systems The address may be empty, when type is 'U' The size is optional */ int load_SymbolTable(char *filename) { AtomicFile *f; char buf[MAX_LONGLINE], namebuf[MAX_LONGLINE], type; unsigned long addr; SymbolTable *st; listdestroy_SymbolTable(symbol_table); symbol_table = NULL; if ((f = openfile(filename, "r")) == NULL) return 1; while(fgets(buf, MAX_LONGLINE, f->f) != NULL) { cstrip_line(buf); if (!*buf) continue; addr = 0UL; type = '#'; cstrcpy(namebuf, "<unknown>", MAX_LONGLINE); if (*buf == ' ') sscanf(buf, " %c %lx", &type, &addr); else sscanf(buf, "%s %c %lx", namebuf, &type, &addr); if ((st = new_SymbolTable()) == NULL) { closefile(f); listdestroy_SymbolTable(symbol_table); symbol_table = NULL; return -1; } st->name = cstrdup(namebuf); st->type = type; st->addr = addr; symbol_table = add_SymbolTable(&symbol_table, st); } closefile(f); symbol_table = rewind_SymbolTable(symbol_table); return 0; }
int lex_fini(void) { stats_elapse_stop(Lexelapse); closefile(); if (Lexecho) { outfl(O_OK, File, Line, "lex: "); dumpline(O_OK); } return (Errcount); }
int main(int argc, char **argv) { struct radixdb db; if (argc != 2) { fprintf(stderr, "usage: radixdbdump f\n"); return 1; } openfile(argv[1], &db); radixdb_dump2dot(&db); closefile(&db); return 0; }
int removefile(File *f) { File *fp; Filelist *fl; fp = f->parent; if(fp == nil){ werrstr("no parent"); closefile(f); return -1; } if(fp == f){ werrstr("cannot remove root"); closefile(f); return -1; } wlock(&f->RWLock); wlock(&fp->RWLock); if(f->nchild != 0){ werrstr("has children"); wunlock(&fp->RWLock); wunlock(&f->RWLock); closefile(f); return -1; } if(f->parent != fp){ werrstr("parent changed underfoot"); wunlock(&fp->RWLock); wunlock(&f->RWLock); closefile(f); return -1; } for(fl=fp->filelist; fl; fl=fl->link) if(fl->f == f) break; assert(fl != nil && fl->f == f); fl->f = nil; fp->nchild--; fp->nxchild++; f->parent = nil; wunlock(&f->RWLock); cleanfilelist(fp); wunlock(&fp->RWLock); closefile(fp); /* reference from child */ closefile(f); /* reference from tree */ closefile(f); return 0; }
int save_StringList(StringList *sl, char *file) { AtomicFile *f; if ((f = openfile(file, "w")) == NULL) return -1; while(sl != NULL) { fprintf(f->f, "%s\n", sl->str); sl = sl->next; } return closefile(f); }
static boolean dbclosefile (hdlodbrecord hodb) { if (!odbclosefile ((**hodb).odb)) return (false); closefile ((**hodb).fref); listunlink ((hdllinkedlist) hodblist, (hdllinkedlist) hodb); disposehandle ((Handle) hodb); return (true); } /*dbclosefile*/
EDFfilehandler::~EDFfilehandler(){ closefile(); if (edfdatarecoffset) free(edfdatarecoffset); if (samplesperrecord) free(samplesperrecord); if (numofsignaldata) free(numofsignaldata); if (readbuffer) free(readbuffer); if (annotbuffer!=NULL) free(annotbuffer); }
int POL::inchar() { int c = 0; if (currentf < 0) return (EOF); while (currentf >= 0 && (c = getch(filep[currentf])) == EOF && filep[currentf] != NULL) { closefile (); } return (c); }
void filldir(File *t, Ofile *f, int dnum, int nrecur) { Odir d; int i; Rune rbuf[40]; char buf[UTFmax*nelem(rbuf)]; File *nt; if(dnum == 0xFFFFFFFF || oreaddir(f, dnum, &d) != 1) return; /* * i hope there are no broken files with * circular trees. i hate infinite loops. */ if(nrecur > 100) sysfatal("tree too large in office file: probably circular"); filldir(t, f, d.left, nrecur+1); /* add current tree entry */ runestrecpy(rbuf, rbuf+sizeof rbuf, d.name); for(i=0; rbuf[i]; i++) if(rbuf[i] == L' ') rbuf[i] = L'␣'; else if(rbuf[i] <= 0x20 || rbuf[i] == L'/' || (0x80 <= rbuf[i] && rbuf[i] <= 0x9F)) rbuf[i] = ':'; snprint(buf, sizeof buf, "%S", rbuf); if(d.dir == 0xFFFFFFFF) { /* make file */ nt = createfile(t, buf, nil, 0444, nil); if(nt == nil) sysfatal("nt nil: create %s: %r", buf); nt->aux = copydir(&d); nt->Dir.length = d.size; } else /* make directory */ nt = createfile(t, buf, nil, DMDIR|0777, nil); filldir(t, f, d.right, nrecur+1); if(d.dir != 0xFFFFFFFF) filldir(nt, f, d.dir, nrecur+1); closefile(nt); }
void sbbs_t::backout() { char str[256],code[128],*buf; int i,file; long length,l; file_t f; sprintf(str,"%sbackout.dab",cfg.node_dir); if(flength(str)<1L) { remove(str); return; } if((file=nopen(str,O_RDONLY))==-1) { errormsg(WHERE,ERR_OPEN,str,O_RDONLY); return; } length=(long)filelength(file); if((buf=(char *)malloc(length))==NULL) { close(file); errormsg(WHERE,ERR_ALLOC,str,length); return; } if(read(file,buf,length)!=length) { close(file); free(buf); errormsg(WHERE,ERR_READ,str,length); return; } close(file); for(l=0;l<length;l+=BO_LEN) { switch(buf[l]) { case BO_OPENFILE: /* file left open */ memcpy(code,buf+l+1,8); code[8]=0; for(i=0;i<cfg.total_dirs;i++) /* search by code */ if(!stricmp(cfg.dir[i]->code,code)) break; if(i<cfg.total_dirs) { /* found internal code */ f.dir=i; memcpy(&f.datoffset,buf+l+9,4); closefile(&f); } break; default: errormsg(WHERE,ERR_CHK,str,buf[l]); } } free(buf); remove(str); /* always remove the backout file */ }
StringList *load_StringList(char *file) { StringList *sl = NULL; AtomicFile *f; char buf[1024]; if ((f = openfile(file, "r")) == NULL) return NULL; while(fgets(buf, 1024, f->f) != NULL) { chop(buf); (void)add_StringList(&sl, new_StringList(buf)); } closefile(f); return sl; }
static int io_fromto (lua_State *L, int inout, const char *mode) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *current; lua_pop(L, 1); /* remove upvalue */ if (lua_isnull(L, 1)) { closefile(L, ctrl, getfilebyref(L, ctrl, inout)); current = (inout == 0) ? stdin : stdout; } else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */ current = (FILE *)lua_touserdata(L, 1); else { const char *s = luaL_check_string(L, 1); current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); } return setreturn(L, ctrl, current, inout); }
static int closeit(int success) { int rc; rc = closefile(); fout = NULL; sbytes = rxbytes - sbytes; gettimeofday(&endtime, &tz); if (success) Syslog('+', "TCP: OK %s", transfertime(starttime, endtime, sbytes, FALSE)); else Syslog('+', "TCP: dropped after %ld bytes", sbytes); rcvdbytes += sbytes; return rc; }
/* * close the files making up a tar file */ void closetar(Dfile *df, Symbol *dp) { int i; uchar *vec; File *f; f = &df->file[dp->fno]; vec = f->refvec; /* find file */ for(i = 0; i < df->nfile; i++){ if((vec[i/8] & (1<<(i&7))) == 0) continue; closefile(&df->file[i]); } }
int load_Category(void) { AtomicFile *f; char buf[1024]; if ((f = openfile(PARAM_CATEGORIES_FILE, "r")) == NULL) return -1; while(fgets(buf, 1024, f->f) != NULL) { chop(buf); if (!*buf) continue; add_Category(buf); } closefile(f); return 0; }