Changeset 611 for trunk/user/ksh


Ignore:
Timestamp:
Jan 9, 2019, 3:02:51 PM (5 years ago)
Author:
alain
Message:

Introduce sigificant modifs in VFS to support the <ls> command,
and the . and .. directories entries.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/user/ksh/ksh.c

    r610 r611  
    4343#include <signal.h>
    4444#include <unistd.h>
     45#include <dirent.h>
    4546#include <almosmkh.h>
    4647#include <semaphore.h>
     
    5455#define MAX_ARGS           (32)     // max number of arguments in a command
    5556
    56 #define MAIN_DEBUG          0
    57 
    58 #define CMD_CAT_DEBUG       0
    59 #define CMD_CP_DEBUG        0
    60 #define CMD_LOAD_DEBUG      0
     57#define DEBUG_MAIN          0
     58
     59#define DEBUG_CMD_CAT       0
     60#define DEBUG_CMD_CP        0
     61#define DEBUG_CMD_LOAD      0
     62#define DEBUG_CMD_LS        0
    6163
    6264//////////////////////////////////////////////////////////////////////////////////////////
     
    105107{
    106108        char         * path;
    107     stat_t         st;      // stat structure
     109    struct stat    st;
    108110    int            fd;
    109111    int            size;
     
    116118        size = 0;
    117119                printf("  usage: cat pathname\n");
    118         goto exit;
     120            goto cmd_cat_exit;
    119121    }
    120122
     
    127129        buf  = NULL;
    128130        size = 0;
    129             printf("  error: cannot open %s\n", path);
    130             goto exit;
    131     }
    132 
    133 #if CMD_CAT_DEBUG
     131            printf("  error: cannot open file <%s>\n", path);
     132            goto cmd_cat_exit;
     133    }
     134
     135#if DEBUG_CMD_CAT
    134136long long unsigned cycle;
    135137get_cycle( &cycle );
     
    143145        buf  = NULL;
    144146        size = 0;
    145             printf("  error: cannot stat %s\n", path);
    146             goto exit;
     147            printf("  error: cannot stat <%s>\n", path);
     148            goto cmd_cat_exit;
    147149    }
    148150
     
    151153        buf  = NULL;
    152154        size = 0;
    153             printf("  error: %s is a directory\n", path);
    154             goto exit;
     155            printf("  error: <%s> is a directory\n", path);
     156            goto cmd_cat_exit;
    155157    }
    156158
     
    158160    size = st.st_size;
    159161
    160 #if CMD_CAT_DEBUG
     162#if DEBUG_CMD_CAT
    161163get_cycle( &cycle );
    162164printf("\n[%s] get size %d / cycle %d\n",
     
    169171    if ( buf == NULL )
    170172    {
    171             printf("  error: cannot map %s\n", path );
    172             goto exit;
    173     }
    174 
    175 #if CMD_CAT_DEBUG
     173            printf("  error: cannot map file <%s>\n", path );
     174            goto cmd_cat_exit;
     175    }
     176
     177#if DEBUG_CMD_CAT
    176178get_cycle( &cycle );
    177179printf("\n[%s] map file %d to buffer %x / cycle %d\n",
     
    188190    return;
    189191
    190 exit:
     192cmd_cat_exit:
    191193
    192194        if (buf != NULL) munmap(buf, size);
     
    226228static void cmd_cp(int argc, char **argv)
    227229{
    228         int    src_fd;
    229     int    dst_fd;
    230         char * srcpath;
    231     char * dstpath;
    232         int    size;          // source file size
    233         int    bytes;         // number of transfered bytes
    234         char   buf[4096];
    235         stat_t st;
     230        int          src_fd;
     231    int          dst_fd;
     232        char       * srcpath;
     233    char       * dstpath;
     234        int          size;          // source file size
     235        int          bytes;         // number of transfered bytes
     236        char         buf[4096];
     237        struct stat st;
    236238
    237239        if (argc != 3)
     
    240242        dst_fd = -1;
    241243                printf("  usage: cp src_pathname dst_pathname\n");
    242         goto exit;
     244        goto cmd_cp_exit;
    243245        }
    244246
     
    252254    {
    253255        dst_fd = -1;
    254             printf("  error: cannot open %s\n", srcpath );
    255             goto exit;
    256     }
    257 
    258 #if CMD_CP_DEBUG
     256            printf("  error: cannot open <%s>\n", srcpath );
     257            goto cmd_cp_exit;
     258    }
     259
     260#if DEBUG_CMD_CP
    259261long long unsigned cycle;
    260262get_cycle( &cycle );
     
    267269    {
    268270        dst_fd = -1;
    269             printf("  error: cannot stat %s\n", srcpath);
    270             goto exit;
    271     }
    272 
    273 #if CMD_CP_DEBUG
     271            printf("  error: cannot stat <%s>\n", srcpath);
     272            goto cmd_cp_exit;
     273    }
     274
     275#if DEBUG_CMD_CP
    274276get_cycle( &cycle );
    275277printf("\n[%s] stats file <%s> done / cycle %d\n",
     
    280282    {
    281283        dst_fd = -1;
    282                 printf("  error: %s is a directory\n", srcpath);
    283                 goto exit;
     284                printf("  error: <%s> is a directory\n", srcpath);
     285                goto cmd_cp_exit;
    284286        }
    285287
     
    292294        if ( dst_fd < 0 )
    293295    {
    294                 printf("  error: cannot open %s\n", dstpath );
    295                 goto exit;
    296         }
    297 
    298 #if CMD_CP_DEBUG
     296                printf("  error: cannot open <%s>\n", dstpath );
     297                goto cmd_cp_exit;
     298        }
     299
     300#if DEBUG_CMD_CP
    299301get_cycle( &cycle );
    300302printf("\n[%s] open file <%s> done / cycle %d\n",
     
    304306        if ( stat( dstpath , &st ) )
    305307    {
    306                 printf("  error: cannot stat %s\n", dstpath );
    307                 goto exit;
    308         }
    309 
    310 #if CMD_CP_DEBUG
     308                printf("  error: cannot stat <%s>\n", dstpath );
     309                goto cmd_cp_exit;
     310        }
     311
     312#if DEBUG_CMD_CP
    311313get_cycle( &cycle );
    312314printf("\n[%s] stats file <%s> done / cycle %d\n",
     
    316318        if ( S_ISDIR(st.st_mode ) )
    317319    {
    318                 printf("  error: %s is a directory\n", dstpath );
    319                 goto exit;
     320                printf("  error: <%s> is a directory\n", dstpath );
     321                goto cmd_cp_exit;
    320322        }
    321323
     
    329331                if ( read( src_fd , buf , len ) != len )
    330332        {
    331                         printf("  error: cannot read from file %s\n", srcpath);
    332                         goto exit;
     333                        printf("  error: cannot read from file <%s>\n", srcpath);
     334                        goto cmd_cp_exit;
    333335                }
    334336
    335 #if CMD_CP_DEBUG
     337#if DEBUG_CMD_CP
    336338get_cycle( &cycle );
    337339printf("\n[%s] %d bytes read from <%s> / cycle %d\n",
     
    342344                if ( write( dst_fd , buf , len ) != len )
    343345        {
    344                         printf("  error: cannot write to file %s\n", dstpath);
    345                         goto exit;
     346                        printf("  error: cannot write to file <%s>\n", dstpath);
     347                        goto cmd_cp_exit;
    346348                }
    347349
    348 #if CMD_CP_DEBUG
     350#if DEBUG_CMD_CP
    349351get_cycle( &cycle );
    350352printf("\n[%s] %d bytes writen to <%s> / cycle %d\n",
     
    355357        }
    356358
    357 exit:
     359cmd_cp_exit:
    358360
    359361        if (src_fd >= 0) close(src_fd);
     
    370372    if( argc < 2 )
    371373    {
    372         printf("  usage: display  vmm      cxy  pid   \n"
    373                "         display  sched    cxy  lid   \n"             
    374                "         display  process  cxy        \n"             
    375                "         display  txt      txtid      \n"             
    376                "         display  vfs                 \n"             
    377                "         display  chdev               \n"             
    378                "         display  dqdt                \n"             
    379                "         display  locks    pid  trdid \n");
     374        printf("  usage: display  vmm      cxy    pid\n"
     375               "         display  sched    cxy    lid\n"             
     376               "         display  process  cxy\n"             
     377               "         display  txt      txtid\n"             
     378               "         display  vfs\n"             
     379               "         display  chdev\n"             
     380               "         display  dqdt\n"             
     381               "         display  locks    pid    trdid\n"
     382               "         display  mapper   path   page_id  nbytes\n");
    380383    }
    381384    ////////////////////////////////////
     
    500503            {
    501504                printf("  error: illegal arguments pid = %x / trdid = %x\n", pid, trdid );
     505            }
     506        }
     507    }
     508    ///////////////////////////////////////////
     509    else if( strcmp( argv[1] , "mapper" ) == 0 )
     510    {
     511        if( argc != 5 )
     512        {
     513                    printf("  usage: display mapper path page_id nbytes\n");
     514            }
     515        else
     516        {
     517                unsigned int page_id   = atoi(argv[3]);
     518            unsigned int nbytes    = atoi(argv[4]);
     519
     520            if( display_mapper( argv[2] , page_id, nbytes ) )
     521            {
     522                printf("  error: cannot display page %d of mapper %s\n", page_id, argv[2] );
    502523            }
    503524        }
     
    643664        ksh_pid = getpid();
    644665
    645 #if CMD_LOAD_DEBUG
     666#if DEBUG_CMD_LOAD
    646667long long unsigned cycle;
    647668get_cycle( &cycle );
    648 printf("\n[KSH] %s : ksh_pid %x / path %s / bg %d / place %d (%x) / cycle %d\n",
     669printf("\n[ksh] %s : ksh_pid %x / path %s / bg %d / place %d (%x) / cycle %d\n",
    649670__FUNCTION__, ksh_pid, argv[1], background, placement, cxy, (int)cycle );
    650671#endif
     
    663684        {
    664685
    665 #if CMD_LOAD_DEBUG
     686#if DEBUG_CMD_LOAD
    666687get_cycle( &cycle );
    667 printf("\n[KSH] %s : child_pid %x after fork, before exec / cycle %d\n",
     688printf("\n[ksh] %s : child_pid %x after fork, before exec / cycle %d\n",
    668689__FUNCTION__ , getpid(), (int)cycle );
    669690#endif
     
    672693            ret_exec = execve( pathname , NULL , NULL );
    673694
    674 #if CMD_LOAD_DEBUG
     695#if DEBUG_CMD_LOAD
    675696get_cycle( &cycle );
    676 printf("\n[KSH] %s : child_pid %x after exec / ret_exec %d / cycle %d\n",
     697printf("\n[ksh] %s : child_pid %x after exec / ret_exec %d / cycle %d\n",
    677698__FUNCTION__ , getpid(), ret_exec, (int)cycle );
    678699#endif
     
    688709        {
    689710
    690 #if CMD_LOAD_DEBUG
     711#if DEBUG_CMD_LOAD
    691712get_cycle( &cycle );
    692 printf("\n[KSH] %s : ksh_pid %x after fork / ret_fork %x / cycle %d\n",
     713printf("\n[ksh] %s : ksh_pid %x after fork / ret_fork %x / cycle %d\n",
    693714__FUNCTION__, getpid(), ret_fork, (int)cycle );
    694715#endif
     
    733754static void cmd_ls( int argc , char **argv )
    734755{
    735         char  * path;
    736 
    737 //  struct dirent * file;
    738 //  DIR *dir;
    739 
    740         if (argc > 2 )
     756        char           * pathname;
     757    struct dirent  * entry;
     758    DIR            * dir;
     759
     760        if (argc != 2 )
    741761    {
    742762                printf("  usage: ls [path]\n");
     
    744764    else
    745765    {
    746         if ( argc == 1 ) path = ".";
    747         else             path = argv[1];
    748 
    749         printf("  error: not implemented yet\n");
    750 /*
    751         dir = opendir( path );
    752         while ((file = readdir(dir)) != NULL)
    753         {
    754                 printf(" %s\n", file->d_name);
    755         }
    756         closedir(dir);
    757 */
    758     }
     766
     767// handle case with no argument
     768// TODO if ( argc == 1 ) path = ".";
     769
     770        // get target directory path
     771        pathname = argv[1];
     772
     773        // open target directory
     774            dir = opendir( pathname );
     775
     776#if DEBUG_CMD_LS
     777printf("\n[ksh] %s : directory <%s> open / DIR %x\n",
     778__FUNCTION__, pathname , dir );
     779#endif
     780
     781        if( dir == NULL)
     782            {
     783                    printf("  error : directory <%s> not found\n", pathname );
     784            goto cmd_ls_exit;
     785            }
     786
     787        // loop on directory entries   
     788        while ( (entry = readdir(dir)) != NULL )
     789            {
     790                    printf(" - %s\n", entry->d_name);
     791            }
     792
     793        // close target directory
     794            closedir( dir );
     795
     796#if DEBUG_CMD_LS
     797printf("\n[ksh] %s : directory <%s> closed\n",
     798__FUNCTION__, pathname );
     799#endif
     800
     801    }
     802
     803cmd_ls_exit:
    759804
    760805    // release semaphore to get next command
     
    887932        if ( unlink( pathname ) )
    888933        {
    889                     printf("  error: unable to remove %s\n", pathname );
     934                    printf("  error: unable to remove <%s>\n", pathname );
    890935            }
    891936    }
     
    10481093
    10491094
    1050 // To lauch one command without interactive mode
     1095/* To lauch one command without interactive mode
    10511096   
    10521097if( sem_wait( &semaphore ) )
     
    10571102else
    10581103{
    1059     printf("\n[ksh] cp /home/Makefile /home/bloup\n");
     1104    printf("\n[ksh] ls bin/user\n");
    10601105}
    10611106
    1062 strcpy( buf , "cp /home/Makefile /home/bloup" );
     1107strcpy( buf , "ls bin/user" );
    10631108parse( buf );
    10641109
    1065 //
     1110*/
    10661111
    10671112        enum fsm_states
     
    12701315    get_core( &cxy , &lid );
    12711316
    1272 #if MAIN_DEBUG
     1317#if DEBUG_MAIN
    12731318printf("\n[ksh] main started on core[%x,%d]\n", cxy , lid );
    12741319#endif
     
    12811326    }
    12821327
    1283 #if MAIN_DEBUG
     1328#if DEBUG_MAIN
    12841329printf("\n[ksh] main initialized semaphore\n" );
    12851330#endif
     
    12941339                    &interactive,   // entry function
    12951340                    NULL );
    1296 #if MAIN_DEBUG
     1341#if DEBUG_MAIN
    12971342printf("\n[ksh] main launched interactive thread => wait children termination\n" );
    12981343#endif
     
    13071352        child_pid = wait( &status );
    13081353
    1309 #if MAIN_DEBUG
     1354#if DEBUG_MAIN
    13101355if( WIFEXITED  (status) ) printf("\n[ksh] child process %x exit\n"   , child_pid );
    13111356if( WIFSIGNALED(status) ) printf("\n[ksh] child process %x killed\n" , child_pid );
Note: See TracChangeset for help on using the changeset viewer.