LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-04-2015, 07:22 AM   #1
koxp
LQ Newbie
 
Registered: May 2015
Posts: 15

Rep: Reputation: Disabled
simple Tree/Linux


i have a problem with my code. It should list all directories but it seems like that it doesnt enter the if(content->d_type = DT_DIR) statement. can someone help me? yesterday it works and i dont knw what i have done wrong


the output should look like:
Directory 1
file 1
file 2
Directory 2
file 1
file 2
directory 2.1
file.....

Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include <string.h>

void opendirectory(char *direc, int tab)
{
	DIR *dir;
	struct dirent *content;
	char *newdir;
	int i;

dir = opendir(direc);
	if (dir){
		while (0 != (content = readdir(dir))) {
			if (strcmp(".", content->d_name) == 0)
				continue;
			if (strcmp("..", content->d_name) == 0)
				continue;
if (content->d_type == DT_DIR) {
	for (i = 0 ;  i < tab; i++)
		printf("\t");
			printf("%s/ (d)\n", content->d_name);
		newdir = malloc(strlen(direc) + strlen(content->d_name) + 2);
			sprintf(newdir, "%s/%s", direc, content->d_name);
			opendirectory(newdir, tab + 1);
	} else {
for (i = 0 ;  i < tab; i++)
		printf("\t");
			printf("%s/ (f)\n", content->d_name);
			}
		}
	}
}

void usage(void)
{
	printf("usage: tree [-hil]\n");
			exit(1);
}

int main(int argc, char *argv[])
{
	int h, i, l;
	int optargs;
	int hflag, iflag, lflag;
	char *firstdir;
	char *helper = ".";
	int j;

while ((optargs = getopt(argc, argv, "hil:")) != -1) {
		switch (optargs) {
		case 'h':
			hflag = 1;
			helpfile();
			break;
		case 'i':
			iflag = 1;
			break;
		case 'l':
			lflag = 1;
			break;
		default:
			usage();
		}
	}
	if (argc < 2)
		firstdir = helper;
	else
		firstdir = argv[1];
		opendirectory(firstdir, 0);
	return EXIT_SUCCESS;
}
 
Old 06-04-2015, 09:42 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 167

Rep: Reputation: Disabled
Not all file types will return the value. Check and see if DT_UNKNOWN is being returned in the field.
 
Old 06-04-2015, 10:01 AM   #3
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Posts: 2,679

Rep: Reputation: Disabled
...or maybe just use tree itself.
(man tree)
 
Old 06-04-2015, 10:24 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You can also use the S_ISDIR macro from stat.h.
 
Old 06-04-2015, 10:46 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,609

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
couldn't test your program:
Code:
make koxp
gcc -o koxp -m32 -std=c99 -W -Wall -Wextra -pedantic -g koxp.c 
koxp.c: In function 'main':
koxp.c:60: warning: implicit declaration of function 'helpfile'
koxp.c:54: warning: unused variable 'j'
koxp.c:49: warning: unused variable 'l'
koxp.c:49: warning: unused variable 'i'
koxp.c:49: warning: unused variable 'h'
/tmp/cc0Xgdcj.o: In function `main':
/home/zsiga/proba/koxp.c:60: undefined reference to `helpfile'
collect2: ld returned 1 exit status
 
Old 06-04-2015, 04:52 PM   #6
SCSIraidGURU
Member
 
Registered: Oct 2014
Posts: 69

Rep: Reputation: Disabled
I am not sure this will help you. I am writing a C# program that compares the var/www folder to a backup drive on another workstation and writes the changes to the backup drive pathB. It does a directory list from both var/www and the other drive. It reads directories, subdirectories and files. Visual studio for C# is what I wrote it in. I have used it on Linux.

http://www.scsiraidguru.com/CSharp/CopyChange.html
 
Old 06-04-2015, 05:14 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,805
Blog Entries: 6

Rep: Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619
Quote:
Originally Posted by SCSIraidGURU View Post
I am not sure this will help you. I am writing a C# program that compares the var/www folder to a backup drive on another workstation and writes the changes to the backup drive pathB. It does a directory list from both var/www and the other drive. It reads directories, subdirectories and files. Visual studio for C# is what I wrote it in. I have used it on Linux.
I am sure it doesn't help.

Running an M$ application to write C-{pound-sign, number-sign, octothorpe} code to do a very simple built-in operation on a GNU/Linux machine is nonsense, at best. Please do not pollute LQ threads with such noise posing as answers.

To OP - as already suggested, simply use the tree command, built in on most GNU/Linux platforms:

Code:
man tree

...OR...

tree --help
No need to reinvent the wheel yet again.
 
Old 06-04-2015, 06:12 PM   #8
koxp
LQ Newbie
 
Registered: May 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
first of all thx for all answers

I know that i could use the TREE programm itself, but this projekt is somethink like a homework.
I found the mistake but now I have another problem.

If I put the argument -i in the command line (:/Tree -i) I want that the programm should list me the INO_number.
I know that i can use the dirent structure an make a simple if statement, but my problem is how i can use the iflag in my opendirectory funktion.. (i hate pointers ^^)
 
