Hey I am a new unix student, please help me in this Exercise
" Write a C code to represent a file system tree into an XML file. The XML is an
exact representation of the file system nested files and directories that starts with the root directory “/”.
The generated file can be useful when searching for a specific file or directory."

Dani AI

Generated

- this is a solid learning project. As reminded, the forum won't produce a finished homework submission, and as suggested the best start is a clear text/pseudo-code plan. Below is a compact, practical outline that points to the POSIX APIs and the pitfalls to watch for without giving a copy‑and‑paste solution.

A concise pseudocode walkthrough:

write_xml_header(out);
walk("/", out);

function walk(path, out):
    info = lstat(path)
    if S_ISLNK(info.mode):
        target = readlink(path)
        write "<symlink name=\"xml_escape(basename(path))\" target=\"xml_escape(target)\"/>"
        return
    if S_ISDIR(info.mode):
        write "<directory name=\"xml_escape(basename(path))\">"
        dir = opendir(path)
        for entry in readdir(dir):
            if entry == "." or entry == "..": continue
            child = join(path, entry)
            walk(child, out)
        closedir(dir)
        write "</directory>"
    else:
        write "<file name=\"xml_escape(basename(path))\" size=\"info.st_size\"/>"

Key implementation notes: use opendir/readdir/closedir and lstat (not stat) to detect symlinks; use readlink to capture symlink targets. Always escape XML special characters (implement xml_escape() or use libxml2 to avoid manual escaping). Prevent infinite loops from symlink cycles by tracking visited (st_dev, st_ino) pairs. Stream output to a file (do not build the whole tree in memory). Be careful with path buffers—use snprintf and allocate dynamically rather than relying blindly on PATH_MAX. Handle permission errors and skip sockets/devices or represent them explicitly. Test on a small subtree instead of "/" and compile with -Wall -Wextra; run under valgrind for leaks.

Suggested incremental plan: (1) write a directory lister that prints indentation; (2) add lstat/type detection; (3) add robust path handling and XML escaping; (4) add symlink/inode-cycle protection; (5) swap in libxml2 if full XML robustness is required.

Recommended Answers

All 3 Replies

Can I refer you to Daniwebs Rules.

They specifically mention that we shall not write code, or write your homework.
If you need help on a content specific section then ask but you must show your own work when doing this.

How much C do you know so far, official resources and Google (other search engines are available) are always good places to start and then when you get stuck on a section then we are always happy to help.

This is an excellent excercize! You will learn how to code C structures, and generate XML data. Start by describing the solution to the problem in text/pseudo-code. Think about the structures you need to reflect the data (classes in C++), and what XML contructs will reflect that data to write to disc. Make an effort, and we will be happy to help you sort out your issues, but AHarrisGsy is absolutely correct in that we are NOT here to help you cheat! I'm SURE that is not what you intended, correct? :-)

As i said befor am new in this field and I just need a help in the way of starting in it , I don't want the Solution

thanks :)

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.