Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to make a simple daemon for continuously deleting files from purple/icons ( the pidgin dir ). However, remove() and unlink() are not deleting files. Here is my code, please, tell me where is my error.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <dirent.h>
#include <time.h>

int remove_files(const char* directory);
int remove_files(const char* directory) {

    DIR *dp;
    struct dirent *dirp;
    int files_deleted = 0;
    int count=0;
    dp = opendir(directory);
    while ( (dirp = readdir(dp)) != 0x00 ) {
    return files_deleted;

int main(int argc, char *argv[]) {
    time_t sec;
    //dir handlers
    DIR *dp;
    struct dirent *ep;
    FILE *fp = 0x00;
    pid_t process_id = 0;
    pid_t sid = 0;

    process_id = fork();
    if ( process_id < 0 ) {
        fprintf(stderr, "fork() failed \n");
    } else if ( process_id > 0 ){
        fprintf(stdout, "process_id of child process %d\n", process_id);


    if ( (sid=setsid() ) < 0 ) {



    fp = fopen("Log.txt", "w+");
    if ( fp == 0x00 ) exit(1);
    fprintf(fp, "Started logging at %.24s \n", ctime(&sec));
    while ( 1 ) {
        fprintf(fp, "%d files deleted \n", remove_files("/home/ilian/.purple/icons/"));
    return (0);


Nothing is deleted, but the daemon is running. The log shows how much files are but the files are untouched.

share|improve this question

closed as off topic by Journeyman Geek, Tog, Ƭᴇcʜιᴇ007, terdon, mpy Jun 18 '13 at 14:09

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Have you considered rm -r ~/.purple/icons; ln -s /dev/null ~/.purple/icons ? In that way, you do not even have to delete anything! – Lekensteyn Jun 18 '13 at 9:28
@Lekensteyn I had problems before with Pidgin simply overriding any symlinks and creating directories, so I don't think that'll work. – l0b0 Jun 18 '13 at 9:35
Did you try reporting this bug to Pidgin? You could save yourself (and others) some work... – l0b0 Jun 18 '13 at 9:36
coding questions are off topic for SU, I'm guessing this would be a better fit for SO. In your shoes, I'd just use inocron, and skip the coding. – Journeyman Geek Jun 18 '13 at 9:41
@l0b0 With other programs, yes. I have tried it on pidgin and it works. Illian, instead of a busy loop, consider using inotify. – Lekensteyn Jun 18 '13 at 12:21
up vote 8 down vote accepted

Two things:

  • You should always check return values from system and library functions. If you had


    then you'd see what was happening.

  • The reason your code doesn't work is because remove() and unlink() require the full path to the file in question, whereas readdir() only populates d_name with the filename.

    The easiest thing to do is chdir() into the directory first:

    dp = opendir(".");
share|improve this answer
Thanks, I just didnt figure it.. Never knew that d_name did not give the full path :( Thanks again, youve been of great help. – Ilian Zapryanov Jun 18 '13 at 9:38

Not the answer you're looking for? Browse other questions tagged .