The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Major OS errors/Bash errors help!!!! wcmmlynn UNIX for Dummies Questions & Answers 12 11-13-2007 04:50 AM
Adapter Errors and Link Errors mcastill66 AIX 2 08-02-2005 06:51 PM
Adapter Errors and Link Errors mcastill66 UNIX for Advanced & Expert Users 0 08-02-2005 06:11 PM
errors nobody HP-UX 1 12-14-2003 10:08 AM
Help with errors Phaedirean Filesystems, Disks and Memory 5 08-12-2003 02:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-05-2007
anything2 anything2 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 2
Exclamation errors....

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.....
Code:
//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 *
 }*/

Last edited by reborg; 03-05-2007 at 03:30 PM..
  #2 (permalink)  
Old 03-05-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
This compiles. It still has logic problems, I think. You can fix those.
Moved the definition of struct NODE above the first reference to it, and added a }
on the second while loop in main(). ..and it's always int main()
Please use code tages next time - I did not fix up your code
Code:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>

int flag=0; 
//for option s
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 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 *);

int 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[] )
{
	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;
}


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 *
}
*/
  #3 (permalink)  
Old 03-06-2007
matrixmadhan matrixmadhan is online now Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,950
this looks similiar post to me
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0