Imported Upstream version 2.0.0
[platform/upstream/fdupes.git] / fdupes.c
index ef64c45..29b2921 100644 (file)
--- a/fdupes.c
+++ b/fdupes.c
@@ -1,4 +1,4 @@
-/* FDUPES Copyright (c) 1999-2002 Adrian Lopez
+/* FDUPES Copyright (c) 1999-2018 Adrian Lopez
 
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation files
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
+#include "config.h"
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
+#include <strings.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <stdlib.h>
-#ifndef OMIT_GETOPT_LONG
+#include <time.h>
+#ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #endif
-#include <string.h>
 #include <errno.h>
 #include <libgen.h>
+#include <locale.h>
+#ifdef HAVE_NCURSESW_CURSES_H
+  #include <ncursesw/curses.h>
+#else
+  #include <curses.h>
+#endif
+#include "fdupes.h"
+#include "errormsg.h"
+#include "ncurses-interface.h"
+#include "log.h"
+#include "sigint.h"
+#include "flags.h"
 
-#include "md5/md5.h"
-
-#define ISFLAG(a,b) ((a & b) == b)
-#define SETFLAG(a,b) (a |= b)
-
-#define F_RECURSE           0x0001
-#define F_HIDEPROGRESS      0x0002
-#define F_DSAMELINE         0x0004
-#define F_FOLLOWLINKS       0x0008
-#define F_DELETEFILES       0x0010
-#define F_EXCLUDEEMPTY      0x0020
-#define F_CONSIDERHARDLINKS 0x0040
-#define F_SHOWSIZE          0x0080
-#define F_OMITFIRST         0x0100
-#define F_RECURSEAFTER      0x0200
-#define F_NOPROMPT          0x0400
-#define F_SUMMARIZEMATCHES  0x0800
-#define F_EXCLUDEHIDDEN     0x1000
-#define F_PERMISSIONS       0x2000
-#define F_REVERSE           0x4000
-#define F_IMMEDIATE         0x8000
+long long minsize = -1;
+long long maxsize = -1;
 
 typedef enum {
-  ORDER_TIME = 0,
+  ORDER_MTIME = 0,
+  ORDER_CTIME,
   ORDER_NAME
 } ordertype_t;
 
 char *program_name;
 
-unsigned long flags = 0;
+ordertype_t ordertype = ORDER_MTIME;
 
 #define CHUNK_SIZE 8192
 
@@ -90,35 +86,12 @@ typedef struct _signatures
 
 */
 
-typedef struct _file {
-  char *d_name;
-  off_t size;
-  md5_byte_t *crcpartial;
-  md5_byte_t *crcsignature;
-  dev_t device;
-  ino_t inode;
-  time_t mtime;
-  int hasdupes; /* true only if file is first on duplicate chain */
-  struct _file *duplicates;
-  struct _file *next;
-} file_t;
-
 typedef struct _filetree {
   file_t *file; 
   struct _filetree *left;
   struct _filetree *right;
 } filetree_t;
 
