// partial solution to replacement for ls -l // // Last modified 29 March 2005 // Brian D. Davison #include #include #include #include #include int main(int argc, char *argv[]) { struct dirent *ds = 0; DIR *dirp = 0; struct stat sbuf; char pathname[1024]; char *dirname; char *mytime; struct passwd *pw; struct group *gr; if (argc == 2) { dirname = argv[1]; } else { dirname = "."; } dirp = opendir(dirname); if (dirp) { while ((ds = readdir(dirp)) != NULL) { strcpy(pathname,dirname); strcat(pathname,"/"); strcat(pathname,ds->d_name); lstat(pathname, &sbuf); // should check more file types if ((sbuf.st_mode & S_IFBLK) == S_IFBLK) printf("b"); else if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) printf("l"); else if (sbuf.st_mode & S_IFDIR) printf("d"); else if (sbuf.st_mode & S_IFREG) printf("-"); if (sbuf.st_mode & S_IREAD) printf("r"); else printf("-"); if (sbuf.st_mode & S_IWRITE) printf("w"); else printf("-"); if (sbuf.st_mode & S_IEXEC) if (sbuf.st_mode & S_ISUID) printf("s"); else printf("x"); else printf("-"); if (sbuf.st_mode & S_IRGRP) printf("r"); else printf("-"); if (sbuf.st_mode & S_IWGRP) printf("w"); else printf("-"); if (sbuf.st_mode & S_IXGRP) if (sbuf.st_mode & S_ISGID) printf("s"); else printf("x"); else printf("-"); if (sbuf.st_mode & S_IROTH) printf("r"); else printf("-"); if (sbuf.st_mode & S_IWOTH) printf("w"); else printf("-"); if (sbuf.st_mode & S_IXOTH) printf("x"); else printf("-"); // should check error conditions pw = getpwuid(sbuf.st_uid); gr = getgrgid(sbuf.st_gid); // quick hack for formatting time mytime = ctime(&(sbuf.st_mtime)); if (mytime) *(mytime+16)=0; printf(" %3d %-8s %-8s %8ld %s %s\n", sbuf.st_nlink, pw->pw_name, gr->gr_name, sbuf.st_size, mytime+4, ds->d_name); } closedir(dirp); } else printf("Invalid directory name\n"); return 0; }