예제 #1
0
static void Serverlist_Sort (void)
{
	int i;
	int remap = 0;
	char addr[32];

	int (*comparefunc)(const void *, const void *) = NULL;

	switch((int)Cvar_Get("serverlist_order","0",0)->value)
	{
	case 1:
		comparefunc = &Serverlist_SortCompare_Name;
		break;
	case 2:
		comparefunc = &Serverlist_SortCompare_Ping;
		break;
	case 3:
		comparefunc = &Serverlist_SortCompare_Mapname;
		break;
	case 4:
		comparefunc = &Serverlist_SortCompare_Players;
		break;
	default:
		comparefunc = &Serverlist_SortCompare;
	}

	qsort(m_serverlist.server, m_serverlist.numservers, sizeof(m_serverlist.server[0]), comparefunc);

	// viciouz - apparently the following doesn't work for alphabetical sorting. how odd.
	// anyway, as it's only running one frame the small efficiency saving isn't really necessary.
	// for (i = 0; i < m_serverlist.nummapped; ++i)
	for (i = 0; i < m_serverlist.numservers; ++i)
	{
		if (m_serverlist.server[i].remap >= 0)
		{
			if (remap < m_serverlist.nummapped) // probably unnecessary to check, but just in case
			{
				NetAdrToString(m_serverlist.server[i].adr, addr, sizeof(addr));
				m_serverlist.server[i].remap = remap;
				Z_Free(m_serverlist.ips[remap]);
				m_serverlist.ips[remap] = text_copy(addr);
				Z_Free(m_serverlist.info[remap]);
				m_serverlist.info[remap] = text_copy(format_info_from_serverlist_server(&m_serverlist.server[i]));
				++remap;
			}
		}
	}
}
예제 #2
0
static Object *
state_copy(State *state)
{
  int i;
  State *newstate;
  Element *elem, *newelem;
  Object *newobj;
  
  elem = &state->element;
  
  newstate = g_malloc(sizeof(State));
  newelem = &newstate->element;
  newobj = (Object *) newstate;

  element_copy(elem, newelem);

  newstate->text = text_copy(state->text);
  
  for (i=0;i<8;i++) {
    newobj->connections[i] = &newstate->connections[i];
    newstate->connections[i].object = newobj;
    newstate->connections[i].connected = NULL;
    newstate->connections[i].pos = state->connections[i].pos;
    newstate->connections[i].last_pos = state->connections[i].last_pos;
  }

  newstate->state_type = state->state_type;

  state_update_data(newstate);
  
  return (Object *)newstate;
}
예제 #3
0
파일: biniku.c 프로젝트: amayra/arc_conv
static int biniku_put(FILE *fi, FILE *fo) {
	uint a, i, n, p = 0, x = 0; void *r[2] = { 0, 0 };

	READ(&n, 4); fwrite(&n, 1, 4, fo);
	for(i = 0; i < n; i++) {
	  READ(&a, 4); fwrite(&a, 1, 4, fo);
	}
	n = n * 4 + 4;

	for(;;) {
	  if(!x) x = text_num();
	  // printf("replace %08X\n", x + n);
	  while(p != x) {
	    if((a = fgetc(fi)) == EOF) goto end; p++; fputc(a, fo);
	  }
	  x = text_copy(fo);
	  do { if((a = fgetc(fi)) == EOF) goto end; p++; } while(a);
	  fputc(0, fo);

	  reloc_add(r, p, ftell(fo) - p - n);
	}
end:	if(r[0]) {
	  biniku_fix(fi, fo, r[1]);
	  blk_destroy(r[0]);
	}
	return 0;
}
예제 #4
0
파일: flow.c 프로젝트: brunetton/dia
static DiaObject *
flow_copy(Flow *flow)
{
  Flow *newflow;
  Connection *conn, *newconn;
  DiaObject *newobj;
  
  conn = &flow->connection;
  
  newflow = g_malloc0(sizeof(Flow));
  newconn = &newflow->connection;
  newobj = &newconn->object;

  connection_copy(conn, newconn);

  newflow->text_handle = flow->text_handle;
  newflow->text_handle.connected_to = NULL;
  newobj->handles[2] = &newflow->text_handle;
  newflow->textpos = flow->textpos;
  newflow->text = text_copy(flow->text);
  newflow->type = flow->type;

  flow_update_data(newflow);
  return &newflow->connection.object;
}
예제 #5
0
int cavan_inotify_register_watch(struct cavan_inotify_descriptor *desc, const char *pathname, uint32_t mask, void *data)
{
	int wd;
	struct cavan_inotify_watch *p;

	if (desc->watch_count >= NELEM(desc->watchs))
	{
		error_msg("Too match watch");
		return -EFAULT;
	}

	if (pathname == NULL || *pathname == 0)
	{
		error_msg("pathname == NULL || *pathname == 0");
		return -EINVAL;
	}

	wd = inotify_add_watch(desc->fd, pathname, mask);
	if (wd < 0)
	{
		print_error("inotify_add_watch");
		return wd;
	}

	for (p = desc->watchs; p->wd >= 0; p++);

	p->wd = wd;
	p->data = data;
	text_copy(p->pathname, pathname);
	desc->watch_count++;

	pr_green_info("Add watch: %s", pathname);

	return 0;
}
예제 #6
0
static Object *
smallpackage_copy(SmallPackage *pkg)
{
  int i;
  SmallPackage *newpkg;
  Element *elem, *newelem;
  Object *newobj;
  
  elem = &pkg->element;
  
  newpkg = g_malloc(sizeof(SmallPackage));
  newelem = &newpkg->element;
  newobj = (Object *) newpkg;

  element_copy(elem, newelem);

  newpkg->text = text_copy(pkg->text);
  
  for (i=0;i<8;i++) {
    newobj->connections[i] = &newpkg->connections[i];
    newpkg->connections[i].object = newobj;
    newpkg->connections[i].connected = NULL;
    newpkg->connections[i].pos = pkg->connections[i].pos;
    newpkg->connections[i].last_pos = pkg->connections[i].last_pos;
  }

  smallpackage_update_data(newpkg);
  
  return (Object *)newpkg;
}
예제 #7
0
파일: function.c 프로젝트: krattai/monoflow
static DiaObject *
function_copy(Function *pkg)
{
  int i;
  Function *newpkg;
  Element *elem, *newelem;
  DiaObject *newobj;
  
  elem = &pkg->element;
  
  newpkg = g_malloc0(sizeof(Function));
  newelem = &newpkg->element;
  newobj = &newelem->object;

  element_copy(elem, newelem);

  newpkg->text = text_copy(pkg->text);
  
  for (i=0;i<NUM_CONNECTIONS;i++) {
    newobj->connections[i] = &newpkg->connections[i];
    newpkg->connections[i].object = newobj;
    newpkg->connections[i].connected = NULL;
    newpkg->connections[i].pos = pkg->connections[i].pos;
    newpkg->connections[i].last_pos = pkg->connections[i].last_pos;
    newpkg->connections[i].flags = pkg->connections[i].flags;
  }
  newpkg->is_wish = pkg->is_wish ;
  newpkg->is_user = pkg->is_user ;

  newpkg->element.extra_spacing.border_trans = pkg->element.extra_spacing.border_trans ;

  function_update_data(newpkg);
  
  return &newpkg->element.object;
}
예제 #8
0
파일: cftp.c 프로젝트: Jack19881218/cavan
static int cftp_client_send_directory(struct cftp_descriptor *desc, const char *dir_in, const char *dir_out)
{
	DIR *dp;
	struct dirent *entry;
	int ret;
	char tmp_file_in[256], tmp_file_out[256];
	char *p_in, *p_out;

	dp = opendir(dir_in);
	if (dp == NULL)
	{
		print_error("fail to open directory %s", dir_in);
		return -EFAULT;
	}

	p_in = text_path_cat(tmp_file_in, sizeof(tmp_file_in), dir_in, NULL);
	p_out = text_path_cat(tmp_file_out, sizeof(tmp_file_out), dir_out, NULL);

	while ((entry = readdir(dp)))
	{
		if (text_is_dot_name(entry->d_name))
		{
			continue;
		}

		text_copy(p_in, entry->d_name);
		text_copy(p_out, entry->d_name);

		ret = cftp_client_send_file(desc, tmp_file_in, 0, tmp_file_out, 0, 0);
		if (ret < 0)
		{
			goto out_close_dir;
		}
	}

	ret = 0;

out_close_dir:
	closedir(dp);

	return ret;
}
예제 #9
0
int property_get(const char *key, char *value, const char *default_value)
{
	int length;

	length = __system_property_get(key, value);
	if (length < 0 && default_value) {
		return text_copy(value, default_value) - value;
	}

	return length;
}
예제 #10
0
파일: uevent.c 프로젝트: FuangCao/cavan
char *uevent_get_property_base(char *props[], int prop_count, const char *prefix, char *buff)
{
	for (prop_count--; prop_count >= 0; prop_count--) {
		if (text_lhcmp(prefix, props[prop_count]) == 0) {
			text_copy(buff, props[prop_count] + text_len(prefix));

			return buff;
		}
	}

	return NULL;
}
예제 #11
0
// Color all the items grey:
static void grey_serverlist (void)
{
	char *str;
	int i;

	for(i=0; i<m_serverlist.nummapped; i++)
	{
		if ((unsigned char)m_serverlist.info[i][0] != SCHAR_COLOR)
		{
			str = text_copy(va("%c4%s", SCHAR_COLOR, m_serverlist.info[i]));
			Z_Free(m_serverlist.info[i]);
			m_serverlist.info[i] = str;
		}
	}
}
예제 #12
0
int			get_next_line(int const fd, char **line)
{
	static char	*offset = NULL;
	int			ret;

	if (!(line))
		return (-1);
	if (!offset)
		offset = ft_strnew(0);
	if ((ret = text_copy(fd, &offset)) == -1)
		return (-1);
	offset = get_line(offset, line);
	if ((!ret && !ft_strlen(offset) && !ft_strlen(*line)))
		return (0);
	else
		return (1);
}
예제 #13
0
파일: cache.c 프로젝트: Jack19881218/cavan
int file_cache_init(struct file_cache *cache, const char *pathname, int flags, mode_t mode)
{
	int ret;
	int fd;

	if (cache == NULL)
	{
		return -EINVAL;
	}

	fd = open(pathname, flags, mode);
	if (fd < 0)
	{
		return fd;
	}

	if (cache->cache_blocks == 0)
	{
		cache->cache_blocks = DEFAULT_CACHE_BLOCKS;
	}

	if (cache->block_size == 0)
	{
		cache->block_size = DEFAULT_BLOCK_SIZE;
	}

	ret = ffile_cache_init(cache, cache->cache_blocks * cache->block_size, cache->cache_blocks * cache->block_size);
	if (ret < 0)
	{
		goto out_close_fd;
	}

	cache->fd = fd;
	cache->start_block = 0;
	cache->read_pointer = cache->write_pointer = cache->start_block;
	text_copy(cache->pathname, pathname);

	return 0;

out_close_fd:
	close(fd);

	return ret;
}
예제 #14
0
파일: cftp.c 프로젝트: FuangCao/cavan
static ssize_t cftp_send_file_reuest(struct cftp_descriptor *desc, struct cftp_file_request *req, const char *filename, struct stat *st, u32 offset, u32 size, int read)
{
	ssize_t sendlen;

	if (read) {
		req->type = CFTP_PACKAGE_FILE_READ;
	} else {
		req->type = CFTP_PACKAGE_FILE_WRITE;
		req->st_mode = st->st_mode;
		req->st_rdev = st->st_rdev;
	}

	req->size = size;
	req->offset = offset;

	sendlen = text_copy(req->filename, filename) - (char *) req + 1;

	return cftp_send_data_retry(desc, req, sendlen, desc->retry_count);
}
예제 #15
0
static char *get_bt_mac_file(const char *system_mnt_point, char *pathname, size_t size)
{
	unsigned int i;
	const char *bt_mac_files[] = {I600_BT_MAC_FILE, I200_BT_MAC_FILE};
	char *name_p;

	name_p = text_path_cat(pathname, size, system_mnt_point, NULL);

	for (i = 0; i < ARRAY_SIZE(bt_mac_files); i++)
	{
		text_copy(name_p, bt_mac_files[i]);

		if (access(pathname, W_OK) == 0)
		{
			return pathname;
		}
	}

	return NULL;
}
예제 #16
0
static Object *
rendobj_copy(RenderObject *rend_obj)
{
  int i;
  RenderObject *newrend_obj;
  Element *elem, *newelem;
  Object *newobj;
  
  elem = &rend_obj->element;
  
  newrend_obj = g_malloc(sizeof(RenderObject));
  newelem = &newrend_obj->element;
  newobj = &newelem->object;

  element_copy(elem, newelem);

  newrend_obj->desc = rend_obj->desc;
  newrend_obj->magnify = rend_obj->magnify;
  if (rend_obj->desc->use_text) {
    newrend_obj->text = text_copy(rend_obj->text);
  } else {
    newrend_obj->text = NULL;
  }
  
  newrend_obj->connections = g_new(ConnectionPoint,
				   rend_obj->desc->num_connection_points);
  
  for (i=0;i<rend_obj->desc->num_connection_points;i++) {
    newobj->connections[i] = &newrend_obj->connections[i];
    newrend_obj->connections[i].object = newobj;
    newrend_obj->connections[i].pos = rend_obj->connections[i].pos;
    newrend_obj->connections[i].last_pos = rend_obj->connections[i].last_pos;
    newrend_obj->connections[i].connected = NULL;
  }

  rendobj_update_data(newrend_obj);

  return (Object *)newrend_obj;  
}
예제 #17
0
파일: kogado.c 프로젝트: amayra/arc_conv
static int kogado_put(FILE *fi, FILE *fo) {
	uint a, i, n, t[13], p, x = 0;
	READ(t, 0x34);
	if(memcmp(t, "SR01", 4)) return -1;
	fwrite(t, 1, 0x34, fo);
	for(i = 0x34; i < t[10]; i++) {
	  LODSB; fputc(a, fo);
	}
	for(i = 0; i < t[12]; i++) {
	  if(!x) x = text_num();
	  if(i == x) {
	    if(fread(&a, 1, 2, fi) != 2) break;
	    n = (ushort)a;
	    if(n < 2) break; n -= 2;
	    if(n) do { LODSB; } while(--n);
	    a = ftell(fo);
	    fwrite(&n, 1, 2, fo);
	    x = text_copy(fo);
	    fputc(0, fo);
	    n = ftell(fo) - a;
	    if(n & 1) { n++; fputc(0, fo); }
	    if(fseek(fo, a, SEEK_SET)) return -1;
	    fwrite(&n, 1, 2, fo);
	    if(fseek(fo, a + n, SEEK_SET)) return -1;
	  } else {
	    if(fread(&a, 1, 2, fi) != 2) break;
	    fwrite(&a, 1, 2, fo);
	    n = (ushort)a;
	    if(n < 2) break; n -= 2;
	    if(n) do { LODSB; fputc(a, fo); } while(--n);
	  }
	}
	a = ftell(fo);
	t[1] = a;
	t[11] = a - t[10];
	if(fseek(fo, 0, SEEK_SET)) return -1;
	fwrite(t, 1, 0x34, fo);
	return 0;
}
예제 #18
0
int bootimg_unpack(const char *input, const char *output, bool dt_support)
{
	int fd;
	int ret;
	int count;
	char *filename;
	char pathname[1024];
	struct bootimg_header hdr;
	struct bootimg_image images[4];

	fd = open(input, O_RDONLY);
	if (fd < 0)
	{
		pr_error_info("open file `%s'", input);
		return fd;
	}

	ret = bootimg_read_header(fd, &hdr);
	if (ret < 0)
	{
		pr_red_info("bootimg_read_header");
		goto out_close_fd;
	}

	bootimg_header_dump(&hdr);

	filename = text_copy(pathname, output);
	ret = mkdir_hierarchy(pathname, 0777);
	if (ret < 0)
	{
		pr_red_info("mkdir_hierarchy");
		goto out_close_fd;
	}

	*filename++ = '/';

	count = 0;

	if (hdr.kernel_size > 0)
	{
		images[count].size = hdr.kernel_size;
		images[count++].name = FILE_KERNEL_NAME;
	}

	if (hdr.ramdisk_size > 0)
	{
		images[count].size = hdr.ramdisk_size;
		images[count++].name = FILE_RAMDISK_NAME;
	}

	if (hdr.second_size > 0)
	{
		images[count].size = hdr.second_size;
		images[count++].name = FILE_SECOND_NAME;
	}
	else
	{
		strcpy(filename, FILE_SECOND_NAME);
		unlink(filename);
	}

	if (hdr.dt_size > 0 && dt_support)
	{
		images[count].size = hdr.dt_size;
		images[count++].name = FILE_DT_NAME;
	}
	else
	{
		strcpy(filename, FILE_DT_NAME);
		unlink(filename);
	}

	images[count].size = 0;
	images[count++].name = FILE_REMAIN_NAME;

	if (count > 0)
	{
		struct bootimg_image *p, *p_end;

		for (p = images, p_end = p + count; p < p_end; p++)
		{
			strcpy(filename, p->name);
			println("%s -> %s", p->name, pathname);

			if (p->size > 0)
			{
				ret = cavan_file_seek_next_page(fd, hdr.page_size);
				if (ret < 0)
				{
					pr_red_info("cavan_file_seek_next_page");
					goto out_close_fd;
				}

				ret = file_ncopy2(fd, pathname, p->size, O_WRONLY | O_TRUNC | O_CREAT, 0777);
			}
			else
			{
				ret = file_copy2(fd, pathname, O_WRONLY | O_TRUNC | O_CREAT, 0777);
			}

			if (ret < 0)
			{
				pr_red_info("file_copy2");
				goto out_close_fd;
			}
		}
	}

	if (hdr.cmdline[0])
	{
		strcpy(filename, FILE_CMDLINE_TXT);
		println("cmdline -> %s", pathname);

		if (hdr.extra_cmdline[0])
		{
			char *p;
			char buff[sizeof(hdr.cmdline) + sizeof(hdr.extra_cmdline)];

			p = text_copy(buff, (char *) hdr.cmdline);
			p = text_copy(p, (char *) hdr.extra_cmdline);

			ret = file_writeto(pathname, buff, p - buff, 0, O_TRUNC);
			if (ret < 0)
			{
				pr_red_info("file_writeto");
				goto out_close_fd;
			}
		}
		else
		{
			ret = file_writeto(pathname, hdr.cmdline, strlen((char *) hdr.cmdline), 0, O_TRUNC);
			if (ret < 0)
			{
				pr_red_info("file_writeto");
				goto out_close_fd;
			}
		}
	}

	strcpy(filename, FILE_REPACK_SH);

	ret = bootimg_gen_repack_script(&hdr, pathname, dt_support);
	if (ret < 0)
	{
		pr_red_info("bootimg_gen_pack_script");
		goto out_close_fd;
	}

	strcpy(filename, FILE_CONFIG_TXT);

	ret = bootimg_write_config_file(&hdr, pathname);
	if (ret < 0)
	{
		pr_red_info("bootimg_write_config_file");
		goto out_close_fd;
	}

	ret = 0;
out_close_fd:
	close(fd);
	return ret;
}
예제 #19
0
int package(const char *pkg_name, const char *dir_name)
{
	int ret;
	int pkg_fd;
	struct swan_package_info pkg_info;
	struct swan_file_info file_info;
	char tmp_path[1024], *name_p;
	int shrink_image_table[] = {
		SWAN_IMAGE_SYSTEM,
		SWAN_IMAGE_RECOVERY,
		SWAN_IMAGE_USERDATA,
		SWAN_IMAGE_VENDOR,
	};
	struct swan_image_info *p, *p_end;

	name_p = text_path_cat(tmp_path, sizeof(tmp_path), dir_name, NULL);

	if (swan_machine_type == SWAN_BOARD_UNKNOWN) {
		text_copy(name_p, "system.img");
		swan_machine_type = get_swan_board_type_by_system_img(tmp_path);
	}

	if (swan_machine_type == SWAN_BOARD_UNKNOWN) {
		pr_err_info("Machine type no set");
	} else {
		struct swan_image_info *img_info;

		img_info = get_swan_image_info_by_type(SWAN_IMAGE_LOGO);
		if (img_info) {
			text_copy(img_info->filename, get_logo_name_by_board_type(swan_machine_type));
		}

		img_info = get_swan_image_info_by_type(SWAN_IMAGE_BUSYBOX);
		if (img_info) {
			text_copy(img_info->filename, get_busybox_name_by_board_type(swan_machine_type));
		}
	}

	pkg_fd = open(pkg_name, O_RDWR | O_CREAT | O_SYNC | O_TRUNC | O_BINARY, 0777);
	if (pkg_fd < 0) {
		pr_err_info("open file \"%s\"", pkg_name);
		return -1;
	}

	text_copy(name_p, HEADER_BIN_NAME);
	ret = write_upgrade_program(pkg_fd, &file_info, tmp_path);
	if (ret < 0) {
		pr_err_info("write_upgrade_program");
		goto out_close_pkg;
	}

	ret = lseek(pkg_fd, sizeof(pkg_info), SEEK_CUR);
	if (ret < 0) {
		pr_err_info("lseek");
		goto out_close_pkg;
	}

	ret = write_resource_image(pkg_fd, &pkg_info, dir_name, get_resource_name_by_board_type(swan_machine_type),
		RESOURCE_IMAGE_NAME, I600_RESOURCE_NAME, I200_RESOURCE_NAME, MODEM_RESOURCE_NAME, NULL);
	if (ret < 0) {
		pr_err_info("write_resource_image");
		goto out_close_pkg;
	}

	pkg_info.image_count = 0;

	for (p = swan_images, p_end = p + ARRAY_SIZE(swan_images); p < p_end; p++) {
		text_copy(name_p, p->filename);

		if (array_has_element(p->type, (int *) swan_exclude_images, swan_exclude_image_count) || file_test(tmp_path, "r") < 0) {
			println_cyan("exclude image \"%s\"", tmp_path);
			continue;
		}

		if (swan_need_shrink && array_has_element(p->type, shrink_image_table, ARRAY_SIZE(shrink_image_table))) {
			ret = swan_shrink_image(dir_name, p);
			if (ret < 0) {
				pr_err_info("image_shrink");
				goto out_close_pkg;
			}
		}

		ret = write_simple_image(pkg_fd, dir_name, p, &swan_emmc_part_table);
		if (ret < 0) {
			pr_err_info("write_image");
			goto out_close_pkg;
		}

		pkg_info.image_count++;
	}

	pkg_info.crc32 = 0;
	ret = ffile_crc32_seek(pkg_fd, sizeof(file_info) + file_info.header_size + sizeof(pkg_info), 0, &pkg_info.crc32);
	if (ret < 0) {
		pr_err_info("ffile_crc32");
		goto out_close_pkg;
	}

	strncpy(pkg_info.volume, swan_vfat_volume, sizeof(pkg_info.volume));
	pkg_info.mkfs_mask = swan_mkfs_mask;
	pkg_info.board_type = swan_machine_type;
	pkg_info.upgrade_flags = swan_upgrade_flags;
	pkg_info.part_table = swan_emmc_part_table;

	show_package_info(&pkg_info);

	ret = lseek(pkg_fd, sizeof(file_info) + file_info.header_size, SEEK_SET);
	if (ret < 0) {
		pr_err_info("lseek");
		goto out_close_pkg;
	}

	ret = write_package_info(pkg_fd, &pkg_info);
	if (ret < 0) {
		pr_err_info("write_package_info");
		goto out_close_pkg;
	}

	ret = swan_set_md5sum(pkg_name);
	if (ret < 0) {
		pr_err_info("swan_set_md5sum");
	}

out_close_pkg:
	close(pkg_fd);

	if (ret < 0) {
		remove(pkg_name);
		return ret;
	}

	return 0;
}
예제 #20
0
파일: ftp.c 프로젝트: Jack19881218/cavan
static int ftp_list_directory1(const char *dirpath, const char *newline)
{
	int fd;
	DIR *dp;
	int ret;
	struct dirent *ep;
	char *filename;
	char pathname[1024];
	char buff[1024];
	char *buff_end = buff + sizeof(buff);

#if FTP_DEBUG
	pr_bold_info("dirpath = `%s'", dirpath);
#endif

	dp = opendir(dirpath);
	if (dp == NULL)
	{
		pr_red_info("opendir failed");
		return -EFAULT;
	}

	fd = cavan_temp_file_open(pathname, sizeof(pathname), "cavan-ftp-XXXXXX");
	if (fd < 0)
	{
		pr_red_info("cavan_temp_file_open");
		ret = fd;
		goto out_closedir;
	}

	filename = text_path_cat(pathname, sizeof(pathname), dirpath, NULL);

	while ((ep = readdir(dp)))
	{
		char *p;

		text_copy(filename, ep->d_name);

		p = ftp_file_stat_tostring(pathname, buff, buff_end);
		if (p == NULL)
		{
			continue;
		}

		p += snprintf(p, buff_end - p, " %s%s", ep->d_name, newline);

		ret = ffile_write(fd, buff, p - buff);
		if (ret < 0)
		{
			pr_red_info("ffile_write");
			goto out_close_fd;
		}
	}

	if (lseek(fd, 0, SEEK_SET) != 0)
	{
		pr_error_info("lseek");
		ret = -EFAULT;
		goto out_close_fd;
	}

	closedir(dp);
	return fd;

out_close_fd:
	close(fd);
out_closedir:
	closedir(dp);
	return ret;
}
예제 #21
0
파일: ftp.c 프로젝트: Jack19881218/cavan
static int ftp_client_receive_file(struct network_client *client, const char *ip, u16 port)
{
	int ret;
	int sockfd, data_sockfd;
	ssize_t rdlen;
	char buff[1024], *p;
	struct sockaddr_in addr;
	socklen_t addrlen;

	sockfd = inet_create_tcp_service(port);
	if (sockfd < 0)
	{
		pr_red_info("inet_create_tcp_service");
		return sockfd;
	}

	p = text_copy(buff, "PORT ");
	p = text_replace_char2(ip, p, '.', ',');
	p += sprintf(p, ",%d,%d\r\n", port >> 8, port & 0xFF);

	rdlen = ftp_client_send_command(client, buff, p - buff, buff, sizeof(buff));
	if (rdlen < 0)
	{
		pr_red_info("ftp_client_send_command");
		return rdlen;
	}

	rdlen = ftp_client_send_command(client, "LIST\r\n", 0, buff, sizeof(buff));
	if (rdlen < 0)
	{
		pr_red_info("ftp_client_send_command");
		return rdlen;
	}

	data_sockfd = inet_accept(sockfd, &addr, &addrlen);
	if (data_sockfd < 0)
	{
		pr_red_info("inet_accept");
		ret = data_sockfd;
		goto out_close_sockfd;
	}

	while (1)
	{
		rdlen = inet_recv(data_sockfd, buff, sizeof(buff));
		if (rdlen < 0)
		{
			pr_red_info("inet_recv");
			ret = rdlen;
			goto out_close_data_link;
		}

		if (rdlen == 0)
		{
			pr_green_info("data receive complete");
			break;
		}

#if FTP_DEBUG
		print_ntext(buff, rdlen);
#endif
	}

	ret = 0;

out_close_data_link:
	shutdown(data_sockfd, SHUT_RDWR);
	inet_close_tcp_socket(data_sockfd);
out_close_sockfd:
	inet_close_tcp_socket(sockfd);

	return ret;
}
예제 #22
0
파일: ftp.c 프로젝트: Jack19881218/cavan
static int ftp_service_cmdline(struct cavan_ftp_service *service, struct network_client *client)
{
	int fd;
	int ret;
	u32 command;
	char file_type;
	const char *reply;
	char rep_pasv[64];
	struct pollfd pfds[2];
	enum cavan_ftp_state state;
	struct network_url data_url;
	ssize_t wrlen, rdlen, replen;
	struct network_client data_link;
	struct network_service data_service;
	char abs_path[1024], curr_path[1024];
	char cmd_buff[1024], rep_buff[1024], *cmd_arg, *cmd_end;

	file_type = 0;
	data_link.sockfd = -1;
	data_service.sockfd = -1;
	network_url_init(&data_url, "tcp", NULL, 0, NULL);

	replen = 0;
	reply = "220 Cavan ftp server ready.\r\n";

	state = FTP_STATE_READY;
	text_copy(curr_path, service->home);

	pfds[0].fd = -1;
	pfds[1].fd = client->sockfd;
	pfds[0].events = pfds[1].events = POLLIN;

	while (1)
	{
		if (replen == 0)
		{
			replen = strlen(reply);
		}

#if FTP_DEBUG
		println("reply = %s", reply);
#endif

		wrlen = client->send(client, reply, replen);
		if (wrlen < 0)
		{
			pr_red_info("inet_send_text");
			goto out_close_data_link;
		}

		replen = 0;
		reply = rep_buff;

label_poll:
		ret = poll(pfds, NELEM(pfds), FTP_TIMEOUT_MS);
		if (ret < 1)
		{
			if (ret < 0)
			{
				pr_error_info("poll");
				goto out_close_data_link;
			}

			goto label_poll;
		}

		if (pfds[0].revents == POLLIN)
		{
			if (data_link.sockfd >= 0)
			{
				network_client_close(&data_link);
			}

			ret = network_service_accept(&data_service, &data_link);
			if (ret < 0)
			{
				pr_red_info("network_service_accept");
				goto out_network_service_close;
			}

			if (pfds[1].revents != POLLIN)
			{
				goto label_poll;
			}
		}

		rdlen = client->recv(client, cmd_buff, sizeof(cmd_buff) - 1);
		if (rdlen < 3)
		{
			goto out_close_data_link;
		}

		for (cmd_end = cmd_buff + rdlen - 1; cmd_end > cmd_buff && byte_is_lf(*cmd_end); cmd_end--);

		*++cmd_end = 0;

		for (cmd_arg = cmd_buff; cmd_arg < cmd_end && !byte_is_space(*cmd_arg); cmd_arg++);

		while ( cmd_arg < cmd_end && byte_is_space(*cmd_arg))
		{
			*cmd_arg++ = 0;
		}

#if FTP_DEBUG
		println("command = `%s', args = `%s'", cmd_buff, cmd_arg);
#endif

		command = *(u32 *) cmd_buff;

		switch (command)
		{
		case FTP_BUILD_CMD('q', 'u', 'i', 't'):
		case FTP_BUILD_CMD('Q', 'U', 'I', 'T'):
			network_client_send_text(client, "221 Goodbye.\r\n");
			return 0;

		case FTP_BUILD_CMD('u', 's', 'e', 'r'):
		case FTP_BUILD_CMD('U', 'S', 'E', 'R'):
			if (state < FTP_STATE_USER_RECVED)
			{
				state = FTP_STATE_USER_RECVED;
				reply = "331 Please input password.\r\n";
			}
			else
			{
				reply = "331 Any password will do.\r\n";
			}

			break;

		case FTP_BUILD_CMD('p', 'a', 's', 's'):
		case FTP_BUILD_CMD('P', 'A', 'S', 'S'):
			if (state < FTP_STATE_USER_RECVED)
			{
				reply = "530 Please input username.\r\n";
			}
			else if (state < FTP_STATE_LOGINED)
			{
				state = FTP_STATE_LOGINED;
				reply = "230 User login successfull.\r\n";
			}
			else
			{
				reply = "230 Alreay logged in.\r\n";
			}

			break;

		default:
			if (state < FTP_STATE_LOGINED)
			{
				if (state < FTP_STATE_USER_RECVED)
				{
					reply = "530 Please login with USER and PASS.\r\n";
				}
				else
				{
					reply = "530 Please login with PASS.\r\n";
				}

				break;
			}

			switch (command)
			{
			case FTP_BUILD_CMD('p', 'o', 'r', 't'):
			case FTP_BUILD_CMD('P', 'O', 'R', 'T'):
			{
				int temp[6];

				ret = sscanf(cmd_arg, "%d,%d,%d,%d,%d,%d", temp, temp + 1, temp + 2, temp + 3, temp + 4, temp + 5);
				if (ret == 6)
				{
					snprintf(data_url.memory, sizeof(data_url.memory), "%d.%d.%d.%d", temp[0], temp[1], temp[2], temp[3]);
					data_url.hostname = data_url.memory;
					data_url.port = temp[4] << 8 | temp[5];
					reply = "200 PORT command complete.\r\n";
				}
				else
				{
					reply = "501 Argument error.\r\n";
				}

				break;
			}

			case FTP_BUILD_CMD('o', 'p', 't', 's'):
			case FTP_BUILD_CMD('O', 'P', 'T', 'S'):
				reply = "200 OPTS command complete.\r\n";
				break;

			case FTP_BUILD_CMD('p', 'w', 'd', 0):
			case FTP_BUILD_CMD('P', 'W', 'D', 0):
			case FTP_BUILD_CMD('x', 'p', 'w', 'd'):
			case FTP_BUILD_CMD('X', 'P', 'W', 'D'):
				replen = snprintf(rep_buff, sizeof(rep_buff), "257 \"%s\"\r\n", curr_path);
				break;

			case FTP_BUILD_CMD('t', 'y', 'p', 'e'):
			case FTP_BUILD_CMD('T', 'Y', 'P', 'E'):
				reply = "200 TYPE commnd complete.\r\n";
				file_type = cmd_buff[5];
				break;

			case FTP_BUILD_CMD('s', 'y', 's', 't'):
			case FTP_BUILD_CMD('S', 'Y', 'S', 'T'):
				reply = "215 UNIX Type L8.\r\n";
				break;

			case FTP_BUILD_CMD('c', 'w', 'd', 0):
			case FTP_BUILD_CMD('C', 'W', 'D', 0):
			case FTP_BUILD_CMD('x', 'c', 'w', 'd'):
			case FTP_BUILD_CMD('X', 'C', 'W', 'D'):
				if (*cmd_arg)
				{
					ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path));
					if (access(abs_path, F_OK) < 0)
					{
						reply = "550 Failed to change directory.\r\n";
						break;
					}

					prettify_pathname_base(abs_path, curr_path, sizeof(curr_path));
				}
				else
				{
					text_copy(curr_path, service->home);
				}

				reply = "250 CWD commnd complete.\r\n";
				break;

			case FTP_BUILD_CMD('l', 'i', 's', 't'):
			case FTP_BUILD_CMD('L', 'I', 'S', 'T'):
				if (data_link.sockfd < 0 && data_url.port == 0)
				{
					reply = "550 Please run PORT or PASV first.\r\n";
					break;
				}

				fd = ftp_list_directory1(curr_path, file_type == 'I' ? "\n" : "\r\n");
				if (fd < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 List directory failed: %s.\r\n", strerror(errno));
					break;
				}

				wrlen = network_client_send_text(client, "150 List directory complete.\r\n");
				if (wrlen < 0)
				{
					pr_red_info("ftp_send_text");
					close(fd);
					goto out_close_data_link;
				}

				ret = ftp_server_send_file1(&data_link, &data_url, fd);
				close(fd);

				if (ret < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 Send list failed: %s.\r\n", strerror(errno));
				}
				else
				{
					reply = "226 List send complete.\r\n";
				}

				break;

			case FTP_BUILD_CMD('s', 'i', 'z', 'e'):
			case FTP_BUILD_CMD('S', 'I', 'Z', 'E'):
			{
				struct stat st;

				if (*cmd_arg == 0 || stat(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)), &st))
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 get file size failed: %s.\r\n", strerror(errno));
				}
				else
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "213 %" PRINT_FORMAT_OFF "\r\n", st.st_size);
				}

				break;
			}

			case FTP_BUILD_CMD('r', 'e', 't', 'r'):
			case FTP_BUILD_CMD('R', 'E', 'T', 'R'):
				fd = open(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)), O_RDONLY);
				if (fd < 0)
				{
					reply = "550 Open file failed.\r\n";
					break;
				}

				wrlen = network_client_send_text(client, "125 Starting transfer\r\n");
				if (wrlen < 0)
				{
					pr_red_info("network_client_send_text");
					close(fd);
					goto out_close_data_link;
				}

				ret = ftp_server_send_file1(&data_link, &data_url, fd);
				close(fd);

				if (ret < 0)
				{
					reply = "550 Send file failed.\r\n";
				}
				else
				{
					reply = "226 Transfer complete.\r\n";
				}

				break;

			case FTP_BUILD_CMD('s', 't', 'o', 'r'):
			case FTP_BUILD_CMD('S', 'T', 'O', 'R'):
				fd = open(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)), O_WRONLY | O_CREAT, 0777);
				if (fd < 0)
				{
					reply = "550 Open file failed.\r\n";
					break;
				}

				wrlen = network_client_send_text(client, "125 Starting transfer\r\n");
				if (wrlen < 0)
				{
					pr_error_info("ftp_send_text");
					close(fd);
					goto out_close_data_link;
				}

				ret = ftp_server_receive_file1(&data_link, &data_url, fd);
				close(fd);

				if (ret < 0)
				{
					reply = "550 Receive file failed.\r\n";
				}
				else
				{
					reply = "226 Transfer complete.\r\n";
				}

				break;

			case FTP_BUILD_CMD('p', 'a', 's', 'v'):
			case FTP_BUILD_CMD('P', 'A', 'S', 'V'):
				if (data_service.sockfd < 0)
				{
					ret = ftp_data_service_open(&data_service, client, rep_pasv, sizeof(rep_pasv));
					if (ret < 0)
					{
						pr_red_info("ftp_data_service_open");
						return ret;
					}

					replen = ret;
					pfds[0].fd = data_service.sockfd;
				}

				reply = rep_pasv;
				break;

			case FTP_BUILD_CMD('d', 'e', 'l', 'e'):
			case FTP_BUILD_CMD('D', 'E', 'L', 'E'):
				ret = remove(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)));
				if (ret < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 remove %s failed: %s.\r\n", cmd_arg, strerror(errno));
				}
				else
				{
					reply = "200 DELE command complete.\r\n";
				}

				break;

			case FTP_BUILD_CMD('r', 'm', 'd', 0):
			case FTP_BUILD_CMD('R', 'M', 'D', 0):
				ret = rmdir(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)));
				if (ret < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 remove %s failed: %s.\r\n", cmd_arg, strerror(errno));
				}
				else
				{
					reply = "200 DELE command complete.\r\n";
				}

				break;

			case FTP_BUILD_CMD('m', 'k', 'd', 0):
			case FTP_BUILD_CMD('M', 'K', 'D', 0):
				ret = mkdir(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)), 0777);
				if (ret < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 create directory %s failed: %s.\r\n", cmd_arg, strerror(errno));
				}
				else
				{
					reply = "200 MKD command complete.\r\n";
				}

				break;

			case FTP_BUILD_CMD('m', 'd', 't', 'm'):
			case FTP_BUILD_CMD('M', 'D', 'T', 'M'):
			{
				struct stat st;

				ret = stat(ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path)), &st);
				if (ret < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 get file stat failed: %s.\r\n", strerror(errno));
				}
				else
				{
					struct tm ti;

					if (localtime_r((time_t *) &st.st_atime, &ti) == NULL)
					{
						replen = snprintf(rep_buff, sizeof(rep_buff), "550 get localtime failed: %s.\r\n", strerror(errno));
					}
					else
					{
						replen = snprintf(rep_buff, sizeof(rep_buff), "213 %04d%02d%02d%02d%02d%02d\r\n", \
							ti.tm_year + 1900, ti.tm_mon + 1, ti.tm_mday, ti.tm_hour, ti.tm_min, ti.tm_sec);
					}
				}

				break;
			}

			case FTP_BUILD_CMD('n', 'o', 'o', 'p'):
			case FTP_BUILD_CMD('N', 'O', 'O', 'P'):
				reply = "200 NOOP commnd complete.\r\n";
				break;

			case FTP_BUILD_CMD('r', 'n', 'f', 'r'):
			case FTP_BUILD_CMD('R', 'N', 'F', 'R'):
			{
				char rnfr_path[1024];

				ftp_get_abs_path(curr_path, cmd_arg, rnfr_path, sizeof(rnfr_path));
				reply = "350 RNFR command complete.\r\n";
				break;
			}

			case FTP_BUILD_CMD('r', 'n', 't', 'o'):
			case FTP_BUILD_CMD('R', 'N', 'T', 'O'):
			{
				char rnfr_path[1024];

				ftp_get_abs_path(curr_path, cmd_arg, abs_path, sizeof(abs_path));
				ret = rename(rnfr_path, abs_path);
				if (ret < 0)
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "550 Rename %s to %s failed[%s].\r\n", rnfr_path, abs_path, strerror(errno));
				}
				else
				{
					replen = snprintf(rep_buff, sizeof(rep_buff), "250 Rename %s to %s successfully.\r\n", rnfr_path, abs_path);
				}

				break;
			}

			case FTP_BUILD_CMD('s', 'i', 't', 'e'):
			case FTP_BUILD_CMD('S', 'I', 'T', 'E'):
				command = *(u32 *) cmd_arg;
				switch (command)
				{
				case FTP_BUILD_CMD('h', 'e', 'l', 'p'):
				case FTP_BUILD_CMD('H', 'E', 'L', 'P'):
					reply = "214 UNMASK HELP\r\n";
					break;

				default:
					goto label_unsupport_command;
				}

				break;

			case FTP_BUILD_CMD('f', 'e', 'a', 't'):
			case FTP_BUILD_CMD('F', 'E', 'A', 'T'):
				reply = "211 MDTM SIZE PASV PORT RNTO MKD RMD DELE RETR STOR\r\n";
				break;

			default:
