Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <dirent.h> // за struct dirent, readdir(dp)
- #include <sys/stat.h> // за fstat
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/ioctl.h> // за winsize struct
- #include <pwd.h>
- #include <grp.h>
- #include <time.h>
- #ifdef HAVE_ST_BIRTHTIME
- #define birthtime(x) x.st_birthtime
- #else
- #define birthtime(x) x.st_ctime
- #endif
- int main(int argc, char* argv[])
- {
- char *current_dir = NULL;
- DIR *dp = NULL;
- struct dirent *dptr = NULL; // dirent - структура, която връща информация за въведената директория
- unsigned int count = 0;
- long *ptr = NULL;
- struct winsize w; // w = screen_size
- int num_files = 0; //Променлива, която държи броя файлове в текущата директория
- char arguments;
- //ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); //to get the number of rows and column visible on terminal
- current_dir = getenv("PWD"); //взимаме текущата директория чрез getenv("PWD") ако върне нула значи има грешка
- if(current_dir == NULL)
- {
- printf("\n ERROR : Could not get the working directory\n");
- return -1;
- }
- if (strcmp(argv[1], "ls") == 0 && argc == 2)
- {
- dp = opendir((const char*)current_dir); // open the directory
- while((dptr = readdir(dp)) != NULL) //zapochvame da chetem sudurjanieto na direktoriqta
- {
- if(dptr -> d_name[0] != '.') //ne broi failovete koito zapochvat s '.'
- {
- num_files++;
- }
- }
- closedir(dp); //zatvarqme q
- dp = NULL;
- dptr = NULL;
- if(!num_files) //proverqvame dali ima failove v direktoriqta
- {
- return 0;
- }
- else
- {
- ptr = malloc(num_files*8); //zadelqme pamet za adresine na imenata na sudurjanieto na direktoriqta
- if(ptr == NULL)
- {
- printf("\n Memory allocation failed\n");
- return -1;
- }
- else
- {
- memset(ptr, 0, num_files*8); // inicializirame pametta s 0li
- //ptr - pointer to the block of memory to fill
- // 0 = value to be set.
- // 8 = num - number of bytes to be set to the value
- // num_files - an insigned int type
- }
- }
- dp = opendir((const char*)current_dir);
- if(dp == NULL)
- {
- printf("\n ERROR : Could not open the working directory\n");
- free(ptr);
- return -1;
- }
- // Start iterating the directory and read all its contents
- // inside an array allocated above.
- unsigned int j = 0;
- for(count = 0; NULL != (dptr = readdir(dp)); count++) // NULL != (dptr = readdir(dp)) ако е различно от NULL значи няма грешка
- {
- if(dptr->d_name[0] != '.')
- {
- ptr[j] = (long)dptr->d_name;
- j++;
- }
- }
- // Start displaying on console.
- for(count = 0; count< num_files; count++)
- {
- // Check if the file/folder is executable.
- if(!access((const char*)ptr[count],X_OK))
- {
- int fd = -1; // fd - file descriptor = индентификатор, който използваме за достъп до даден файл
- struct stat st;
- fd = open((char*)ptr[count], O_RDONLY, 0);
- if(-1 == fd)
- {
- printf("\n Opening file/Directory failed\n");
- free(ptr);
- return -1;
- }
- fstat(fd, &st);
- if(S_ISDIR(st.st_mode)) // S_ISDIR - проверява дали това е файл или директория
- {
- // If folder
- printf("d %s\n",(char*)ptr[count]);
- }
- else
- {
- // If executable file
- printf("%s\n",(char*)ptr[count]);
- }
- close(fd);
- }
- else
- {
- // If normal file, print by the default way(black color)
- printf("- %s\n",(char*)ptr[count]);
- }
- }
- printf("\n");
- }
- else if (strcmp(argv[1], "-a") == 0)
- {
- dp = opendir((const char*)current_dir); // opne the directory
- while((dptr = readdir(dp)) != NULL) //zapochvame da chetem sudurjanieto na direktoriqta
- {
- num_files++;
- }
- closedir(dp); // close the directory
- dp = NULL;
- dptr = NULL;
- if(!num_files) //proverqvame dali ima failove v direktoriqta
- {
- return 0;
- }
- else
- {
- ptr = malloc(num_files*8); //zadelqme pamet za adresine na imenata na sudurjanieto na direktoriqta
- if(ptr == NULL)
- {
- printf("\n Memory allocation failed\n");
- return -1;
- }
- else
- {
- memset(ptr, 0, num_files*8); // Initialize the memory by zeros
- }
- }
- dp = opendir((const char*)current_dir);
- if(dp == NULL)
- {
- printf("\n ERROR : Could not open the working directory\n");
- free(ptr);
- return -1;
- }
- //Chetem sudurjanieto na direktoriqta
- unsigned int j = 0;
- for(count = 0;(dptr = readdir(dp)) != NULL; count++) // (dptr = readdir(dp)) != NULL ако е различно от NULL значи няма грешка
- {
- ptr[j] = (long)dptr -> d_name;
- j++;
- }
- for(count = 0; count< num_files; count++)
- {
- if(!access((const char*)ptr[count],X_OK)) // Proverqva dali failut/papkata e executable.
- {
- int fd = -1; // fd - file descriptor = indetifikator, koito izpolzvame za daden fail
- struct stat st;
- fd = open((char*)ptr[count], O_RDONLY, 0);
- if(fd == -1)
- {
- printf("\n Opening file/Directory failed\n");
- free(ptr);
- return -1;
- }
- fstat(fd, &st);
- if(S_ISDIR(st.st_mode)) // S_ISDIR - проверява дали това е файл или директория
- {
- // If folder
- printf("d %s\n",(char*)ptr[count]);
- }
- else
- {
- // If executable file
- printf("%s\n",(char*)ptr[count]);
- }
- close(fd);
- }
- else
- {
- // If normal file, print by the default way(black color)
- printf("- %s\n",(char*)ptr[count]);
- }
- }
- printf("\n");
- }
- else if (strcmp(argv[1], "-l") == 0)
- {
- dp = opendir((const char*)current_dir); // opne the directory
- while((dptr = readdir(dp)) != NULL) //zapochvame da chetem sudurjanieto na direktoriqta
- {
- if(dptr -> d_name[0] != '.') //ne broi failovete koito zapochvat s '.'
- {
- num_files++;
- }
- }
- closedir(dp); //zatvarqme q
- dp = NULL;
- dptr = NULL;
- if(!num_files) //proverqvame dali ima failove v direktoriqta
- {
- return 0;
- }
- else
- {
- ptr = malloc(num_files*8); //zadelqme pamet za adresine na imenata na sudurjanieto na direktoriqta
- if(ptr == NULL)
- {
- printf("\n Memory allocation failed\n");
- return -1;
- }
- else
- {
- memset(ptr, 0, num_files*8); // inicializirame pametta s 0li
- //ptr - pointer to the block of memory to fill
- // 0 = value to be set.
- // 8 = num - number of bytes to be set to the value
- // num_files - an insigned int type
- }
- }
- dp = opendir((const char*)current_dir);
- if(dp == NULL)
- {
- printf("\n ERROR : Could not open the working directory\n");
- free(ptr);
- return -1;
- }
- // Start iterating the directory and read all its contents
- // inside an array allocated above.
- unsigned int j = 0;
- for(count = 0; NULL != (dptr = readdir(dp)); count++) // NULL != (dptr = readdir(dp)) ако е различно от NULL значи няма грешка
- {
- if(dptr->d_name[0] != '.')
- {
- ptr[j] = (long)dptr->d_name;
- j++;
- }
- }
- // Start displaying on console.
- for(count = 0; count< num_files; count++)
- {
- // Check if the file/folder is executable.
- if(!access((const char*)ptr[count],X_OK))
- {
- int fd = -1; // fd - file descriptor = индентификатор, който използваме за достъп до даден файл
- struct stat st;
- fd = open((char*)ptr[count], O_RDONLY, 0);
- if(-1 == fd)
- {
- printf("\n Opening file/Directory failed\n");
- free(ptr);
- return -1;
- }
- fstat(fd, &st);
- if(S_ISDIR(st.st_mode)) // S_ISDIR - проверява дали това е файл или директория
- {
- // If folder
- 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" : "-"); //permissions
- printf(" %ld",st.st_nlink); //number of links
- uid_t uid = st.st_uid;
- struct passwd *pass = getpwuid(uid);
- printf(" %s",pass -> pw_name); //user
- gid_t gid = st.st_gid;
- struct group *grp = getgrgid(gid);
- printf(" %s", grp -> gr_name); //group
- printf(" %ld",st.st_size); //size
- struct timeval tv;
- char str[100];
- struct tm *tm;
- tm = localtime( (time_t*)&st.st_mtim );
- strftime(str, sizeof(str), "%b %d %H:%M", tm);
- printf(" %s", str); //date
- printf(" %s\n",(char*)ptr[count]); //file name
- }
- else // If executable file
- {
- //if()
- 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" : "-"); //permissions
- printf(" %ld",st.st_nlink); //number of links
- uid_t uid = st.st_uid;
- struct passwd *pass = getpwuid(uid);
- printf(" %s",pass -> pw_name); //user
- gid_t gid = st.st_gid;
- struct group *grp = getgrgid(gid);
- printf(" %s", grp -> gr_name); //group
- printf(" %ld",st.st_size); //size
- struct timeval tv;
- char str[100];
- struct tm *tm;
- tm = localtime( (time_t*)&st.st_mtim );
- strftime(str, sizeof(str), "%b %d %H:%M", tm);
- printf(" %s", str); //date
- printf("- %s\n",(char*)ptr[count]);
- }
- close(fd);
- }
- else // If normal file
- {
- int fd = -1; // fd - file descriptor = индентификатор, който използваме за достъп до даден файл
- struct stat st;
- fd = open((char*)ptr[count], O_RDONLY, 0);
- if(-1 == fd)
- {
- printf("\n Opening file/Directory failed\n");
- free(ptr);
- return -1;
- }
- fstat(fd, &st);
- 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" : "-"); //permissions
- printf(" %ld",st.st_nlink); //number of links
- uid_t uid = st.st_uid;
- struct passwd *pass = getpwuid(uid);
- printf(" %s",pass -> pw_name); //user
- gid_t gid = st.st_gid;
- struct group *grp = getgrgid(gid);
- printf(" %s", grp -> gr_name); //group
- printf(" %ld",st.st_size); //size
- struct timeval tv;
- char str[100];
- struct tm *tm;
- tm = localtime( (time_t*)&st.st_mtim );
- strftime(str, sizeof(str), "%b %d %H:%M", tm);
- printf(" %s", str); //date
- printf(" %s\n",(char*)ptr[count]); //file name
- }
- }
- printf("\n");
- }
- else if (strcmp(argv[1], "ls") == 0 && strcmp(argv[2], "-R") == 0)
- {
- printf("LS -R\n");
- }
- else { printf("ls: cannot access %s: No such file or directory\n", argv[2]); }
- free(ptr); //Free the allocated memory
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement