Subversion Repositories Kolibri OS

Rev

Rev 7802 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. int cmd_ls(char dir[]) {
  3.  
  4.         kol_struct70    k70;
  5.         unsigned        *n;
  6.         unsigned        num_of_file; // number of files in directory
  7.         unsigned        *t;
  8.         unsigned        type_of_file; // check is this a file or a folder
  9.         int             i, result;
  10.         char tmp[FILENAME_MAX];
  11.  
  12.         bool single_column_mode = FALSE;
  13.  
  14.  
  15.         k70.p00 = 1;
  16.         k70.p04 = 0;
  17.         //k70.p08 = 0;
  18.         k70.p12 = 2;  // just for test exist & read number of entries
  19.         k70.p16 =  (unsigned) malloc(32+k70.p12*560);
  20.         k70.p20 = 0;
  21.  
  22.  
  23.         if (!strnicmp(dir,"-1",1))
  24.                 {
  25.                 single_column_mode = TRUE;
  26.                 dir += 3;
  27.                 }
  28.  
  29.         if ( !strlen(dir) ) // if argument is empty, list current directory
  30.                 k70.p21 = cur_dir;
  31.         else
  32.         {
  33.                 if (dir[0] != '/') // if given directory is relative path, then append cur_dir on left side
  34.                 {
  35.                         strcpy(tmp, cur_dir);
  36.                         if (tmp[strlen(tmp)-1] != '/')
  37.                         {
  38.                                 strcat(tmp, "/"); // add slash
  39.                         }
  40.                         strcat(tmp, dir);
  41.                         k70.p21 = tmp;
  42.                 } else // if given directory is an absolute path
  43.                 {
  44.                         k70.p21 = dir;
  45.                 }
  46.         }
  47.  
  48.         result = kol_file_70(&k70);
  49.         if ( !((result==0) || (result==6)) ) // check does the directory exists
  50.                 {
  51.                 free( (void*) k70.p16);
  52.                 return FALSE;
  53.                 }
  54.  
  55.         n =  (unsigned*) (k70.p16+8);
  56.         num_of_file = *n;
  57.  
  58.         // now read full directory
  59.         k70.p12 = num_of_file;  
  60.         free( (void*) k70.p16);
  61.         k70.p16 =  (unsigned) malloc(32+k70.p12*560);
  62.         if ( !k70.p16 )
  63.                 return FALSE;
  64.                
  65.         result = kol_file_70(&k70);
  66.         if ( !((result==0) || (result==6)) )
  67.                 {
  68.                 free( (void*) k70.p16);
  69.                 return FALSE;
  70.                 }
  71.  
  72.         // if there was '-1' param, then show single column mode
  73.         // otherwise show files in several columns
  74.         if (single_column_mode == TRUE)
  75.         {
  76.         _SINGLE_COLUMN_MODE:
  77.                 for (i = 0; i < num_of_file; i++)
  78.                 {
  79.                 printf ("  %s", k70.p16+32+40+(264+40)*i);
  80.                 t =  (unsigned*) (k70.p16+32+(264+40)*i);
  81.                 type_of_file = *t;
  82.                 if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) )
  83.                         printf ("/");
  84.                 printf ("\n\r");
  85.                 }
  86.         }
  87.         else
  88.         {
  89.                 int longest_name_len = 0;
  90.                 int console_window_width = 78; //need to get this value from console.obj if it's possible
  91.                 for (i = 0; i < num_of_file; i++)
  92.                         {
  93.                         int current_name_len;
  94.                         current_name_len = strlen( (char*)k70.p16+32+40+(264+40)*i);
  95.                         if (current_name_len > longest_name_len) longest_name_len = current_name_len;
  96.                         }
  97.  
  98.                 longest_name_len+=2; //consider space separator and '/' symbol for folders
  99.                 int columns_max = console_window_width / longest_name_len;
  100.  
  101.                 if (longest_name_len >= console_window_width) goto _SINGLE_COLUMN_MODE; //there was too long filename
  102.  
  103.                 for (i = 0; i < num_of_file; i++)
  104.                         {
  105.                         char cur_file[2048];
  106.                         strncpy(cur_file, (char*)k70.p16+32+40+(304)*i, sizeof(cur_file)-2);
  107.  
  108.                         t =  (unsigned*) (k70.p16+32+(304)*i);
  109.                         type_of_file = *t;
  110.                        
  111.                         int is_folder = 0;
  112.                         if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) ) { is_folder = 1; strcat(cur_file, "/"); }
  113.  
  114.                         if (is_folder) { printf("\033[0;36m"); } // set cyan for folder
  115.                         printf ("%*s", -longest_name_len, cur_file);
  116.                         if (is_folder) { printf("\033[0m"); } // is had been set, reset
  117.  
  118.                         if ((i>0) && ((i+1)%columns_max == 0)) printf ("\n\r");
  119.                         }      
  120.                 if ((i)%columns_max != 0) printf("\n\r");
  121.         }
  122.  
  123.         free((void*)k70.p16);
  124.         return TRUE;
  125. }
  126.  
  127.