int
globus_args_bytestr_to_num( 
    const char *                        str,
    globus_off_t *                      out) 
{ 
    char *                              end = NULL; 
    globus_off_t                        size = 0;
    int                                 consumed;
    int                                 rc;
    
    if(str == NULL || !(isdigit(*str) || *str == '-'))
    {
        return 1;
    }
    
    rc = globus_libc_scan_off_t((char *)str, &size, &consumed);
    end = (char *)str + consumed;
    if(size && end && *end)
    { 
        switch(*end) 
        { 
            case 'g': 
            case 'G': 
                size *= 1024; 
            case 'm': 
            case 'M': 
                size *= 1024; 
            case 'k': 
            case 'K': 
                size *= 1024; 
            case 'b': 
            case 'B': 
                break; 
            
            default: 
                return 1;
                break; 
        } 
    } 
    
    *out = size;
    
    return 0;
} 
/**
 * Set the value of a header in a hashtable
 * @ingroup globus_i_xio_http_header
 *
 * Adds a new header to a header info structure, or updates the value of an
 * existing header. Copies of the name and value will be stored in a
 * #globus_xio_http_header_t in a hashtable in the header info structure.
 * 
 * @param headers
 *     Pointer to the header info structure.
 * @param header_name
 *     Name of the header.
 * @param header_value
 *     Value of the header.
 * @param store_all
 *     Set to GLOBUS_TRUE to add all entries to the hashtable, otherwise
 *     Content-Length, Transfer-Encoding, and Connection are discarded.
 *     You probably only want to do this when acting as a client and parsing
 *     the server response.
 *
 * @retval GLOBUS_SUCCESS
 *     Header successfully added to the hashtable.
 * @retval GLOBUS_XIO_ERROR_MEMORY
 *     Unable to add header due to memory constraints.
 */
