Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:27:47+00:00 2026-06-15T15:27:47+00:00

I’ve tried several times to write a function to remove directories and the files

  • 0

I’ve tried several times to write a function to remove directories and the files within the directories, like rm -r, but I haven’t managed to do it. The technique I’ve tried is:

/* rm command */
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define DEBUG

int rm_file(const char **argv);
int rm_tree(const char **argv);
void usage(void);

int rflag = 0;

int main(int argc, char *argv[])
{
    int ch;

    while((ch = getopt(argc, argv, "Rr")) != -1) {
        switch(ch) {
            case 'R':
            case 'r':
                rflag = 1;
                break;
            default:
                usage();
        }
    }
    argc -= optind;
    argv += optind;

    while(*argv) {
        if(rflag)
            rm_tree(argv);
        else
            rm_file(argv);
        argv++;
    }

    return 0;
}

int rm_tree(const char **argv)
{
    FTS *ftsp;
    FTSENT *ftsent;

    if((ftsp = fts_open(argv, 0, NULL)) == NULL) {
        fprintf(stderr, "error: rm: can't stat directory\n");
        return 1;
    }

    while((ftsent = fts_read(ftsp)) != NULL) {
        switch(ftsent->fts_info) {
            case FTS_DNR:
                fprintf(stderr, "error: rm: can't stat directory\n");
                break;
#ifndef DEBUG
            case FTS_D:
                rm_tree(&ftsent->fts_accpath);
                break;
#endif
            case FTS_F:
                if(unlink(ftsent->fts_accpath) == -1) {
                    fprintf(stderr, "error: rm: can't remove file\n");
                }
                break;
            case FTS_SL:
                if(unlink(ftsent->fts_accpath) == -1) {
                    fprintf(stderr, "error: rm: can't remove file\n");
                }
                break;
            default:
                if(unlink(ftsent->fts_accpath) == -1) {
                    fprintf(stderr, "error: rm: can't remove file\n");
                }
                break;
        }
    }

    if(rmdir(*argv) == -1) {
        fprintf(stderr, "error: rm: can't remove directory\n");
        return 1;
    }

    return 0;
}

int rm_file(const char **argv)
{
    struct stat st;

    if(lstat(*argv, &st) == -1) {
        fprintf(stderr, "error: rm: can't stat file\n");
        return 1;
    }

    if(S_ISDIR(st.st_mode)) {
        fprintf(stderr, "error: rm: can't remove directory\n");
        return 1;
    }

    if(unlink(*argv) == -1) {
        fprintf(stderr, "error: rm: can't remove file\n");
        return 1;
    }

    return 0;
}

void usage(void) 
{
    fprintf(stderr, "usage: rm file1 file2\n");
    exit(1);
}

But invariably, I get a segmentation fault or it just doesn’t work. Am I looking in the right direction? I’ve tried using the ftw() functions and the opendir() functions, but I just haven’t been able to figure out how to get this to work. I would appreciate it if someone could help.

The code included in the debug tags is where it tends to fail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:27:48+00:00Added an answer on June 15, 2026 at 3:27 pm

    Take a look at how it is done in practice by analysing source code of rm in open source operating systems, e.g. OpenBSD (look for rm_tree).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.