ls 명령어 구현관련 질문입니다.
안녕하세요. 리눅스 프로그래밍을 이제 막 시작한 초보입니다. 어제도 질문을 올려서 좋은 답변을 주셔서 해결했는데요. 오늘도 막히는 부분이 있어서 질문드립니다. 일단 밑의 소스는 ls명령어를 구현한 코드인데요. 질문은 그것에 관려된것이 아니라 문법적인 질문입니다. 일단 소스는 다음과 같습니다.
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
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 );
main(int ac, char *av[])
{
if ( ac == 1 )
do_ls( "." );
else
while ( --ac ){
printf("%s\n", *++av );
do_ls( *av );
}
}
void do_ls( char dirname[] )
/*
* list files in directory called dirname
*/
{
DIR *dir_ptr; /* the directory */
struct dirent *direntp; /* each entry */
if ( ( dir_ptr = opendir( dirname ) ) == NULL )
fprintf(stderr,"ls1 cannot open %s\n", dirname);
else
{
while ( ( direntp = readdir( dir_ptr ) ) != NULL )
dostat( direntp->d_name );
closedir(dir_ptr);
}
}
void dostat( char *filename )
{
struct stat info;
if ( stat(filename, &info) == -1 ) /* cannot stat */
perror( filename ); /* say why */
else /* else show info */
show_file_info( filename, &info );
}
void show_file_info( char *filename, struct stat *info_p )
/*
* display the info about 'filename'. The info is stored in struct at *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 );
}
/*
* utility functions
*/
/*
* This function takes a mode value and a char array
* and puts into the char array the file type and the
* nine letters that correspond to the bits in mode.
* NOTE It does not code setuid, setgid, and sticky
* codes
*/
void mode_to_letters( int mode, char str[] )
{
strcpy( str, "----------" ); /* default=no perms */
if ( S_ISDIR(mode) ) str[0] = 'd'; /* directory? */
if ( S_ISCHR(mode) ) str[0] = 'c'; /* char devices */
if ( S_ISBLK(mode) ) str[0] = 'b'; /* block device */
if ( mode & S_IRUSR ) str[1] = 'r'; /* 3 bits for user */
if ( mode & S_IWUSR ) str[2] = 'w';
if ( mode & S_IXUSR ) str[3] = 'x';
if ( mode & S_IRGRP ) str[4] = 'r'; /* 3 bits for group */
if ( mode & S_IWGRP ) str[5] = 'w';
if ( mode & S_IXGRP ) str[6] = 'x';
if ( mode & S_IROTH ) str[7] = 'r'; /* 3 bits for other */
if ( mode & S_IWOTH ) str[8] = 'w';
if ( mode & S_IXOTH ) str[9] = 'x';
}
#include <pwd.h>
char *uid_to_name( uid_t uid )
/*
* returns pointer to username associated with uid, uses getpw()
*/
{
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 ;
}
#include <grp.h>
char *gid_to_name( gid_t gid )
/*
* returns pointer to group number gid. used getgrgid(3)
*/
{
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;
}
소스가 좀 길지만 별로 어렵지 않습니다. 근데 show_file_info함수를
보면 처음에
char *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
void mode_to_letters();
이 두줄이 이해가 안갑니다. 처음줄은 char형 포인터 변수를 선언하는것도
아니고 char형 포인터를 반환하는 함수를 선언하는것도 아닌것 같은데요.
무슨 용도로 쓰였는지 궁금합니다. 두번째 줄인 void mode_to_letters()
도 왜 있는건지 궁금합니다. 혹시 함수선언 때문에 있는지 함수선언은 메인함수
시작하기전에 하였고 또한 함수선언을 함수안에서도 선언할수있나요?
그리구 왜 매개변수가 하나도 없이 선언이 되었나요?
초보적인 질문인것 같지만 도저히 이해가 안가서요..^^ 가르침 부탁드립니다.
Re: ls 명령어 구현관련 질문입니다.
char형 포인터를 반환하는 함수를 선언하는 것 맞습니다. :)
선언은 몇 번을 해도 됩니다. 식별자의 뜻이 달라지면 안되지만요.
물론 함수 안에서 선언을 해도 됩니다. 이때는 선언하는 식별자의 통용 범위가
그 함수 내부로 제한됩니다.
C의 경우 매개변수가 없는 선언은 매개변수에 대한 정보는 알려지지 않았다는 뜻입니다.
void f();
void g(void);
이렇게 하면 아무 것도 반환하지 않고 어떤 매개변수를 갖는지 알 수 없는 함수
f와 반환값도 매개변수도 없는 함수 g를 선언한 것입니다.
만약 C++이라면 void g(); 는 void g(void); 와 같은 뜻입니다.
ps. 코드를 올리실 때는 질문에 꼭 필요한 부분만 정리해서 올리는 것이 보기 편합니다.
아 그렇군요. 답변 감사합니다..^^ 앞으로 필요한 부분만 짧게 잘라서
아 그렇군요. 답변 감사합니다..^^ 앞으로 필요한 부분만 짧게 잘라서 올리겠습니다. 수고하세요..
댓글 달기