globus_result_t
globus_i_xio_http_header_info_set_header(
    globus_i_xio_http_header_info_t *   headers,
    const char *                        header_name,
    const char *                        header_value,
    globus_bool_t                       store_all)
{
    char *                              save_header;
    globus_result_t                     result = GLOBUS_SUCCESS;
    globus_xio_http_header_t *          header;
    int                                 rc;
    globus_off_t                        length;
    globus_bool_t                       store;
    GlobusXIOName(globus_l_xio_http_header_set);

    store = store_all;
    /* Special cases for entity-body handling headers */
    if (strcasecmp(header_name, "Content-Length") == 0)
    {
        rc = globus_libc_scan_off_t(header_value, &length, NULL);
        if (rc < 1)
        {
            result = GlobusXIOHttpErrorInvalidHeader(header_name, header_value);

            goto error_exit;
        }
        headers->content_length = length;
        headers->flags |= GLOBUS_I_XIO_HTTP_HEADER_CONTENT_LENGTH_SET;
    }
    else if (strcasecmp(header_name, "Transfer-Encoding") == 0)
    {
        if (strcasecmp(header_value, "identity") == 0)
        {
            headers->transfer_encoding =
                GLOBUS_XIO_HTTP_TRANSFER_ENCODING_IDENTITY;
        }
        else if (strcasecmp(header_value, "chunked") == 0)
        {
            headers->transfer_encoding =
                GLOBUS_XIO_HTTP_TRANSFER_ENCODING_CHUNKED;
        }
        else
        {
            result = GlobusXIOHttpErrorInvalidHeader(header_name, header_value);

            goto error_exit;
        }
    }
    else if (strcasecmp(header_name, "Connection") == 0)
    {
        if (strcasecmp(header_value, "close") == 0)
        {
            headers->flags |= GLOBUS_I_XIO_HTTP_HEADER_CONNECTION_CLOSE;
        }
        else if (strcasecmp(header_value, "keep-alive") == 0)
        {
            headers->flags &= ~GLOBUS_I_XIO_HTTP_HEADER_CONNECTION_CLOSE;
        }
        else
        {
            result = GlobusXIOHttpErrorInvalidHeader(header_name, header_value);

            goto error_exit;
        }
    }
    else
    {
        store = GLOBUS_TRUE;
    }
    
    if(store)
    {
        /*
         * Either modify the header's value in the hashtable, if it's a
         * duplicate, or create a new entry in the hashtable
         */
        header = globus_hashtable_lookup(
                &headers->headers,
                (void *) header_name);

        if (header != NULL)
        {
            /* Replace current header's value */
            save_header = header->value;

            header->value = globus_libc_strdup(header_value);

            if (header->value == NULL)
            {
                header->value = save_header;

                result = GlobusXIOErrorMemory("header");

                goto error_exit;
            }
            globus_libc_free(save_header);
        }
        else
        {
            header = globus_libc_malloc(sizeof(globus_xio_http_header_t));

            if (header == NULL)
            {
                result = GlobusXIOErrorMemory("header");

                goto error_exit;
            }
            header->name = globus_libc_strdup(header_name);

            if (header->name == NULL)
            {
                result = GlobusXIOErrorMemory("header");
                goto free_header_exit;
            }

            header->value = globus_libc_strdup(header_value);

            if (header->value == NULL)
            {
                result = GlobusXIOErrorMemory("header");
                goto free_header_name_exit;
            }

            rc = globus_hashtable_insert(
                    &headers->headers,
                    header->name,
                    header);

            if (rc != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorMemory("header");

                goto free_header_value_exit;
            }
        }
    }
    return result;

free_header_value_exit:
    globus_libc_free(header->value);
free_header_name_exit:
    globus_libc_free(header->name);
free_header_exit:
    globus_libc_free(header);
error_exit:
    return result;
}
int main(int argc,
         char *argv[])
{
    globus_ftp_client_handle_t			handle;
    globus_ftp_client_operationattr_t		attr;
    globus_byte_t				buffer[SIZE];
    globus_size_t				buffer_length = sizeof(buffer);
    globus_result_t				result;
    char *					src;
    char *					dst;
    globus_ftp_client_handleattr_t		handle_attr;
    globus_off_t				start_offset=5;
    globus_off_t				end_offset=15;
    int						i;
    globus_ftp_control_mode_t			mode;

    globus_module_activate(GLOBUS_FTP_CLIENT_MODULE);
    globus_ftp_client_handleattr_init(&handle_attr);
    globus_ftp_client_operationattr_init(&attr);

    mode = GLOBUS_FTP_CONTROL_MODE_STREAM;

    /* Parse local arguments */
    for(i = 1; i < argc; i++)
    {
        if(strcmp(argv[i], "-R") == 0 && i + 2 < argc)
        {
            globus_libc_scan_off_t(argv[i+1], &start_offset, GLOBUS_NULL);
            globus_libc_scan_off_t(argv[i+2], &end_offset, GLOBUS_NULL);

            test_remove_arg(&argc, argv, &i, 2);
        }
        else if(strcmp(argv[i], "-E") == 0 && i < argc)
        {
            mode = GLOBUS_FTP_CONTROL_MODE_EXTENDED_BLOCK;

            test_remove_arg(&argc, argv, &i, 0);
        }
    }
    test_parse_args(argc,
                    argv,
                    &handle_attr,
                    &attr,
                    &src,
                    &dst);

    if(start_offset < 0) start_offset = 0;
    if(end_offset < 0) end_offset = 0;

    globus_mutex_init(&lock, GLOBUS_NULL);
    globus_cond_init(&cond, GLOBUS_NULL);

    globus_ftp_client_handle_init(&handle,  &handle_attr);

    globus_ftp_client_operationattr_set_mode(&attr,
            mode);
    done = GLOBUS_FALSE;
    result = globus_ftp_client_partial_get(&handle,
                                           src,
                                           &attr,
                                           GLOBUS_NULL,
                                           start_offset,
                                           end_offset,
                                           done_cb,
                                           0);
    if(result != GLOBUS_SUCCESS)
    {
        error = GLOBUS_TRUE;
        done = GLOBUS_TRUE;
    }
    else
    {
        globus_ftp_client_register_read(
            &handle,
            buffer,
            buffer_length,
            data_cb,
            0);
    }
    globus_mutex_lock(&lock);
    while(!done)
    {
        globus_cond_wait(&cond, &lock);
    }
    globus_mutex_unlock(&lock);

    globus_ftp_client_handle_destroy(&handle);

    globus_module_deactivate_all();

    if(test_abort_count && error)
    {
        return 0;
    }
    return error;
}