Old 06-05-2015, 04:40 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,609

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
It's unrelated to pointers, it is a logical value (yes/no).
In this case, you might use a global variable:
Code:
static struct {
    int debug;
    int show_inode;
} opt = {
/* debug */    1,
/* inode */    0
};

...
if (opt.show_inode)
 
1 members found this post helpful.
Old 06-09-2015, 02:53 PM   #10
koxp
LQ Newbie
 
Registered: May 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
so thanks for all answers . a new problem occurred and now i need help again ^^.

in this Project i have to implemnet these thinks.

timeout()
allow passing a maximum execution time in seconds
nice()
proper use of nice via commandline argument
error handling (specifically permissions issues)
reasonable failure information
proper cleanup (memory) in case of failure

so what he is meaning with these points. i dont understant that. should i use the timeout funktion or should i write a own timeout? and how i can implement this

and i also dont understand how to implement the other thinks.
my current code to list all directories/Files in the current directory

Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>

int activ;
int hflag, iflag, lflag;

void helpfile(void)
{
	printf("With this tool, you're able to list all Directories/Files");
	printf(" in your current Directorie.\n");
	printf("-i = OUTPUT of the Inode Number\n");
	printf("-l = Marking symbolic links with a [l]\n");
}
int opendirectory(char *direc, int tab, int i, int l)
{
	DIR *dir;
	struct dirent *content;
	char *newdir;
	int tabcounter;

dir = opendir(direc);
	if (NULL == dir) {
		perror(NULL);
			return EXIT_FAILURE;
}
if (dir) {
while (0 != (content = readdir(dir))) {
			if (strcmp(".", content->d_name) == 0)
				continue;
			if (strcmp("..", content->d_name) == 0)
				continue;
if (content->d_type == DT_DIR) {
	for (tabcounter = 0 ;  tabcounter < tab; tabcounter++)
		printf("\t");
			if (i == 1)
				printf("[ld] ", content->d_ino);
		printf("s/ (d)\n", content->d_name);
		newdir = malloc(strlen(direc) + strlen(content->d_name) + 2);
		sprintf(newdir, "%s/%s", direc, content->d_name);
		opendirectory(newdir, tab + 1, iflag, lflag);
} else if (content->d_type == DT_REG) {
	for (tabcounter = 0 ;  tabcounter < tab; tabcounter++)
		printf("\t");
			if (i == 1)
				printf("[%ld] ", content->d_ino);
		printf("%s/ (f)\n", content->d_name);
} else if (content->d_type == DT_LNK)
		printf("%s/ (l)\n", content->d_name);
		}
	}
	free(newdir);
}
void failure(void)
{
	printf("-h for help\n");
			exit(1);
}
void *timeout(void *arg)
{
}
int main(int argc, char *argv[])
{
	pthread_t thread1;
	int h, i, l;
	int optargs;
	char *firstdir = ".";
	int t_check;

while ((optargs = getopt(argc, argv, "hil")) != -1) {
		switch (optargs) {
		case 'h':
			hflag = 1;
			helpfile();
			return EXIT_SUCCESS;
		case 'i':
			iflag = 1;
			break;
		case 'l':
			lflag = 1;
			break;
		default:
			failure();
			return EXIT_FAILURE;
		}
	}
	if (pthread_create(&thread1, NULL, *timeout, NULL)) {
		perror("ERROR");
		exit(EXIT_FAILURE);
	}
		opendirectory(firstdir, 0, iflag, lflag);
	return EXIT_SUCCESS;
}

Last edited by koxp; 06-09-2015 at 02:54 PM.
 
Old 06-09-2015, 03:11 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,569

Rep: Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130
Quote:
Originally Posted by koxp View Post
so what he is meaning with these points. i dont understant that. should i use the timeout funktion or should i write a own timeout? and how i can implement this

and i also dont understand how to implement the other thinks.
Sounds like a good question for your teacher.
 
Old 06-09-2015, 05:17 PM   #12
koxp
LQ Newbie
 
Registered: May 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
his response ...
We gone through this part in the lecture, so it has to be clear what you have to do. Think 5 seconds more about it.

the problem is, everyone has a different project so , i cannot ask anyone strange teacher i know.
Firstly can you explain me how to implement a timeout in seconds?
 
Old 06-10-2015, 09:03 AM   #13
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 878

Rep: Reputation: 208Reputation: 208Reputation: 208
equals is == (2 equal signs) whereas 1 equal sign as you used in post #1 is an assignment.

OK
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference strict binary tree nad extended binary tree . tushar_pandey Programming 1 07-18-2012 12:30 PM
Path to Linux Tree _cih_ Linux - Newbie 3 11-20-2006 05:54 PM
the bible = the tree of the knowledge of good and evil () Jesus = the tree of life Michael111 General 2 04-14-2004 05:28 PM
Linux heirarchy tree arkamir Linux - Newbie 2 12-29-2003 05:54 PM
need a P-Tree (Patricia Tree) library manaskb Programming 1 11-02-2002 07:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration