예제 #1
0
파일: chkrand.c 프로젝트: dakrone/one-offs
int main(int argc, char **argv)
{
      unsigned int blocksize = 0;
      unsigned int shiftsize = 0;
      ssize_t len = 0;
      unsigned int matches = 0;
      unsigned int totalbytes = 0;
      float ioc = 0.0;
      FILE *infile = NULL;
      char *filename = NULL;
      char *b1 = NULL;
      char *b2 = NULL;

      if (argc < 4)
            usage();

      blocksize = atoi(argv[1]);
      shiftsize = atoi(argv[2]);
      filename  = argv[3];

      printf("Blocksize: %u\n",blocksize);
      printf("Shiftsize: %u\n",shiftsize);
      printf("Filename : %s\n",filename);

      b1 = malloc(blocksize * sizeof(char));
      b2 = malloc(blocksize * sizeof(char));
      if ((b1 == NULL) || (b2 == NULL)) {
            printf("Unable to allocate buffers");
            exit(-1);
      }

      infile = fopen(filename, "r");
      if (infile == NULL) {
            printf("Unable to open file '%s'\n",filename);
            exit(-1);
      }

      while ((len = read(fileno(infile),b1,blocksize)) > 0) {
            totalbytes += len;

            if (shiftsize >= len)
                  shiftsize = len - 1;

            shift_buffer(b1,b2,len,shiftsize);

            matches += byte_match(b1,b2,len);

            /* reset the buffers */
            memset(b1, 0, blocksize);
            memset(b2, 0, blocksize);
      }
      fclose(infile);

      printf("\n%u matches from %u total bytes.\n",matches,totalbytes);
      ioc = (float)matches / (float)totalbytes;
      printf("Index of Coincidence: %f (%.2f%%) -- lower means more random.\n\n",ioc,ioc*100);

      return 0;
}
예제 #2
0
static bool extract_exactly(Client *c, void *dst, size_t bytes)
{
	if (c->bufrecved < bytes) return false;

	memcpy(dst, c->buf, bytes);
	shift_buffer(c, bytes);
	return true;
}
예제 #3
0
static int extract_nomore(Client *c, void *dst, size_t bytes)
{
	if (c->bufrecved < bytes) bytes = c->bufrecved;
	
	memcpy(dst, c->buf, bytes);
	shift_buffer(c, bytes);

	return bytes;
}
예제 #4
0
파일: getcwd.c 프로젝트: lherrada/C
char *getcwd(char *filename) {

 struct stat buf_child;
 struct dev_ino *root_dev_ino=(struct dev_ino *)malloc(sizeof(struct dev_ino));;
 get_root_dev_ino(root_dev_ino);
 char buf[256]; 

 while(1) {

 DIR *dirp;
 stat(".",&buf_child);
 ino_t dir_inode=getinode_number(&buf_child);

 if (SAME_INODE(buf_child,*root_dev_ino)) {
    filename[strlen(filename)-1]='\0';
    memset((void *)buf,0,256);
    shift_buffer(filename,buf);
    break;
    }
  dirp = opendir("..");
 struct dirent const *dp;

//Looking for name entry for inode found in current directory "."
  while ( (dp=readdir(dirp)) !=NULL) {
   if (dp->d_ino == dir_inode) {
     memset((void *)buf,0,256);
     strcpy(buf,dp->d_name);
   break;
    }
   }
shift_buffer(filename,buf);

 closedir(dirp);
 chdir("..");
 }
 return(filename);
}
예제 #5
0
void PhotonMap::send_photons() {
    printf("Each photon used %ld bytes\n",sizeof(Photon));
    std::vector<std::vector<Photon> > raw_photons(MAX_THREADS);
    #pragma omp parallel for
    for(unsigned int j=0; j<MAX_THREADS; j++) {
        raw_photons[j].reserve(scene->num_lights()*PHOTON_COUNT*(MAX_PHOTON_DEPTH+1));
        for(unsigned int i=0; i<scene->num_lights(); i++) {
            SphereLight light = scene->get_lights()[i];
            Color3 c=light.color;
            real_t prob=montecarlo(c);
            unsigned int photonCount=PHOTON_COUNT*prob/MAX_THREADS;
            printf("Sending %d photons.\n",photonCount);
            for(unsigned int k=0; k<photonCount; k++) {
                Ray ray;
                ray.d = random_sphere_indexed(k,photonCount);
                ray.e = light.position+random_sphere()*light.radius;
                trace_photon(raw_photons[j],c,ray,MAX_PHOTON_DEPTH);
            }
        }
    }
    std::vector<size_t> offsets(raw_photons.size());
    size_t total=0;
    for(size_t i=0; i<raw_photons.size(); i++) {
        offsets[i]=total;
        total+=raw_photons[i].size();
        raw_photons[i].shrink_to_fit();
    }
    printf("Made %ld photons\n",total);
    delete all_raw_photons;
    all_raw_photons = new std::vector<Photon>(total);
    for(size_t i=0; i<raw_photons.size(); i++) {
        std::vector<Photon>::iterator dest=all_raw_photons->begin()+offsets[i];
        while(raw_photons[i].size()>0) {
            assert(dest<all_raw_photons->end());
            //rough attempt at page-aligning during copy
            size_t copySize=1024*1024;
            size_t extra=raw_photons[i].size()%copySize;
            if(extra!=0) {
                copySize=extra;
            }
            shift_buffer(&(raw_photons[i]),dest,copySize);
        }
    }
    printf("Collected %ld photons\n",all_raw_photons->size());
    //TODO: organize the photons into some sort of kd-tree
}
예제 #6
0
/*
// Name: read_msg_in_buffer
// In: c, the client connection to read from.
// 	   return_msg, the message which was parsed from the buffer.
// Out: Positive number if a message was read otherwise a negative number.
// Purpose: Reads from the client buffer until the first occurence of a \n. 
*/
int read_msg_in_buffer(clientconn_t *c, char *return_msg) {
	char read_buf[MAXBUFSIZE];
	memset(read_buf, '\0', MAXBUFSIZE);
	char *token = NULL;
	strcpy(read_buf, c->buf);
	// Read until \n.
	token = strtok(read_buf, "\n");
	if(token!=NULL) {
		// Copy the client buffer to the returning buffer
		strcpy(return_msg, token);
		// Shift the client buffer.
		shift_buffer(c->buf, strlen(token));
		return strlen(token);
	} else {
		memset(c->buf, '\0', MAXBUFSIZE);
	}
	return -1;
}