void convert_tunix (FILE *infile, FILE *outfile) { unsigned char a; unsigned char *in_bufpos; unsigned char *out_bufpos; unsigned char *in_bufend; unsigned char *out_bufend; size_t file_remain; in_bufpos = in_buffer; out_bufpos = out_buffer; (void) fseek(infile,0L,SEEK_END); file_remain = ftell(infile); rewind(infile); in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); out_bufend = out_buffer + BUFFERSIZE; while (file_remain != 0) { a = *in_bufpos; in_bufpos++; if (in_bufpos >= in_bufend) { file_remain -= in_bufend - in_buffer; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); in_bufpos = in_buffer; } if(a == CR) { *out_bufpos = LF; out_bufpos++; if (out_bufpos == out_bufend) { my_fwrite(out_buffer,outfile,BUFFERSIZE); out_bufpos = out_buffer; } if (*in_bufpos == LF && file_remain != 0) { in_bufpos++; if (in_bufpos >= in_bufend) { file_remain -= in_bufend - in_buffer; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); in_bufpos = in_buffer; } } } else { *out_bufpos = a; out_bufpos++; if (out_bufpos == out_bufend) { my_fwrite(out_buffer,outfile,BUFFERSIZE); out_bufpos = out_buffer; } } } /* Check for remainder in output buffer */ if (out_bufpos != out_buffer) my_fwrite(out_buffer,outfile,(int)(out_bufpos - out_buffer)); }
/* reads/writes one int */ void in(int *x, int modus) { if(modus) my_fread(x, 1, sizeof(int), fd); else my_fwrite(x, 1, sizeof(int), fd); }
/* reads/writes n bytes */ void byten(void *x, int n, int modus) { if(modus) my_fread(x, n, 1, fd); else my_fwrite(x, n, 1, fd); }
/* Copy contents of an IO_CACHE to a file. SYNOPSIS my_b_copy_to_file() cache IO_CACHE to copy from file File to copy to DESCRIPTION Copy the contents of the cache to the file. The cache will be re-inited to a read cache and will read from the beginning of the cache. If a failure to write fully occurs, the cache is only copied partially. TODO Make this function solid by handling partial reads from the cache in a correct manner: it should be atomic. RETURN VALUE 0 All OK 1 An error occured */ int my_b_copy_to_file(IO_CACHE *cache, FILE *file) { size_t bytes_in_cache; DBUG_ENTER("my_b_copy_to_file"); /* Reinit the cache to read from the beginning of the cache */ if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE)) DBUG_RETURN(1); bytes_in_cache= my_b_bytes_in_cache(cache); do { if (my_fwrite(file, cache->read_pos, bytes_in_cache, MYF(MY_WME | MY_NABP)) == (size_t) -1) DBUG_RETURN(1); cache->read_pos= cache->read_end; } while ((bytes_in_cache= my_b_fill(cache))); DBUG_RETURN(0); }
void do_log(const char *logfile) { FILE *f; uchar buf[4096]; int size; if (!logfile) do_usage(); f= my_fopen(logfile, O_WRONLY|O_APPEND|O_CREAT, MYF(MY_WME)); if (!f) my_exit(1); while ((size= my_fread(stdin, buf, sizeof(buf), MYF(MY_WME))) > 0) if ((int)my_fwrite(f, buf, size, MYF(MY_WME)) != size) my_exit(1); my_fclose(f, MYF(0)); my_exit(0); }
void write_output(char *prefix) { FILE *fo; char fname[256]; int ix,num_grids; sprintf(fname,"%s_dens.%04d",prefix,NodeThis); num_grids=1; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { lint index0=2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); my_fwrite(&(Dens_local[index0]),sizeof(float),Ngrid,fo); } } fclose(fo); if(TaskVel) { sprintf(fname,"%s_vel.%04d",prefix,NodeThis); num_grids=3; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { int iz; for(iz=0;iz<Ngrid;iz++) { lint index=iz+2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); my_fwrite(&(Vel_local[0][index]),sizeof(float),1,fo); my_fwrite(&(Vel_local[1][index]),sizeof(float),1,fo); my_fwrite(&(Vel_local[2][index]),sizeof(float),1,fo); } } } fclose(fo); } if(TaskSmooth) { sprintf(fname,"%s_dens_sm.%04d",prefix,NodeThis); num_grids=1; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { lint index0=2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); my_fwrite(&(Dens_sm_local[index0]),sizeof(float),Ngrid,fo); } } fclose(fo); } if(TaskLinvel) { sprintf(fname,"%s_linvel.%04d",prefix,NodeThis); num_grids=3; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { int iz; for(iz=0;iz<Ngrid;iz++) { lint index=iz+2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); my_fwrite(&(Lvel_local[0][index]),sizeof(float),1,fo); my_fwrite(&(Lvel_local[1][index]),sizeof(float),1,fo); my_fwrite(&(Lvel_local[2][index]),sizeof(float),1,fo); } } } fclose(fo); } if(TaskNlvel) { sprintf(fname,"%s_nlvel.%04d",prefix,NodeThis); num_grids=3; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { int iz; for(iz=0;iz<Ngrid;iz++) { lint index=iz+2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); my_fwrite(&(Nlvel_local[0][index]),sizeof(float),1,fo); my_fwrite(&(Nlvel_local[1][index]),sizeof(float),1,fo); my_fwrite(&(Nlvel_local[2][index]),sizeof(float),1,fo); } } } fclose(fo); } if(TaskTidal && !TaskTidalDiag) { sprintf(fname,"%s_tidal.%04d",prefix,NodeThis); num_grids=6; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { int iz; for(iz=0;iz<Ngrid;iz++) { lint index=iz+2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); my_fwrite(&(Tid_local[0][index]),sizeof(float),1,fo); my_fwrite(&(Tid_local[1][index]),sizeof(float),1,fo); my_fwrite(&(Tid_local[2][index]),sizeof(float),1,fo); my_fwrite(&(Tid_local[3][index]),sizeof(float),1,fo); my_fwrite(&(Tid_local[4][index]),sizeof(float),1,fo); my_fwrite(&(Tid_local[5][index]),sizeof(float),1,fo); } } } fclose(fo); } if(TaskTidalDiag) { Esys leig[3]; gsl_vector *eval=gsl_vector_alloc(3); gsl_matrix *tij=gsl_matrix_alloc(3,3); gsl_matrix *evec=gsl_matrix_alloc(3,3); gsl_eigen_symmv_workspace *w=gsl_eigen_symmv_alloc(3); sprintf(fname,"%s_tidal_eigsys.%04d",prefix,NodeThis); num_grids=12; fo=my_fopen(fname,"wb"); my_fwrite(&(num_grids),sizeof(int),1,fo); my_fwrite(&(Ngrid),sizeof(int),1,fo); my_fwrite(&(Nx_here),sizeof(int),1,fo); for(ix=0;ix<Nx_here;ix++) { int iy; int ix_id=Ix0_here+ix; my_fwrite(&(ix_id),sizeof(int),1,fo); for(iy=0;iy<Ngrid;iy++) { int iz; for(iz=0;iz<Ngrid;iz++) { int im; lint index=iz+2*(Ngrid/2+1)*((lint)(iy+ix*Ngrid)); gsl_matrix_set(tij,0,0,Tid_local[0][index]); gsl_matrix_set(tij,1,1,Tid_local[1][index]); gsl_matrix_set(tij,2,2,Tid_local[2][index]); gsl_matrix_set(tij,0,1,Tid_local[3][index]); gsl_matrix_set(tij,1,0,Tid_local[3][index]); gsl_matrix_set(tij,1,2,Tid_local[4][index]); gsl_matrix_set(tij,2,1,Tid_local[4][index]); gsl_matrix_set(tij,0,2,Tid_local[5][index]); gsl_matrix_set(tij,2,0,Tid_local[5][index]); gsl_eigen_symmv(tij,eval,evec,w); for(im=0;im<3;im++) { leig[im].eval=(float)(gsl_vector_get(eval,im)); leig[im].evec[0]=(float)(gsl_matrix_get(evec,0,im)); leig[im].evec[1]=(float)(gsl_matrix_get(evec,1,im)); leig[im].evec[2]=(float)(gsl_matrix_get(evec,2,im)); } qsort(leig,3,sizeof(Esys),compare_evals); for(im=0;im<3;im++) { my_fwrite(&(leig[im].eval),sizeof(float),1,fo); my_fwrite(&(leig[im].evec[0]),sizeof(float),1,fo); my_fwrite(&(leig[im].evec[1]),sizeof(float),1,fo); my_fwrite(&(leig[im].evec[2]),sizeof(float),1,fo); } } } } fclose(fo); gsl_eigen_symmv_free(w); gsl_vector_free(eval); gsl_matrix_free(evec); gsl_matrix_free(tij); } }
static int create_sys_files(struct languages *lang_head, struct errors *error_head, uint row_count) { FILE *to; uint csnum= 0, length, i, row_nr; uchar head[32]; char outfile[FN_REFLEN], *outfile_end; long start_pos; struct message *tmp; struct languages *tmp_lang; struct errors *tmp_error; MY_STAT stat_info; DBUG_ENTER("create_sys_files"); /* going over all languages and assembling corresponding error messages */ for (tmp_lang= lang_head; tmp_lang; tmp_lang= tmp_lang->next_lang) { /* setting charset name */ if (!(csnum= get_charset_number(tmp_lang->charset, MY_CS_PRIMARY))) { fprintf(stderr, "Unknown charset '%s' in '%s'\n", tmp_lang->charset, TXTFILE); DBUG_RETURN(1); } outfile_end= strxmov(outfile, DATADIRECTORY, tmp_lang->lang_long_name, NullS); if (!my_stat(outfile, &stat_info,MYF(0))) { if (my_mkdir(outfile, 0777,MYF(0)) < 0) { fprintf(stderr, "Can't create output directory for %s\n", outfile); DBUG_RETURN(1); } } strxmov(outfile_end, FN_ROOTDIR, OUTFILE, NullS); if (!(to= my_fopen(outfile, O_WRONLY | FILE_BINARY, MYF(MY_WME)))) DBUG_RETURN(1); /* 4 is for 4 bytes to store row position / error message */ start_pos= (long) (HEADER_LENGTH + row_count * 4); fseek(to, start_pos, 0); row_nr= 0; for (tmp_error= error_head; tmp_error; tmp_error= tmp_error->next_error) { /* dealing with messages */ tmp= find_message(tmp_error, tmp_lang->lang_short_name, FALSE); if (!tmp) { fprintf(stderr, "Did not find message for %s neither in %s nor in default " "language\n", tmp_error->er_name, tmp_lang->lang_short_name); goto err; } if (copy_rows(to, tmp->text, row_nr, start_pos)) { fprintf(stderr, "Failed to copy rows to %s\n", outfile); goto err; } row_nr++; } /* continue with header of the errmsg.sys file */ length= ftell(to) - HEADER_LENGTH - row_count * 4; memset(head, 0, HEADER_LENGTH); memmove(head, file_head, 4); head[4]= 1; int4store(head + 6, length); int4store(head + 10, row_count); head[30]= csnum; my_fseek(to, 0l, MY_SEEK_SET, MYF(0)); if (my_fwrite(to, (uchar*) head, HEADER_LENGTH, MYF(MY_WME | MY_FNABP))) goto err; for (i= 0; i < row_count; i++) { int4store(head, file_pos[i]); if (my_fwrite(to, (uchar*) head, 4, MYF(MY_WME | MY_FNABP))) goto err; } my_fclose(to, MYF(0)); } DBUG_RETURN(0); err: my_fclose(to, MYF(0)); DBUG_RETURN(1); }
/*! This function writes an actual snapshot file containing the data from * processors 'writeTask' to 'lastTask'. 'writeTask' is the one that actually * writes. Each snapshot file contains a header first, then particle * positions, velocities and ID's. Particle masses are written only for * those particle types with zero entry in MassTable. After that, first the * internal energies u, and then the density is written for the SPH * particles. If cooling is enabled, mean molecular weight and neutral * hydrogen abundance are written for the gas particles. This is followed by * the SPH smoothing length and further blocks of information, depending on * included physics and compile-time flags. If HDF5 is used, the header is * stored in a group called "/Header", and the particle data is stored * separately for each particle type in groups calles "/PartType0", * "/PartType1", etc. The sequence of the blocks is unimportant in this case. */ void write_file(char *fname, int writeTask, int lastTask) { int type, bytes_per_blockelement, npart, nextblock, typelist[6]; int n_for_this_task, ntask, n, p, pc, offset = 0, task; int blockmaxlen, ntot_type[6], nn[6]; enum iofields blocknr; int blksize; MPI_Status status; FILE *fd = 0; #ifdef HAVE_HDF5 hid_t hdf5_file = 0, hdf5_grp[6], hdf5_headergrp = 0, hdf5_dataspace_memory; hid_t hdf5_datatype = 0, hdf5_dataspace_in_file = 0, hdf5_dataset = 0; herr_t hdf5_status; hsize_t dims[2], count[2], start[2]; int rank, pcsum = 0; char buf[500]; #endif #define SKIP {my_fwrite(&blksize,sizeof(int),1,fd);} /* determine particle numbers of each type in file */ if(ThisTask == writeTask) { for(n = 0; n < 6; n++) ntot_type[n] = n_type[n]; for(task = writeTask + 1; task <= lastTask; task++) { MPI_Recv(&nn[0], 6, MPI_INT, task, TAG_LOCALN, MPI_COMM_WORLD, &status); for(n = 0; n < 6; n++) ntot_type[n] += nn[n]; } for(task = writeTask + 1; task <= lastTask; task++) MPI_Send(&ntot_type[0], 6, MPI_INT, task, TAG_N, MPI_COMM_WORLD); } else { MPI_Send(&n_type[0], 6, MPI_INT, writeTask, TAG_LOCALN, MPI_COMM_WORLD); MPI_Recv(&ntot_type[0], 6, MPI_INT, writeTask, TAG_N, MPI_COMM_WORLD, &status); } /* fill file header */ for(n = 0; n < 6; n++) { header.npart[n] = ntot_type[n]; header.npartTotal[n] = (unsigned int) ntot_type_all[n]; header.npartTotalHighWord[n] = (unsigned int) (ntot_type_all[n] >> 32); } for(n = 0; n < 6; n++) header.mass[n] = All.MassTable[n]; header.time = All.Time; if(All.ComovingIntegrationOn) header.redshift = 1.0 / All.Time - 1; else header.redshift = 0; header.flag_sfr = 0; header.flag_feedback = 0; header.flag_cooling = 0; header.flag_stellarage = 0; header.flag_metals = 0; #ifdef COOLING header.flag_cooling = 1; #endif #ifdef SFR header.flag_sfr = 1; header.flag_feedback = 1; #ifdef STELLARAGE header.flag_stellarage = 1; #endif #ifdef METALS header.flag_metals = 1; #endif #endif header.num_files = All.NumFilesPerSnapshot; header.BoxSize = All.BoxSize; header.Omega0 = All.Omega0; header.OmegaLambda = All.OmegaLambda; header.HubbleParam = All.HubbleParam; /* open file and write header */ if(ThisTask == writeTask) { if(All.SnapFormat == 3) { #ifdef HAVE_HDF5 sprintf(buf, "%s.hdf5", fname); hdf5_file = H5Fcreate(buf, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hdf5_headergrp = H5Gcreate(hdf5_file, "/Header", 0); for(type = 0; type < 6; type++) { if(header.npart[type] > 0) { sprintf(buf, "/PartType%d", type); hdf5_grp[type] = H5Gcreate(hdf5_file, buf, 0); } } write_header_attributes_in_hdf5(hdf5_headergrp); #endif } else { if(!(fd = fopen(fname, "w"))) { printf("can't open file `%s' for writing snapshot.\n", fname); endrun(123); } if(All.SnapFormat == 2) { blksize = sizeof(int) + 4 * sizeof(char); SKIP; my_fwrite("HEAD", sizeof(char), 4, fd); nextblock = sizeof(header) + 2 * sizeof(int); my_fwrite(&nextblock, sizeof(int), 1, fd); SKIP; } blksize = sizeof(header); SKIP; my_fwrite(&header, sizeof(header), 1, fd); SKIP; } } ntask = lastTask - writeTask + 1; for(blocknr = 0; blocknr < IO_NBLOCKS; blocknr++) { if(blockpresent(blocknr)) { bytes_per_blockelement = get_bytes_per_blockelement(blocknr); blockmaxlen = ((int) (All.BufferSize * 1024 * 1024)) / bytes_per_blockelement; npart = get_particles_in_block(blocknr, &typelist[0]); if(npart > 0) { if(ThisTask == writeTask) { if(All.SnapFormat == 1 || All.SnapFormat == 2) { if(All.SnapFormat == 2) { blksize = sizeof(int) + 4 * sizeof(char); SKIP; my_fwrite(Tab_IO_Labels[blocknr], sizeof(char), 4, fd); nextblock = npart * bytes_per_blockelement + 2 * sizeof(int); my_fwrite(&nextblock, sizeof(int), 1, fd); SKIP; } blksize = npart * bytes_per_blockelement; SKIP; } } for(type = 0; type < 6; type++) { if(typelist[type]) { #ifdef HAVE_HDF5 if(ThisTask == writeTask && All.SnapFormat == 3 && header.npart[type] > 0) { switch (get_datatype_in_block(blocknr)) { case 0: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT); break; case 1: hdf5_datatype = H5Tcopy(H5T_NATIVE_FLOAT); break; case 2: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT64); break; } dims[0] = header.npart[type]; dims[1] = get_values_per_blockelement(blocknr); if(dims[1] == 1) rank = 1; else rank = 2; get_dataset_name(blocknr, buf); hdf5_dataspace_in_file = H5Screate_simple(rank, dims, NULL); hdf5_dataset = H5Dcreate(hdf5_grp[type], buf, hdf5_datatype, hdf5_dataspace_in_file, H5P_DEFAULT); pcsum = 0; } #endif for(task = writeTask, offset = 0; task <= lastTask; task++) { if(task == ThisTask) { n_for_this_task = n_type[type]; for(p = writeTask; p <= lastTask; p++) if(p != ThisTask) MPI_Send(&n_for_this_task, 1, MPI_INT, p, TAG_NFORTHISTASK, MPI_COMM_WORLD); } else MPI_Recv(&n_for_this_task, 1, MPI_INT, task, TAG_NFORTHISTASK, MPI_COMM_WORLD, &status); while(n_for_this_task > 0) { pc = n_for_this_task; if(pc > blockmaxlen) pc = blockmaxlen; if(ThisTask == task) fill_write_buffer(blocknr, &offset, pc, type); if(ThisTask == writeTask && task != writeTask) MPI_Recv(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, task, TAG_PDATA, MPI_COMM_WORLD, &status); if(ThisTask != writeTask && task == ThisTask) MPI_Ssend(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, writeTask, TAG_PDATA, MPI_COMM_WORLD); if(ThisTask == writeTask) { if(All.SnapFormat == 3) { #ifdef HAVE_HDF5 start[0] = pcsum; start[1] = 0; count[0] = pc; count[1] = get_values_per_blockelement(blocknr); pcsum += pc; H5Sselect_hyperslab(hdf5_dataspace_in_file, H5S_SELECT_SET, start, NULL, count, NULL); dims[0] = pc; dims[1] = get_values_per_blockelement(blocknr); hdf5_dataspace_memory = H5Screate_simple(rank, dims, NULL); hdf5_status = H5Dwrite(hdf5_dataset, hdf5_datatype, hdf5_dataspace_memory, hdf5_dataspace_in_file, H5P_DEFAULT, CommBuffer); H5Sclose(hdf5_dataspace_memory); #endif } else my_fwrite(CommBuffer, bytes_per_blockelement, pc, fd); } n_for_this_task -= pc; } } #ifdef HAVE_HDF5 if(ThisTask == writeTask && All.SnapFormat == 3 && header.npart[type] > 0) { if(All.SnapFormat == 3) { H5Dclose(hdf5_dataset); H5Sclose(hdf5_dataspace_in_file); H5Tclose(hdf5_datatype); } } #endif } } if(ThisTask == writeTask) { if(All.SnapFormat == 1 || All.SnapFormat == 2) SKIP; } } } } if(ThisTask == writeTask) { if(All.SnapFormat == 3) { #ifdef HAVE_HDF5 for(type = 5; type >= 0; type--) if(header.npart[type] > 0) H5Gclose(hdf5_grp[type]); H5Gclose(hdf5_headergrp); H5Fclose(hdf5_file); #endif } else fclose(fd); } }
int convert_fast_tunix (FILE *infile, FILE *outfile) { unsigned char a; unsigned char *in_bufpos; unsigned char *in_bufend; size_t file_remain; enum file_format infile_type; in_bufpos = in_buffer; (void) fseek(infile,0L,SEEK_END); file_remain = ftell(infile); rewind(infile); in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); *in_bufend = '\0'; infile_type = get_file_format (in_buffer); switch (infile_type) { case tunix: if (verbose) printf("%s: %s is already in Unix format, skipping.\n", program_name,current_file); return (FALSE); break; case apple: /* Replace "in-vitro", so out_buffer isn't used */ while (file_remain != 0) { a = *in_bufpos; if (a == CR) *in_bufpos++ = LF; else if (a == '\0'){ /* End of buffer reached */ /* Write changed buffer out */ my_fwrite(in_buffer,outfile,(int)(in_bufend - in_buffer)); /* And reload */ file_remain -= in_bufend - in_buffer; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); *in_bufend = '\0'; in_bufpos = in_buffer; } else in_bufpos++; } return (TRUE); break; case dos: /* Couldn't speed it up, so use old routine */ convert_tunix (infile, outfile); return (TRUE); break; case binary: return (FALSE); break; default: fprintf(stderr,"%s: Fatal internal error\n", program_name); exit (EXIT_FAILURE); break; } /* switch */ }
void save_local_data(void) { #define BUFFER 10 size_t bytes; float *block; int *blockid; long long *blocklongid; int blockmaxlen, maxidlen, maxlongidlen; int4byte dummy; FILE *fd; char buf[300]; int i, k, pc; double meanspacing, shift_gas, shift_dm; if(NumPart == 0) return; if(NTaskWithN > 1) sprintf(buf, "%s/%s.%d", OutputDir, FileBase, ThisTask); else sprintf(buf, "%s/%s", OutputDir, FileBase); if(!(fd = fopen(buf, "w"))) { printf("Error. Can't write in file '%s'\n", buf); FatalError(10); } for(i = 0; i < 6; i++) { header.npart[i] = 0; header.npartTotal[i] = 0; header.mass[i] = 0; } header.npart[1] = NumPart; header.npartTotal[1] = TotNumPart; header.npartTotal[2] = (TotNumPart >> 32); header.mass[1] = (Omega) * 3 * Hubble * Hubble / (8 * PI * G) * pow(Box, 3) / TotNumPart; #ifdef PRODUCEGAS header.npart[0] = NumPart; header.npartTotal[0] = TotNumPart; header.mass[0] = (OmegaBaryon) * 3 * Hubble * Hubble / (8 * PI * G) * pow(Box, 3) / TotNumPart; header.mass[1] = (Omega - OmegaBaryon) * 3 * Hubble * Hubble / (8 * PI * G) * pow(Box, 3) / TotNumPart; #endif header.time = InitTime; header.redshift = 1.0 / InitTime - 1; header.flag_sfr = 0; header.flag_feedback = 0; header.flag_cooling = 0; header.flag_stellarage = 0; header.flag_metals = 0; header.num_files = NTaskWithN; header.BoxSize = Box; header.Omega0 = Omega; header.OmegaLambda = OmegaLambda; header.HubbleParam = HubbleParam; header.flag_stellarage = 0; header.flag_metals = 0; header.hashtabsize = 0; dummy = sizeof(header); my_fwrite(&dummy, sizeof(dummy), 1, fd); my_fwrite(&header, sizeof(header), 1, fd); my_fwrite(&dummy, sizeof(dummy), 1, fd); meanspacing = Box / pow(TotNumPart, 1.0 / 3); shift_gas = -0.5 * (Omega - OmegaBaryon) / (Omega) * meanspacing; shift_dm = +0.5 * OmegaBaryon / (Omega) * meanspacing; if(!(block = malloc(bytes = BUFFER * 1024 * 1024))) { printf("failed to allocate memory for `block' (%g bytes).\n", (double) bytes); FatalError(24); } blockmaxlen = bytes / (3 * sizeof(float)); blockid = (int *) block; blocklongid = (long long *) block; maxidlen = bytes / (sizeof(int)); maxlongidlen = bytes / (sizeof(long long)); /* write coordinates */ dummy = sizeof(float) * 3 * NumPart; #ifdef PRODUCEGAS dummy *= 2; #endif my_fwrite(&dummy, sizeof(dummy), 1, fd); for(i = 0, pc = 0; i < NumPart; i++) { for(k = 0; k < 3; k++) { block[3 * pc + k] = P[i].Pos[k]; #ifdef PRODUCEGAS block[3 * pc + k] = periodic_wrap(P[i].Pos[k] + shift_gas); #endif } pc++; if(pc == blockmaxlen) { my_fwrite(block, sizeof(float), 3 * pc, fd); pc = 0; } } if(pc > 0) my_fwrite(block, sizeof(float), 3 * pc, fd); #ifdef PRODUCEGAS for(i = 0, pc = 0; i < NumPart; i++) { for(k = 0; k < 3; k++) { block[3 * pc + k] = periodic_wrap(P[i].Pos[k] + shift_dm); } pc++; if(pc == blockmaxlen) { my_fwrite(block, sizeof(float), 3 * pc, fd); pc = 0; } } if(pc > 0) my_fwrite(block, sizeof(float), 3 * pc, fd); #endif my_fwrite(&dummy, sizeof(dummy), 1, fd); /* write velocities */ dummy = sizeof(float) * 3 * NumPart; #ifdef PRODUCEGAS dummy *= 2; #endif my_fwrite(&dummy, sizeof(dummy), 1, fd); for(i = 0, pc = 0; i < NumPart; i++) { for(k = 0; k < 3; k++) block[3 * pc + k] = P[i].Vel[k]; pc++; if(pc == blockmaxlen) { my_fwrite(block, sizeof(float), 3 * pc, fd); pc = 0; } } if(pc > 0) my_fwrite(block, sizeof(float), 3 * pc, fd); #ifdef PRODUCEGAS for(i = 0, pc = 0; i < NumPart; i++) { for(k = 0; k < 3; k++) block[3 * pc + k] = P[i].Vel[k]; if(WDM_On == 1 && WDM_Vtherm_On == 1) add_WDM_thermal_speeds(&block[3 * pc]); pc++; if(pc == blockmaxlen) { my_fwrite(block, sizeof(float), 3 * pc, fd); pc = 0; } } if(pc > 0) my_fwrite(block, sizeof(float), 3 * pc, fd); #endif my_fwrite(&dummy, sizeof(dummy), 1, fd); /* write particle ID */ #ifdef NO64BITID dummy = sizeof(int) * NumPart; #else dummy = sizeof(long long) * NumPart; #endif #ifdef PRODUCEGAS dummy *= 2; #endif my_fwrite(&dummy, sizeof(dummy), 1, fd); for(i = 0, pc = 0; i < NumPart; i++) { #ifdef NO64BITID blockid[pc] = P[i].ID; #else blocklongid[pc] = P[i].ID; #endif pc++; if(pc == maxlongidlen) { #ifdef NO64BITID my_fwrite(blockid, sizeof(int), pc, fd); #else my_fwrite(blocklongid, sizeof(long long), pc, fd); #endif pc = 0; } } if(pc > 0) { #ifdef NO64BITID my_fwrite(blockid, sizeof(int), pc, fd); #else my_fwrite(blocklongid, sizeof(long long), pc, fd); #endif } #ifdef PRODUCEGAS for(i = 0, pc = 0; i < NumPart; i++) { #ifdef NO64BITID blockid[pc] = P[i].ID + TotNumPart; #else blocklongid[pc] = P[i].ID + TotNumPart; #endif pc++; if(pc == maxlongidlen) { #ifdef NO64BITID my_fwrite(blockid, sizeof(int), pc, fd); #else my_fwrite(blocklongid, sizeof(long long), pc, fd); #endif pc = 0; } } if(pc > 0) { #ifdef NO64BITID my_fwrite(blockid, sizeof(int), pc, fd); #else my_fwrite(blocklongid, sizeof(long long), pc, fd); #endif } #endif my_fwrite(&dummy, sizeof(dummy), 1, fd); /* write zero temperatures if needed */ #ifdef PRODUCEGAS dummy = sizeof(float) * NumPart; my_fwrite(&dummy, sizeof(dummy), 1, fd); for(i = 0, pc = 0; i < NumPart; i++) { block[pc] = 0; pc++; if(pc == blockmaxlen) { my_fwrite(block, sizeof(float), pc, fd); pc = 0; } } if(pc > 0) my_fwrite(block, sizeof(float), pc, fd); my_fwrite(&dummy, sizeof(dummy), 1, fd); #endif free(block); fclose(fd); }
int main (int argc, char **argv) { int v_rfd; int v_wfd; int len = 0; int i, rem; int LOOP = 20; struct timeval t_start, t_end; int w_time, r_time; char * fname = "big_file"; char buff[1024 * 20]; /* Init the library */ rfs_init(argc, argv); /* prepare a buffer */ i = 0; rem = sizeof(buff); while (rem) { char str[256]; sprintf(str, "This is the %s file, line %d\n", fname, i++); len = strlen(str); rem -= len; if (rem > 0) strncpy(buff, str, rem); else rem = 0; } /* open new file for writing */ printf("File write test\n"); i = LOOP; gettimeofday(&t_start, NULL); do { v_wfd = my_fopen(fname, O_CREAT | O_WRONLY); /* write to file */ len = my_fwrite(v_wfd, buff, sizeof(buff)); /* close */ my_fclose(v_wfd); } while (--i); gettimeofday(&t_end, NULL); w_time = (int) (t_end.tv_sec - t_start.tv_sec); /* Read test */ printf("File Read test\n"); sleep(1); i = LOOP; gettimeofday(&t_start, NULL); do { /* Open remote file */ if ((v_rfd = my_fopen(fname, O_RDONLY)) < 0) { perror("File open"); exit(1); } /* Read its contents */ if ((len = my_fread(v_rfd, buff, sizeof(buff))) < 0) { perror("File read"); my_fclose(v_rfd); exit(1); } my_fclose(v_rfd); } while(--i); gettimeofday(&t_end, NULL); r_time = (int) (t_end.tv_sec - t_start.tv_sec); /* Report */ printf("******* Time ****** taken to do %d writes: %d\n", LOOP, w_time); printf("***** Time ****** taken to do %d reads %d\n", LOOP, r_time); return(0); }
int main (int argc, char **argv) { unsigned long i, j; int fd; char *buf, *read_buf, *mmap_buf, *filename = "/dev/asgn1"; int nproc = 12345; srandom (getpid ()); if (argc > 1) filename = argv[1]; if ((fd = open (filename, O_RDWR)) < 0) { fprintf (stderr, "open of %s failed: %s\n", filename, strerror (errno)); exit (1); } assert(buf = malloc(SIZE)); for (i = 0; i < SIZE; i++) { buf[i] = random() % 256; } if (SIZE != my_fwrite(fd, buf, SIZE)) { fprintf (stderr, "write problem: %s\n", strerror (errno)); exit(1); } #if 0 /* have the driver tell us the buffer size */ if (ioctl (fd, MMAP_DEV_CMD_GET_BUFSIZE, &len) < 0) { fprintf (stderr, "ioctl failed: %s\n", strerror (errno)); exit (1); } printf ("driver's ioctl says buffer size is %ld\n", len); #endif assert(read_buf = malloc(SIZE)); mmap_buf = mmap (NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mmap_buf == (char *)MAP_FAILED) { fprintf (stderr, "mmap of %s failed: %s\n", filename, strerror (errno)); exit (1); } printf ("mmap succeeded: %p\n", mmap_buf); (void)lseek (fd, 0, SEEK_SET); read_and_compare (fd, buf, mmap_buf, SIZE); printf ("comparison of same data via read() and mmap() successful\n"); /* Change one randomly chosen byte in the mmap region */ j = random () % SIZE; *(mmap_buf + j) = random () % j; printf ("modified mmap_buf\n"); /* repeat the read-back comparison. */ (void)lseek (fd, 0, SEEK_SET); read_and_compare (fd, read_buf, mmap_buf, SIZE); printf ("comparison of modified data via read() and mmap() successful\n"); (void)lseek (fd, 0, SEEK_SET); if (ioctl (fd, ASGN1_SET_NPROC, &nproc) < 0) { fprintf (stderr, "ioctl failed: %s\n", strerror (errno)); exit (1); } printf("nproc set to %d\n", nproc); return 0; }
static int examine_log(my_string file_name, char **table_names) { uint command,result,files_open; ulong access_time,length; my_off_t filepos; int lock_command,mi_result; char isam_file_name[FN_REFLEN],llbuff[21],llbuff2[21]; uchar head[20]; gptr buff; struct test_if_open_param open_param; IO_CACHE cache; File file; FILE *write_file; enum ha_extra_function extra_command; TREE tree; struct file_info file_info,*curr_file_info; DBUG_ENTER("examine_log"); if ((file=my_open(file_name,O_RDONLY,MYF(MY_WME))) < 0) DBUG_RETURN(1); write_file=0; if (write_filename) { if (!(write_file=my_fopen(write_filename,O_WRONLY,MYF(MY_WME)))) { my_close(file,MYF(0)); DBUG_RETURN(1); } } init_io_cache(&cache,file,0,READ_CACHE,start_offset,0,MYF(0)); bzero((gptr) com_count,sizeof(com_count)); init_tree(&tree,0,0,sizeof(file_info),(qsort_cmp2) file_info_compare,1, (tree_element_free) file_info_free, NULL); VOID(init_key_cache(KEY_CACHE_SIZE)); files_open=0; access_time=0; while (access_time++ != number_of_commands && !my_b_read(&cache,(byte*) head,9)) { isamlog_filepos=my_b_tell(&cache)-9L; file_info.filenr= mi_uint2korr(head+1); isamlog_process=file_info.process=(long) mi_uint4korr(head+3); if (!opt_processes) file_info.process=0; result= mi_uint2korr(head+7); if ((curr_file_info=(struct file_info*) tree_search(&tree,&file_info))) { curr_file_info->accessed=access_time; if (update && curr_file_info->used && curr_file_info->closed) { if (reopen_closed_file(&tree,curr_file_info)) { command=sizeof(com_count)/sizeof(com_count[0][0])/3; result=0; goto com_err; } } } command=(uint) head[0]; if (command < sizeof(com_count)/sizeof(com_count[0][0])/3 && (!table_names[0] || (curr_file_info && curr_file_info->used))) { com_count[command][0]++; if (result) com_count[command][1]++; } switch ((enum myisam_log_commands) command) { case MI_LOG_OPEN: if (!table_names[0]) { com_count[command][0]--; /* Must be counted explicite */ if (result) com_count[command][1]--; } if (curr_file_info) printf("\nWarning: %s is opened with same process and filenumber\nMaybe you should use the -P option ?\n", curr_file_info->show_name); if (my_b_read(&cache,(byte*) head,2)) goto err; file_info.name=0; file_info.show_name=0; file_info.record=0; if (read_string(&cache,(gptr*) &file_info.name, (uint) mi_uint2korr(head))) goto err; { uint i; char *pos,*to; /* Fix if old DOS files to new format */ for (pos=file_info.name; (pos=strchr(pos,'\\')) ; pos++) *pos= '/'; pos=file_info.name; for (i=0 ; i < prefix_remove ; i++) { char *next; if (!(next=strchr(pos,'/'))) break; pos=next+1; } to=isam_file_name; if (filepath) to=convert_dirname(isam_file_name,filepath,NullS); strmov(to,pos); fn_ext(isam_file_name)[0]=0; /* Remove extension */ } open_param.name=file_info.name; open_param.max_id=0; VOID(tree_walk(&tree,(tree_walk_action) test_if_open,(void*) &open_param, left_root_right)); file_info.id=open_param.max_id+1; /* * In the line below +10 is added to accomodate '<' and '>' chars * plus '\0' at the end, so that there is place for 7 digits. * It is improbable that same table can have that many entries in * the table cache. * The additional space is needed for the sprintf commands two lines * below. */ file_info.show_name=my_memdup(isam_file_name, (uint) strlen(isam_file_name)+10, MYF(MY_WME)); if (file_info.id > 1) sprintf(strend(file_info.show_name),"<%d>",file_info.id); file_info.closed=1; file_info.accessed=access_time; file_info.used=1; if (table_names[0]) { char **name; file_info.used=0; for (name=table_names ; *name ; name++) { if (!strcmp(*name,isam_file_name)) file_info.used=1; /* Update/log only this */ } } if (update && file_info.used) { if (files_open >= max_files) { if (close_some_file(&tree)) goto com_err; files_open--; } if (!(file_info.isam= mi_open(isam_file_name,O_RDWR, HA_OPEN_WAIT_IF_LOCKED))) goto com_err; if (!(file_info.record=my_malloc(file_info.isam->s->base.reclength, MYF(MY_WME)))) goto end; files_open++; file_info.closed=0; if (opt_myisam_with_debug) file_info.isam->s->rnd= 0; else file_info.isam->s->rnd= isamlog_process; } VOID(tree_insert(&tree,(gptr) &file_info,0)); if (file_info.used) { if (verbose && !record_pos_file) printf_log("%s: open -> %d",file_info.show_name, file_info.filenr); com_count[command][0]++; if (result) com_count[command][1]++; } break; case MI_LOG_CLOSE: if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s -> %d",FILENAME(curr_file_info), command_name[command],result); if (curr_file_info) { if (!curr_file_info->closed) files_open--; VOID(tree_delete(&tree,(gptr) curr_file_info)); } break; case MI_LOG_EXTRA: if (my_b_read(&cache,(byte*) head,1)) goto err; extra_command=(enum ha_extra_function) head[0]; if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s(%d) -> %d",FILENAME(curr_file_info), command_name[command], (int) extra_command,result); if (update && curr_file_info && !curr_file_info->closed) { if (mi_extra(curr_file_info->isam, extra_command, 0) != (int) result) { fflush(stdout); VOID(fprintf(stderr, "Warning: error %d, expected %d on command %s at %s\n", my_errno,result,command_name[command], llstr(isamlog_filepos,llbuff))); fflush(stderr); } } break; case MI_LOG_DELETE: if (my_b_read(&cache,(byte*) head,8)) goto err; filepos=mi_sizekorr(head); if (verbose && (!record_pos_file || ((record_pos == filepos || record_pos == NO_FILEPOS) && !cmp_filename(curr_file_info,record_pos_file))) && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s at %ld -> %d",FILENAME(curr_file_info), command_name[command],(long) filepos,result); if (update && curr_file_info && !curr_file_info->closed) { if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos)) { if (!recover) goto com_err; if (verbose) printf_log("error: Didn't find row to delete with mi_rrnd"); com_count[command][2]++; /* Mark error */ } mi_result=mi_delete(curr_file_info->isam,curr_file_info->record); if ((mi_result == 0 && result) || (mi_result && (uint) my_errno != result)) { if (!recover) goto com_err; if (mi_result) com_count[command][2]++; /* Mark error */ if (verbose) printf_log("error: Got result %d from mi_delete instead of %d", mi_result, result); } } break; case MI_LOG_WRITE: case MI_LOG_UPDATE: if (my_b_read(&cache,(byte*) head,12)) goto err; filepos=mi_sizekorr(head); length=mi_uint4korr(head+8); buff=0; if (read_string(&cache,&buff,(uint) length)) goto err; if ((!record_pos_file || ((record_pos == filepos || record_pos == NO_FILEPOS) && !cmp_filename(curr_file_info,record_pos_file))) && (!table_names[0] || (curr_file_info && curr_file_info->used))) { if (write_file && (my_fwrite(write_file,buff,length,MYF(MY_WAIT_IF_FULL | MY_NABP)))) goto end; if (verbose) printf_log("%s: %s at %ld, length=%ld -> %d", FILENAME(curr_file_info), command_name[command], filepos,length,result); } if (update && curr_file_info && !curr_file_info->closed) { if (curr_file_info->isam->s->base.blobs) fix_blob_pointers(curr_file_info->isam,buff); if ((enum myisam_log_commands) command == MI_LOG_UPDATE) { if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos)) { if (!recover) { result=0; goto com_err; } if (verbose) printf_log("error: Didn't find row to update with mi_rrnd"); if (recover == 1 || result || find_record_with_key(curr_file_info,buff)) { com_count[command][2]++; /* Mark error */ break; } } mi_result=mi_update(curr_file_info->isam,curr_file_info->record, buff); if ((mi_result == 0 && result) || (mi_result && (uint) my_errno != result)) { if (!recover) goto com_err; if (verbose) printf_log("error: Got result %d from mi_update instead of %d", mi_result, result); if (mi_result) com_count[command][2]++; /* Mark error */ } } else { mi_result=mi_write(curr_file_info->isam,buff); if ((mi_result == 0 && result) || (mi_result && (uint) my_errno != result)) { if (!recover) goto com_err; if (verbose) printf_log("error: Got result %d from mi_write instead of %d", mi_result, result); if (mi_result) com_count[command][2]++; /* Mark error */ } if (!recover && filepos != curr_file_info->isam->lastpos) { printf("error: Wrote at position: %s, should have been %s", llstr(curr_file_info->isam->lastpos,llbuff), llstr(filepos,llbuff2)); goto end; } } } my_free(buff,MYF(0)); break; case MI_LOG_LOCK: if (my_b_read(&cache,(byte*) head,sizeof(lock_command))) goto err; memcpy_fixed(&lock_command,head,sizeof(lock_command)); if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s(%d) -> %d\n",FILENAME(curr_file_info), command_name[command],lock_command,result); if (update && curr_file_info && !curr_file_info->closed) { if (mi_lock_database(curr_file_info->isam,lock_command) != (int) result) goto com_err; } break; case MI_LOG_DELETE_ALL: if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s -> %d\n",FILENAME(curr_file_info), command_name[command],result); break; default: fflush(stdout); VOID(fprintf(stderr, "Error: found unknown command %d in logfile, aborted\n", command)); fflush(stderr); goto end; } } end_key_cache(); delete_tree(&tree); VOID(end_io_cache(&cache)); VOID(my_close(file,MYF(0))); if (write_file && my_fclose(write_file,MYF(MY_WME))) DBUG_RETURN(1); DBUG_RETURN(0); err: fflush(stdout); VOID(fprintf(stderr,"Got error %d when reading from logfile\n",my_errno)); fflush(stderr); goto end; com_err: fflush(stdout); VOID(fprintf(stderr,"Got error %d, expected %d on command %s at %s\n", my_errno,result,command_name[command], llstr(isamlog_filepos,llbuff))); fflush(stderr); end: end_key_cache(); delete_tree(&tree); VOID(end_io_cache(&cache)); VOID(my_close(file,MYF(0))); if (write_file) VOID(my_fclose(write_file,MYF(MY_WME))); DBUG_RETURN(1); }
int main(int argc, char *argv[]) { const char *starter_name; const char *exe_name; FILE *exe_file; int err; ElfW(Ehdr) ehdr_exe; ElfW(Phdr) *phdrs_exe; off_t offset = 0; off_t exe_size; off_t starter_size; unsigned char *starter_mem; pgm_name = argv[0]; if (argc != 3) { fprintf( stderr, "Usage: %s <starter> <exe>\n", pgm_name ); exit(1); } starter_name = argv[1]; exe_name = argv[2]; /* Get executable's ehdr */ err = get_ehdr_phdrs_and_shdrs( exe_name, &ehdr_exe, &phdrs_exe, NULL, NULL, NULL ); if (err == -1) exit(1); /* Find where in the file starter should be placed */ { int i; for (i = 0; i < ehdr_exe.e_phnum; i++) { /* It should be in the PT_LOAD segment */ if (phdrs_exe[i].p_type != PT_LOAD) continue; if ( (ehdr_exe.e_entry >= phdrs_exe[i].p_vaddr) && (ehdr_exe.e_entry < phdrs_exe[i].p_vaddr + phdrs_exe[i].p_memsz) ) { offset = ehdr_exe.e_entry - phdrs_exe[i].p_vaddr + phdrs_exe[i].p_offset; break; } } } if (offset == 0) { fprintf( stderr, "%s: can't find PT_LOAD segment with e_entry=0x%lx.\n", pgm_name, (unsigned long)ehdr_exe.e_entry ); exit(1); } /* Get size of the exe file */ exe_size = my_file_size(exe_name, &err); if (err == -1) exit(1); /* read starter file */ starter_mem = my_fread_whole_file( starter_name, "starter", &starter_size ); /* Sanity */ if (exe_size < (starter_size + offset) ) { fprintf( stderr, "%s: mismatch: starter offset=%lu(0x%lx) + starter size=%lu > exe_size=%lu\n", pgm_name, offset, offset, starter_size, exe_size ); exit(1); } /* Open for read/write */ exe_file = my_fopen(exe_name, "r+"); if (exe_file == NULL) exit(1); /* Position file to the place where starter should begin */ err = my_fseek(exe_file, offset, exe_name); if (err == -1) exit(1); /* Write starter */ err = my_fwrite( starter_mem, starter_size, exe_file, exe_name, "starter" ); if (err == -1) exit(1); /* Close file */ err = my_fclose(exe_file, exe_name); if (err == -1) exit(1); /* all done, exit */ exit(0); }
int modify_defaults_file(const char *file_location, const char *option, const char *option_value, const char *section_name, int remove_option) { FILE *cnf_file; MY_STAT file_stat; char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer; size_t opt_len= 0, optval_len= 0, sect_len; uint nr_newlines= 0, buffer_size; my_bool in_section= FALSE, opt_applied= 0; uint reserve_extended; uint new_opt_len; int reserve_occupied= 0; DBUG_ENTER("modify_defaults_file"); if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0)))) DBUG_RETURN(2); /* my_fstat doesn't use the flag parameter */ if (my_fstat(fileno(cnf_file), &file_stat, MYF(0))) goto malloc_err; if (option && option_value) { opt_len= strlen(option); optval_len= strlen(option_value); } new_opt_len= opt_len + 1 + optval_len + NEWLINE_LEN; /* calculate the size of the buffer we need */ reserve_extended= (opt_len + 1 + /* For '=' char */ optval_len + /* Option value len */ NEWLINE_LEN + /* Space for newline */ RESERVE); /* Some additional space */ buffer_size= (file_stat.st_size + 1); /* The ending zero */ /* Reserve space to read the contents of the file and some more for the option we want to add. */ if (!(file_buffer= (char*) my_malloc(buffer_size + reserve_extended, MYF(MY_WME)))) goto malloc_err; sect_len= strlen(section_name); for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); ) { /* Skip over whitespaces */ for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++) {} if (!*src_ptr) /* Empty line */ { nr_newlines++; continue; } /* correct the option (if requested) */ if (option && in_section && !strncmp(src_ptr, option, opt_len) && (*(src_ptr + opt_len) == '=' || my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) || *(src_ptr + opt_len) == '\0')) { char *old_src_ptr= src_ptr; src_ptr= strend(src_ptr+ opt_len); /* Find the end of the line */ /* could be negative */ reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr); if (reserve_occupied >= (int) reserve_extended) { reserve_extended= (uint) reserve_occupied + RESERVE; if (!(file_buffer= (char*) my_realloc(file_buffer, buffer_size + reserve_extended, MYF(MY_WME|MY_FREE_ON_ERROR)))) goto malloc_err; } opt_applied= 1; dst_ptr= add_option(dst_ptr, option_value, option, remove_option); } else { /* If we are going to the new group and have an option to apply, do it now. If we are removing a single option or the whole section this will only trigger opt_applied flag. */ if (in_section && !opt_applied && *src_ptr == '[') { dst_ptr= add_option(dst_ptr, option_value, option, remove_option); opt_applied= 1; /* set the flag to do write() later */ reserve_occupied= new_opt_len+ opt_len + 1 + NEWLINE_LEN; } for (; nr_newlines; nr_newlines--) dst_ptr= strmov(dst_ptr, NEWLINE); /* Skip the section if MY_REMOVE_SECTION was given */ if (!in_section || remove_option != MY_REMOVE_SECTION) dst_ptr= strmov(dst_ptr, linebuff); } /* Look for a section */ if (*src_ptr == '[') { /* Copy the line to the buffer */ if (!strncmp(++src_ptr, section_name, sect_len)) { src_ptr+= sect_len; /* Skip over whitespaces. They are allowed after section name */ for (; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++) {} if (*src_ptr != ']') { in_section= FALSE; continue; /* Missing closing parenthesis. Assume this was no group */ } if (remove_option == MY_REMOVE_SECTION) dst_ptr= dst_ptr - strlen(linebuff); in_section= TRUE; } else in_section= FALSE; /* mark that this section is of no interest to us */ } } /* File ended. Apply an option or set opt_applied flag (in case of MY_REMOVE_SECTION) so that the changes are saved. Do not do anything if we are removing non-existent option. */ if (!opt_applied && in_section && (remove_option != MY_REMOVE_OPTION)) { /* New option still remains to apply at the end */ if (!remove_option && *(dst_ptr - 1) != '\n') dst_ptr= strmov(dst_ptr, NEWLINE); dst_ptr= add_option(dst_ptr, option_value, option, remove_option); opt_applied= 1; } for (; nr_newlines; nr_newlines--) dst_ptr= strmov(dst_ptr, NEWLINE); if (opt_applied) { /* Don't write the file if there are no changes to be made */ if (my_chsize(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer), 0, MYF(MY_WME)) || my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) || my_fwrite(cnf_file, (uchar*) file_buffer, (size_t) (dst_ptr - file_buffer), MYF(MY_NABP))) goto err; } if (my_fclose(cnf_file, MYF(MY_WME))) DBUG_RETURN(1); my_free(file_buffer, MYF(0)); DBUG_RETURN(0); err: my_free(file_buffer, MYF(0)); malloc_err: my_fclose(cnf_file, MYF(0)); DBUG_RETURN(1); /* out of resources */ }