Advertisement
Guest User

Untitled

a guest
May 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 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, int isDetailed){
  25. if(isDirectory != 1){
  26. printf("%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)){ //Checking if regular file
  40. printf("- %s\n",d->d_name);}
  41. else if(S_ISDIR(path_stat.st_mode)){ //Checking if directory
  42. printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, current_directory->d_name);
  43. printf("d %s\n",d->d_name);}
  44. else if(S_ISCHR(path_stat.st_mode)){ //Checking if character device
  45. printf("c %s\n",d->d_name);}
  46. else if(S_ISBLK(path_stat.st_mode)){ //Checking if block device
  47. printf("b %s\n",d->d_name);}
  48. else if(S_ISFIFO(path_stat.st_mode)){ //Checking if program stream
  49. printf("p %s\n",d->d_name);}
  50. else if(S_ISLNK(path_stat.st_mode)){
  51. printf("l %s\n",d->d_name);}
  52. else if(S_ISSOCK(path_stat.st_mode)){ //Checking if socket
  53. printf("s %s\n",d->d_name);}
  54. }
  55. }
  56. closedir(p);
  57. }
  58. int main(int argc, char **argv){
  59. int hidden = 0;
  60. int flag = 0;
  61. int dirFlag = 0;
  62. if(argc > 1){
  63. if(argc == 2){dirFlag = 1;}
  64. if(argv[1][0] == '.'){printNormalContents("./", hidden, 1);
  65. return;}
  66. for(int i = 1; i < argc; i++){
  67. if(strstr(argv[i], "-") != NULL && (strstr(argv[i], "a") != NULL || strstr(argv[i], "A") != NULL) ){hidden = 1;
  68. }
  69. }
  70. for(int i = 1; i < argc; i++){
  71. if(strstr(argv[i], "-") == NULL){
  72. if(strstr(argv[i], "/") == NULL){
  73. if(strstr(argv[i], ".") != NULL && argv[i][0] != '.'){
  74. if(access(argv[i], F_OK) != -1){
  75. printf("- %s\n", argv[i]);
  76. }
  77. else{
  78. fprintf(stderr, "ls: cannot access %s: No such file or directory", argv[i]);
  79. }
  80. }
  81. else{
  82. printNormalContents(argv[i], hidden, dirFlag);
  83. if(i != argc - 1 && argv[i+1][0] != '.'){printf("\n");}
  84. }
  85. }
  86. else{
  87. printNormalContents(argv[i], hidden, dirFlag);
  88. if(i != argc - 1 && argv[i+1][0] != '.'){printf("\n");}
  89. }
  90. flag = 1;
  91. }
  92. }
  93. if(flag == 0){
  94. printNormalContents("./", hidden, 1);
  95. }
  96. }
  97. else{
  98. printNormalContents("./", hidden, 1);
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement