?? ls.c
字號:
?
+
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> /*int getopt (int argc, char **argv, const char *options)*/
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <assert.h>
static const char *progname = "ls";
static int aflag = 0;
static int lflag = 0;
static int nflag = 0;
static void
show_mode(mode_t mode)
{
char c = '?';
if (S_ISDIR(mode)) {
c = 'd';
} else if (S_ISREG(mode)) {
c = '-';
} else if (S_ISCHR(mode)) {
c = 'c';
} else if (S_ISBLK(mode)) {
c = 'b';
#ifdef S_ISSOCK
} else if (S_ISSOCK(mode)) {
c = 's';
#endif
} else if (S_ISFIFO(mode)) {
c = 'f';
}
putchar(c);
putchar((mode & S_IRUSR) ? 'r' : '-');
putchar((mode & S_IWUSR) ? 'w' : '-');
putchar((mode & S_IXUSR) ? 'x' : '-');
putchar((mode & S_IRGRP) ? 'r' : '-');
putchar((mode & S_IWGRP) ? 'w' : '-');
putchar((mode & S_IXGRP) ? 'x' : '-');
putchar((mode & S_IROTH) ? 'r' : '-');
putchar((mode & S_IWOTH) ? 'w' : '-');
putchar((mode & S_IXOTH) ? 'x' : '-');
}
static void
show_user(uid_t uid)
{
struct passwd *pwd;
pwd = getpwuid(uid);
if (!nflag && pwd) {
printf(" %6s", pwd->pw_name);
} else {
printf(" %6u", (unsigned) uid);
}
}
static void
show_group(gid_t gid)
{
struct group *grp;
grp = getgrgid(gid);
if (!nflag && grp) {
printf(" %6s", grp->gr_name);
} else {
printf(" %6u", (unsigned) gid);
}
}
static void
show_time(time_t t, time_t now)
{
struct tm *tm;
char buffer[80];
const time_t delta = 7905600; /* 366/2 * 12 * 3600 seconds */
size_t n = 0;
tm = localtime(&t);
if (now != (time_t) -1 && t < now - delta) {
n = strftime(buffer, sizeof(buffer), "%b %d %Y", tm);
} else {
n = strftime(buffer, sizeof(buffer), "%b %d %H:%M", tm);
}
if (n) {
printf(" %12s", buffer);
} else {
printf(" %12lu", (unsigned long) t);
}
}
static void
ls(const char *directory)
{
DIR *d;
struct dirent *e;
time_t now;
int blocks;
int file;
struct stat sd;
d = opendir(directory);
if (! d) {
file=open(directory,0);
if(file==-1) /*如果路徑名代表一個存在的文件則顯示該文件,否則提示出錯*/
perror(progname);
else {printf("%s\n",directory); close(file);}
return;
}
if (chdir(directory) == -1) {
perror(progname);
return;
}
now = time(NULL);
printf("%s:\n",directory) ;
while (1) {
e = readdir(d);
if (! e) {
break;
}
if (aflag || e->d_name[0] != '.') { /*列出所有文件或者該文件不是隱藏文件*/
struct stat s;
if (stat(e->d_name, &s) == -1) {
fprintf(stderr, "%s: %s: ", progname, e->d_name);
perror(NULL);
continue;
}
if (lflag || nflag) {
show_mode(s.st_mode); /*file type */
printf(" %4d", s.st_nlink); /*number of links*/
show_user(s.st_uid); /*user ID of owner*/
show_group(s.st_gid); /*group ID of owner*/
printf(" %9lu", s.st_size); /*size in bytes*/
printf(" %9lu", s.st_blocks); /*number of 512 byte blocks allocated*/
show_time(s.st_mtime, now); /*time of last modification*/
printf(" %s", e->d_name); /*file name*/
if (S_ISDIR(s.st_mode)) printf("/");
else if ((s.st_mode & S_IXUSR)||(s.st_mode & S_IXGRP)||(s.st_mode & S_IXOTH)) printf("*");
printf("\n");
} else {
printf("%s", e->d_name);
if (S_ISDIR(s.st_mode)) printf("/");
else if ((s.st_mode & S_IXUSR)||(s.st_mode & S_IXGRP)||(s.st_mode & S_IXOTH)) printf("*");
printf("\n");
}
}
}
closedir(d);
}
int
main(int argc, char **argv)
{
char *old_dir;
int size;
int file;
old_dir = path_alloc(&size);
if (getcwd(old_dir,size) == NULL)
err_sys("getcwd failed");
int c;
int i;
while ((c = getopt(argc, argv, "aln")) >= 0) { /*When there are no more options to be parsed, it returns -1*/
switch (c) {
case 'a':
aflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'n':
nflag = 1;
break;
case '?': /*If an option that requires an argument is found, but there is no argument present*/
exit(EXIT_FAILURE);
}
}
/*Variable: int optind
This variable is set by getopt to the index of the next element of the argv array to be processed. Once getopt has found all of the option arguments, you can use this variable to determine where the remaining non-option arguments begin. The initial value of this variable is 1.
*/
if (argc == optind) {
ls(".");
} else {
for (i = optind; i < argc; i++) {
if (chdir(old_dir) == -1) {
perror(progname);
return;
}
ls(argv[i]);
printf("\n");
}
}
return EXIT_SUCCESS;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -