ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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 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:
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 ^^)
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;
}
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.