LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple Tree/Linux (https://www.linuxquestions.org/questions/programming-9/simple-tree-linux-4175544468/)

koxp 06-04-2015 06:22 AM

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


SoftSprocket 06-04-2015 08:42 AM

Not all file types will return the value. Check and see if DT_UNKNOWN is being returned in the field.

fatmac 06-04-2015 09:01 AM

...or maybe just use tree itself. ;)
(man tree)

millgates 06-04-2015 09:24 AM

You can also use the S_ISDIR macro from stat.h.

NevemTeve 06-04-2015 09:46 AM

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


SCSIraidGURU 06-04-2015 03:52 PM

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

astrogeek 06-04-2015 04:14 PM

Quote:

Originally Posted by SCSIraidGURU (Post 5372264)
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.

koxp 06-04-2015 05:12 PM

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 ^^)

NevemTeve 06-05-2015 03:40 AM

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)


koxp 06-09-2015 01:53 PM

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


suicidaleggroll 06-09-2015 02:11 PM

Quote:

Originally Posted by koxp (Post 5374758)
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.

koxp 06-09-2015 04:17 PM

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?

AnanthaP 06-10-2015 08:03 AM

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

OK


All times are GMT -5. The time now is 06:06 AM.