label_unsupport_command:
				pr_red_info("unsupport command: `%s'", cmd_buff);
				reply = "500 Unknown command.\r\n";
			}
		}
	}

out_close_data_link:
	if (data_link.sockfd >= 0)
	{
		network_client_close(&data_link);
	}
out_network_service_close:
	if (data_service.sockfd >= 0)
	{
		network_service_close(&data_service);
	}
	return 0;
}
예제 #23
0
파일: ftp.c 프로젝트: Jack19881218/cavan
int ftp_client_run(struct network_url *url, const char *username, const char *password)
{
	int ret;
	char buff[1024], *p;
	ssize_t wrlen, rdlen;
	struct network_client client;

	ret = network_client_open(&client, url, 0);
	if (ret < 0)
	{
		pr_red_info("network_client_open");
		return ret;
	}

	ret = ftp_client_login(&client, username, password);
	if (ret < 0)
	{
		pr_red_info("ftp_client_login");
		return ret;
	}

	while (1)
	{
		while (1)
		{
			print("cavan-ftp> ");

			for (p = buff; (*p = getchar()) != '\n'; p++);

			if (p > buff)
			{
				p = text_copy(p, "\r\n");
				break;
			}
		}

		if (text_lhcmp("ls", buff) == 0)
		{
			println("list command");
			ftp_client_receive_file(&client, url->hostname, 6789);
		}
		else
		{
			wrlen = client.send(&client, buff, p - buff);
			if (wrlen < 0)
			{
				pr_red_info("inet_send");
				ret = wrlen;
				break;
			}
		}

		rdlen = client.recv(&client, buff, sizeof(buff));
		if (rdlen < 0)
		{
			pr_red_info("client.recv");
			ret = rdlen;
			break;
		}

		buff[rdlen] = 0;
		println("%s", buff);
	}

	network_client_close(&client);

	return ret;
}
예제 #24
0
파일: cftp.c 프로젝트: FuangCao/cavan
int cftp_client_send_file(struct cftp_descriptor *desc, const char *file_in, u32 offset_in, const char *file_out, u32 offset_out, size_t size)
{
	int fd;
	int ret;
	struct stat st;
	ssize_t recvlen, sendlen, readlen;
	u16 blk_num;
	union cftp_message *msg;
	struct cftp_data_package *data_msg;
	size_t max_xfer_length, max_data_length;
	struct progress_bar bar;

	ret = file_lstat(file_in, &st);
	if (ret < 0) {
		pr_err_info("fstat");
		return ret;
	}

	max_xfer_length = desc->max_xfer_length;

	msg = malloc(max_xfer_length);
	if (msg == NULL) {
		pr_err_info("malloc");
		return -ENOMEM;
	}

	switch (st.st_mode & S_IFMT) {
	case S_IFREG:
		println("Send File: %s => %s", file_in, file_out);
		break;

	case S_IFBLK:
	case S_IFCHR:
		println("Send Devicce: %s => %s", file_in, file_out);
		return cftp_client_send_special_file(desc, &st, &msg->file_req, text_copy(msg->file_req.filename, file_out) - msg->file_req.filename + 1);

	case S_IFLNK:
		println("Send Symlink: %s => %s", file_in, file_out); {
			char *p;

			p = text_copy(msg->file_req.filename, file_out) + 1;

			ret = readlink(file_in, p, 1024);
			if (ret < 0) {
				pr_err_info("readlink");
				return ret;
			}

			return cftp_client_send_special_file(desc, &st, &msg->file_req, p - msg->file_req.filename + ret + 1);
		}

	case S_IFDIR:
		println("Send Directory: %s => %s", file_in, file_out);
		ret = cftp_client_send_special_file(desc, &st, &msg->file_req, text_copy(msg->file_req.filename, file_out) - msg->file_req.filename + 1);
		if (ret < 0) {
			pr_err_info("cftp_client_send_special_file");
			return ret;
		}
		return cftp_client_send_directory(desc, file_in, file_out);

	default:
		pr_warn_info("File %s type is unknown", file_in);
		return 0;
	}

	fd = open(file_in, O_RDONLY);
	if (fd < 0) {
		pr_err_info("open file %s failed", file_in);
		goto out_free_msg;
	}

	if (offset_in) {
		ret = lseek(fd, offset_in, SEEK_SET);
		if (ret < 0) {
			pr_err_info("lseek");
			goto out_close_file;
		}
	}

	if (size == 0) {
		size = st.st_size - offset_in;
	}

	data_msg = malloc(max_xfer_length);
	if (data_msg == NULL) {
		pr_err_info("malloc");
		ret = -ENOMEM;
		goto out_close_file;
	}

	sendlen = cftp_send_file_reuest(desc, (void *) msg, file_out, &st, offset_out, size, 0);
	if (sendlen < 0) {
		ret = sendlen;
		pr_err_info("cftp_send_data_retry");
		goto out_free_data_msg;
	}

	println("seek = %s", size2text(offset_out));
	println("skip = %s", size2text(offset_in));
	println("size = %s", size2text(size));

	blk_num = 0;
	data_msg->type = CFTP_PACKAGE_DATA;
	max_data_length = max_xfer_length - sizeof(*data_msg);
	progress_bar_init(&bar, size, 0, PROGRESS_BAR_TYPE_DATA);
	readlen = max_data_length;

	while (1) {
		recvlen = cftp_receive_data(desc, msg, max_xfer_length);
		if (recvlen < 0) {
			pr_err_info("cftp_receive_data");
			ret = recvlen;
			goto out_free_data_msg;
		}

		switch (msg->type) {
		case CFTP_PACKAGE_ERROR:
			show_error_message((struct cftp_error_message *) msg);
			ret = -EFAULT;
			goto out_free_data_msg;

		case CFTP_PACKAGE_ACK:
			if (msg->ack_msg.blk_num == blk_num) {
				if ((size_t) readlen < max_data_length) {
					ret = 0;
					progress_bar_finish(&bar);
					println("Send data complete");
					goto out_free_data_msg;
				}

				readlen = read(fd, data_msg->data, max_data_length);
				if (readlen < 0) {
					pr_err_info("read");
					cftp_send_error_message(desc, (struct cftp_error_message *) msg, "read file failed");
					ret = readlen;
					goto out_free_data_msg;
				}

				data_msg->blk_num = blk_num++;
				progress_bar_add(&bar, readlen);
			} else {
				pr_warn_info("%d != %d", msg->ack_msg.blk_num, blk_num);

				if (blk_num == 0) {
					continue;
				}
			}

			sendlen = cftp_send_data_retry(desc, data_msg, sizeof(*data_msg) + readlen, desc->retry_count);
			if (sendlen < 0) {
				pr_err_info("cftp_send_data_retry");
				goto out_free_data_msg;
			}
			break;

		default:
			pr_err_info("invalid package type");
			cftp_send_error_message(desc, (struct cftp_error_message *) msg, "invalid package type");
			ret = -EINVAL;
			goto out_free_data_msg;
		}
	}

out_free_data_msg:
	free(data_msg);
out_close_file:
	close(fd);
out_free_msg:
	free(msg);

	return ret;
}
예제 #25
0
int
TextTest(int verbose, struct cfg *cfg, char *args[])
{
	tchar buf[1024];
	tchar *hello = TEXT("hello");
	tchar *a = TEXT("a");
	tchar *e = TEXT("");
	tchar *cpy;
	cfg = NULL; *args = NULL; verbose = 1;

                                        /* text_length */

	T(text_length(NULL, NULL) == 0);
	T(text_length(hello, NULL) == 0);
	T(text_length(hello + 3, hello) == 0);
	T(text_length(hello, hello) == 0);
	T(text_length(hello, hello + 1) == 0);
	T(text_length(hello, hello + 4) == 0);
	T(text_length(a, a + 1) == 0);
	T(text_length(a, a + 2) == 1);
	T(text_length(a + 1, a + 2) == 0);
	T(text_length(hello, hello + 100) == 5);

                                          /* text_size */

	T(text_size(NULL, NULL) == 0);
	T(text_size(hello, NULL) == 0);
	T(text_size(hello + 3, hello) == 0);
	T(text_size(hello, hello) == 0);
	T(text_size(hello, hello + 1) == 0);
	T(text_size(hello, hello + 4) == 0);
	T(text_size(a, a + 1) == 0);
	T(text_size(a, a + 2) == (2 * sizeof *a));
	T(text_size(a + 1, a + 2) == (1 * sizeof *a));
	T(text_size(hello, hello + 100) == (6 * sizeof *hello));

                                          /* text_copy */

	T(text_copy(hello, hello + 100, NULL, buf + 1024, 0) == 0);

	T(text_copy(hello, hello + 100, buf + 3, buf, 0) == 0);

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(NULL, hello + 100, buf, buf + 1024, 0) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello + 3, hello, buf, buf + 1024, 0) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 100, 1) == 1);
	T(tcsncmp(TEXT("h"), buf, 100) == 0);

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 100, 5) == 5);
	T(tcsncmp(hello, buf, 100) == 0);

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 1024, 0) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(e, e + 100, buf, buf + 1024, 100) == 0);
	T(tcsncmp(e, buf, 100) == 0);

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 1024, 100) == 5);
	T(tcsncmp(hello, buf, 100) == 0);

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 1, buf, buf + 1024, 100) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 5, buf, buf + 1024, 100) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 6, buf, buf + 1024, 100) == 5);
	T(tcsncmp(hello, buf, 100) == 0);

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 1, 100) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 5, 100) == 0);
	T(buf[0] == TEXT('\0'));

	memset(buf, 'x', 1024 * sizeof(tchar));
	T(text_copy(hello, hello + 100, buf, buf + 6, 100) == 5);
	T(tcsncmp(hello, buf, 100) == 0);

                                      /* text_copy_new */

	T(text_copy_new(hello, hello + 100, &cpy, 100, NULL) == 5);
	T(tcsncmp(hello, cpy, 100) == 0);
	allocator_free(NULL, cpy);

	T(text_copy_new(hello, hello + 100, NULL, 100, NULL) == 0);

	T(text_copy_new(NULL, hello + 100, &cpy, 100, NULL) == 0);
	T(cpy == NULL);

	T(text_copy_new(hello + 3, hello, &cpy, 100, NULL) == 0);
	T(cpy == NULL);

	T(text_copy_new(hello, hello + 100, &cpy, 0, NULL) == 0);
	T(tcsncmp(e, cpy, 100) == 0);
	allocator_free(NULL, cpy);

	T(text_copy_new(hello, hello + 100, &cpy, 4, NULL) == 4);
	T(tcsncmp(TEXT("hell"), cpy, 100) == 0);
	allocator_free(NULL, cpy);

	T(text_copy_new(hello, hello + 100, &cpy, 5, NULL) == 5);
	T(tcsncmp(hello, cpy, 100) == 0);
	allocator_free(NULL, cpy);

    return 0;
}
예제 #26
0
// Parse info string into server name, players, maxplayers
// Warning! Modifies string!
static void update_serverlist_server (m_serverlist_server_t *server, char *info, int ping)
{
	char *s;
	const char *servername;

	// start at end of string:
	s = strlen(info) + info; 

	// find max players
	while(s > info && *s != '/')
		s--;
	server->maxplayers = atoi(s+1);

	// find current number of players:
	*s = 0;
	s--;
	while(s > info && *s >= '0' && *s <= '9')
		s--;
	server->players = atoi(s+1);
	
	// find map name:
	while(s > info && *s == 32) // clear whitespace;
		s--;

	*(s + 1) = 0;

	while(s > info && *s > 32)
		s--;

	if (!server->mapname)
	{
		server->mapname = text_copy(s + 1);
	}
	else if (!Q_streq(server->mapname, s + 1))
	{
		char buf[MAX_QPATH];

		Z_Free(server->mapname);
		sprintf(buf, "%s/maps/%s.bsp", FS_Gamedir(), s + 1);

		if (!FileExists(buf))
		{
			// put maps clients don't have in italics
			sprintf(buf, "%c%s%c", SCHAR_ITALICS, s + 1, SCHAR_ITALICS);
			server->mapname = text_copy(buf);
		}
		else
		{
			server->mapname = text_copy(s+1);
		}
	}

	// servername is what's left over:
	*s = 0;
	servername = info;

	while (*servername == ' ') // skip leading spaces
		++servername;

	if (!server->servername || strlen(server->servername) < strlen(servername))
	{
		if (server->servername)
			Z_Free(server->servername);

		server->servername = text_copy(servername);
	}

	strip_garbage(server->servername, servername);

	// and the ping
	server->ping = ping;
}
예제 #27
0
void M_AddToServerList (netadr_t adr, char *info, qboolean pinging)
{
	int i;
	char addrip[32];
	int ping;
	qboolean added = false;

	NetAdrToString(adr, addrip, sizeof(addrip));

	pthread_mutex_lock(&m_mut_serverlist); // jitmultithreading -- todo, deadlock
	pthread_mutex_lock(&m_mut_widgets);

	// check if server exists in current serverlist:
	for (i = 0; i < m_serverlist.numservers; i++)
	{
		if (adrs_equal(adr, m_serverlist.server[i].adr))
		{
			// update info from server:
			if (pinging)
			{
				set_serverlist_server_pingtime(&m_serverlist.server[i], Sys_Milliseconds());
			}
			else
			{
				ping = Sys_Milliseconds() - m_serverlist.server[i].ping_request_time;
				update_serverlist_server(&m_serverlist.server[i], info, ping);

				if (m_serverlist.server[i].remap < 0)
				{
					m_serverlist.server[i].remap = m_serverlist.nummapped++;
					m_serverlist.ips[m_serverlist.server[i].remap] = text_copy(addrip);
				}
				else
				{
					Z_Free(m_serverlist.info[m_serverlist.server[i].remap]);
				}

				m_serverlist.info[m_serverlist.server[i].remap] =
					text_copy(format_info_from_serverlist_server(&m_serverlist.server[i]));
			}

			added = true;
			break;
		}
	}

	if (!added) // doesn't exist.  Add it.
	{
		i++;

		// List too big?  Alloc more memory:
		// STL would be useful about now
		if (i > m_serverlist.actualsize) 
		{
			char **tempinfo;
			char **tempips;
			m_serverlist_server_t *tempserver;

			// Double the size:
			tempinfo = Z_Malloc(sizeof(char*) * m_serverlist.actualsize * 2);
			tempips = Z_Malloc(sizeof(char*) * m_serverlist.actualsize * 2);
			tempserver = Z_Malloc(sizeof(m_serverlist_server_t) * m_serverlist.actualsize * 2);

			for(i = 0; i < m_serverlist.actualsize; i++)
			{
				tempinfo[i] = m_serverlist.info[i];
				tempips[i] = m_serverlist.ips[i];
				tempserver[i] = m_serverlist.server[i];
			}

			for (i = m_serverlist.actualsize; i < m_serverlist.actualsize * 2; i++)
			{
				memset(&tempserver[i], 0, sizeof(m_serverlist_server_t));
				tempserver[i].remap = -1;
			}

			Z_Free(m_serverlist.info);
			Z_Free(m_serverlist.ips);
			Z_Free(m_serverlist.server);

			m_serverlist.info = tempinfo;
			m_serverlist.ips = tempips;
			m_serverlist.server = tempserver;

			m_serverlist.actualsize *= 2;
		}

		m_serverlist.server[m_serverlist.numservers].adr = adr; // jitodo - test

		// add data to serverlist:
		if (pinging)
		{
			set_serverlist_server_pingtime(&m_serverlist.server[m_serverlist.numservers], Sys_Milliseconds());
			ping = 999;
			update_serverlist_server(&m_serverlist.server[m_serverlist.numservers], info, ping);
		}
		else
		{
			// this will probably only happen with LAN servers.
			ping = Sys_Milliseconds() - m_serverPingSartTime;
			m_serverlist.ips[m_serverlist.nummapped] = text_copy(addrip);
			update_serverlist_server(&m_serverlist.server[m_serverlist.numservers], info, ping);
			m_serverlist.info[m_serverlist.nummapped] =
				text_copy(format_info_from_serverlist_server(&m_serverlist.server[m_serverlist.numservers]));
			m_serverlist.server[m_serverlist.numservers].remap = m_serverlist.nummapped;
			m_serverlist.nummapped++;
		}

		m_serverlist.numservers++;
	}

	m_serverlist.sortthisframe = true;
	// Tell the widget the serverlist has updated:
	pthread_mutex_unlock(&m_mut_widgets);
	//M_RefreshActiveMenu(); // jitodo - target serverlist window specifically.
	M_RefreshWidget("serverlist", false);
	pthread_mutex_unlock(&m_mut_serverlist); // jitmultithreading
}
예제 #28
0
static qboolean serverlist_load (void)
{
	int size;
	char *data;
	char *ptr;

	size = FS_LoadFile("serverlist.dat", (void**)&data);

	if (size > -1)
	{
		int endiantest;
		int actualsize;
		register int i;

		// Check header to make sure it's a valid file:
		if (memcmp(data, "PB2Serverlist1.00", sizeof("PB2Serverlist1.00")-1) != 0)
		{
			FS_FreeFile(data);
			return false;
		}

		ptr = data + sizeof("PB2Serverlist1.00")-1;
		memcpy(&endiantest, ptr, sizeof(int));

		if (endiantest != 123123123)
		{
			FS_FreeFile(data);
			return false;
		}

		// Read our data:
		ptr += sizeof(int);
		memcpy(&actualsize, ptr, sizeof(int));

		if (actualsize < INITIAL_SERVERLIST_SIZE)
		{
			FS_FreeFile(data);
			return false;
		}

		ptr += sizeof(int);
		create_serverlist(actualsize);
		memcpy(&m_serverlist.numservers, ptr, sizeof(int));
		ptr += sizeof(int);
		memcpy(&m_serverlist.nummapped, ptr, sizeof(int));
		ptr += sizeof(int);

		for (i=0; i<m_serverlist.numservers; i++)
		{
			memcpy(&m_serverlist.server[i].adr, ptr, sizeof(netadr_t));
			ptr += sizeof(netadr_t);
			m_serverlist.server[i].remap = *(short*)ptr;
			ptr += sizeof(short);
			m_serverlist.server[i].servername = text_copy(ptr);
			ptr = skip_string(ptr);
			m_serverlist.server[i].mapname = text_copy(ptr);
			ptr = skip_string(ptr);
			m_serverlist.server[i].ping = *(unsigned short*)ptr;
			ptr += sizeof(unsigned short);
			m_serverlist.server[i].players = *(unsigned char*)ptr;
			ptr += sizeof(unsigned char);
			m_serverlist.server[i].maxplayers = *(unsigned char*)ptr;
			ptr += sizeof(unsigned char);
		}

		for (i=0; i<m_serverlist.nummapped; i++)
		{
			m_serverlist.ips[i] = text_copy(ptr);
			ptr = skip_string(ptr);
			m_serverlist.info[i] = text_copy(ptr);
			ptr = skip_string(ptr);
		}

		FS_FreeFile(data);
		return true;
	}

	return false;
}
예제 #29
0
int ext2_find_file(struct ext2_desc *desc, const char *pathname, struct ext2_inode *inode)
{
	struct ext2_directory_entry entry;
	char path_temp[1024], *filename, *p;

	text_copy(path_temp, pathname);
	filename = p = path_temp;

	entry.inode = 2;
	entry.file_type = EXT_FILE_TYPE_DIRECTORY;

	while (1)
	{
		int ret;
		ssize_t rdlen;

		rdlen = ext2_read_inode(desc, entry.inode, inode);
		if (rdlen < 0)
		{
			print_error("ext2_read_inode");
			return rdlen;
		}

#if CAVAN_EXT2_DEBUG
		show_ext2_inode(inode);
#endif

		while (*filename == '/')
		{
			filename++;
		}

		if (*filename == 0)
		{
			break;
		}

		if (entry.file_type != EXT_FILE_TYPE_DIRECTORY)
		{
			ERROR_RETURN(ENOENT);
		}

		for (p = filename; *p && *p != '/'; p++);

		*p = 0;

		if (inode->flags & EXT2_INODE_FLAG_EXTENTS)
		{
			ret = ext4_find_file_base(desc, filename, (struct ext4_extent_header *) inode->block, &entry);
		}
		else
		{
			ret = ext2_find_file_base(desc, filename, inode->block, inode->blocks, &entry);
		}

		if (ret < 0)
		{
			ERROR_RETURN(ENOENT);
		}

		filename = p + 1;
	}

	return 0;
}