I officially have the worst C professor in the world. The guy hasn't answered my email in weeks and I don't have a class meeting before this assignment is due, so I have come here for some help. If anyone could point me in the right direction any part of this project it would be greatly appreciated. I have to do the following things on a Unix system.

1. Write a C program that only lists all files in the current directory and all files in subsequent sub directories.
2. Write a C program to print the lines of a file which contain a word given as the program argument (a simple version of grep UNIX utility).
3. Write a C program to emulate the ls -l UNIX command that prints all files in a current directory and lists access privileges etc. DO NOT simply exec ls -l from the program.
4. Write a C program that gives the user the opportunity to remove any or all of the files in a current working directory. The name of the file should appear followed by a prompt as to whether it should be removed.

I am not certain where to start. These are four separate programs, so help with any part would be awesome. I am not asking for code, just a point in the right direction.

>I am not certain where to start.
Then you should consider switching your major. "I don't know where to start" is equivalent to "I'm not cut out for programming".

>1. Write a C program that only lists all files in the current
>directory and all files in subsequent sub directories.

This is a simple program. Do a man for opendir, readdir, and closedir, and stat. You can recursively go through the directory tree.

>2. Write a C program to print the lines of a file which contain a
>word given as the program argument (a simple version of grep
>UNIX utility).

This can be written using standard C (no Unix-specific code). Do a man for strstr. Presumably you know how to read lines from a file. :icon_rolleyes:

>3. Write a C program to emulate the ls -l UNIX command that
>prints all files in a current directory and lists access privileges etc.

Do a man for stat.

>4. Write a C program that gives the user the opportunity to
>remove any or all of the files in a current working directory.

This can be done in standard C too. Do a man for remove.

Thanks for the reply, that was what I was looking for. And in my defense there are a lot more jobs in computer science than programming... Also, I shouldn't have said I have no idea, because I did have an idea. I just wanted come clarification, because my professor sure isn't giving any.

I can print all the files and directories like this

#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int
main (void) {
  
  DIR *dp;
  struct dirent *ep;

  dp = opendir (".");
  if (dp != NULL) {
      while (ep = readdir (dp))
        printf("%s\n", ep->d_name);
      closedir (dp);
  }
  else
    perror ("Couldn't open the directory");

  return 0;
}

But how do I do this?

If (file is a sub directory)
open it, and print those files.

I can print all the files and directories like this

#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int
main (void) {
  
  DIR *dp;
  struct dirent *ep;

  dp = opendir (".");
  if (dp != NULL) {
      while (ep = readdir (dp))
        printf("%s\n", ep->d_name);
      closedir (dp);
  }
  else
    perror ("Couldn't open the directory");

  return 0;
}

But how do I do this?

If (file is a sub directory)
open it, and print those files.

First of all, you need to be able to tell files from directories. I am not sure if a dirent has such information; in any case you may stat the name:

struct stat statbuf;
stat(ep->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
    // It is a directory
}

Second should be obvious: once you detect a directory, recurse into it.

That code compiles but I get a wicked error that I have never seen before. What's wrong with this?

#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>

int
main (void) {
  
  DIR *dp;
  struct dirent *ep;
  struct stat statbuf;

  dp = opendir (".");
  if (dp != NULL) {
      while (ep = readdir (dp)){
        printf("%s\n", ep->d_name);
        
        stat(ep->d_name, &statbuf);
        if(S_ISDIR(statbuf.st_mode)){ 
           printf("   DIR");
        }
        
      closedir (dp);
      }
  }

  return 0;
}

I get a screen filling error that starts like:

.
   DIRtest
*** glibc detected *** ./test: double free or corruption (top): 0x0000000017ee8010 ***

That code compiles but I get a wicked error that I have never seen before. What's wrong with this?

dp = opendir (".");
  if (dp != NULL) {
      while (ep = readdir (dp)){
...
      closedir (dp);
      }
  }

Do you see anything funny about this fragment?

I can vouch for this guy I am attending the same class and it's being taught by a grad student who has no idea how to teach. It blew his mind weeks ago that we didn't already know C programming. So on top of not being prepared to teach of this subject, he does a poor job at teaching anything to begin with because of lack of experience.

It's funny I found this because I to am surfing google looking for some info to point me in the right direction.

>I can vouch for this guy
Yea, that's not suspicious at all. :icon_rolleyes: From past experience, I'm more inclined to believe that you're a sock puppet rather than a concerned classmate.

>I can vouch for this guy
Yea, that's not suspicious at all. :icon_rolleyes: From past experience, I'm more inclined to believe that you're a sock puppet rather than a concerned classmate.

Believe what you will, I know what is true. Why even bother posting when someone is asking for help, you have a very poor attitude.

>Why even bother posting when someone is asking for help, you have a very poor attitude.
Pardon? You must have missed the fact that I was the first person to help Mattpd in this thread. You must have also missed the fact that the OP hasn't replied saying that he still needs help. What right do you have to complain about my attitude when you've contributed absolutely nothing to the thread?

First off, I did not come here to contribute any help to the post, as I said I stumbled upon this site while searching for help myself. I only posted because I agreed with the OP about our instructor. Then you proceed to call me a sock puppet...
On top of this comment you left in your first post:

Then you should consider switching your major. "I don't know where to start" is equivalent to "I'm not cut out for programming".

That's why I say you have a poor attitude.

>I only posted because I agreed with the OP about our instructor.
>Then you proceed to call me a sock puppet...

Exactly. I call it like I see it, and if you have a problem with that, you're welcome to kiss my ass. :)

>That's why I say you have a poor attitude.
It's always amusing when some wannabe net nanny joins Daniweb and immediately tries to tell us how to do things. This is a meritocracy. Until you've earned the right to have an opinion, nobody gives a damn what you think.

So as to avoid driving this thread off topic for too long, please PM me if you want to continue being verbally abused. kthxbye.

Thanks for the help guys, I finished the project on time and received a 100. This is how I ended up doing the file listing program for anyone that is interested.

#include <stdio.h>
     #include <dirent.h>
     #include <string.h>
     
     void dir (char name[]);
     char name[25]; 
     static int
     one (const struct dirent *unused)
     {
       return 1;
     }
     
     int
     main (void)
     {
       struct dirent **eps;
       int n;
       
     
       n = scandir (".", &eps, one, alphasort);
       if (n >= 0)
         {
           int cnt;
           for (cnt = 0; cnt < n; ++cnt){
             puts (eps[cnt]->d_name);
             if (eps[cnt]->d_type == DT_DIR && (strcmp(eps[cnt]->d_name,"..") != 0 ) && (strcmp(eps[cnt]->d_name,".") != 0 ) ) {
               strcpy(name, eps[cnt]->d_name);
               
               dir(name);
             }
           }
         }
       else
         perror ("Couldn't open the directory");
     
       return 0;
     }

void dir (char name[]) {
       struct dirent **eps2;
       int p;
       int cnt2 = 0;
       p = scandir (name, &eps2, 0, alphasort);
       puts(" [DIR]");
       
           if (p >= 0) {
           int cnt;
           for (cnt2 = 0; cnt2 < p; ++cnt2)
             printf (" %s/%s\n",name,eps2[cnt2]->d_name);
           
}

return;

}

>I can vouch for this guy
Yea, that's not suspicious at all. :icon_rolleyes: From past experience, I'm more inclined to believe that you're a sock puppet rather than a concerned classmate.

am I the puppet master? Because that is just silly if you think so. There are too many classes being taught by grad students that don't really care their students. Not to mention my instructor can barley speak English. It's like learning a foreign language from someone that doesn't even speak your native language. You can't ask questions because he can't understand what you are asking. All he is really good for is reading things too us. It leads to a lot of confusion, but thanks again for helping me get straightened out.

And to Trad, are you literally in my 3320 class or just in a similar situation?

sock puppet just means an "empty head", and not implying who is in control. the emphasis is on sock; not so much on puppet.

and get used to shitty ESL grad students teaching low level programming classes. i too used to be annoyed by this, but have realized that it helps weed people out who need everything spoon-fed to them.

sock puppet just means an "empty head", and not implying who is in control. the emphasis is on sock; not so much on puppet.

and get used to shitty ESL grad students teaching low level programming classes. i too used to be annoyed by this, but have realized that it helps weed people out who need everything spoon-fed to them.

Per the Urban Dictionary.com "a sock puppet is an account made on an internet message board, by a person who already has an account, for the purpose of posting more-or-less anonymously."

At least that's how I took it. I don't really know how wasting my time doing something like that would be a benefit, but apparently Narue has spotted these so called "sock puppets" before.

Anyway, my problems are solved for now, but I am sure I will be back next semester. Thanks to all that helped.

>At least that's how I took it.
And that's how I meant it.

>I don't really know how wasting my time doing something like that would be a benefit
Me neither. Usually sock puppets start showing up with spammers (the fake question/spam answer pattern), trolls trying to attack or defend someone, and cheaters trying to get help after they've been busted by the forum. You don't fit any of those patterns, but someone from the same class posting to your thread with nothing to add but confirmation of something you claimed is rather suspicious.

I honestly don't care one way or another, but it was fun seeing the replies to my speculation. :)

am I the puppet master? Because that is just silly if you think so. There are too many classes being taught by grad students that don't really care their students. Not to mention my instructor can barley speak English. It's like learning a foreign language from someone that doesn't even speak your native language. You can't ask questions because he can't understand what you are asking. All he is really good for is reading things too us. It leads to a lot of confusion, but thanks again for helping me get straightened out.

And to Trad, are you literally in my 3320 class or just in a similar situation?

Yeah I'm in your class MW 1:30-2:40.

Yeah I'm in your class MW 1:30-2:40.

Haha that is too funny. I thought you were just another student in my situation. I hope you learned more than I did this semester, and good luck on Wednesday.

Haha that is too funny. I thought you were just another student in my situation. I hope you learned more than I did this semester, and good luck on Wednesday.

Ha! Not really, but I'll get by. Actually me and another guy from our classes are studying for the exam Weds right now. But, yeah good luck to you too, I know I need it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.