Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/stat.h>
  4. #include<sys/types.h>
  5. #include<dirent.h>
  6. #include<unistd.h>
  7. #include<pwd.h>
  8. #include<grp.h>
  9. #include<string.h>
  10. #include<time.h>
  11.  
  12. const double version=1.0;
  13.  
  14. int command_l=0;
  15. int command_R=0;
  16. int command_h=0;
  17.  
  18. int currentYear;
  19.  
  20. typedef struct File{
  21. char accessRights[11];
  22. int userId;
  23. int groupId;
  24. unsigned long size;
  25. time_t time;
  26. char name[256];
  27. int isDir;
  28. } File;
  29.  
  30. char *getPath(char *path, char *file)
  31. {
  32. int pathLen=strlen(path);
  33. int fileLen=strlen(path);
  34. char *fullPath=(char*)malloc((pathLen+fileLen+2)*sizeof(char));
  35. strcpy(fullPath,path);
  36. if(pathLen && path[pathLen-1]!='/')
  37. {
  38. fullPath[pathLen]='/';
  39. strcpy(fullPath+pathLen+1,file);
  40. fullPath[pathLen+fileLen+1]='\0';
  41. }
  42. else
  43. {
  44. strcpy(fullPath+pathLen+1,file);
  45. fullPath[pathLen+fileLen+1]='\0';
  46. }
  47. return fullPath;
  48. }
  49.  
  50. void getAccessRights(File *container, struct stat *file)
  51. {
  52. container->accessRights[0]=S_ISDIR(file->st_mode) ? 'd' : '-';
  53. container->accessRights[1]=(file->st_mode & S_IRUSR) ? 'r' : '-';
  54. container->accessRights[2]=(file->st_mode & S_IWUSR) ? 'w' : '-';
  55. container->accessRights[3]=(file->st_mode & S_IXUSR) ? 'x' : '-';
  56. container->accessRights[4]=(file->st_mode & S_IRGRP) ? 'r' : '-';
  57. container->accessRights[5]=(file->st_mode & S_IWGRP) ? 'w' : '-';
  58. container->accessRights[6]=(file->st_mode & S_IXGRP) ? 'x' : '-';
  59. container->accessRights[7]=(file->st_mode & S_IROTH) ? 'r' : '-';
  60. container->accessRights[8]=(file->st_mode & S_IWOTH) ? 'w' : '-';
  61. container->accessRights[9]=(file->st_mode & S_IXOTH) ? 'x' : '-';
  62. container->accessRights[10]='\0';
  63. }
  64.  
  65. char *humanReadable(unsigned long bytes)
  66. {
  67. double bytesDouble=(double)bytes;
  68. char *sizestr=(char*)malloc(12*sizeof(char));
  69. char symbols[6]={'B','K','M','G','T','P'};
  70. int i=0;
  71. while(i<=5 && bytesDouble>=1024.0)
  72. {
  73. bytesDouble/=1024.0;
  74. i++;
  75. }
  76. if((int)(10*bytesDouble)%10==0)
  77. sprintf(sizestr,"%d%c",(int)bytesDouble,symbols[i]);
  78. else
  79. sprintf(sizestr,"%.1lf%c",bytesDouble,symbols[i]);
  80. return sizestr;
  81. }
  82.  
  83. char *convertTime(unsigned long time)
  84. {
  85. time_t now=time;
  86. struct tm *ptm=localtime(&time);
  87. char *timestr=(char*) malloc(256*sizeof(char));
  88. if (ptm->tm_year==currentYear)
  89. strftime(timestr,256,"%d %b %R",ptm);
  90. else
  91. strftime(timestr,256,"%d %b %G",ptm);
  92. return timestr;
  93. }
  94.  
  95. void printFile(File *container, int l, int h)
  96. {
  97. if(container->name[0]=='.')
  98. return;
  99. if(l)
  100. {
  101. struct passwd *uid=getpwuid(container->userId);
  102. struct group *gid=getgrgid(container->groupId);
  103. char *tm=convertTime(container->time);
  104. printf("%s %s %s ", container->accessRights, uid->pw_name, gid->gr_name);
  105. if(h)
  106. {
  107. char *humanReadableSize=humanReadable(container->size);
  108. printf("%s ", humanReadableSize);
  109. }
  110. else
  111. printf("%ld ",container->size);
  112. printf("%s %s\n",tm,container->name);
  113. }
  114. else
  115. printf("%s\t",container->name);
  116. }
  117.  
  118. void printHelp()
  119. {
  120. printf("This program lists the files and subdirectories in a given directory.\n\n");
  121. printf("The following options are available:\n\n");
  122. printf(" -l\t\tLists files in the long format, including access rights, number of links, owner name, group name, size, date of last modification and name of file.\n\n");
  123. printf(" -h\t\tWhen used with the -l option, lists sizes with unit suffixes: (B)yte, (K)ilobyte, (M)egabyte, (G)igabyte, (T)erabyte and (P)etabyte.\n\n");
  124. printf(" -R\t\tRecursively lists subdirectories encountered.\n\n");
  125. printf(" --help\t\tLists available options.\n\n");
  126. printf(" --version\tPrints current version and author.\n");
  127. exit(0);
  128. }
  129.  
  130. void printVersion()
  131. {
  132. printf("MyLS version %.1lf\n",version);
  133. printf("Made by Mateusz Wlodarczyk\n");
  134. printf("UMK 2019\n");
  135. exit(0);
  136. }
  137.  
  138. void listDir(char *name, int l, int R, int h)
  139. {
  140. DIR *dir;
  141. struct dirent *dp;
  142. struct stat statbuf;
  143. if((dir=opendir(name))==NULL)
  144. {
  145. perror("Couldn't open the directory");
  146. return;
  147. }
  148. int filesNumber=0;
  149. while((dp=readdir(dir))!=NULL)
  150. filesNumber++;
  151. closedir(dir);
  152. if(filesNumber==2) return;
  153. File *allFiles=(File*)malloc(sizeof(File)*(filesNumber));
  154. int filesOpened=0;
  155. int dirNumber= 0;
  156. int iDir=0;
  157. int i;
  158. if((dir=opendir(name))==NULL)
  159. {
  160. perror("Couldn't open the directory");
  161. return;
  162. }
  163. while((dp=readdir(dir))!=NULL)
  164. {
  165. char *fullName=getPath(name,dp->d_name);
  166. File *file=allFiles+filesOpened;
  167. if(stat(fullName,&statbuf))
  168. {
  169. perror("Couldn't open the file");
  170. free(fullName);
  171. continue;
  172. }
  173. strcpy(file->name,dp->d_name);
  174. getAccessRights(file,&statbuf);
  175. file->size=(long)statbuf.st_size;
  176. file->userId=statbuf.st_uid;
  177. file->groupId=statbuf.st_gid;
  178. file->time=statbuf.st_mtime;
  179. file->isDir=dp->d_type==DT_DIR ? 1 : 0;
  180. if(file->isDir==1)
  181. dirNumber++;
  182. filesOpened++;
  183. free(fullName);
  184. }
  185. for(i=0;i<filesOpened;i++)
  186. printFile(allFiles+i, l, h);
  187. printf("\n");
  188. if(!R || dirNumber==2)
  189. {
  190. free(allFiles);
  191. closedir(dir);
  192. return;
  193. }
  194. File *dirList=(File*)malloc(sizeof(File)*dirNumber);
  195. for(i=0;i<filesOpened;i++)
  196. {
  197. if(allFiles[i].isDir)
  198. {
  199. if(strcmp(allFiles[i].name,".")==0 || strcmp(allFiles[i].name,"..")==0)
  200. continue;
  201. dirList[iDir++]=allFiles[i];
  202. }
  203. }
  204. free(allFiles);
  205. closedir(dir);
  206. for(i = 0;i<iDir;i++)
  207. {
  208. char *Name=getPath(name,dirList[i].name);
  209. listDir(Name,l,R,h);
  210. free(Name);
  211. }
  212. free(dirList);
  213. }
  214.  
  215. int main(int argc, char **argv)
  216. {
  217. int i;
  218. while((i=getopt(argc,argv,"lRh"))!=-1)
  219. {
  220. switch(i)
  221. {
  222. case 'l':
  223. command_l=1;
  224. break;
  225. case 'R':
  226. command_R=1;
  227. break;
  228. case 'h':
  229. command_h=1;
  230. break;
  231. }
  232. }
  233. for(i=1;i<argc;i++)
  234. {
  235. if(strcmp(argv[i],"--help")==0)
  236. printHelp();
  237. else if(strcmp(argv[i],"--version")==0)
  238. printVersion();
  239. }
  240. time_t currentTime=time(NULL);
  241. struct tm *pctm=localtime(&currentTime);
  242. currentYear=pctm->tm_year;
  243. if(optind<argc)
  244. listDir(argv[optind],command_l,command_R,command_h);
  245. else
  246. listDir(".",command_l,command_R,command_h);
  247. exit(0);
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement