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 7054503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:38:05+00:00 2026-05-28T03:38:05+00:00

Background is I have an existing application which lists directory entries; strace reveals it

  • 0

Background is I have an existing application which lists directory entries; strace reveals it just calls getdents and lists them in the order returned. I would like them displayed in the same order as a call to ls with no arguments. Is it possible to update the directory data in some way to achieve this?

FS is ext4, if that makes any difference.

  • 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-05-28T03:38:06+00:00Added an answer on May 28, 2026 at 3:38 am

    If you really are determined to change this program’s behaviour (of which I assume that you don’t have the source code available), you can use LD_PRELOAD to hook the call to opendir and readdir and replace it with your own, sorting wrapper. An example how such a hook could look like is the following:

    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <dirent.h>
    #include <dlfcn.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct __dirstream
    {
      int __fd;
      char *__data;
      size_t __allocation;
      size_t __offset;
      size_t __size;
      struct dirent __entry;
    };
    
    typedef struct _dirent_list {
      struct dirent *value;
      struct _dirent_list *next;
    } dirent_list;
    
    typedef struct _my_DIR {
      struct __dirstream orig;
      dirent_list *first_entry;
      int first_readdir;
    } my_DIR;
    
    DIR *opendir(const char *name) {
      DIR *(*orig_opendir)(const char*) = dlsym(RTLD_NEXT, "opendir");
      DIR *dir = orig_opendir(name);
    
      // save additional information along with the
      // original DIR structure
      my_DIR *my_dir = calloc(1, sizeof(*my_dir));
      my_dir->first_readdir = 1;
      memcpy(my_dir, dir, sizeof(*dir));
      return (DIR*)my_dir;
    }
    
    struct dirent *readdir(DIR *dir) {
      struct dirent *(*orig_readdir)(DIR*) = dlsym(RTLD_NEXT, "readdir");
      my_DIR *my_dir = (my_DIR*)dir;
      dirent_list *item;
    
      if (my_dir->first_readdir) {
        struct dirent *entry;
        while ((entry = orig_readdir(dir))) {
          // exercise for the reader:
          // implement insertion sort here 
          item = calloc(1, sizeof(*item));
          item->value = entry;
          item->next = my_dir->first_entry;
          my_dir->first_entry = item;
        }
        my_dir->first_readdir = 0;
      }
    
      if (!my_dir->first_entry)
        return NULL;
    
      item = my_dir->first_entry;
      struct dirent *result = item->value;
      my_dir->first_entry = item->next;
      free(item);
    
      return result;
    }
    

    It overrides opendir and readdir to return the entries in reverse order (you can adapt this for sorting too). This is how you use it with a program test that simply lists the directory entries in the order they are received:

    $ gcc -Wall -shared -fPIC -o libhookdir.so hookdir.c -ldl
    $ ./test
    ..
    test
    .
    hookdir.c
    libhookdir.so
    test.c
    $ LD_PRELOAD=./libhookdir.so ./test
    test.c
    libhookdir.so
    hookdir.c
    .
    test
    ..
    

    Hah! This works. We just hooked a libc function.

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

Sidebar

Related Questions

First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I have an existing asp.net c# application for which I'd like to implement a
I have a windows form application in which i am using a background worker
Background I have an existing extension designed to accompany a browser-based game (The extension
Background: we have an application that generates reports from HTML (that may or may
Background: I have a module which declares a number of instance methods module UsefulThings
Background I have an application written in native C++ over the course of several
Background: I am customizing an existing ASP .NET / C# application. It has it's
I have an object in my application which performs processing on the items in
Ubuntu 11.10, Python 2.6. Background: I have an existing Python app that is using

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.