Exemple #1
0
int main(int argc, char* argv[]) {
	int fd;
	ssize_t ret;
	char buf[BUFLEN];
	int len = BUFLEN;
	if (argc != 2) {
		myerr("usage: ./a.out filename");
	}
	fd = open(argv[1], O_RDWR | O_APPEND);
	if (fd == -1) {
		myerr("open error");
	}
	printf("fd = %d\n",fd);
	while ((ret = read(fd, buf, len)) != 0) {
		if (ret == -1) {
			if (errno == EINTR)
				continue;
			perror("read");
			break;
		}
		write(1,buf,ret);
	}

	close(fd);
	return 0;
}
Exemple #2
0
static bool seek_to_regname(FILE *f, const char *regname)
{
	char str[1024];

	while (true) {
		fpos_t pos;
		int r;

		r = fgetpos(f, &pos);
		if (r)
			myerr2("fgetpos failed");

		if (!fgets(str, sizeof(str), f))
			return false;

		char *parts[3] = { 0 };

		r = split_str(str, ",", parts, 3);
		if (r != 3)
			myerr("Failed to parse register description: '%s'", str);

		if (strcmp(regname, parts[0]) == 0) {
			r = fsetpos(f, &pos);
			if (r)
				myerr2("fsetpos failed");

			return true;
		}

		if (!seek_to_next_reg(f))
			return false;
	}

	return false;
}
Exemple #3
0
void parse_base(const char *cfgfile, const char *basestr, uint64_t *base,
		const char **regfile)
{
	char *endptr;

	*base = strtoull(basestr, &endptr, 0);
	if (*endptr == 0) {
		regfile = NULL;
		return;
	}

	char path[256];

	if (!cfgfile) {
		const char *home = getenv("HOME");
		if (!home)
			myerr("No $HOME");

		sprintf(path, "%s/.rwmem/%s", home, "rwmemrc");
	} else {
		strcpy(path, cfgfile);
	}

	find_base_address(path, basestr, base, regfile);

	/* regfile is relative to the cfgfile, so fix the path */
	strcpy(rindex(path, '/') + 1, *regfile);

	*regfile = strdup(path);
}
Exemple #4
0
void hashInit()
{
  double A, B;

  /* empty hash list */
  newhashlist = NULL;

  /* for the multifield key, need these global vars */
  keylen = offsetof(struct_cp, k) + sizeof(int) - offsetof(struct_cp, i);
  lookup_key = malloc(sizeof(*lookup_key));
  memset(lookup_key, 0, sizeof(*lookup_key));  /* zero fill */

  /* Some checking for bounds.  Can't check if an integer is larger
     than LONG_MAX so use floating points. */
  /*
  dbg_printf(10, "KEYMULTFAC^3 = %llu, LONG_MAX = %llu,%llu,%llu,%llu\n", \
	     KEYMULTFAC*KEYMULTFAC*KEYMULTFAC, ULONG_MAX, ULLONG_MAX, LONG_MAX, LLONG_MAX);
  dbg_printf(10, "KEYMULTFAC = %d, KEYMULTFAC^3 = %ld, KEYMULTFAC^3 = %llu\n", \
	     KEYMULTFAC, KEYMULTFAC*KEYMULTFAC*KEYMULTFAC, KEYMULTFAC*KEYMULTFAC*KEYMULTFAC);
  */
  A = (double) KEYMULTFAC;
  A = A*A*A;
  B = (double) ULLONG_MAX;
  dbg_printf(10, "(in FP) KEYMULTFAC^3 = %g < ULLONG_MAX = %g\n", A, B);
  dbg_printf(99, "sizeof(unsigned int)=%d\n", sizeof(unsigned int));
  dbg_printf(99, "sizeof(unsigned long)=%d\n", sizeof(unsigned long));
  dbg_printf(99, "sizeof(unsigned long long)=%d\n", sizeof(unsigned long long));
  if (0.95*A > B) {
    myerr("not enough space in unsigned long long int");
  }
}
Exemple #5
0
void
copy_file(char const *src, char const *dst)
{
  if (link(src, dst) >= 0) return;

  if (errno != EXDEV && errno != EPERM)
    myerr("link(%s, %s) failed: %s", src, dst, strerror(errno));

  {
    int fdr, fdw;
    unsigned char buf[4096], *p;
    int r, w;

    if ((fdr = open(src, O_RDONLY, 0)) < 0)
      myerr("open `%s' for read failed: %s", src, strerror(errno));
    if ((fdw = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
      myerr("open `%s' for write failed: %s", dst, strerror(errno));

    while (1) {
      r = read(fdr, buf, sizeof(buf));
      if (r < 0) myerr("read failed: %s", strerror(errno));
      if (!r) break;
      p = buf;
      while (r) {
        w = write(fdw, p, r);
        if (r <= 0) myerr("write failed: %s", strerror(errno));
        p += w;
        r -= w;
      }
    }

    if (close(fdr) < 0) myerr("close failed: %s", strerror(errno));
    if (close(fdw) < 0) myerr("close failed: %s", strerror(errno));
  }
}
Exemple #6
0
void
cat_file(char *path)
{
  FILE *f;
  int c;

  if (!(f = fopen(path, "r"))) myerr("cannot open %s", path);
  while ((c = getc(f)) != EOF) fputc(c, stderr);
  fclose(f);
}
Exemple #7
0
/*
 * Find 'basestr' from the given file.
 * Return found base address and (optional) register file name
 */
static void find_base_address(const char *path, const char *basestr,
			      uint64_t *base, const char **regfile)
{
	char str[256];
	int r;

	FILE *f = fopen(path, "r");
	if (f == NULL)
		myerr2("Failed to open '%s'", path);

	size_t arglen = strlen(basestr);

	while (fgets(str, sizeof(str), f)) {
		if (str[0] == 0 || isspace(str[0]))
			continue;

		if (strncmp(basestr, str, arglen) != 0)
			continue;

		if (!isblank(str[arglen]))
			continue;

		r = sscanf(str, "%*s %" SCNx64 " %ms", base, regfile);
		if (r == 2) {
			fclose(f);
			return;
		}

		regfile = NULL;

		r = sscanf(str, "%*s %" SCNx64 "", base);
		if (r == 1) {
			fclose(f);
			return;
		}

		myerr("Failed to parse offset");
	}

	myerr("Failed to find base");
}
Exemple #8
0
void
clean_dir(char *path)
{
  DIR *d;
  struct dirent *e;
  char buf[1024];

  while (1) {
    if (!(d = opendir(path))) myerr("cannot open dir %s", path);
    while ((e = readdir(d))) {
      if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
      break;
    }
    if (!e) break;
    snprintf(buf, sizeof(buf), "%s/%s", path, e->d_name);
    closedir(d);
    //fprintf(stderr, "remove %s\n", buf);
    if (unlink(buf) < 0) myerr("unlink failed: %s", strerror(errno));
  }
  closedir(d);
}
Exemple #9
0
void write_chat2(struct data_bag bag)//私聊聊天信息记录1
{
	int fd;
	if((fd=open(bag.targetname,O_RDWR|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR))==-1)
		myerr("open",__LINE__);
	write_time(bag.targetname);
	write(fd,bag.name,strlen(bag.name)+1);
	write(fd,"悄悄地对你说:",20);
	write(fd,bag.buf,strlen(bag.buf)+1);
	write(fd,"\n",2);
	close(fd);
}
Exemple #10
0
void write_chat1(struct data_bag bag)//群聊聊天信息记录
{
	int fd;
	if((fd=open("group_chat",O_RDWR|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR))==-1)
		myerr("open",__LINE__);
	write_time("group_chat");
	write(fd,bag.name,strlen(bag.name)+1);
	write(fd,"说:",4);
	write(fd,bag.buf,strlen(bag.buf)+1);
	write(fd,"\n",2);
	close(fd);
}
Exemple #11
0
uint64_t readmem(void *addr, unsigned regsize)
{
	switch (regsize) {
	case 8:
		return *((uint8_t *)addr);
	case 16:
		return *((uint16_t *)addr);
	case 32:
		return *((uint32_t *)addr);
	case 64:
		return *((uint64_t *)addr);
	default:
		myerr("Illegal data regsize '%c'", regsize);
	}
}
Exemple #12
0
static void parse_reg_fields(FILE *f, struct reg_desc *reg)
{
	char str[1024];
	unsigned field_num = 0;
	int r;

	while (fgets(str, sizeof(str), f)) {
		unsigned fh, fl;

		if (str[0] == 0 || isspace(str[0]))
			break;

		char *parts[3] = { 0 };

		r = split_str(str, ",", parts, 3);
		if (r != 3)
			myerr("Failed to parse field description: '%s'", str);

		struct field_desc *fd = &reg->fields[field_num];

		fd->name = strdup(parts[0]);
		fh = strtoul(parts[1], NULL, 0);
		fl = strtoul(parts[2], NULL, 0);

		size_t len = strlen(fd->name);
		if (len > reg->max_field_name_len)
			reg->max_field_name_len = len;

		fd->low = fl;
		fd->high = fh;
		fd->width = fh - fl + 1;
		fd->mask = GENMASK(fh, fl);

		field_num++;
	}

	reg->num_fields = field_num;
}
Exemple #13
0
static bool seek_to_regaddr(FILE *f, uint64_t addr)
{
	char str[1024];

	while (true) {
		fpos_t pos;
		int r;

		r = fgetpos(f, &pos);
		if (r)
			myerr2("fgetpos failed");

		if (!fgets(str, sizeof(str), f))
			return false;

		char *parts[3] = { 0 };

		r = split_str(str, ",", parts, 3);
		if (r != 3)
			myerr("Failed to parse register description: '%s'", str);

		uint64_t a = strtoull(parts[1], NULL, 0);

		if (addr == a) {
			r = fsetpos(f, &pos);
			if (r)
				myerr2("fsetpos failed");

			return true;
		}

		if (!seek_to_next_reg(f))
			return false;
	}

	return false;
}
Exemple #14
0
static void
test(struct global * registry) {
    struct timeval timeout, now;
    fd_set sel_read, sel_except, sel_write;
    int i;

    registry->con = calloc(registry->concurrency, sizeof(struct connection));
    memset(registry->con, 0, registry->concurrency * sizeof(struct connection));

#ifdef AB_DEBUG
    printf("AB_DEBUG: start of test()\n");
#endif

    for (i = 0; i < registry->concurrency; i++) {
        registry->con[i].url = registry->ready_to_run_queue[i].url;
        registry->con[i].run = registry->ready_to_run_queue[i].run;
        registry->con[i].state = STATE_READY;
        registry->con[i].thread = registry->ready_to_run_queue[i].thread;
    }

#ifdef AB_DEBUG
    printf("AB_DEBUG: test() - stage 1\n");
#endif

    registry->stats = calloc(registry->number_of_urls, sizeof(struct data *));
    for (i = 0; i < registry->number_of_runs; i++) {
        int j;
        for (j = registry->position[i]; j < registry->position[i+1]; j++)
            registry->stats[j] = calloc(registry->repeats[i], sizeof(struct data));
    }

#ifdef AB_DEBUG
    printf("AB_DEBUG: test() - stage 2\n");
#endif

    FD_ZERO(&registry->readbits);
    FD_ZERO(&registry->writebits);

#ifdef AB_DEBUG
    printf("AB_DEBUG: test() - stage 3\n");
#endif

    /* ok - lets start */
    gettimeofday(&registry->starttime, 0);

#ifdef AB_DEBUG
    printf("AB_DEBUG: test() - stage 4\n");
#endif

    /* initialise lots of requests */

    registry->head = registry->concurrency;
    for (i = 0; i < registry->concurrency; i++)
        start_connect(registry, &registry->con[i]);

#ifdef AB_DEBUG
    printf("AB_DEBUG: test() - stage 5\n");
#endif

    while (registry->done < registry->need_to_be_done) {
        int n;

#ifdef AB_DEBUG
        printf("AB_DEBUG: test() - stage 5.1, registry->done = %d\n", registry->done);
#endif

        /* setup bit arrays */
        memcpy(&sel_except, &registry->readbits, sizeof(registry->readbits));
        memcpy(&sel_read, &registry->readbits, sizeof(registry->readbits));
        memcpy(&sel_write, &registry->writebits, sizeof(registry->writebits));

#ifdef AB_DEBUG
        printf("AB_DEBUG: test() - stage 5.2, registry->done = %d\n", registry->done);
#endif

        /* Timeout of 30 seconds, or minimum time limit specified by config. */
        timeout.tv_sec = registry->min_tlimit.tv_sec;
        timeout.tv_usec = registry->min_tlimit.tv_usec;
        n = select(FD_SETSIZE, &sel_read, &sel_write, &sel_except, &timeout);
#ifdef AB_DEBUG
        printf("AB_DEBUG: test() - stage 5.3, registry->done = %d\n", registry->done);
#endif
        if (!n)
            myerr(registry->warn_and_error, "Server timed out");
        if (n < 1)
            myerr(registry->warn_and_error, "Select error.");
#ifdef AB_DEBUG
        printf("AB_DEBUG: test() - stage 5.4, registry->done = %d\n", registry->done);
#endif
        /* check for time limit expiry */
        gettimeofday(&now, 0);
        if (registry->tlimit &&
            timedif(now, registry->starttime) > (registry->tlimit * 1000)) {
            char *warn = malloc(256 * sizeof(char));
            sprintf(warn, "Global time limit reached (%.2f sec), premature exit", registry->tlimit);
            myerr(registry->warn_and_error, warn);
            free(warn);
            registry->need_to_be_done = registry->done;        /* break out of loop */
        }

        for (i = 0; i < registry->concurrency; i++) {
            int s = registry->con[i].fd;
#ifdef AB_DEBUG
            printf("AB_DEBUG: test() - stage 5.5, registry->done = %d, i = %d\n", registry->done, i);
#endif
            if (registry->started[registry->con[i].url]
                > registry->finished[registry->con[i].url]) {
                struct connection * c = &registry->con[i];
                struct timeval url_now;

                /* check for per-url time limit expiry */
                gettimeofday(&url_now, 0);

#ifdef AB_DEBUG
                printf("AB_DEBUG: test() - stage 5.5.4, Time taken for current request = %d ms; Per-url time limit = %.4f sec; for run %d, url %d\n", timedif(url_now, c->start_time), registry->url_tlimit[c->url], c->run, c->url - registry->position[c->run]);
                printf("AB_DEBUG: test() - stage 5.5.5, registry->done = %d, i = %d\n", registry->done, i);
#endif
                if (registry->url_tlimit[c->url] &&
                    timedif(url_now, c->start_time) > (registry->url_tlimit[c->url] * 1000)) {
                    char *warn = malloc(256 * sizeof(char));
#ifdef AB_DEBUG
                    printf("AB_DEBUG: test() - stage 5.5.5.3, registry->done = %d, i = %d\n", registry->done, i);
#endif
                    sprintf(warn, "Per-url time limit reached (%.3f sec) for run %d, url %d, iteration %d; connection closed prematurely", registry->url_tlimit[c->url], c->run, c->url - registry->position[c->run], c->thread);
                    myerr(registry->warn_and_error, warn);
                    free(warn);

                    registry->failed[c->url]++;
                    close_connection(registry, c);
                    continue;
                }
            }

            if (registry->con[i].state == STATE_DONE)
                continue;
#ifdef AB_DEBUG
            printf("AB_DEBUG: test() - stage 5.6, registry->done = %d, i = %d\n", registry->done, i);
#endif
            if (FD_ISSET(s, &sel_except)) {
                registry->failed[registry->con[i].url]++;
                start_connect(registry, &registry->con[i]);
                continue;
            }
#ifdef AB_DEBUG
            printf("AB_DEBUG: test() - stage 5.7, registry->done = %d, i = %d\n", registry->done, i);
#endif
            if (FD_ISSET(s, &sel_read)) {
                read_connection(registry, &registry->con[i]);
                continue;
            }
#ifdef AB_DEBUG
            printf("AB_DEBUG: test() - stage 5.8, registry->done = %d, i = %d\n", registry->done, i);
#endif
            if (FD_ISSET(s, &sel_write))
                write_request(registry, &registry->con[i]);        
#ifdef AB_DEBUG
            printf("AB_DEBUG: test() - stage 5.9, registry->done = %d, i = %d\n", registry->done, i);
#endif
        }
    }

#ifdef AB_DEBUG
    printf("AB_DEBUG: test() - stage 6\n");
#endif

    gettimeofday(&registry->endtime, 0);
    if (strlen(registry->warn_and_error) == 28)
        myerr(registry->warn_and_error, "None.\n");
    else myerr(registry->warn_and_error, "Done.\n");
}
Exemple #15
0
int
main(int argc, char *argv[])
{
  FILE *f;
  char buf[1024];
  int pid, stat;
  struct stat ss;
  int fd;

  if (argc != 3) myerr("wrong number of arguments: %d", argc);

#if defined EJUDGE_LOCAL_DIR
  snprintf(emupath, sizeof(emupath), "%s/dosemu/run", EJUDGE_LOCAL_DIR);
#elif defined EJUDGE_CONTESTS_HOME_DIR
  snprintf(emupath, sizeof(emupath), "%s/dosemu/run", EJUDGE_CONTESTS_HOME_DIR);
#else
  snprintf(emupath, sizeof(emupath), "/home/judges/dosemu/run");
#endif

  atexit(cleanup_hnd);
  if (chmod(emupath, 0700) < 0) myerr("chmod failed: %s", strerror(errno));
  clean_dir(emupath);
  snprintf(buf, sizeof(buf), "%s/command.txt", emupath);
  if (!(f = fopen(buf, "w")))
    myerr("fopen w failed on %s: %s", buf, strerror(errno));
  fprintf(f,
          "\r\n"
          "\r\n"
          "output.txt\r\n"
          "errors.txt\r\n"
          "bcc\r\n"
          "-ml\r\n"
          "program.c\r\n");
  fclose(f);

  snprintf(buf, sizeof(buf), "%s/output.txt", emupath);
  if (!(f = fopen(buf, "w")))
    myerr("fopen failed on %s: %s", buf, strerror(errno));
  fclose(f);
  snprintf(buf, sizeof(buf), "%s/errors.txt", emupath);
  if (!(f = fopen(buf, "w")))
    myerr("fopen failed on %s: %s", buf, strerror(errno));
  fclose(f);

  snprintf(buf, sizeof(buf), "%s/program.c", emupath);
  copy_file(argv[1], buf);
  fflush(0);

  if ((pid = fork()) < 0) myerr("fork failed: %s", strerror(errno));
  if (!pid) {
    snprintf(buf, sizeof(buf), "%s/../bin/dos", emupath);
    if ((fd = open("/dev/null", O_RDONLY)) < 0)
      myerr("open(/dev/null failed: %s", strerror(errno));
    dup2(fd, 0);
    close(fd);
    if ((fd = open("/dev/null", O_WRONLY)) < 0)
      myerr("open(/dev/null failed: %s", strerror(errno));
    dup2(fd, 1);
    dup2(fd, 2);
    close(fd);
    execl(buf, buf, "-I", "keystroke \"\\r\" video { none } dpmi 4096", NULL);
    myerr("execl failed: %s", strerror(errno));
    _exit(1);
  }
  wait(&stat);
  if (WIFSIGNALED(stat)) myerr("dos terminated by signal");
  if (!WIFEXITED(stat)) myerr("dos terminated by unknown reason");
  if (WEXITSTATUS(stat)) myerr("dos exited with code %d", WEXITSTATUS(stat));

  snprintf(buf, sizeof(buf), "%s/output.txt", emupath);
  cat_file(buf);
  snprintf(buf, sizeof(buf), "%s/errors.txt", emupath);
  cat_file(buf);

  fprintf(stderr, "\n\n");

  snprintf(buf, sizeof(buf), "%s/retcode.txt", emupath);
  if (!(f = fopen(buf, "r"))) myerr("fopen %s failed: %s",buf,strerror(errno));
  if (fscanf(f, "%d", &stat) != 1)
    myerr("cannot parse retcode.txt");
  fscanf(f, " ");
  if (fgetc(f) != EOF) myerr("garbage in retcode.txt");
  fclose(f);

  if (stat != 0) {
    myerr("compilation process error code is %d", stat);
  }

  snprintf(buf, sizeof(buf), "%s/program.exe", emupath);
  if (lstat(buf, &ss) < 0) myerr("output file %s does not exist", buf);
  copy_file(buf, argv[2]);

  return 0;
}
Exemple #16
0
int main(int argc,char *argv[])
{
	struct data_bag bag;
	struct sockaddr_in client_addr,serv_addr;
	int sock_fd;
	char name[6];
	char buf[MAX_CHAT];
	int i;
	pthread_t thid;
	if((sock_fd=socket(AF_INET,SOCK_STREAM,0))<0)
		myerr("socket",__LINE__);
	memset(&serv_addr,0,sizeof(struct sockaddr_in));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(4507);
	if(inet_aton(argv[1],&serv_addr.sin_addr)<0)
		myerr("inet_aton",__LINE__);
	if(connect(sock_fd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr_in))<0)
		myerr("connect",__LINE__);
	time_get();
	printf("已链接上服务器。。。\n");
//	printf("请输入昵称:");
//	fflush(stdin);
//	scanf("%s",bag.name);
//	getchar();
	bag=screen(sock_fd);
	sleep(1);
	printf("登陆成功,开始聊天!\n");
	printf("tips:默认进入群聊模式,私聊输入“-昵称:内容”\n");
	printf("输入“lc”可查看该用户的私聊聊天记录\n");
	printf("输入“l”可查看群聊聊天记录");
	printf("输入“lo”可查看当前在线用户列表\n");
	printf("输入“exit”可退出客户端\n");
	if(pthread_create(&thid,NULL,(void *)rec,(void *)&sock_fd)!=0)
		myerr("pthread_create",__LINE__);
	while(1)
	{
		fflush(stdin);
		scanf("%s",bag.buf);
		if(bag.buf[0]=='-')
		{
			bag.flag=1;
			for(i=0;i<100;i++)
				if(bag.buf[i]==':')
					break;
			if(i!=100)
				send(sock_fd,(void *)&bag,sizeof(bag),0);
			else
				printf("输入不符合规范\n");
				

		}
		else if(strcmp(bag.buf,"lc")==0)
		{
			read_chat(bag);
		}
		else if(strcmp(bag.buf,"l")==0)
		{
			system("cat group_chat");
		}
		else if(strcmp(bag.buf,"lo")==0)
		{
			bag.flag=4;
			send(sock_fd,(void *)&bag,sizeof(bag),0);
			sleep(1);
		}
		else if(strcmp(bag.buf,"exit")==0)
		{
			printf("已成功退出聊天!\n");
			exit(0);
		}
		else if((strcmp(bag.buf,"\0")!=0)&&(strlen(bag.buf)<=MAX_CHAT))
		{
			bag.flag=0;
			send(sock_fd,(void *)&bag,sizeof(bag),0);
			write_chat1(bag);
		}
		else if(strlen(bag.buf)>MAX_CHAT)
			printf("字数过长,请分次输入!\n");
		else
			printf("不能输入空消息\n");
	}
	return 0;
}