-void errormsg(char *message, ...)
-{
-  va_list ap;
-
-  va_start(ap, message);
-
-  fprintf(stderr, "\r%40s\r%s: ", "", program_name);
-  vfprintf(stderr, message, ap);
-}
-
 void escapefilename(char *escape_list, char **filename_ptr)
 {
   int x;
@@ -183,6 +156,22 @@ time_t getmtime(char *filename) {
   return s.st_mtime;
 }
 
+time_t getctime(char *filename) {
+  struct stat s;
+
+  if (stat(filename, &s) != 0) return 0;
+
+  return s.st_ctime;
+}
+
+char *fmtmtime(char *filename) {
+  static char buf[64];
+  time_t t = getmtime(filename);
+
+  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", localtime(&t));
+  return buf;
+}
+
 char **cloneargs(int argc, char **argv)
 {
   int x;
@@ -239,7 +228,25 @@ int nonoptafter(char *option, int argc, char **oldargv,
   return x;
 }
 
-int grokdir(char *dir, file_t **filelistp)
+void getfilestats(file_t *file)
+{
+  file->size = filesize(file->d_name);
+  file->inode = getinode(file->d_name);
+  file->device = getdevice(file->d_name);
+
+  switch (ordertype)
+  {
+    case ORDER_CTIME:
+      file->sorttime = getctime(file->d_name);
+      break;
+    case ORDER_MTIME:
+    default:
+      file->sorttime = getmtime(file->d_name);
+      break;
+  }
+}
+
+int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
 {
   DIR *cd;
   file_t *newfile;
@@ -251,6 +258,7 @@ int grokdir(char *dir, file_t **filelistp)
   static int progress = 0;
   static char indicator[] = "-\\|/";
   char *fullname, *name;
+  off_t size;
 
   cd = opendir(dir);
 
@@ -298,6 +306,13 @@ int grokdir(char *dir, file_t **filelistp)
       
       if (ISFLAG(flags, F_EXCLUDEHIDDEN)) {
        fullname = strdup(newfile->d_name);
+       if (fullname == 0)
+       {
+         errormsg("out of memory!\n");
+         free(newfile);
+         closedir(cd);
+         exit(1);
+       }
        name = basename(fullname);
        if (name[0] == '.' && strcmp(name, ".") && strcmp(name, "..") ) {
          free(newfile->d_name);
@@ -307,16 +322,24 @@ int grokdir(char *dir, file_t **filelistp)
        free(fullname);
       }
 
-      if (filesize(newfile->d_name) == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) {
-       free(newfile->d_name);
-       free(newfile);
-       continue;
+      if (stat(newfile->d_name, &info) == -1) {
+        free(newfile->d_name);
+        free(newfile);
+        continue;
+      }
+      
+      size = filesize(newfile->d_name);
+      if (!S_ISDIR(info.st_mode) && (((size == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) || size < minsize || (size > maxsize && maxsize != -1)))) {
+        free(newfile->d_name);
+        free(newfile);
+        continue;
       }
 
-      if (stat(newfile->d_name, &info) == -1) {
-       free(newfile->d_name);
-       free(newfile);
-       continue;
+      if (info.st_dev == logfile_status->st_dev && info.st_ino == logfile_status->st_ino)
+      {
+        free(newfile->d_name);
+        free(newfile);
+        continue;
       }
 
       if (lstat(newfile->d_name, &linfo) == -1) {
@@ -327,11 +350,12 @@ int grokdir(char *dir, file_t **filelistp)
 
       if (S_ISDIR(info.st_mode)) {
        if (ISFLAG(flags, F_RECURSE) && (ISFLAG(flags, F_FOLLOWLINKS) || !S_ISLNK(linfo.st_mode)))
-         filecount += grokdir(newfile->d_name, filelistp);
+         filecount += grokdir(newfile->d_name, filelistp, logfile_status);
        free(newfile->d_name);
        free(newfile);
       } else {
        if (S_ISREG(linfo.st_mode) || (S_ISLNK(linfo.st_mode) && ISFLAG(flags, F_FOLLOWLINKS))) {
+         getfilestats(newfile);
          *filelistp = newfile;
          filecount++;
        } else {
@@ -430,18 +454,8 @@ void purgetree(filetree_t *checktree)
   free(checktree);
 }
 
-void getfilestats(file_t *file)
-{
-  file->size = filesize(file->d_name);
-  file->inode = getinode(file->d_name);
-  file->device = getdevice(file->d_name);
-  file->mtime = getmtime(file->d_name);
-}
-
 int registerfile(filetree_t **branch, file_t *file)
 {
-  getfilestats(file);
-
   *branch = (filetree_t*) malloc(sizeof(filetree_t));
   if (*branch == NULL) {
     errormsg("out of memory!\n");
@@ -470,14 +484,9 @@ int same_permissions(char* name1, char* name2)
 int is_hardlink(filetree_t *checktree, file_t *file)
 {
   file_t *dupe;
-  ino_t inode;
-  dev_t device;
 
-  inode = getinode(file->d_name);
-  device = getdevice(file->d_name);
-
-  if ((inode == checktree->file->inode) && 
-      (device == checktree->file->device))
+  if ((file->inode == checktree->file->inode) &&
+      (file->device == checktree->file->device))
         return 1;
 
   if (checktree->file->hasdupes)
@@ -485,8 +494,8 @@ int is_hardlink(filetree_t *checktree, file_t *file)
     dupe = checktree->file->duplicates;
 
     do {
-      if ((inode == dupe->inode) &&
-          (device == dupe->device))
+      if ((file->inode == dupe->inode) &&
+          (file->device == dupe->device))
             return 1;
 
       dupe = dupe->duplicates;
@@ -496,20 +505,124 @@ int is_hardlink(filetree_t *checktree, file_t *file)
   return 0;
 }
 
+/* check whether two paths represent the same file (deleting one would delete the other) */
+int is_same_file(file_t *file_a, file_t *file_b)
+{
+  char *filename_a;
+  char *filename_b;
+  char *dirname_a;
+  char *dirname_b;
+  char *basename_a;
+  char *basename_b;
+  struct stat dirstat_a;
+  struct stat dirstat_b;
+
+  /* if files on different devices and/or different inodes, they are not the same file */
+  if (file_a->device != file_b->device || file_a->inode != file_b->inode)
+    return 0;
+
+  /* copy filenames (basename and dirname may modify these) */
+  filename_a = strdup(file_a->d_name);
+  if (filename_a == 0)
+    return -1;
+
+  filename_b = strdup(file_b->d_name);
+  if (filename_b == 0)
+    return -1;
+
+  /* get file basenames */
+  basename_a = basename(filename_a);
+  memmove(filename_a, basename_a, strlen(basename_a) + 1);
+
+  basename_b = basename(filename_b);
+  memmove(filename_b, basename_b, strlen(basename_b) + 1);
+
+  /* if files have different names, they are not the same file */
+  if (strcmp(filename_a, filename_b) != 0)
+  {
+    free(filename_b);
+    free(filename_a);
+    return 0;
+  }
+
+  /* restore paths */
+  strcpy(filename_a, file_a->d_name);
+  strcpy(filename_b, file_b->d_name);
+
+  /* get directory names */
+  dirname_a = dirname(filename_a);
+  if (stat(dirname_a, &dirstat_a) != 0)
+  {
+    free(filename_b);
+    free(filename_a);
+    return -1;
+  }
+
+  dirname_b = dirname(filename_b);
+  if (stat(dirname_b, &dirstat_b) != 0)
+  {
+    free(filename_b);
+    free(filename_a);
+    return -1;
+  }
+
+  free(filename_b);
+  free(filename_a);
+
+  /* if directories on which files reside are different, they are not the same file */
+  if (dirstat_a.st_dev != dirstat_b.st_dev || dirstat_a.st_ino != dirstat_b.st_ino)
+    return 0;
+
+  /* same device, inode, filename, and directory; therefore, same file */
+  return 1;
+}
+
+/* check whether given tree node already contains a copy of given file */
+int has_same_file(filetree_t *checktree, file_t *file)
+{
+  file_t *dupe;
+
+  if (is_same_file(checktree->file, file))
+    return 1;
+
+  if (checktree->file->hasdupes)
+  {
+    dupe = checktree->file->duplicates;
+
+    do {
+      if (is_same_file(dupe, file))
+        return 1;
+
+      dupe = dupe->duplicates;
+    } while (dupe != NULL);
+  }
+
+  return 0;
+}
+
 file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
 {
   int cmpresult;
   md5_byte_t *crcsignature;
   off_t fsize;
 
-  /* If device and inode fields are equal one of the files is a 
-     hard link to the other or the files have been listed twice 
-     unintentionally. We don't want to flag these files as
-     duplicates unless the user specifies otherwise.
-  */    
-
-  if (!ISFLAG(flags, F_CONSIDERHARDLINKS) && is_hardlink(checktree, file))
-    return NULL;
+  if (ISFLAG(flags, F_CONSIDERHARDLINKS))
+  {
+    /* If node already contains file, we don't want to add it again.
+    */
+    if (has_same_file(checktree, file))
+      return NULL;
+  }
+  else
+  {
+    /* If device and inode fields are equal one of the files is a
+       hard link to the other or the files have been listed twice
+       unintentionally. We don't want to flag these files as
+       duplicates unless the user specifies otherwise.
+    */
+    if (is_hardlink(checktree, file))
+      return NULL;
+  }
 
   fsize = filesize(file->d_name);
   
@@ -605,7 +718,6 @@ file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)
     }
   } else 
   {
-    getfilestats(file);
     return &checktree->file;
   }
 }
@@ -682,11 +794,15 @@ void printmatches(file_t *files)
       if (!ISFLAG(flags, F_OMITFIRST)) {
        if (ISFLAG(flags, F_SHOWSIZE)) printf("%lld byte%seach:\n", (long long int)files->size,
         (files->size != 1) ? "s " : " ");
+        if (ISFLAG(flags, F_SHOWTIME))
+          printf("%s ", fmtmtime(files->d_name));
        if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &files->d_name);
        printf("%s%c", files->d_name, ISFLAG(flags, F_DSAMELINE)?' ':'\n');
       }
       tmpfile = files->duplicates;
       while (tmpfile != NULL) {
+        if (ISFLAG(flags, F_SHOWTIME))
+          printf("%s ", fmtmtime(tmpfile->d_name));
        if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &tmpfile->d_name);
        printf("%s%c", tmpfile->d_name, ISFLAG(flags, F_DSAMELINE)?' ':'\n');
        tmpfile = tmpfile->duplicates;
@@ -756,7 +872,7 @@ int relink(char *oldfile, char *newfile)
   return 1;
 }
 
-void deletefiles(file_t *files, int prompt, FILE *tty)
+void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
 {
   int counter;
   int groups = 0;
@@ -773,6 +889,8 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
   int max = 0;
   int x;
   int i;
+  struct log_info *loginfo;
+  int log_error;
 
   curfile = files;
   
@@ -804,19 +922,37 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
     exit(1);
   }
 
+  loginfo = 0;
+  if (logfile != 0)
+    loginfo = log_open(logfile, &log_error);
+
+  register_sigint_handler();
+
   while (files) {
     if (files->hasdupes) {
       curgroup++;
       counter = 1;
       dupelist[counter] = files;
 
-      if (prompt) printf("[%d] %s\n", counter, files->d_name);
+      if (prompt) 
+      {
+        if (ISFLAG(flags, F_SHOWTIME))
+          printf("[%d] [%s] %s\n", counter, fmtmtime(files->d_name), files->d_name);
+        else
+          printf("[%d] %s\n", counter, files->d_name);
+      }
 
       tmpfile = files->duplicates;
 
       while (tmpfile) {
        dupelist[++counter] = tmpfile;
-       if (prompt) printf("[%d] %s\n", counter, tmpfile->d_name);
+        if (prompt)
+        {
+          if (ISFLAG(flags, F_SHOWTIME))
+            printf("[%d] [%s] %s\n", counter, fmtmtime(tmpfile->d_name), tmpfile->d_name);
+          else
+            printf("[%d] %s\n", counter, tmpfile->d_name);
+        }
        tmpfile = tmpfile->duplicates;
       }
 
@@ -831,7 +967,7 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
       else /* prompt for files to preserve */
 
       do {
-       printf("Set %d of %d, preserve files [1 - %d, all]", 
+       printf("Set %d of %d, preserve files [1 - %d, all, quit]",
           curgroup, groups, counter);
        if (ISFLAG(flags, F_SHOWSIZE)) printf(" (%lld byte%seach)", (long long int)files->size,
          (files->size != 1) ? "s " : " ");
@@ -839,7 +975,24 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
        fflush(stdout);
 
        if (!fgets(preservestr, INPUT_SIZE, tty))
+       {
          preservestr[0] = '\n'; /* treat fgets() failure as if nothing was entered */
+         preservestr[1] = '\0';
+
+         if (got_sigint)
+         {
+           if (loginfo)
+             log_close(loginfo);
+
+           free(dupelist);
+           free(preserve);
+           free(preservestr);
+
+           printf("\n");
+
+           exit(0);
+         }
+       }
 
        i = strlen(preservestr) - 1;
 
@@ -855,17 +1008,32 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
          if (!fgets(preservestr + i + 1, INPUT_SIZE, tty))
          {
            preservestr[0] = '\n'; /* treat fgets() failure as if nothing was entered */
+           preservestr[1] = '\0';
            break;
          }
          i = strlen(preservestr)-1;
        }
 
+       if (strcmp(preservestr, "q\n") == 0 || strcmp(preservestr, "quit\n") == 0)
+       {
+         if (loginfo)
+           log_close(loginfo);
+
+         free(dupelist);
+         free(preserve);
+         free(preservestr);
+
+         printf("\n");
+
+         exit(0);
+       }
+
        for (x = 1; x <= counter; x++) preserve[x] = 0;
        
        token = strtok(preservestr, " ,\n");
        
        while (token != NULL) {
-         if (strcasecmp(token, "all") == 0)
+         if (strcasecmp(token, "all") == 0 || strcasecmp(token, "a") == 0)
            for (x = 0; x <= counter; x++) preserve[x] = 1;
          
          number = 0;
@@ -880,24 +1048,44 @@ void deletefiles(file_t *files, int prompt, FILE *tty)
 
       printf("\n");
 
+      if (loginfo)
+        log_begin_set(loginfo);
+
       for (x = 1; x <= counter; x++) { 
        if (preserve[x])
+        {
          printf("   [+] %s\n", dupelist[x]->d_name);
+
+          if (loginfo)
+            log_file_remaining(loginfo, dupelist[x]->d_name);
+        }
        else {
          if (remove(dupelist[x]->d_name) == 0) {
            printf("   [-] %s\n", dupelist[x]->d_name);
+
+            if (loginfo)
+              log_file_deleted(loginfo, dupelist[x]->d_name);
          } else {
            printf("   [!] %s ", dupelist[x]->d_name);
            printf("-- unable to delete file!\n");
+
+            if (loginfo)
+              log_file_remaining(loginfo, dupelist[x]->d_name);
          }
        }
       }
       printf("\n");
+
+      if (loginfo)
+        log_end_set(loginfo);
     }
     
     files = files->next;
   }
 
+  if (loginfo)
+    log_close(loginfo);
+
   free(dupelist);
   free(preserve);
   free(preservestr);
@@ -911,11 +1099,11 @@ int sort_pairs_by_arrival(file_t *f1, file_t *f2)
   return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
 }
 
-int sort_pairs_by_mtime(file_t *f1, file_t *f2)
+int sort_pairs_by_time(file_t *f1, file_t *f2)
 {
-  if (f1->mtime < f2->mtime)
+  if (f1->sorttime < f2->sorttime)
     return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
-  else if (f1->mtime > f2->mtime)
+  else if (f1->sorttime > f2->sorttime)
     return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
 
   return 0;
@@ -923,7 +1111,8 @@ int sort_pairs_by_mtime(file_t *f1, file_t *f2)
 
 int sort_pairs_by_filename(file_t *f1, file_t *f2)
 {
-  return strcmp(f1->d_name, f2->d_name);
+  int strvalue = strcmp(f1->d_name, f2->d_name);
+  return !ISFLAG(flags, F_REVERSE) ? strvalue : -strvalue;
 }
 
 void registerpair(file_t **matchlist, file_t *newmatch, 
@@ -973,7 +1162,7 @@ void registerpair(file_t **matchlist, file_t *newmatch,
 }
 
 void deletesuccessor(file_t **existing, file_t *duplicate, 
-      int (*comparef)(file_t *f1, file_t *f2))
+      int (*comparef)(file_t *f1, file_t *f2), struct log_info *loginfo)
 {
   file_t *to_keep;
   file_t *to_delete;
@@ -994,11 +1183,21 @@ void deletesuccessor(file_t **existing, file_t *duplicate,
   if (!ISFLAG(flags, F_HIDEPROGRESS)) fprintf(stderr, "\r%40s\r", " ");
 
   printf("   [+] %s\n", to_keep->d_name);
+
+  if (loginfo)
+    log_file_remaining(loginfo, to_keep->d_name);
+
   if (remove(to_delete->d_name) == 0) {
     printf("   [-] %s\n", to_delete->d_name);
+
+    if (loginfo)
+      log_file_deleted(loginfo, to_delete->d_name);
   } else {
     printf("   [!] %s ", to_delete->d_name);
     printf("-- unable to delete file!\n");
+
+    if (loginfo)
+      log_file_remaining(loginfo, to_delete->d_name);
   }
 
   printf("\n");
@@ -1017,11 +1216,14 @@ void help_text()
   printf(" -H --hardlinks   \tnormally, when two or more files point to the same\n");
   printf("                  \tdisk area they are treated as non-duplicates; this\n"); 
   printf("                  \toption will change this behavior\n");
+  printf(" -G --minsize=SIZE\tconsider only files greater than or equal to SIZE\n");
+  printf(" -L --maxsize=SIZE\tconsider only files less than or equal to SIZE\n");
   printf(" -n --noempty     \texclude zero-length files from consideration\n");
   printf(" -A --nohidden    \texclude hidden files from consideration\n");
   printf(" -f --omitfirst   \tomit the first file in each set of matches\n");
   printf(" -1 --sameline    \tlist each set of matches on a single line\n");
   printf(" -S --size        \tshow size of duplicate files\n");
+  printf(" -t --time        \tshow modification time of duplicate files\n");
   printf(" -m --summarize   \tsummarize dupe information\n");
   printf(" -q --quiet       \thide progress indicator\n");
   printf(" -d --delete      \tprompt user for files to preserve and delete all\n"); 
@@ -1030,7 +1232,10 @@ void help_text()
   printf("                  \twith -s or --symlinks, or when specifying a\n");
   printf("                  \tparticular directory more than once; refer to the\n");
   printf("                  \tfdupes documentation for additional information\n");
-  /*printf(" -l --relink      \t(description)\n");*/
+#ifndef NO_NCURSES
+  printf(" -P --plain       \twith --delete, use line-based prompt (as with older\n");
+  printf("                  \tversions of fdupes) instead of screen-mode interface\n");
+#endif
   printf(" -N --noprompt    \ttogether with --delete, preserve the first file in\n");
   printf("                  \teach set of duplicates and delete the rest without\n");
   printf("                  \tprompting the user\n");
@@ -1038,12 +1243,14 @@ void help_text()
   printf("                  \tgrouping into sets; implies --noprompt\n");
   printf(" -p --permissions \tdon't consider files with different owner/group or\n");
   printf("                  \tpermission bits as duplicates\n");
-  printf(" -o --order=BY    \tselect sort order for output, linking and deleting; by\n");
-  printf("                  \tmtime (BY='time'; default) or filename (BY='name')\n");
+  printf(" -o --order=BY    \tselect sort order for output and deleting; by file\n");
+  printf("                  \tmodification time (BY='time'; default), status\n");
+  printf("                  \tchange time (BY='ctime'), or filename (BY='name')\n");
   printf(" -i --reverse     \treverse order while sorting\n");
+  printf(" -l --log=LOGFILE \tlog file deletion choices to LOGFILE\n");
   printf(" -v --version     \tdisplay fdupes version\n");
   printf(" -h --help        \tdisplay this help message\n\n");
-#ifdef OMIT_GETOPT_LONG
+#ifndef HAVE_GETOPT_H
   printf("Note: Long options are not supported in this fdupes build.\n\n");
 #endif
 }
@@ -1061,25 +1268,30 @@ int main(int argc, char **argv) {
   int progress = 0;
   char **oldargv;
   int firstrecurse;
-  ordertype_t ordertype = ORDER_TIME;
+  char *logfile = 0;
+  struct log_info *loginfo;
+  int log_error;
+  struct stat logfile_status;
+  char *endptr;
   
-#ifndef OMIT_GETOPT_LONG
+#ifdef HAVE_GETOPT_H
   static struct option long_options[] = 
   {
     { "omitfirst", 0, 0, 'f' },
     { "recurse", 0, 0, 'r' },
-    { "recursive", 0, 0, 'r' },
     { "recurse:", 0, 0, 'R' },
-    { "recursive:", 0, 0, 'R' },
     { "quiet", 0, 0, 'q' },
     { "sameline", 0, 0, '1' },
     { "size", 0, 0, 'S' },
+    { "time", 0, 0, 't' },
     { "symlinks", 0, 0, 's' },
     { "hardlinks", 0, 0, 'H' },
-    { "relink", 0, 0, 'l' },
+    { "minsize", 1, 0, 'G' },
+    { "maxsize", 1, 0, 'L' },
     { "noempty", 0, 0, 'n' },
     { "nohidden", 0, 0, 'A' },
     { "delete", 0, 0, 'd' },
+    { "plain", 0, 0, 'P' },
     { "version", 0, 0, 'v' },
     { "help", 0, 0, 'h' },
     { "noprompt", 0, 0, 'N' },
@@ -1089,6 +1301,7 @@ int main(int argc, char **argv) {
     { "permissions", 0, 0, 'p' },
     { "order", 1, 0, 'o' },
     { "reverse", 0, 0, 'i' },
+    { "log", 1, 0, 'l' },
     { 0, 0, 0, 0 }
   };
 #define GETOPT getopt_long
@@ -1098,10 +1311,12 @@ int main(int argc, char **argv) {
 
   program_name = argv[0];
 
+  setlocale(LC_CTYPE, "");
+
   oldargv = cloneargs(argc, argv);
 
-  while ((opt = GETOPT(argc, argv, "frRq1SsHlnAdvhNImpo:i"
-#ifndef OMIT_GETOPT_LONG
+  while ((opt = GETOPT(argc, argv, "frRq1StsHG:L:nAdPvhNImpo:il:"
+#ifdef HAVE_GETOPT_H
           , long_options, NULL
 #endif
           )) != EOF) {
@@ -1124,12 +1339,31 @@ int main(int argc, char **argv) {
     case 'S':
       SETFLAG(flags, F_SHOWSIZE);
       break;
+    case 't':
+      SETFLAG(flags, F_SHOWTIME);
+      break;
     case 's':
       SETFLAG(flags, F_FOLLOWLINKS);
       break;
     case 'H':
       SETFLAG(flags, F_CONSIDERHARDLINKS);
       break;
+    case 'G':
+      minsize = strtoll(optarg, &endptr, 10);
+      if (optarg[0] == '\0' || *endptr != '\0' || minsize < 0)
+      {
+        errormsg("invalid value for --minsize: '%s'\n", optarg);
+        exit(1);
+      }
+      break;
+    case 'L':
+      maxsize = strtoll(optarg, &endptr, 10);
+      if (optarg[0] == '\0' || *endptr != '\0' || maxsize < 0)
+      {
+        errormsg("invalid value for --maxsize: '%s'\n", optarg);
+        exit(1);
+      }
+      break;
     case 'n':
       SETFLAG(flags, F_EXCLUDEEMPTY);
       break;
@@ -1139,6 +1373,9 @@ int main(int argc, char **argv) {
     case 'd':
       SETFLAG(flags, F_DELETEFILES);
       break;
+    case 'P':
+      SETFLAG(flags, F_PLAINPROMPT);
+      break;
     case 'v':
       printf("fdupes %s\n", VERSION);
       exit(0);
@@ -1161,7 +1398,9 @@ int main(int argc, char **argv) {
       if (!strcasecmp("name", optarg)) {
         ordertype = ORDER_NAME;
       } else if (!strcasecmp("time", optarg)) {
-        ordertype = ORDER_TIME;
+        ordertype = ORDER_MTIME;
+      } else if (!strcasecmp("ctime", optarg)) {
+        ordertype = ORDER_CTIME;
       } else {
         errormsg("invalid value for --order: '%s'\n", optarg);
         exit(1);
@@ -1170,7 +1409,26 @@ int main(int argc, char **argv) {
     case 'i':
       SETFLAG(flags, F_REVERSE);
       break;
+    case 'l':
+      loginfo = log_open(logfile=optarg, &log_error);
+      if (loginfo == 0)
+      {
+        if (log_error == LOG_ERROR_NOT_A_LOG_FILE)
+          errormsg("%s: doesn't look like an fdupes log file\n", logfile);
+        else
+          errormsg("%s: could not open log file\n", logfile);
+
+        exit(1);
+      }
+      log_close(loginfo);
+
+      if (stat(logfile, &logfile_status) != 0)
+      {
+        errormsg("could not read log file status\n");
+        exit(1);
+      }
 
+      break;
     default:
       fprintf(stderr, "Try `fdupes --help' for more information.\n");
       exit(1);
@@ -1205,16 +1463,16 @@ int main(int argc, char **argv) {
 
     /* F_RECURSE is not set for directories before --recurse: */
     for (x = optind; x < firstrecurse; x++)
-      filecount += grokdir(argv[x], &files);
+      filecount += grokdir(argv[x], &files, &logfile_status);
 
     /* Set F_RECURSE for directories after --recurse: */
     SETFLAG(flags, F_RECURSE);
 
     for (x = firstrecurse; x < argc; x++)
-      filecount += grokdir(argv[x], &files);
+      filecount += grokdir(argv[x], &files, &logfile_status);
   } else {
     for (x = optind; x < argc; x++)
-      filecount += grokdir(argv[x], &files);
+      filecount += grokdir(argv[x], &files, &logfile_status);
   }
 
   if (!files) {
@@ -1247,10 +1505,12 @@ int main(int argc, char **argv) {
       if (confirmmatch(file1, file2)) {
         if (ISFLAG(flags, F_DELETEFILES) && ISFLAG(flags, F_IMMEDIATE))
           deletesuccessor(match, curfile,
-              (ordertype == ORDER_TIME) ? sort_pairs_by_mtime : sort_pairs_by_filename );
+              (ordertype == ORDER_MTIME || 
+               ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename, loginfo );
         else
           registerpair(match, curfile,
-              (ordertype == ORDER_TIME) ? sort_pairs_by_mtime : sort_pairs_by_filename );
+              (ordertype == ORDER_MTIME ||
+               ordertype == ORDER_CTIME) ? sort_pairs_by_time : sort_pairs_by_filename );
       }
       
       fclose(file1);
@@ -1272,17 +1532,43 @@ int main(int argc, char **argv) {
   {
     if (ISFLAG(flags, F_NOPROMPT))
     {
-      deletefiles(files, 0, 0);
+      deletefiles(files, 0, 0, logfile);
     }
     else
     {
-      if (freopen("/dev/tty", "r", stdin) == 0)
+#ifndef NO_NCURSES
+      if (!ISFLAG(flags, F_PLAINPROMPT))
+      {
+        if (newterm(getenv("TERM"), stdout, stdin) != 0)
+        {
+          deletefiles_ncurses(files, logfile);
+        }
+        else
+        {
+          errormsg("could not enter screen mode; falling back to plain mode\n\n");
+          SETFLAG(flags, F_PLAINPROMPT);
+        }
+      }
+
+      if (ISFLAG(flags, F_PLAINPROMPT))
+      {
+        if (freopen("/dev/tty", "r", stdin) == NULL)
+        {
+          errormsg("could not open terminal for input\n");
+          exit(1);
+        }
+
+        deletefiles(files, 1, stdin, logfile);
+      }
+#else
+      if (freopen("/dev/tty", "r", stdin) == NULL)
       {
         errormsg("could not open terminal for input\n");
         exit(1);
       }
 
-      deletefiles(files, 1, stdin);
+      deletefiles(files, 1, stdin, logfile);
+#endif
     }
   }