tw_cli_fs.c.svn-base
资源名称:cli.rar [点击查看]
上传用户:hujq123
上传日期:2016-04-10
资源大小:153k
文件大小:11k
源码类别:
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_fs.c
- *
- * brief -
- *
- */
- /*-----------------------------------------------------------*
- *
- * %version: 4 %
- * instance: DS_6
- * %date_created: Fri Feb 21 08:04:52 2003 %
- *
- *###########################################################
- */
- /****************************************************************************
- *
- * Filename : tw_cli_fs.c
- * Purpose : CLI command objects and functions to support FS commands.
- *
- *
- *****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <string.h>
- #include <errno.h>
- #include <tmtypes.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <tw_cli.h>
- #include <crc32.h>
- #include "tm_common.h"
- /*** 2nd level CLI Command Option - fs "copy"
- ***/
- static int CliCoVoproc_FsCopy(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsCopy =
- {
- "copy",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "copy a file {FROM_FILENAME TO_FILENAME}",
- NULL,
- NULL,
- CliCoVoproc_FsCopy
- };
- /*** 2nd level CLI Command Option - fs "crc"
- ***/
- static int CliCoVoproc_FsCrc(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsCrc =
- {
- "crc",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "calculate crc of {FILE}",
- NULL,
- NULL,
- CliCoVoproc_FsCrc
- };
- /*** 2nd level CLI Command Option - fs "dir"
- ***/
- static int CliCoVoproc_FsDir(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsDir =
- {
- "dir",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "display directory {DIRECTORY}",
- NULL,
- NULL,
- CliCoVoproc_FsDir
- };
- /*** 2nd level CLI Command Option - fs "delete"
- ***/
- static int CliCoVoproc_FsDelete(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsDelete =
- {
- "delete",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "delete a file {FILENAME}",
- NULL,
- NULL,
- CliCoVoproc_FsDelete
- };
- /*** 2nd level CLI Command Option - fs "dump"
- ***/
- static int CliCoVoproc_FsDump(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsDump =
- {
- "dump",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "dump a file {FILENAME}",
- NULL,
- NULL,
- CliCoVoproc_FsDump
- };
- /*** 2nd level CLI Command Option - fs "test"
- ***/
- static int CliCoVoproc_FsTest(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsTest =
- {
- "test",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "write/read test filesystem {FILECOUNT}",
- NULL,
- NULL,
- CliCoVoproc_FsTest
- };
- /*** 2nd level CLI Command Option - fs "type"
- ***/
- static int CliCoVoproc_FsType(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr);
- Cli_CmdStruct_t CliCo_FsType =
- {
- "type",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- NULL,
- "print the content of a file {FILENAME}",
- NULL,
- NULL,
- CliCoVoproc_FsType
- };
- /*** 1st level CLI Command Option - "fs"
- ***/
- Cli_CmdStruct_t *CliRcoOptionList_Fs[] =
- {
- &CliCo_FsCopy,
- &CliCo_FsCrc,
- &CliCo_FsDelete,
- &CliCo_FsDir,
- &CliCo_FsDump,
- &CliCo_FsTest,
- &CliCo_FsType,
- NULL
- };
- Cli_CmdStruct_t CliRco_Fs =
- {
- "fs",
- ALL_ACCESS_MODE | ALL_PRODUCT_TYPE,
- CliRcoOptionList_Fs,
- "File system manipulation command",
- NULL,
- NULL,
- NULL
- };
- static int CliCoVoproc_FsType(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- char FileName[96];
- if (sscanf(CmdStr + Ptr, "%s", FileName) != 1)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- Cli_PrintFileContent(pSess, FileName);
- return 0;
- }
- #define COPY_BUFF_LEN 4096
- int CopyFile (const char *FileName, const char* NewFileName)
- {
- FILE *InStream;
- FILE *OutStream;
- char *Line ;
- int ReadBytes ;
- int WriteBytes ;
- int TotalBytes ;
- int status ;
- if ( (InStream = fopen ( FileName, "r")) == NULL)
- {
- return 1 ;
- }
- if ( (OutStream = fopen ( NewFileName, "w")) == NULL)
- {
- return 1 ;
- }
- Line = malloc(COPY_BUFF_LEN) ;
- if (Line == NULL)
- {
- return 1 ;
- }
- TotalBytes = 0 ;
- status = 1 ;
- while (True)
- {
- ReadBytes = fread(Line, 1, COPY_BUFF_LEN, InStream) ;
- if (ReadBytes == 0)
- {
- break ;
- }
- WriteBytes = fwrite(Line, 1, ReadBytes, OutStream) ;
- if (WriteBytes != ReadBytes)
- {
- status = 0 ;
- break ;
- }
- TotalBytes += ReadBytes ;
- }
- fclose (OutStream);
- fclose (InStream);
- free(Line) ;
- return status;
- }
- static int CliCoVoproc_FsCopy(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- char FromFileName[96], ToFileName[96];
- if (sscanf(CmdStr + Ptr, "%s %s",
- FromFileName, ToFileName) != 2)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- if (CopyFile(FromFileName, ToFileName) != 0)
- {
- Cli_Printf(pSess, Cli_FailedStr);
- return 0;
- }
- Cli_Printf(pSess, "copied from %s to %sn",
- FromFileName, ToFileName);
- return 0;
- }
- static int CliCoVoproc_FsCrc(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- char FileName[64] ;
- UInt8 Data ;
- FILE *File ;
- UInt Bytes ;
- crc_t Crc ;
- if (sscanf(CmdStr + Ptr, "%s", FileName) != 1)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- File = fopen(FileName, "rb") ;
- if (File == NULL)
- {
- Cli_Printf(pSess, "Could not open file %sn", FileName) ;
- return 0 ;
- }
- Bytes = 0 ;
- crc32_init(&Crc) ;
- while (!feof(File))
- {
- if (fread(&Data, 1, 1, File) != 1)
- {
- break ;
- }
- crc32_add(&Crc, Data) ;
- Bytes++ ;
- }
- crc32_end(&Crc) ;
- fclose(File) ;
- printf("CRC %08x Size %08x File %sn", Crc, Bytes, FileName) ;
- return 0;
- }
- static int CliCoVoproc_FsDump(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- char FileName[64] ;
- UInt8 Data ;
- FILE *File ;
- UInt Offset ;
- if (sscanf(CmdStr + Ptr, "%s", FileName) != 1)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- File = fopen(FileName, "rb") ;
- if (File == NULL)
- {
- Cli_Printf(pSess, "Could not open file %sn", FileName) ;
- return 0 ;
- }
- Offset = 0 ;
- while (!feof(File))
- {
- if ((Offset & 0xf) == 0)
- {
- Cli_Printf(pSess, "%07o", Offset) ;
- }
- if (fread(&Data, 1, 1, File) != 1)
- {
- break ;
- }
- Cli_Printf(pSess, " %02x", Data) ;
- Offset++ ;
- if ((Offset & 0xf) == 0)
- {
- Cli_Printf(pSess, "n") ;
- }
- }
- fclose(File) ;
- if ((Offset & 0xf) != 0)
- {
- Cli_Printf(pSess, "n") ;
- }
- return 0;
- }
- static int CliCoVoproc_FsDir(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- DIR *pDir;
- struct dirent *pDirEntry;
- char DirName[64], FullFileName[96];
- struct stat StatBuf;
- if (sscanf(CmdStr + Ptr, "%s", DirName) != 1)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- pDir = opendir(DirName);
- if (pDir == NULL)
- {
- Cli_Printf(pSess, "opendir() of %s failed: %sn",
- DirName,
- _sys_errlist[errno]) ;
- return 0;
- }
- while ((pDirEntry = readdir(pDir)) != NULL)
- {
- sprintf(FullFileName, "%s/%s", DirName, pDirEntry->d_name);
- if (stat(FullFileName, &StatBuf) != 0)
- {
- continue;
- }
- Cli_Printf(pSess, "%-20s", pDirEntry->d_name);
- if ((StatBuf.st_mode & S_IFMT) == S_IFDIR)
- {
- Cli_Printf(pSess, "<DIR>n");
- }
- else
- {
- Cli_Printf(pSess, " %d bytesn", StatBuf.st_size);
- }
- }
- closedir(pDir);
- return 0;
- }
- static int CliCoVoproc_FsDelete(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- char FullFileName[96], Ans;
- if (sscanf(CmdStr + Ptr, "%s", FullFileName) != 1)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- Cli_Printf(pSess, "Press Y to confirm...n");
- Ans = Cli_GetChar(pSess);
- if ((Ans == 'y') || (Ans == 'Y'))
- {
- if (unlink(FullFileName) == 0)
- {
- Cli_Printf(pSess, "%s removedn", FullFileName);
- }
- else
- {
- Cli_Printf(pSess, Cli_FailedStr);
- }
- }
- else
- {
- Cli_Printf(pSess, "Delete operation abortedn");
- }
- return 0;
- }
- /*****************************************************************************
- *
- * ffs_test_task - Flash file system test task
- *
- * Returns: nothing.
- *
- * Description:
- * This routine runs a flash file system test
- *
- * History:
- * 11/27/99 jvs Initial revision
- *
- *****************************************************************************/
- #define FFS_TEST_TASK_COUNT 4
- #define __FFS_TEST_IN_TW_CLI_FS__
- #include "ffs_tst.c"
- static void ffs_test_task(FfsTstPrintf_t Printf, void *Handle, int Task, int FileCount)
- {
- srand(Task) ;
- ffs_test(Printf, Handle, Task, FileCount) ;
- t_delete(0) ;
- }
- /*****************************************************************************
- *
- * ffs_test_start - Start flash file system test task
- *
- * Returns: nothing.
- *
- * Description:
- * This routine starts a task to
- *
- * History:
- * 11/27/99 jvs Initial revision
- *
- *****************************************************************************/
- static void ffs_test_start(FfsTstPrintf_t Printf, void *Handle, int Task, int FileCount)
- {
- char TaskName[5] ;
- ULONG TaskId ;
- ULONG Status ;
- ULONG Args[4] ;
- sprintf(TaskName, "fst%x", Task) ;
- Status = t_create(TaskName, PRIORITY_NORMAL, 4096, 4096, T_LOCAL, &TaskId);
- if (Status != 0)
- {
- Printf(Handle, "Could not start task '%s', error %08xn",
- TaskName, Status) ;
- }
- else
- {
- Args[0] = (ULONG) Printf ;
- Args[1] = (ULONG) Handle ;
- Args[2] = (ULONG) Task ;
- Args[3] = (ULONG) FileCount ;
- Status = t_start(TaskId, T_PREEMPT | T_TSLICE, ffs_test_task, Args);
- }
- }
- /*****************************************************************************
- *
- * ffs_test - test flash file system
- *
- * Returns: nothing.
- *
- * Description:
- * This runs a 100 file test of the flash file system.
- *
- * History:
- * 11/27/99 jvs Initial revision
- *
- *****************************************************************************/
- static int CliCoVoproc_FsTest(Cli_cmdSession_t *pSess, char *CmdStr, int Ptr)
- {
- UInt FileCount ;
- UInt Task ;
- if (sscanf(CmdStr + Ptr, "%d", &FileCount) != 1)
- {
- Cli_Printf(pSess, badParamStr);
- return 0;
- }
- for (Task = 0; Task < FFS_TEST_TASK_COUNT; Task++)
- {
- ffs_test_start((FfsTstPrintf_t) Cli_Printf, pSess, Task, FileCount) ;
- }
- return 0;
- }
- /*****************************************************************************/