errors....


 
Thread Tools Search this Thread
Top Forums Programming errors....
# 1  
Old 03-05-2007
Error 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  
Old 03-05-2007
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  
Old 03-06-2007
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Errors using ln

ln: Filename1 and Filename2 are identical. Seems to be an error.. Unable to proceed further Can you ppl help me? (5 Replies)
Discussion started by: DevM
5 Replies

2. Shell Programming and Scripting

Errors while using echo

Hi I am new to shell scripting. I am using a shell script to create a SQL script file and then call it as well. The created SQL script file has calls to 2 other sql script file, as well as compile and execute a procedure. all this works fine. I am getting some simple errors. My code is as below ... (8 Replies)
Discussion started by: man_expo
8 Replies

3. UNIX for Advanced & Expert Users

UDP errors

Hi, I can see "udpInOverflows" errors when I execute 'netstat -s' on my Solaris box. The number of errors are small - about 40. e.g. $ netstat -s|grep udp UDP udpInDatagrams =1249190732 udpInErrors = 0 udpOutDatagrams =31663030 udpOutErrors = 0... (1 Reply)
Discussion started by: chaandana
1 Replies

4. UNIX for Dummies Questions & Answers

Major OS errors/Bash errors help!!!!

Hi all, dummy here.... I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: [: =: unary... (12 Replies)
Discussion started by: wcmmlynn
12 Replies

5. UNIX for Dummies Questions & Answers

X errors

I'm trying to set up x2x between my desktop and laptop, and as a precondition to that, I'm just trying to get each to be able to display programs on the other (e.g. "desktop$ xterm -display laptop:0"). OS is Ubuntu 5.10 and X version is X.org 6.8.2 I've set up X to run without -nolisten, and... (1 Reply)
Discussion started by: vertigo23
1 Replies

6. UNIX for Advanced & Expert Users

What are these errors?

root@INTSETICADB # su - oracle Killed root@INTSETICADB # su - selectica Killed root@INTSETICADB # man prstat Reformatting page. Please Wait...Segmentation Fault - core dumped sys(cd /usr/man; /usr/lib/sgml/sgml2roff /usr/man/sman1m/prstat.1m > /tmp/sman_iUaW5x) fail! aborted (sorry) (3 Replies)
Discussion started by: abhijeetkul
3 Replies

7. AIX

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (2 Replies)
Discussion started by: mcastill66
2 Replies

8. UNIX for Advanced & Expert Users

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (0 Replies)
Discussion started by: mcastill66
0 Replies

9. HP-UX

errors

fatal server error: couldn't open x pointer device! is one attached? xinit: connection refused (errno 239): unable to connect to X server xinit: no such process (errno3): server error. (1 Reply)
Discussion started by: nobody
1 Replies

10. Filesystems, Disks and Memory

Help with errors

I am very new to the world of Unix, in the middle of a crash course as I write this due to a system failure for my company. This system went down approximately 24 hours ago due to some apparent thunderstorms that passed through. The problem is that my company, like many others, has laid off... (5 Replies)
Discussion started by: Phaedirean
5 Replies
Login or Register to Ask a Question