/*
* Output flags
*/
-#define D_HEADER 1 /* Print a header/footer between files */
-#define D_EMPTY1 2 /* Treat first file as empty (/dev/null) */
-#define D_EMPTY2 4 /* Treat second file as empty (/dev/null) */
+enum {
+ /* Print a header/footer between files */
+ /* D_HEADER = 1, - unused */
+ /* Treat file as empty (/dev/null) */
+ D_EMPTY1 = 2 * ENABLE_FEATURE_DIFF_DIR,
+ D_EMPTY2 = 4 * ENABLE_FEATURE_DIFF_DIR,
+};
/*
* Status values for print_status() and diffreg() return values
smallint exit_status;
int opt_U_context;
size_t max_context; /* size of context_vec_start */
- USE_FEATURE_DIFF_DIR(int dl_count;)
- USE_FEATURE_DIFF_DIR(char **dl;)
+ IF_FEATURE_DIFF_DIR(int dl_count;)
+ IF_FEATURE_DIFF_DIR(char **dl;)
char *opt_S_start;
const char *label1;
const char *label2;
} while (0)
-/*static void print_only(const char *path, size_t dirlen, const char *entry)*/
+#if ENABLE_FEATURE_DIFF_DIR
static void print_only(const char *path, const char *entry)
{
printf("Only in %s: %s\n", path, entry);
}
+#endif
-/*static void print_status(int val, char *path1, char *path2, char *entry)*/
static void print_status(int val, char *_path1, char *_path2)
{
/*const char *const _entry = entry ? entry : "";*/
}
+/* Our diff implementation is using seek.
+ * When we meet non-seekable file, we must make a temp copy.
+ */
static char *make_temp(FILE *f, struct stat *sb)
{
char *name;
int fd;
- if (S_ISREG(sb->st_mode))
+ if (S_ISREG(sb->st_mode) || S_ISBLK(sb->st_mode))
return NULL;
name = xstrdup("/tmp/difXXXXXX");
fd = mkstemp(name);
* Check to see if the given files differ.
* Returns 0 if they are the same, 1 if different, and -1 on error.
*/
-static NOINLINE int files_differ(FILE *f1, FILE *f2, int flags)
+static NOINLINE int files_differ(FILE *f1, FILE *f2)
{
size_t i, j;
+ /* Prevent making copies for "/dev/null" (too common) */
+ /* Deal with input from pipes etc */
tempname1 = make_temp(f1, &stb1);
tempname2 = make_temp(f2, &stb2);
- if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size) {
+ if (stb1.st_size != stb2.st_size) {
return 1;
}
while (1) {
J[i] = 0;
if (c != '\n' && c != EOF)
ctold += skipline(f1);
-// BUG? Should be "if (d != '\n' && d != EOF)" ?
- if (d != '\n' && c != EOF)
+/* was buggy? "if (d != '\n' && c != EOF)" */
+ if (d != '\n' && d != EOF)
ctnew += skipline(f2);
break;
}
* lines appended (beginning at b). If c is greater than d then there are
* lines missing from the to file.
*/
-static void change(char *file1, FILE *f1, char *file2, FILE *f2,
+static void change(const char *file1, FILE *f1, const char *file2, FILE *f2,
int a, int b, int c, int d)
{
if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
}
-static void output(char *file1, FILE *f1, char *file2, FILE *f2)
+static void output(const char *file1, FILE *f1, const char *file2, FILE *f2)
{
/* Note that j0 and j1 can't be used as they are defined in math.h.
* This also allows the rather amusing variable 'j00'... */
*/
/* NB: files can be not REGular. The only sure thing that they
* are not both DIRectories. */
-static unsigned diffreg(char *file1, char *file2, int flags)
+static unsigned diffreg(const char *file1, const char *file2, int flags)
{
int *member; /* will be overlaid on nfile[1] */
int *class; /* will be overlaid on nfile[0] */
anychange = 0;
context_vec_ptr = context_vec_start - 1;
+ tempname1 = tempname2 = NULL;
/* Is any of them a directory? Then it's simple */
if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
rval = D_SAME;
if (flags & D_EMPTY1)
- f1 = xfopen(bb_dev_null, "r");
- else
- f1 = xfopen_stdin(file1);
+ /* can't be stdin, but xfopen_stdin() is smaller code */
+ file1 = bb_dev_null;
+ f1 = xfopen_stdin(file1);
if (flags & D_EMPTY2)
- f2 = xfopen(bb_dev_null, "r");
- else
- f2 = xfopen_stdin(file2);
-
- /* Quick check whether they are different */
- /* NB: copies non-REG files to tempfiles and fills tempname1/2 */
- i = files_differ(f1, f2, flags);
- if (i != 1) { /* not different? */
- if (i != 0) /* error? */
- exit_status |= 2;
- goto closem;
+ file2 = bb_dev_null;
+ f2 = xfopen_stdin(file2);
+
+ /* NB: if D_EMPTY1/2 is set, other file is always a regular file,
+ * not pipe/fifo/chardev/etc - D_EMPTY is used by "diff -r" only,
+ * and it never diffs non-ordinary files in subdirs. */
+ if (!(flags & (D_EMPTY1 | D_EMPTY2))) {
+ /* Quick check whether they are different */
+ /* NB: copies non-REG files to tempfiles and fills tempname1/2 */
+ i = files_differ(f1, f2);
+ if (i != 1) { /* not different? */
+ if (i != 0) /* error? */
+ exit_status |= 2;
+ goto closem;
+ }
}
if (!asciifile(f1) || !asciifile(f2)) {
member = (int *) nfile[1];
equiv(sfile[0], slen[0], sfile[1], slen[1], member);
+//TODO: xrealloc_vector?
member = xrealloc(member, (slen[1] + 2) * sizeof(int));
class = (int *) nfile[0];
#if ENABLE_FEATURE_DIFF_DIR
static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
{
- int flags = D_HEADER;
+ int flags = 0; /*D_HEADER;*/
int val;
char *fullpath1 = NULL; /* if -N */
char *fullpath2 = NULL;
#if ENABLE_FEATURE_DIFF_DIR
/* This function adds a filename to dl, the directory listing. */
-static int add_to_dirlist(const char *filename,
- struct stat ATTRIBUTE_UNUSED *sb,
+static int FAST_FUNC add_to_dirlist(const char *filename,
+ struct stat *sb UNUSED_PARAM,
void *userdata,
- int depth ATTRIBUTE_UNUSED)
+ int depth UNUSED_PARAM)
{
- /* +2: with space for eventual trailing NULL */
- dl = xrealloc(dl, (dl_count+2) * sizeof(dl[0]));
+ dl = xrealloc_vector(dl, 5, dl_count);
dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata);
dl_count++;
return TRUE;
dl_count = 0;
dl = xzalloc(sizeof(dl[0]));
- /* If -r has been set, then the recursive_action function will be
- * used. Unfortunately, this outputs the root directory along with
- * the recursed paths, so use void *userdata to specify the string
- * length of the root directory - '(void*)(strlen(path)+)'.
- * add_to_dirlist then removes root dir prefix. */
+ /* We need to trim root directory prefix.
+ * Using void *userdata to specify its length,
+ * add_to_dirlist will remove it. */
if (option_mask32 & FLAG_r) {
recursive_action(path, ACTION_RECURSE|ACTION_FOLLOWLINKS,
- add_to_dirlist, NULL,
- (void*)(strlen(path)+1), 0);
+ add_to_dirlist, /* file_action */
+ NULL, /* dir_action */
+ (void*)(ptrdiff_t)(strlen(path) + 1),
+ 0);
} else {
DIR *dp;
struct dirent *ep;
int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int diff_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int diff_main(int argc UNUSED_PARAM, char **argv)
{
int gotstdin = 0;
char *f1, *f2;
while (L_arg) {
if (label1 && label2)
bb_show_usage();
- if (!label1)
- label1 = L_arg->data;
- else { /* then label2 is NULL */
+ if (label1) /* then label2 is NULL */
label2 = label1;
- label1 = L_arg->data;
- }
- /* we leak L_arg here... */
- L_arg = L_arg->link;
+ label1 = llist_pop(&L_arg);
}
/*
*/
f1 = argv[0];
f2 = argv[1];
+ /* Compat: "diff file name_which_doesnt_exist" exits with 2 */
+ xfunc_error_retval = 2;
if (LONE_DASH(f1)) {
fstat(STDIN_FILENO, &stb1);
gotstdin++;
gotstdin++;
} else
xstat(f2, &stb2);
+ xfunc_error_retval = 1;
if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
bb_error_msg_and_die("can't compare stdin to a directory");
/* NB: "diff dir dir2/dir3/file" must become
* "diff dir/file dir2/dir3/file" */
char *slash = strrchr(f2, '/');
- f1 = concat_path_file(f1, slash ? slash+1 : f2);
+ f1 = concat_path_file(f1, slash ? slash + 1 : f2);
xstat(f1, &stb1);
}
if (S_ISDIR(stb2.st_mode)) {
char *slash = strrchr(f1, '/');
- f2 = concat_path_file(f2, slash ? slash+1 : f1);
+ f2 = concat_path_file(f2, slash ? slash + 1 : f1);
xstat(f2, &stb2);
}