hi
i have written a program to display files in a directory
but it doesnt compile on the sun/solaris system....
im using gcc(inbuilt compiler)
im generally used to working on a windows environment so maybe im overlookiing something
ive added comments to help readability of the program
i also need info on how to display "links"
basically if i come across a link in the dir i need to print it out....
heres the prog.....
//make it work
/* ls2.c
* purpose list contents of directory or directories
opens directories recursively and displays contents
need to display link???
and an option -s...only display the directories
probable error in dynamic memory allocation or related
*/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
//#include <iostream.h>
//#include <conio.h>
#include <string.h>
#include <stdlib.h>
int flag=0; //for option s
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 );
void insert( struct NODE *, struct NODE *);
void inorder(struct NODE *);
void printfile(struct NODE *);
main(int ac, char *av[])
{ char ch;
while ((ch=getopt(ac, av, "s")) != -1) {
if(ch=='s') flag=1;
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 );
//if(modestr[0]=='d'){
//struct NODE *root;
//}
struct NODE *new;
new=((struct NODE*)malloc(sizeof(struct NODE)));
new->depth=0;
new->fname=filename;
new->dirtree=NULL;
new->dflag=0;
new->eflag=0;
if(modestr[0]!='d' && (modestr[3]=='x' || modestr[6]=='x' || modestr[9]=='x')) new->eflag=1;//file is an executable
switch(modestr[0]) {
case 'd':
new->value=1;
new->dflag=1;
do_ls(filename);
//isdir(new);
break;
case 'c':
new->value=2;
break;
case 'b':
new->value=3;
break;
case 'l':
new->value=4;
break;
default:break;
}
insert(root, new);
//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 ( S_ISLNK(mode) ) str[0]='l'; //link
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;
}
struct NODE {
struct NODE *left;
int depth;
int dflag; //indicates spacing
int eflag; //indicates executable file
char *fname;
int value; //filetype
struct NODE *right;
struct NODE *dirtree;
};
struct NODE *root=NULL;
struct NODE *nodeptr;
void insert( struct NODE *curr, struct NODE *new ) {
new->right=NULL;
new->left=NULL;
if(curr->value==NULL) curr=new;
else if(new->value <= curr->value) {
if(curr->left != NULL)
insert(curr->left, new);
else
new->depth++;
curr->left = new;
}
else {
if(curr->right != NULL)
insert(curr->right, new);
else{
curr->right = new;new->depth++;}
}
}
void inorder(struct NODE *curr) {
if(curr->left != NULL) inorder(curr->left); /*step-1 & step-2*/
//printf("%d", curr->value); /*step-3*/
printfile(curr);
if(curr->right != NULL) inorder(curr->right); /*step-4*/
}
void printfile(struct NODE *curr) {
if(curr->eflag==1 && flag==1) printf( "%s*\n" , curr->fname );
if(curr->value==4 && flag==1) printf( "%s -> \n" , curr->fname );
if(curr->value==1)
{ printf( "%s/\n" , curr->fname );
inorder(curr->dirtree);
}
}
/*
void isdir(struct NODE *curr)
{
do_ls(curr->fname);
}
*/
/*
struct FILE
{
char type;
struct NODE *
}*/