tw_cli_webclnt.c.svn-base
资源名称:cli.rar [点击查看]
上传用户:hujq123
上传日期:2016-04-10
资源大小:153k
文件大小:5k
源码类别:
Telnet服务器
开发平台:
Unix_Linux
- /*
- * Copyright (C) 2002 Koninklijke Philips Electronics N.V.,
- * All Rights Reserved.
- *
- * This source code and any compilation or derivative thereof is the
- * proprietary information of Koninklijke Philips Electronics N.V.
- * and is confidential in nature.
- * Under no circumstances is this software to be exposed to or placed
- * under an Open Source License of any type without the expressed
- * written permission of Koninklijke Philips Electronics N.V.
- *
- *###########################################################
- */
- /*!
- * file tw_cli_webclnt.c
- *
- * brief -
- *
- */
- /*-----------------------------------------------------------*
- *
- * %version: 3 %
- * instance: DS_5
- * %date_created: Fri Feb 21 08:04:47 2003 %
- *
- *###########################################################
- */
- /* tw_cli_webclnt.c
- web client command line
- wget and wput
- */
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <psos.h>
- #include <sys_conf.h>
- #include <tw_defs.h>
- #include <tw_cli.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <ops/custom_ops.h>
- #include <tw_cycles.h>
- #include <socket_app.h>
- #include <netdb.h>
- #include <inet.h>
- #include <web_client.h>
- /* local definitions */
- #define MAX_WGET_ARGS 7
- #define MAX_URL_LEN 256
- #define MAX_NAME_LEN 128
- #define MAX_BUF_LEN 1024
- /* Structures needed for cli */
- static int CliCoProc_Wget(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliRco_Wget =
- {
- "wget",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "usage: wget [-q] <url> [filename]",
- NULL,
- NULL,
- CliCoProc_Wget,
- };
- /* get_args
- take a command line string and build a list of parameters
- so that I can more easily port code from Linux
- */
- int
- get_args(char *str, char **argv)
- {
- int argc;
- char *p;
- argc = 0;
- for(p = str; *p != ' ';)
- {
- argv[argc] = p;
- while(!isspace(*p) && (*p != ' '))
- {
- p++;
- }
- argc++;
- if(argc == MAX_WGET_ARGS)
- {
- return -1;
- }
- if(*p != ' ')
- {
- /* NULL terminate the string */
- *p = ' ';
- p++;
- }
- while(isspace(*p) && (*p != ' '))
- {
- p++;
- }
- }
- return argc;
- }
- int
- put_args(char **argv, int argc)
- {
- int i;
- char *p;
- /* set every argv value but the last one to
- have a space instead of the NULL termination
- */
- for(i=0; i<(argc-1); i++)
- {
- p = argv[i];
- while(*p != ' ') p++;
- *p = ' ';
- }
- return 0;
- }
- static int
- CliCoProc_Wget(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- int argc, i, s;
- char *argv[MAX_WGET_ARGS];
- char uri[MAX_URL_LEN];
- char name[MAX_NAME_LEN];
- char buf[MAX_BUF_LEN];
- int proto = 0;
- int quiet = 0, sts, err;
- int len;
- int loops;
- int bytes;
- unsigned start_cycles ;
- float bandwidth, secs;
- memset(uri, 0, sizeof(uri));
- memset(name, 0, sizeof(name));
- memset(buf, 0, sizeof(buf));
- argc = get_args(CmdStr, argv);
- if(argc < 0)
- {
- Cli_Printf(pSess,"wget: too many argumentsn");
- return 0;
- }
- for(i=1; i<argc; i++)
- {
- switch(argv[i][0])
- {
- case '-':
- switch(argv[i][1])
- {
- case 'q':
- quiet = 1;
- break;
- }
- break;
- default:
- proto = parse_url(argv[i], name, uri);
- break;
- }
- }
- put_args(argv, argc);
- switch(proto)
- {
- case HTTP_PROTO:
- /* s = webclnt_open(name, HTTP_DEFAULT_PORT);*/
- s = webclnt_open(name, 6454);
- if(s < 0)
- {
- Cli_Printf(pSess, "wget: couldn't open %sn", name);
- return 0;
- }
- len = webclnt_get(s, name, uri);
- if(len < 0)
- {
- Cli_Printf(pSess, "wget: get failed %sn", uri);
- }
- Cli_Printf(pSess,"wget: %s %s %dn", name, uri, len);
- /* while(len > 0)*/
- bytes = 0 ;
- loops = 0 ;
- start_cycles = cycles() ;
- while (TRUE)
- {
- printf("bytes %08x loops %08x cycles %08xn",
- bytes, loops, cycles()-start_cycles) ;
- sts = webclnt_read(s, buf, sizeof(buf));
- if(sts < 0)
- {
- Cli_Printf(pSess, "wget: recv failed from servern");
- break;
- }
- /* len -= sts;*/
- len -= sts ;
- bytes += sts ;
- loops++ ;
- if(!quiet)
- printf("%s", buf);
- }
- printf("donen") ;
- webclnt_close(s);
- break;
- default:
- Cli_Printf(pSess, "Unsupported protocoln");
- break;
- }
- return 0;
- }