genericopenlibs/cstdlib/TSTLIB/WSHELL.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * William's Shell!
       
    16 * Simple Directory Lister, from the GNU C help file 
       
    17 * 
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 #include <stddef.h>
       
    24 #include <stdio.h>
       
    25 #include <sys/types.h>
       
    26 #include <dirent.h>
       
    27 
       
    28 int
       
    29 dir (void)
       
    30 {
       
    31   DIR *dp;
       
    32   struct dirent *ep;
       
    33 
       
    34   dp = opendir ("./");
       
    35   if (dp != NULL)
       
    36     {
       
    37       while (ep = readdir (dp))
       
    38         puts (ep->d_name);
       
    39       (void) closedir (dp);
       
    40     }
       
    41   else
       
    42     puts ("Couldn't open the directory.");
       
    43 
       
    44   return 0;
       
    45 }
       
    46 /**
       
    47 @SYMTestCaseID          SYSLIB-STDLIB-CT-1114
       
    48 @SYMTestCaseDesc	    Tests for command shell behaviour
       
    49 @SYMTestPriority 	    High
       
    50 @SYMTestActions  	    Tests for command shell behaviour
       
    51 @SYMTestExpectedResults Test must not fail
       
    52 @SYMREQ                 REQ0000
       
    53 */		
       
    54 /* A silly shell-like thing */
       
    55 
       
    56 #include <stdlib.h>	/* definition of exit() */
       
    57 #include <stdio.h>
       
    58 #include <errno.h>
       
    59 #include <string.h>
       
    60 
       
    61 #include <unistd.h>	/* for getcwd */
       
    62 #include <sys/stat.h>	/* for mkdir */
       
    63 
       
    64 int main(int argc, char*argv[])
       
    65 	{
       
    66 	char cmd[80];
       
    67 	char path[MAXPATHLEN+1];
       
    68 	int x;
       
    69 
       
    70 	for(;;) 
       
    71 		{
       
    72 		printf("%s> ", getcwd(path,sizeof(path)));
       
    73 		x = scanf("%80s%s", cmd, path);
       
    74 
       
    75 		if (x!=2)
       
    76 			{
       
    77 			printf("\nerror\n");
       
    78 			continue;
       
    79 			}
       
    80 
       
    81 		if (strcmp(cmd,"exit")==0) 
       
    82 			break;
       
    83 		else
       
    84 		if (strcmp(cmd,"ver")==0) 
       
    85 			{
       
    86 			printf("DOS version 3.30\n");
       
    87 			continue;
       
    88 			}
       
    89 		else
       
    90 		if (strcmp(cmd,"dir")==0) 
       
    91 			{
       
    92 			dir();
       
    93 			continue;
       
    94 			}
       
    95 		else
       
    96 		if (strcmp(cmd,"date")==0) 
       
    97 			{
       
    98 			time_t now=time(0);
       
    99 			printf("%s",ctime(&now));
       
   100 			continue;
       
   101 			}
       
   102 		else
       
   103 		if (strcmp(cmd,"cd")==0) 
       
   104 			x = chdir(path);
       
   105 		else
       
   106 		if (strcmp(cmd,"mkdir")==0) 
       
   107 			x = mkdir(path, 0x777);
       
   108 		else
       
   109 		if (strcmp(cmd,"rmdir")==0) 
       
   110 			x = rmdir(path);
       
   111 		else
       
   112 			{
       
   113 			printf("Unrecognised command >%s<\n", cmd);
       
   114 			continue;
       
   115 			}
       
   116 		if (x!=0)
       
   117 			perror(path);
       
   118 		}
       
   119 	}