Results 1 to 5 of 5

Thread: Linux C API? Getting the directory structure.

  1. #1
    norfolknchance
    Guest

    Linux C API? Getting the directory structure.

    I am new to Linux and C and wondering where I can find an API for the main libraries of C. (I am a java programmer - sorry..)

    In the short term, what is the system call that returns to me a directory listing (I don't want to system("ls") because that would be dirty!). I thought to look into the source for ls, but I can't find it?

    While I'm posting - before I start using make files, I wanted to understand the basics. I took to working c files (simple) and created header files for them. I then created another file (main.c) and included both header files and in main tried to call functions from both files. I tried to compile this by doing:

    gcc main.c f1.c f2.c

    but the compiler complained as if I was abusing it! The header files contained their includes, defs and prototypes. Any immediate ideas on what I did wrong?

    Lloyd.

  2. #2
    norfolknchance
    Guest

    Re:Linux C API? Getting the directory structure.

    Urm.. actually ignore all that about compiling with gcc - I figured that out. (I forgot the # from include - school boy error!). Still require help with a C API for linux and getting directory listing??

  3. #3
    JimH
    Guest

    Re:Linux C API? Getting the directory structure.

    The "ls" program is a part of the fileutils package. You can see the source for "ls" by downloading it.

    This will show you a man page listing of references in "Linux programmers guide" (section 3 of the man pages).

    man -k directory | grep "(3)"

  4. #4
    Omani4Ever
    Guest

    Re:Linux C API? Getting the directory structure.

    Here is a simple program that will do what I think you want to do:

    /* ls.c */
    #include <sys/types.h>
    #include <dirent.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>

    int main(int argc, char ** argv)
    {
    DIR *dd; /* dir discriptor */
    struct dirent; *dirp /* dir pinter */

    if (argc != 2)
    {
    printf("Invalid number of args\n");
    exit(1);
    }

    dd = opendir(argv[1]);
    if (dd == NULL)
    {
    printf("Cannot open %s", argv[1]);
    exit(1);
    }

    while((dirp = readdir(dd)) != NULL)
    printf("%s\n", dirp->d_name);
    closedir(dd);
    return 0;
    }

    compile :
    gcc ls.c
    to run use:
    a.out $HOME

  5. #5
    Omani4Ever
    Guest

    Re:Linux C API? Getting the directory structure.

    Ya and one more thing:

    A Great book to read on Unix Programming:

    Advanced Programming on The Unix Enviroment
    By
    W. Richard Stevens

Similar Threads

  1. Linux Active Directory integration
    By jro in forum Linux - Hardware, Networking & Security
    Replies: 0
    Last Post: 01-20-2010, 01:47 PM
  2. virtual directory
    By ammad in forum Linux - General Topics
    Replies: 1
    Last Post: 01-20-2005, 06:07 PM
  3. How to find Linux source directory?
    By adelaideyip in forum Linux - General Topics
    Replies: 1
    Last Post: 08-21-2004, 07:52 PM
  4. How to convert a directory of .wav to .mp3?
    By slackware in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 03-05-2003, 12:22 AM
  5. Linux/Unix directory delimiter.
    By Feztaa in forum Linux - General Topics
    Replies: 1
    Last Post: 03-08-2002, 01:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •