Advertisement
Guest User

Untitled

a guest
May 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. //--------------------------------------------
  2. // NAME:Konstantin Georgiev
  3. // CLASS:XIa
  4. // NUMBER:15
  5. // PROBLEM:#1
  6. // FILE NAME:m.c
  7. // FILE PURPOSE: ls unix function implementation in C
  8. //---------------------------------------------
  9. #include<stdio.h>
  10. #include<dirent.h>
  11. #include<stdlib.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. //--------------------------------------------
  17. // FUNCTION: printNormalContents
  18. // prints a directory's contents
  19. // PARAMETERS:
  20. // char *thing - The name of the directory to be opened
  21. // int hidden - a flag that tells the function if it needs to display hidden files
  22. // int isDirectory - a flag that tells the function if it needs to print the directory name
  23. //--------------------------------------------
  24. void printNormalContents(char *thing, int hidden, int isDirectory){
  25. if(isDirectory != 1){
  26. printf("\n%s:\n", thing);
  27. }
  28. DIR*p;
  29. struct dirent *d;
  30. p = opendir(thing);
  31. if(p == NULL){
  32. fprintf(stderr, "ls: cannot access %s: No such file or directory", thing);
  33. return;
  34. }
  35. while(d=readdir(p)){
  36. if(d->d_name[0] != '.' || hidden == 1){
  37. struct stat path_stat;
  38. stat(d->d_name, &path_stat);
  39. if(S_ISREG(path_stat.st_mode)){
  40. printf("- %s\n",d->d_name);}
  41. else if(S_ISDIR(path_stat.st_mode)){
  42. printf("d %s\n",d->d_name);}
  43. else if(S_ISCHR(path_stat.st_mode)){
  44. printf("c %s\n",d->d_name);}
  45. else if(S_ISBLK(path_stat.st_mode)){
  46. printf("b %s\n",d->d_name);}
  47. else if(S_ISFIFO(path_stat.st_mode)){
  48. printf("p %s\n",d->d_name);}
  49. else if(S_ISLNK(path_stat.st_mode)){
  50. printf("l %s\n",d->d_name);}
  51. else if(S_ISSOCK(path_stat.st_mode)){
  52. printf("s %s\n",d->d_name);}
  53. }
  54. }
  55. }
  56. int main(int argc, char **argv){
  57. int hidden = 0;
  58. int flag = 0;
  59. int dirFlag = 0;
  60. if(argc > 1){
  61. if(argc == 2){dirFlag = 1;}
  62. if(argv[1][0] == '.'){printNormalContents("./", hidden, 1);
  63. return;}
  64. for(int i = 1; i < argc; i++){
  65. if(strstr(argv[i], "-") != NULL && (strstr(argv[i], "a") != NULL || strstr(argv[i], "A") != NULL) ){hidden = 1;}
  66. }
  67. for(int i = 1; i < argc; i++){
  68. if(strstr(argv[i], "-") == NULL){
  69. if(strstr(argv[i], "/") == NULL){
  70. if(strstr(argv[i], ".") != NULL && argv[i][0] != '.'){
  71. if(access(argv[i], F_OK) != -1){
  72. printf("- %s\n", argv[i]);}
  73. else{
  74. fprintf(stderr, "ls: cannot access %s: No such file or directory", argv[i]);}
  75. }
  76. else{
  77. printNormalContents(argv[i], hidden, dirFlag);}}
  78. else{printNormalContents(argv[i], hidden, dirFlag);}
  79. flag = 1;
  80. }
  81. }
  82. if(flag == 0){printNormalContents("./", hidden, 1);}
  83. }
  84. else{
  85. printNormalContents("./", hidden, 1);}
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement