Sponsored Content
Full Discussion: directory as tree
Top Forums Programming directory as tree Post 302108845 by matrixmadhan on Thursday 1st of March 2007 08:38:18 AM
Old 03-01-2007
Code:
#include	<stdio.h>
#include	<pwd.h>
#include	<grp.h>
#include	<sys/types.h>
#include	<dirent.h>
#include	<sys/stat.h>
#include	<time.h>
#include	<unistd.h>

void do_ls(char[], int);
void dostat(char *,char *,int);
void show_file_info( char *, struct stat *,char *,int);
void mode_to_letters( int , char [] );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );

int main(int ac, char *av[])
{	
	//char c;
	//int flag=0;
	int counter=0;
	//if(ch == 's'){flag =1;}
	if ( ac == 1 )
		do_ls( ".",counter );
	else
		while ( --ac ){
			++av;
			do_ls( *av,counter );
		}
  return 0;
}

void do_ls( char dirname[] ,int count)
/*
 *	list files in directory called dirname
 */
{
	DIR		*dir_ptr;		/* the directory */
	struct dirent	*direntp;		/* each entry	 */
	char *cwd;
	count++;

	if ( ( dir_ptr = opendir( dirname ) ) == NULL )
		fprintf(stderr,"ls1: cannot open %s\n", dirname);
	else
	{
		if(dirname=="."){
			if((cwd= getcwd(NULL,64))==NULL){
				perror("pwd");
				exit(2);
				}
				(void)printf("%s\n",cwd);
				}
			else{
				if(dirname[0]=='.'){
				dirname+=2;
				
				printf("%s/\n", dirname);
				dirname-=2;
				}
				else (void)printf("%s/\n", dirname);
			    }
		while ( ( direntp = readdir( dir_ptr ) ) != NULL )
		if ((strcmp(direntp->d_name,"."))!=0 && (strcmp(direntp->d_name,".."))!=0)
		{
			dostat(dirname, direntp->d_name,count );
		}closedir(dir_ptr);
	}
}

void dostat(char* directoryName, char *filename ,int count )
{
	struct stat info;
	char path[128];

	sprintf(path, "%s/%s",directoryName,filename);
	if ( stat(path, &info) == -1 )		/* cannot stat	 */
		perror( path );			/* say why	 */
	else					/* else show info	 */
        {
           show_file_info( path, &info,filename, count);
        }
}

void show_file_info( char *path, struct stat *info_p, char* fname2, int count )
/*
 * display the info about 'filename'.  The info is stored in struct at *info_p
 */
{
	int sizetest;
	int condition=count;
	
	while(condition>0){
	printf("    ");
	condition--;
	}
	
	//char	*uid_to_name(), *ctime(), *gid_to_name(), *filemode();
	
	//void	mode_to_letters();
        char    modestr[11];

	mode_to_letters( info_p->st_mode, modestr );
	
	//print file type
	if(modestr[0]=='d')//dir
	{do_ls(path,count);
	}
	else
	{
             printf( "%s"  , fname2 );
	
	     if(modestr[0]!='d' && (modestr[3]=='x' || modestr[6]=='x' || modestr[9]=='x'))
	     { 	
	       printf( "*");
	     }
        }
}

/*
 * 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';
}


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 ;
}


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;
}

All your compilation problems are free now.

Try it out, and let us know for any difficulty or problems Smilie
 

10 More Discussions You Might Find Interesting

1. Programming

Directory tree search???

Hi all, I've got a problem, what function do i use to list the contents of all the directory tree (simular to "find")? Any other suggestions? Thank you all (3 Replies)
Discussion started by: solvman
3 Replies

2. Shell Programming and Scripting

Searching directory tree

I'm currently trying to write a script that will do the following: search a given directory tree for a file with MMDDYYYY in the name. delete those files only. I can't figure out how to make the script delete the files with the MMDDYYYY in the filename after finding them. Should I export... (7 Replies)
Discussion started by: blane
7 Replies

3. Shell Programming and Scripting

setup directory tree

All, I am new to Unix scripting ans was looking for some guidance. I basically have to: 1. Check if a directory exists - if not create it 2. Check the permissions of the dir - if Wrong change loop this. Sort of creating a directory tree. Thanks. (2 Replies)
Discussion started by: wicked24
2 Replies

4. Shell Programming and Scripting

directory tree

Hi all, The following is a script for displaying directory tree. D=${1:-`pwd`} (cd $D; pwd) find $D -type d -print | sort | sed -e "s,^$D,,"\ -e "/^$/d"\ -e "s,*/\(*\)$,\:-----\1,"\ -e "s,*/,: ,g" | more exit 0 I am trying to understand the above script.But... (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

5. UNIX for Dummies Questions & Answers

Protecting the directory tree

Hello, I am hoping someone maybe able to help me. I have set up an Apache web server on my sun server with user accounts in the main htdocs directory. My question is how to stop these users searching up the directory tree when they ftp/telnet to the server. Also is it possible to restrict the... (2 Replies)
Discussion started by: rooneyl
2 Replies

6. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies

7. Shell Programming and Scripting

creating a directory tree

Hi all, I'd like to create a directory tree, and define from stdin how many levels deep and how many directories in each level should be created. What I wrote does not work properly:#!/bin/bash #set -x read -p " What root directory? " rootDir && { /bin/rm -R $rootDir; mkdir $rootDir; } ||... (2 Replies)
Discussion started by: NBaH
2 Replies

8. UNIX for Dummies Questions & Answers

directory tree with directory size

find . -type d -print 2>/dev/null|awk '!/\.$/ {for (i=1;i<NF;i++){d=length($i);if ( d < 5 && i != 1 )d=5;printf("%"d"s","|")}print "---"$NF}' FS='/' Can someone explain how this works..?? How can i add directory size to be listed in the above command's output..?? (1 Reply)
Discussion started by: vikram3.r
1 Replies

9. UNIX for Dummies Questions & Answers

How to copy a tree of directory

Mi question is how can you copy only de three of directory and not the files in it. Only a need the three of directorys not the files (6 Replies)
Discussion started by: enkei17
6 Replies

10. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies
All times are GMT -4. The time now is 10:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy