LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-11-2004, 09:20 AM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
How to list all the files in a directory


How to list all the files in a directory

I have a code below that list the PID in the directory for /proc. How do I modified it so that it would list
all files name in any directory such as /var/usr/.
I would like to have variable that control whether or not
it would list all files in all subdirectories as well.

==========================
Code:
find_processes_and_shut_it_down (char dvpnd_char[21])
{
   DIR  *d = opendir("/proc");
   FILE *fp, *fopen();
   char *end;
   struct dirent *de;
   pid_t pid;
   char buf[100];

   while ((de = readdir(d)) != NULL)
     {
        /* for this directory, check the name, we want only
            numeric filenames (pid). */
        pid = strtoul(de -> d_name, & end, 10);
        if (*end != '\0')
          continue; // skip this dir.

        sprintf( buf, "/proc/%d/cmdline", pid);
        fp = fopen(buf, "rt");
        if (fp == NULL)
          continue;

        fgets(buf, sizeof(buf), fp);
        fclose(fp);

        /* compare command line to see if it is 
            the program you want.     */
        /* Perhaps drop the first argument */
        /* and just compare the first token and 
            see if the name matches. */

        /* printf ("buf = %s  pid = %d\n", buf, pid); */

        if (strcmp (buf, dvpnd_char) == 0)

          {
            /* the pid in question is in pid! kill this 
                pid (process id) */
            printf ("\n");
            printf ("\n");
            printf ("string match.  buf = %s pid = %d
                       dvpnd_char = %s\n", buf, pid, dvpnd_char);
            kill (pid, SIGTERM);
          }
    }
    closedir(d);
    pclose(fp);
}
 
Old 05-11-2004, 10:08 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I've posted code like this about a dozen times here. You can find other examples by I did by searching, but they are are similar. You will only be concerned with the listdir method.
 
Old 05-11-2004, 10:09 AM   #3
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
pwned by crabboy : my op said what he said but longer :P

Last edited by kooch; 05-11-2004 at 10:29 AM.
 
  


Reply



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
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 12:08 PM
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 06:03 AM
how to list how many files in a directory? malaka56 Linux - Software 8 09-02-2005 05:37 AM
Missing List.* files in URPMI directory Ronw Linux - General 2 10-08-2003 03:57 AM
list only files in current directory xscousr Programming 8 09-22-2003 07:35 AM

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

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

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
Open Source Consulting | Domain Registration