Subversion Repositories Kolibri OS

Rev

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