簡體   English   中英

Linux C程序模擬ls命令

[英]Linux C program emulating ls command

解決

我正在嘗試使用C創建一個與默認ls命令類似的程序。我遇到了一些屬性( -l )和當前格式的麻煩。 對於-l ,它沒有運行應該使其成為帶有inode和名稱(argv [2])的簡單列表的行,而是運行與-f相同的函數。

#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>

struct direct **dirs; 

int main (int argc, char* argv[])
{
  struct stat statb;
  int nent, i;
  int x = 0;
  int y;

  char *F = argv[1];
  char *l = argv[2];
  char *others = argv[3];

  nent = scandir (".", &dirs, NULL, NULL);

  for (i = 0; i < nent; i ++) {       
    if(argv[1]) {   
      stat(dirs[i]->d_name, &statb);  

      if (y == 0) { 
        printf("\n%-16s",dirs[i]->d_name);   
        switch (statb.st_mode & S_IFMT) { 
        case S_IFDIR :                          
          printf("/\t    ");                          
          break;     

        case S_IFREG :                          
          printf("*\t    ");                          
          break;     

        }           
      }                 
      else {  
        printf ("%-16s",dirs[i]->d_name);                   
        switch (statb.st_mode & S_IFMT) { 
        case S_IFDIR :                  
          printf("/\t    ");          
          break;             

        case S_IFREG :                      
          printf("*\t    ");          
          break;      

        }           
      }      

      x++;           
      y = x%5;         
    }         
    else if (argv[2]) {
      printf("I-node = %d   name = %s\n",    dirs[i]->d_ino, dirs[i]->d_name);
}        
else { 

  if (y == 0) {                       
    printf("\n%-16s\t    ",dirs[i]->d_name); 
  }         
  else {       
    printf("%-16s\t    ",dirs[i]->d_name); 
      }    

      x++;    
      y = x%5;      

    }
  } 

  printf("\n");
}

你永遠不會輸入else子句。 如果argv[1]為NULL,則argv[2]將為NULL。 你只需要解析參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM