summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@linbit.com>2012-09-19 01:52:10 +0200
committerAndreas Gruenbacher <agruen@linbit.com>2012-09-19 03:07:31 +0200
commit59609b50c538db1805161cb8afe9986f82b9759a (patch)
tree9f4d169f7f12fe837718829e3c7a8b9f607fc6c0
parentb578985c3cdf160ad58b43715bdfa324cdf15618 (diff)
downloadpatch-59609b50c538db1805161cb8afe9986f82b9759a.tar.gz
Introduce function to lstat all input files
* src/util.c (stat_file): New function. (move_file): Use here. * src/util.h (stat_file): Declare here. * src/inp.c (get_input_file): Use here. * src/patch.c (main): Use here. (delete_file_later): Use here. * src/pch.c (there_is_another_patch): Use here. (intuit_diff_type): Use here.
-rw-r--r--src/inp.c2
-rw-r--r--src/patch.c10
-rw-r--r--src/pch.c27
-rw-r--r--src/util.c7
-rw-r--r--src/util.h1
5 files changed, 26 insertions, 21 deletions
diff --git a/src/inp.c b/src/inp.c
index 5a42e75..386dc9e 100644
--- a/src/inp.c
+++ b/src/inp.c
@@ -138,7 +138,7 @@ get_input_file (char const *filename, char const *outname, mode_t file_type)
char *getbuf;
if (inerrno == -1)
- inerrno = lstat (filename, &instat) == 0 ? 0 : errno;
+ inerrno = stat_file (filename, &instat);
/* Perhaps look for RCS or SCCS versions. */
if (S_ISREG (file_type)
diff --git a/src/patch.c b/src/patch.c
index 0b0fdd4..2055cb9 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -255,19 +255,19 @@ main (int argc, char **argv)
if (! strcmp (inname, outname))
{
if (inerrno == -1)
- inerrno = lstat (inname, &instat) ? errno : 0;
+ inerrno = stat_file (inname, &instat);
outstat = instat;
outerrno = inerrno;
}
else
- outerrno = lstat (outname, &outstat) ? errno : 0;
+ outerrno = stat_file (outname, &outstat);
if (! outerrno)
{
if (has_queued_output (&outstat))
{
output_files (&outstat);
- outerrno = lstat (outname, &outstat) ? errno : 0;
+ outerrno = stat_file (outname, &outstat);
inerrno = -1;
}
if (! outerrno)
@@ -618,7 +618,7 @@ main (int argc, char **argv)
struct stat oldst;
int olderrno;
- olderrno = lstat (rej, &oldst) ? errno : 0;
+ olderrno = stat_file (rej, &oldst);
if (olderrno && olderrno != ENOENT)
write_fatal ();
if (! olderrno && lookup_file_id (&oldst) == CREATED)
@@ -1711,7 +1711,7 @@ delete_file_later (const char *name, const struct stat *st, bool backup)
if (! st)
{
- if (lstat (name, &st_tmp) != 0)
+ if (stat_file (name, &st_tmp) != 0)
pfatal ("Can't get file attributes of %s %s", "file", name);
st = &st_tmp;
}
diff --git a/src/pch.c b/src/pch.c
index da6e68f..3616bc8 100644
--- a/src/pch.c
+++ b/src/pch.c
@@ -293,18 +293,16 @@ there_is_another_patch (bool need_header, mode_t *file_type)
{
inname = savebuf (buf, t - buf);
inname[t - buf - 1] = 0;
- if (lstat (inname, &instat) == 0)
- {
- inerrno = 0;
- invc = -1;
- }
- else
+ inerrno = stat_file (inname, &instat);
+ if (inerrno)
{
perror (inname);
fflush (stderr);
free (inname);
inname = 0;
}
+ else
+ invc = -1;
}
if (!inname) {
ask ("Skip this patch? [y] ");
@@ -914,15 +912,16 @@ intuit_diff_type (bool need_header, mode_t *p_file_type)
if (! stat_errno[i])
st[i] = st[i0];
}
- else if (lstat (p_name[i], &st[i]) != 0)
- stat_errno[i] = errno;
- else if (lookup_file_id (&st[i]) == DELETE_LATER)
- stat_errno[i] = ENOENT;
else
{
- stat_errno[i] = 0;
- if (posixly_correct && name_is_valid (p_name[i]))
- break;
+ stat_errno[i] = stat_file (p_name[i], &st[i]);
+ if (! stat_errno[i])
+ {
+ if (lookup_file_id (&st[i]) == DELETE_LATER)
+ stat_errno[i] = ENOENT;
+ else if (posixly_correct && name_is_valid (p_name[i]))
+ break;
+ }
}
i0 = i;
}
@@ -1007,7 +1006,7 @@ intuit_diff_type (bool need_header, mode_t *p_file_type)
{
if (inname)
{
- inerrno = lstat (inname, &instat) == 0 ? 0 : errno;
+ inerrno = stat_file (inname, &instat);
if (inerrno || (instat.st_mode & S_IFMT) == file_type)
maybe_reverse (inname, inerrno, inerrno || instat.st_size == 0);
}
diff --git a/src/util.c b/src/util.c
index 7fb6b05..271f79f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -437,7 +437,7 @@ move_file (char const *from, bool *from_needs_removal,
struct stat to_st;
int to_errno;
- to_errno = lstat (to, &to_st) == 0 ? 0 : errno;
+ to_errno = stat_file (to, &to_st);
if (backup)
create_backup (to, to_errno ? NULL : &to_st, false);
if (! to_errno)
@@ -1649,3 +1649,8 @@ make_tempfile (char const **name, char letter, char const *real_name,
return fd;
}
}
+
+int stat_file (char const *filename, struct stat *st)
+{
+ return lstat (filename, st) == 0 ? 0 : errno;
+}
diff --git a/src/util.h b/src/util.h
index f0d20aa..9a7946f 100644
--- a/src/util.h
+++ b/src/util.h
@@ -68,6 +68,7 @@ void insert_file_id (struct stat const *, enum file_id_type);
enum file_id_type lookup_file_id (struct stat const *);
void set_queued_output (struct stat const *, bool);
bool has_queued_output (struct stat const *);
+int stat_file (char const *, struct stat *);
enum file_attributes {
FA_TIMES = 1,