Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h> /* Typy zmiennych używane w systemie */
- #include <sys/stat.h> /* Funkcja stat pozwalająca pobrać informacje o pliku */
- #include <stdio.h> /* standard input/output */
- #include <unistd.h> /* standardowe funkcje unixa */
- #include <dirent.h> /* funkcje i definicje pozwalające używać struktury direntry */
- #include <string.h> /* funkcje pozwalające manipulować łańcuchami */
- #include <stdlib.h> /* biblioteka standardowa C */
- #include <time.h> /* funkcje pozwalające manipulować czasem */
- #include <pwd.h> /* struktura passwd i funkcje do manipulacji nią */
- #include <grp.h> /* struktura group i funkcje do manipulacji nią */
- #include <fcntl.h> /* kontrolne funkcje systemowe */
- void ls(char *name)
- {
- DIR *dir;
- struct dirent *dp;
- struct stat mystat;
- struct tm lt;
- struct passwd *pwd;
- struct group *grp;
- char buf[1024];
- dir = opendir(name);
- while((dp = readdir(dir)) != NULL)
- {
- if(dp->d_name[0] != '.') /* Bez ukrytych plikow */
- {
- sprintf(buf, "%s/%s", name, dp->d_name);
- stat(buf, &mystat);
- /* Typ i prawa */
- printf( (S_ISDIR(mystat.st_mode)) ? "d" : "-");
- printf( (mystat.st_mode & S_IRUSR) ? "r" : "-");
- printf( (mystat.st_mode & S_IWUSR) ? "w" : "-");
- printf( (mystat.st_mode & S_IXUSR) ? "x" : "-");
- printf( (mystat.st_mode & S_IRGRP) ? "r" : "-");
- printf( (mystat.st_mode & S_IWGRP) ? "w" : "-");
- printf( (mystat.st_mode & S_IXGRP) ? "x" : "-");
- printf( (mystat.st_mode & S_IROTH) ? "r" : "-");
- printf( (mystat.st_mode & S_IWOTH) ? "w" : "-");
- printf( (mystat.st_mode & S_IXOTH) ? "x " : "- ");
- /* Wypisywanie ilości dowiązań */
- printf("\t %d",mystat.st_nlink);
- /* Uzytkownik */
- pwd = getpwuid(mystat.st_uid);
- printf("\t %s", pwd->pw_name);
- /* Grupa */
- grp=getgrgid(mystat.st_gid);
- printf("\t %s", grp->gr_name);
- /* Rozmiar */
- printf("\t %lld",mystat.st_size);
- time_t t = mystat.st_mtime;
- localtime_r(&t, <);
- char timebuf[80];
- strftime(timebuf, sizeof(timebuf), "%d %b %H:%M", <);
- /* Czas dostępu */
- printf("\t %s", timebuf);
- /* Nazwa pliku */
- printf("\t %s\n", dp->d_name);
- }
- }
- closedir(dir);
- }
- char* human_readable(double size, char* buf)
- {
- const char* units[] = { "B", "K", "M", "G", "T" };
- int i = 0;
- while (size > 1024)
- {
- size /= 1024;
- i++;
- }
- sprintf(buf, "%.*f%s", i, size, units[i]);
- return buf;
- }
- void ls_with_h(char *name)
- {
- DIR *dir;
- struct dirent *dp;
- struct stat mystat;
- struct tm lt;
- struct passwd *pwd;
- struct group *grp;
- char buf[1024];
- dir = opendir(name);
- while((dp = readdir(dir)) != NULL)
- {
- if(dp->d_name[0] != '.') /* Bez ukrytych plikow */
- {
- sprintf(buf, "%s/%s", name, dp->d_name);
- stat(buf, &mystat);
- /* Typ i prawa */
- printf( (S_ISDIR(mystat.st_mode)) ? "d" : "-");
- printf( (mystat.st_mode & S_IRUSR) ? "r" : "-");
- printf( (mystat.st_mode & S_IWUSR) ? "w" : "-");
- printf( (mystat.st_mode & S_IXUSR) ? "x" : "-");
- printf( (mystat.st_mode & S_IRGRP) ? "r" : "-");
- printf( (mystat.st_mode & S_IWGRP) ? "w" : "-");
- printf( (mystat.st_mode & S_IXGRP) ? "x" : "-");
- printf( (mystat.st_mode & S_IROTH) ? "r" : "-");
- printf( (mystat.st_mode & S_IWOTH) ? "w" : "-");
- printf( (mystat.st_mode & S_IXOTH) ? "x " : "- ");
- /* Wypisywanie ilości dowiązań */
- printf("\t %d",mystat.st_nlink);
- /* Uzytkownik */
- pwd = getpwuid(mystat.st_uid);
- printf("\t %s", pwd->pw_name);
- /* Grupa */
- grp=getgrgid(mystat.st_gid);
- printf("\t %s", grp->gr_name);
- /* Rozmiar z uzyciem funkcji human_readable*/
- printf(" %s ", human_readable(mystat.st_size, buf));
- time_t t = mystat.st_mtime;
- localtime_r(&t, <);
- char timebuf[80];
- strftime(timebuf, sizeof(timebuf), "%d %b %H:%M", <);
- /* Czas dostępu */
- printf("\t %s", timebuf);
- /* Nazwa pliku */
- printf("\t %s\n", dp->d_name);
- }
- }
- closedir(dir);
- }
- void help()
- {
- printf("[Just running the program | Display list of files]\n");
- printf("[--help | Show help]\n");
- printf("[--version | Display version of ls]\n");
- printf("[-h | Print sizes in human readable format]\n");
- printf("[-R | Display ls -l for Directory and all directories in it]\n\n");
- }
- void version()
- {
- printf("Wersja 0.0.1\n\n");
- }
- int main(int argc, char* argv[])
- {
- int i;
- if(argc<2)
- {
- ls(".");
- }
- else
- {
- for (i=1; i<argc; i++)
- {
- if (strcmp(argv[i],"--help")==0)
- {
- help();
- }
- else if (strcmp(argv[i],"--version")==0)
- {
- version();
- }
- else if(strcmp(argv[i],"-h")==0)
- {
- printf("%s:\n", argv[i]);
- ls_with_h(argv[i]);
- printf("\n");
- }
- else
- {
- printf("%s:\n", argv[i]);
- ls(argv[i]);
- printf("\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement