自写简单的ls命令
2012-05-22 10:16:38 来源:WEB开发网核心提示:在redhat linux 9下调试通过,实现了ls 及ls -l#include <stdio.h>#include <sys/stat.h>#include <string.h>#include <dirent.h>#include <unistd.h>#
在redhat linux 9下调试通过,实现了ls 及ls -l
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void simplylist ();
void simplydir ();
void detaillist ();
void detailfile ();
main (int argc, char *argv[])
{
switch (argc)
{
case 1:
simplylist (".");
break;
case 2:
{
if (strcmp (argv[argc - 1], "-l") == 0)
detaillist (".");
else
simplylist (argv[argc - 1]);
}
break;
case 3:
{
if (strcmp (argv[argc - 2], "-l") != 0)
{
perror ("Please read command " "help" "");
exit (1);
}
else
detaillist (argv[argc - 1]);
}
break;
default:
printf ("Please read command " "help" "");
}
}
void
detaillist (char *pathname) //print detail list information
{
struct stat fileinfo;
DIR * d1;
struct dirent *dent1;
char pathfile[MAX];
unsigned int i = 0;
unsigned int sum = 0;
if (stat (pathname, &fileinfo) < 0)
{
perror ("Please read command " "help" "");
exit (1);
};
switch (fileinfo.st_mode & S_IFMT)
{
case S_IFDIR:
{
d1 = opendir (pathname);
if (d1 == NULL)
{
perror ("open.failed");
exit (1);
}
errno = 0;
while ((dent1 = readdir (d1)) != NULL)
{
if ((strcmp (dent1->d_name, "..") == 0)
|| (strcmp (dent1->d_name, ".") == 0))
continue;
strcpy (pathfile, pathname);
strcat (pathfile, "/");
strcat (pathfile, dent1->d_name);
detailfile (pathfile);
errno = 0;
}
if (errno != 0)
perror ("read dir error");
closedir (d1);
}
break;
default:
detailfile (pathname);
}
}
void
simplydir (char *pathname) //Print simple directory
{
DIR * d1;
struct dirent *dent1;
int i = 0;
d1 = opendir (pathname);
if (d1 == NULL)
{
perror ("open.failed");
exit (1);
}
errno = 0;
while ((dent1 = readdir (d1)) != NULL)
{
if ((strcmp (dent1->d_name, "..") == 0)
|| (strcmp (dent1->d_name, ".") == 0))
continue;
printf ("%-10s", dent1->d_name);
errno = 0;
if ((++i) % 8 == 0)
putchar ('\n'); //Each line exit 8 files
}
putchar ('\n');
if (errno != 0)
perror ("read dir error");
closedir (d1);
}
void
detailfile (char *pathname) //print detail file information
{
struct stat fileinfo;
struct passwd *passwd;
struct group *group;
struct tm *time;
char str[MAX];
if (lstat (pathname, &fileinfo) < 0)
{
perror ("Please read command " "help" "");
exit (1);
};
switch (fileinfo.st_mode & S_IFMT) //File type
{
case S_IFSOCK:
printf ("s");
break;
case S_IFLNK:
printf ("l");
break;
case S_IFREG:
printf ("-");
break;
case S_IFBLK:
printf ("b");
break;
case S_IFDIR:
printf ("d");
break;
case S_IFCHR:
printf ("c");
break;
case S_IFIFO:
printf ("p");
break;
default:
printf ("?");
}
if (fileinfo.st_mode & S_IRUSR) //File R/W permission
printf ("r");
else
printf ("-");
if (fileinfo.st_mode & S_IWUSR)
printf ("w");
else
printf ("-");
if (fileinfo.st_mode & S_IXUSR)
printf ("x");
else
printf ("-");
if (fileinfo.st_mode & S_IRGRP)
printf ("r");
else
printf ("-");
if (fileinfo.st_mode & S_IWGRP)
printf ("w");
else
printf ("-");
if (fileinfo.st_mode & S_IXGRP)
printf ("x");
else
printf ("-");
if (fileinfo.st_mode & S_IROTH)
printf ("r");
else
printf ("-");
if (fileinfo.st_mode & S_IWOTH)
printf ("w");
else
printf ("-");
if (fileinfo.st_mode & S_IXOTH)
printf ("x");
else
printf ("-");
printf ("%5u ", fileinfo.st_nlink);
passwd = getpwuid (fileinfo.st_uid);
group = getgrgid (fileinfo.st_gid);
printf ("%s", passwd->pw_name);
printf ("\t ");
printf ("%s", group->gr_name);
printf ("%13u ", fileinfo.st_size);
time = localtime (&fileinfo.st_mtime); //the last time of file modification
strftime (str, sizeof (str), "%b %d %R", time); //~~strftime baidu baike~~
printf ("%s ", str);
printf ("%s\n", pathname);
}
void
simplylist (char *pathname) //print simple directory or file information
{
struct stat fileinfo;
if (stat (pathname, &fileinfo) < 0)
{
perror ("Please read command " "help" "");
exit (1);
};
switch (fileinfo.st_mode & S_IFMT)
{
case S_IFDIR:
simplydir (pathname);
break;
default:
printf ("%s\n", pathname);
}
}
更多精彩
赞助商链接

点击下载此文件