#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<dirent.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
void cpMain(char *, char *);
void ls2Main(int, char *);
void oops(char *, char *);
void do_ls(char[]);
void dostat(char *);
void show_file_info( char *, struct stat *);
void mode_to_letters( int , char [] );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
int main(int argc, char *argv[])
{
if(argc == 1)
{
char s1[20];
char s2[20];
cpMain(s1,s2);
int lsVar = 1;
char lsArg[20] = "/home";
ls2Main(lsVar, lsArg);
}
}
void cpMain(char *argv, char *argv2)
{
char s1[20];
char s2[20];
printf("Enter source.\n");
scanf("%s", s1);
printf("Enter destination.\n");
scanf("%s", s2);
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];
if ( (in_fd=open(s1, O_RDONLY)) == -1 )
oops("Cannot open ", s1);
if ( (out_fd=creat(s2, COPYMODE)) == -1 )
oops( "Cannot creat", s2);
while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
if ( write( out_fd, buf, n_chars ) != n_chars )
oops("Write error to ", s2);
if ( n_chars == -1 )
oops("Read error from ", s1);
if ( close(in_fd) == -1 || close(out_fd) == -1 )
oops("Error closing files","");
}
void ls2Main(int ac, char *av)
{
if ( ac == 1 )
do_ls( "." );
return;
}
void oops(char *s1, char *s2)
{
fprintf(stderr,"Error: %s ", s1);
perror(s2);
exit(1);
}
void do_ls( char dirname[] )
{
DIR *dir_ptr;
struct dirent *direntp;
if ( ( dir_ptr = opendir( dirname ) ) == NULL )
fprintf(stderr,"ls2: cannot open %s\n", dirname);
else
{
while ( ( direntp = readdir( dir_ptr ) ) != NULL ) {
if (strcmp(direntp->d_name,".")==0 ||
strcmp(direntp->d_name,"..")==0) continue;
dostat( direntp->d_name );
}
closedir(dir_ptr);
}
}
void dostat( char *filename )
{
struct stat info;
if ( stat(filename, &info) == -1 )
perror( filename );
else
show_file_info( filename, &info );
}
void show_file_info( char *filename, struct stat *info_p )
{
char *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
void mode_to_letters();
char modestr[11];
mode_to_letters( info_p->st_mode, modestr );
printf( "%s" , modestr );
printf( "%4d " , (int) info_p->st_nlink);
printf( "%-8s " , uid_to_name(info_p->st_uid) );
printf( "%-8s " , gid_to_name(info_p->st_gid) );
printf( "%8ld " , (long)info_p->st_size);
printf( "%.12s ", 4+ctime(&info_p->st_mtime));
printf( "%s\n" , filename );
}
void mode_to_letters( int mode, char str[] )
{
strcpy( str, "----------" );
if ( S_ISDIR(mode) ) str[0] = 'd';
if ( S_ISCHR(mode) ) str[0] = 'c';
if ( S_ISBLK(mode) ) str[0] = 'b';
if ( mode & S_IRUSR ) str[1] = 'r';
if ( mode & S_IWUSR ) str[2] = 'w';
if ( mode & S_IXUSR ) str[3] = 'x';
if ( mode & S_IRGRP ) str[4] = 'r';
if ( mode & S_IWGRP ) str[5] = 'w';
if ( mode & S_IXGRP ) str[6] = 'x';
if ( mode & S_IROTH ) str[7] = 'r';
if ( mode & S_IWOTH ) str[8] = 'w';
if ( mode & S_IXOTH ) str[9] = 'x';
}
char *uid_to_name( uid_t uid )
{
struct passwd *getpwuid(), *pw_ptr;
static char numstr[10];
if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
sprintf(numstr,"%d", uid);
return numstr;
}
else
return pw_ptr->pw_name ;
}
char *gid_to_name( gid_t gid )
{
struct group *getgrgid(), *grp_ptr;
static char numstr[10];
if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
sprintf(numstr,"%d", gid);
return numstr;
}
else
return grp_ptr->gr_name;
}
上面的代码首先运行cp命令,然后输出与在终端中键入ls -l
相同的内容。我试图找出如何使ls
部分按大小排序输出(就像您要键入ls -lS
)或最后一次修改(如果您要键入ls -lt
}但我正在努力弄清楚如何这样做。任何人都可以帮我弄清楚如何做到这一点?