Subversion Repositories Kolibri OS

Rev

Rev 6857 | 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.  
  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) )
  30.         k70.p21 = cur_dir;
  31. else
  32.         k70.p21 = dir;
  33.  
  34. result = kol_file_70(&k70);
  35. if ( !((result==0) || (result==6)) ) // check does the directory exists
  36.         {
  37.         free( (void*) k70.p16);
  38.         return FALSE;
  39.         }
  40.  
  41. n =  (unsigned*) (k70.p16+8);
  42. num_of_file = *n;
  43.  
  44. // now read full directory
  45. k70.p12 = num_of_file;  
  46. free( (void*) k70.p16);
  47. k70.p16 =  (unsigned) malloc(32+k70.p12*560);
  48. if ( !k70.p16 )
  49.         return FALSE;
  50.        
  51. result = kol_file_70(&k70);
  52. if ( !((result==0) || (result==6)) )
  53.         {
  54.         free( (void*) k70.p16);
  55.         return FALSE;
  56.         }
  57.  
  58. // if there was '-1' param, then show single column mode
  59. // otherwise show files in several columns
  60. if (single_column_mode == TRUE)
  61. {
  62. _SINGLE_COLUMN_MODE:
  63.         for (i = 0; i < num_of_file; i++)
  64.         {
  65.         printf ("  %s", k70.p16+32+40+(264+40)*i);
  66.         t =  (unsigned*) (k70.p16+32+(264+40)*i);
  67.         type_of_file = *t;
  68.         if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) )
  69.                 printf ("/");
  70.         printf ("\n\r");
  71.         }
  72. }
  73. else
  74. {
  75.         int longest_name_len = 0;
  76.         int console_window_width = 78; //need to get this value from console.obj if it's possible
  77.         for (i = 0; i < num_of_file; i++)
  78.                 {
  79.                 int current_name_len;
  80.                 current_name_len = strlen( (char*)k70.p16+32+40+(264+40)*i);
  81.                 if (current_name_len > longest_name_len) longest_name_len = current_name_len;
  82.                 }
  83.  
  84.         longest_name_len+=2; //consider space separator and '/' symbol for folders
  85.         int columns_max = console_window_width / longest_name_len;
  86.  
  87.         if (longest_name_len >= console_window_width) goto _SINGLE_COLUMN_MODE; //there was too long filename
  88.  
  89.         for (i = 0; i < num_of_file; i++)
  90.                 {
  91.                 char cur_file[2048];
  92.                 strncpy(cur_file, (char*)k70.p16+32+40+(304)*i, sizeof(cur_file)-2);
  93.  
  94.                 t =  (unsigned*) (k70.p16+32+(304)*i);
  95.                 type_of_file = *t;
  96.  
  97.                 if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) ) strcat(cur_file, "/");
  98.  
  99.                 printf ("%*s", -longest_name_len, cur_file);
  100.  
  101.                 if ((i>0) && ((i+1)%columns_max == 0)) printf ("\n\r");
  102.                 }      
  103.         if ((i)%columns_max != 0) printf("\n\r");
  104. }
  105.  
  106. free((void*)k70.p16);
  107. return TRUE;
  108. }
  109.  
  110.