Example #1
0
File: ivl_io.hpp Project: iavr/drvq
void write_array_2d(const array_2d <T, K>& a, std::ostream& s)
{
	size_t arr = a.columns();  // number of arrays
	write_size(arr, s);

	size_t dim = a.rows();  // dimension of each array
	for (size_t n = 0, p = 0; n < arr; n++, p += dim) {
		write_size(dim, s);
		write(a[p], s, dim);
	}
}
Example #2
0
int			write_instructions(int fd, int fdwrite)

{
  char			*tmp;
  int			size;
  int			i;
  char			*ins;
  char			*param;
  t_asm			*my_asm;

  i = 2;
  my_asm = NULL;
  while ((tmp = get_next_line(fd)))
    {
      i++;
      if (tmp != NULL && tmp[0] != '.' && all_space(tmp) != 0)
	{
	  tmp = remove_space(tmp);
	  ins = parse_action(tmp, 0);
	  param = parse_action(tmp, 1);
	  ins = remove_all_space(ins);
	  param = remove_all_space(param);
	  if (check_arg(ins) != 1 ||
	      check_param(ins, param) != 1)
	    return (error_line(i));
	  my_asm = add_action(my_asm, ins, param);
	}
    }
  my_asm = my_asm->next;
  if ((size = instructions_file(fdwrite, my_asm)) == -1
      || write_size(fdwrite, size) == -1)
    return (-1);
  return (0);
}
Example #3
0
void
forward_to_remote(struct remote_context *remote, uint8_t *buf, int buflen) {
    buf -= HEADER_BYTES;
    write_size(buf, buflen);
    buflen += HEADER_BYTES;
    uv_buf_t data = uv_buf_init((char*)buf, buflen);
    remote->write_req.data = remote;
    uv_write(&remote->write_req, &remote->handle.stream, &data, 1, remote_send_cb);
}
Example #4
0
File: tac.c Project: lufb/code
static void
deal_mem(char *pdata, size_t size)
{
    assert(pdata != NULL);
    int                      i, line_size = 0;

    for(i = size - 1; i >= 0; --i)
    {
        ++line_size;
        if(pdata[i] == SEP_CHAR)        /*找到一个分隔符*/
        {
            write_size((void *)(pdata+i), line_size, STDOUT_FILENO);   /*写这一行数据*/
            line_size = 0;
        }
    }

    if(line_size != 0)          /*开始处(除开第一个字节)的数据*/
    {
        write_size("\n", 1, STDOUT_FILENO);/*先写个换行符*/
        write_size((void *)pdata, line_size, STDOUT_FILENO);
    }
    write_size("\n", 1, STDOUT_FILENO);
}
Example #5
0
void
forward_to_source(struct source_context *source, uint8_t *buf, int buflen) {
    uv_read_stop(&source->target->handle.stream);
    source->write_req.data = source;
    if (mode == TUNNEL_MODE_CLIENT) {
        uv_buf_t data = uv_buf_init((char*)buf, buflen);
        uv_write(&source->write_req, &source->handle.stream, &data, 1, source_send_cb);

    } else {
        buf -= HEADER_BYTES;
        write_size(buf, buflen);
        buflen += HEADER_BYTES;
        uv_buf_t data = uv_buf_init((char*)buf, buflen);
        uv_write(&source->write_req, &source->handle.stream, &data, 1, source_send_cb);
    }
}
Example #6
0
void		*set_ptr(t_info info, void *mem)
{
	void	*ptr;

	ptr = write_size(info.size, mem);
	ptr = write_block_count(info.block_count, ptr);
	if (info.type > 3)
	{
		if (info.type == SMALL_N)
			info.type = SMALL;
		else if (info.type == TINY_N)
			info.type = TINY;
		else
			info.type = LARGE;
	}
	ptr = write_mem_pos(info.mem_pos, info.type, ptr);
	return (ptr);
}
Example #7
0
void
tun_to_tcp(uint8_t *buf, int len, uv_stream_t *stream) {
    uint8_t *hdr = malloc(HEADER_BYTES);
    write_size(hdr, len);

    uv_write_t *req = malloc(sizeof(*req) + sizeof(uv_buf_t) * 2);

    uv_buf_t *outbuf1 = (uv_buf_t *) (req + 1);
    uv_buf_t *outbuf2 = outbuf1 + 1;
    *outbuf1 = uv_buf_init((char *) hdr, HEADER_BYTES);
    *outbuf2 = uv_buf_init((char *) buf, len);

    uv_buf_t bufs[2] = {
        *outbuf1,
        *outbuf2,
    };

    int rc = uv_write(req, stream, bufs, 2, send_cb);
    if (rc) {
        logger_log(LOG_ERR, "TCP Write error: %s", uv_strerror(rc));
        free(buf);
    }
}
Example #8
0
File: tar.c Project: calccrypto/tar
int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, const size_t filecount, const char * files[], int * offset, const char verbosity){
    if (fd < 0){
        return -1;
    }

    if (!archive || *archive){
        return -1;
    }

    if (filecount && !files){
        return -1;
    }

    // add new data
    struct tar_t ** tar = archive;  // current entry
    char buf[512];              // one block buffer
    for(unsigned int i = 0; i < filecount; i++){
        *tar = malloc(sizeof(struct tar_t));

        // stat file
        if (format_tar_data(*tar, files[i], verbosity) < 0){
            WRITE_ERROR(stderr, "Error: Failed to stat %s\n", files[i]);
        }

        if (!i){
            *archive = *tar;  // store first address
        }

        (*tar) -> begin = *offset;

        // write different data depending on file type
        if ((*tar) -> type == DIRECTORY){
            // save parent directory name (source will change)
            const size_t len = strlen((*tar) -> name);
            char * parent = calloc(len + 1, sizeof(char));
            strncpy(parent, (*tar) -> name, len);

            // add a '/' character to the end
            if ((len < 99) && ((*tar) -> name[len - 1] != '/')){
                (*tar) -> name[len] = '/';
                (*tar) -> name[len + 1] = '\0';
                calculate_checksum((*tar));
            }

            V_PRINT(stdout, "%s\n", (*tar) -> name);

            // write metadata to (*tar) file
            if (write_size(fd, (*tar) -> block, 512) != 512){
                WRITE_ERROR(stderr, "Error: Failed to write metadata to archive\n");
            }

            // go through directory
            DIR * d = opendir(parent);
            if (!d){
                WRITE_ERROR(stderr, "Error: Cannot read directory %s\n", parent);
            }

            struct dirent * dir;
            while ((dir = readdir(d))){
                // if not special directories . and ..
                const size_t sublen = strlen(dir -> d_name);
                if (strncmp(dir -> d_name, ".", sublen) && strncmp(dir -> d_name, "..", sublen)){
                    char * path = calloc(len + sublen + 2, sizeof(char));
                    sprintf(path, "%s/%s", parent, dir -> d_name);

                    // recursively write each subdirectory
                    if (write_entries(fd, &((*tar) -> next), head, 1, (const char **) &path, offset, verbosity) < 0){
                        WRITE_ERROR(stderr, "Error: Recurse error\n");
                    }

                    // go to end of new data
                    while ((*tar) -> next){
                        tar = &((*tar) -> next);
                    }

                    free(path);
                }
            }

            free(parent);
            closedir(d);
        }
        else{ // if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS) || ((*tar) -> type == SYMLINK) || ((*tar) -> type == CHAR) || ((*tar) -> type == BLOCK) || ((*tar) -> type == FIFO)){
            V_PRINT(stdout, "%s\n", (*tar) -> name);

            char tarred = 0;   // whether or not the file has already been put into the archive
            if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS) || ((*tar) -> type == SYMLINK)){
                struct tar_t * found = exists(*head, files[i], 1);
                tarred = (found != (*tar));

                // if file has already been included, modify the header
                if (tarred){
                    // change type to hard link
                    (*tar) -> type = HARDLINK;

                    // change link name to (*tar)red file name (both are the same)
                    strncpy((*tar) -> link_name, (*tar) -> name, 100);

                    // change size to 0
                    strncpy((*tar) -> size, "00000000000", 11);

                    // recalculate checksum
                    calculate_checksum((*tar));
                }
            }

            // write metadata to (*tar) file
            if (write_size(fd, (*tar) -> block, 512) != 512){
                WRITE_ERROR(stderr, "Error: Failed to write metadata to archive\n");
            }

            if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS)){
                // if the file isn't already in the tar file, copy the contents in
                if (!tarred){
                    int f = open((*tar) -> name, O_RDONLY);
                    if (f < 0){
                        WRITE_ERROR(stderr, "Error: Could not open %s\n", files[i]);
                    }

                    int r = 0;
                    while ((r = read_size(f, buf, 512)) > 0){
                        if (write_size(fd, buf, r) != r){
                            RC_ERROR(stderr, "Error: Could not write to archive: %s\n", strerror(rc));
                        }
                    }

                    close(f);
                }
            }

            // pad data to fill block
            const unsigned int size = oct2uint((*tar) -> size, 11);
            const unsigned int pad = 512 - size % 512;
            if (pad != 512){
                for(unsigned int j = 0; j < pad; j++){
                    if (write_size(fd, "\0", 1) != 1){
                        WRITE_ERROR(stderr, "Error: Could not write padding data\n");
                    }
                }
                *offset += pad;
            }
            *offset += size;
            tar = &((*tar) -> next);
        }

        // add metadata size
        *offset += 512;
    }

    return 0;
}
Example #9
0
File: tar.c Project: calccrypto/tar
int tar_remove(const int fd, struct tar_t ** archive, const size_t filecount, const char * files[], const char verbosity){
    if (fd < 0){
        return -1;
    }

    // archive has to exist
    if (!archive || !*archive){
        return -1;
    }

    if (filecount && !files){
        return -1;
    }

    if (!filecount){
        return 0;
    }

    // get file permissions
    struct stat st;
    if (fstat(fd, &st)){
        RC_ERROR(stderr, "Error: Unable to stat archive: %s\n", strerror(rc));
    }

    // reset offset of original file
    if (lseek(fd, 0, SEEK_SET) == (off_t) (-1)){
        RC_ERROR(stderr, "Error: Unable to seek file: %s\n", strerror(rc));
    }

    // find first file to be removed that does not exist
    int ret = 0;
    char * bad = calloc(filecount, sizeof(char));
    for(int i = 0; i < filecount; i++){
        if (!exists(*archive, files[i], 0)){
            V_PRINT(stderr, "Error: %s not found in archive\n", files[i]);
            bad[i] = 1;
            ret = -1;
        }
    }

    unsigned int read_offset = 0;
    unsigned int write_offset = 0;
    struct tar_t * prev = NULL;
    struct tar_t * curr = *archive;
    while(curr){
        // get original size
        int total = 512;

        if ((curr -> type == REGULAR) || (curr -> type == NORMAL) || (curr -> type == CONTIGUOUS)){
            total += oct2uint(curr -> size, 11);
            if (total % 512){
                total += 512 - (total % 512);
            }
        }
        const int match = check_match(curr, filecount, bad, files);

        if (match < 0){
            V_PRINT(stderr, "Error: Match failed\n");
            return -1;
        }
        else if (!match){
            // if the old data is not in the right place, move it
            if (write_offset < read_offset){
                int got = 0;
                while (got < total){
                    // go to old data
                    if (lseek(fd, read_offset, SEEK_SET) == (off_t) (-1)){
                        RC_ERROR(stderr, "Error: Cannot seek: %s\n", strerror(rc));
                    }

                    char buf[512];

                    // copy chunk out
                    if (read_size(fd, buf, 512) != 512){// guarenteed 512 octets
                        V_PRINT(stderr, "Error: Read error\n");
                        return -1;
                    }

                    // go to new position
                    if (lseek(fd, write_offset, SEEK_SET) == (off_t) (-1)){
                        RC_ERROR(stderr, "Error: Cannot seek: %s\n", strerror(rc));
                    }

                    // write data in
                    if (write_size(fd, buf, 512) != 512){
                        V_PRINT(stderr, "Error: Write error\n");
                        return -1;
                    }

                    // increment offsets
                    got += 512;
                    read_offset += 512;
                    write_offset += 512;
                }
            }
            else{
                read_offset += total;
                write_offset += total;

                // skip past data
                if (lseek(fd, read_offset, SEEK_SET) == (off_t) (-1)){
                    RC_ERROR(stderr, "Error: Cannot seek: %s\n", strerror(rc));
                }
            }
            prev = curr;
            curr = curr -> next;
        }
        else{// if name matches, skip the data
            struct tar_t * tmp = curr;
            if (!prev){
                *archive = curr -> next;
                if (*archive){
                    (*archive) -> begin = 0;
                }
            }
            else{
                prev -> next = curr -> next;

                if (prev -> next){
                    prev -> next -> begin = curr -> begin;
                }
            }
            curr = curr -> next;
            free(tmp);

            // next read starts after current entry
            read_offset += total;
        }
    }

    // resize file
    if (ftruncate(fd, write_offset) < 0){
        RC_ERROR(stderr, "Error: Could not truncate file: %s\n", strerror(rc));
    }

    // add end data
    if (write_end_data(fd, write_offset, verbosity) < 0){
        V_PRINT(stderr, "Error: Could not close file\n");
    }

    return ret;
}
Example #10
0
File: ivl_io.hpp Project: iavr/drvq
void write_array(const CONT_EX <CONT_IN<T, K>, D>& a, std::ostream& s)
{
	write_size(a.size(), s);
	for (size_t n = 0; n < a.size(); n++)
		write_array(a[n], s);
}
Example #11
0
File: ivl_io.hpp Project: iavr/drvq
void write_array(const CONT<T, K>& a, std::ostream& s)
{
	write_size(a.size(), s);
	write(a, s);
}