Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <pwd.h>
- #include <grp.h>
- #include <time.h>
- //Moja kolejka
- struct s_MyDeque{
- struct s_MyDeque *next;
- struct s_MyDeque *prev;
- char *dirPath;
- };
- struct s_MyDeque *first;
- struct s_MyDeque *last;
- //Variables
- DIR* pDIR;
- struct dirent* pDirEnt;
- struct stat st;
- struct passwd* pwd;
- struct group* grp;
- char* presentDir = ".";
- char* fullDir = NULL;
- char* useDate;
- int aFlag = 0;
- int RFlag = 0;
- int lFlag = 0;
- int mFlag = 0;
- int AFlag = 0;
- int GFlag = 0;
- int firstDir = 0;
- //Functions
- void process_record_long(char* targetDir){
- pDIR = opendir(targetDir);
- pDirEnt = readdir(pDIR);
- if(RFlag)
- printf("In dir: %s\n",targetDir);
- while(pDirEnt != NULL){
- if(aFlag == 1 || pDirEnt->d_name[0] != '.'){
- if(firstDir && mFlag){
- printf(", ");
- }
- firstDir = 1;
- asprintf(&fullDir,"%s/%s",targetDir,pDirEnt->d_name);
- lstat(fullDir, &st);
- pwd = getpwuid(st.st_uid);
- grp = getgrgid(st.st_gid);
- if(lFlag){
- printf((S_ISDIR(st.st_mode)) ? "d" : "-");
- printf((st.st_mode & S_IRUSR) ? "r" : "-");
- printf((st.st_mode & S_IWUSR) ? "w" : "-");
- printf((st.st_mode & S_IXUSR) ? "x" : "-");
- printf((st.st_mode & S_IRGRP) ? "r" : "-");
- printf((st.st_mode & S_IWGRP) ? "w" : "-");
- printf((st.st_mode & S_IXGRP) ? "x" : "-");
- printf((st.st_mode & S_IROTH) ? "r" : "-");
- printf((st.st_mode & S_IWOTH) ? "w" : "-");
- printf((st.st_mode & S_IXOTH) ? "x" : "-");
- printf(" %d", (int)st.st_nlink);
- printf(" %s", pwd->pw_name);
- if(!GFlag)
- printf(" %s", grp->gr_name);
- printf(" %d", (int)st.st_size);
- useDate = ctime(&st.st_mtime);
- memmove (useDate,useDate+4,12);
- useDate[12] = NULL;
- printf( " %s ", useDate);
- }
- printf("%s", pDirEnt->d_name);
- if(lFlag)
- printf("\n");
- else if(mFlag){ }
- else
- printf(" ");
- if(RFlag){
- if(S_ISDIR(st.st_mode)){
- last->next = (struct s_MyDeque*)malloc(sizeof(struct s_MyDeque));
- last->next->dirPath = fullDir;
- last = last->next;
- last->next = NULL;
- }
- }
- }
- pDirEnt=readdir(pDIR);
- }
- first = first->next;
- /*if(first->prev != NULL){
- free(first->prev);
- first->prev = NULL;
- }*/
- if(first != NULL)
- process_record_long(first->dirPath);
- if(!lFlag)
- printf("\n");
- }
- int main(int argc, char *argv[])
- {
- int option;
- int index;
- while((option = getopt(argc, argv, "GlaRm")) != -1)
- {
- switch(option)
- {
- case 'l':
- lFlag = 1;
- break;
- case 'a':
- aFlag = 1;
- break;
- case 'R':
- RFlag = 1;
- break;
- case 'm':
- mFlag = 1;
- break;
- case 'G':
- GFlag = 1;
- break;
- case '?':
- printf("%c", optopt);
- break;
- }
- }
- first = (struct s_MyDeque*)malloc(sizeof *first);
- first->next = NULL;
- first->prev = NULL;
- last = first;
- for(index = optind; index < argc; index++)
- {
- presentDir = argv[index];
- }
- first->dirPath = presentDir;
- process_record_long(first->dirPath);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment