Skip to content

Commit

Permalink
Merge pull request #29 from tansy/fstat
Browse files Browse the repository at this point in the history
preserve file attributes
  • Loading branch information
mcmilk authored Apr 6, 2024
2 parents f1ee584 + 33afcc2 commit d6c1fda
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions programs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,34 @@ static int str_casestart(const char *a, const char *b)
return 0;
}

int get_fstat(FILE* file, struct stat* const f_stat)
{
int fd = fileno(file);
int i = fstat(fd, f_stat);
return i;
}

int set_fstat(FILE* file, const char* fname, struct stat* const f_stat)
{
int fd = fileno(file);
mode_t mode;
int warn = 0;
struct utimbuf ftm;

if (f_stat)
{
mode = f_stat->st_mode;
if(fchmod(fd, mode) != 0)
warn = 1;

ftm.actime = f_stat->st_atime;
ftm.modtime = f_stat->st_mtime;
if(utime(fname, &ftm) != 0)
warn = 1;
}
return warn;
}

static void treat_stdin()
{
const char *filename = "(stdin)";
Expand Down Expand Up @@ -573,6 +601,7 @@ static void treat_file(char *filename)
static int first = 1;
FILE *local_fout = NULL;
char *fn2 = 0;
struct stat fin_stat;

/* reset counter */
bytes_written = bytes_read = 0;
Expand All @@ -593,6 +622,7 @@ static void treat_file(char *filename)
if (errmsg)
return;
fin = fopen(filename, "rb");
get_fstat(fin, &fin_stat);
}

/* setup fout stream */
Expand Down Expand Up @@ -655,8 +685,12 @@ static void treat_file(char *filename)

/* close outstream */
if (!global_fout && local_fout != stdout)
{
fflush(local_fout);
set_fstat(local_fout, fn2, &fin_stat);
if (fclose(local_fout) != 0 && opt_verbose)
fprintf(stderr, "Closing outfile failed.");
}

/* listing mode */
if (opt_mode == MODE_LIST)
Expand Down Expand Up @@ -684,6 +718,8 @@ static void treat_file(char *filename)
return;
}



int main(int argc, char **argv)
{
if (argc < 2){
Expand Down Expand Up @@ -938,3 +974,4 @@ int main(int argc, char **argv)
/* exit should flush stdout / stderr */
exit(exit_code);
}

1 change: 1 addition & 0 deletions programs/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" {
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <utime.h>

extern int getcpucount(void);

Expand Down

0 comments on commit d6c1fda

Please sign in to comment.