* New version 2.21.999
[alpine.git] / pith / osdep / filesize.c
blob82504411472c5f90d9f846db7ab8d9f745fc4c3e
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: filesize.c 761 2007-10-23 22:35:18Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2018 Eduardo Chappa
8 * Copyright 2006 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include <system.h>
20 #include "../charconv/utf8.h"
21 #include "../charconv/filesys.h"
22 #include "filesize.h"
25 /*----------------------------------------------------------------------
26 Return the number of bytes in given file
28 Args: file -- file name
30 Result: the number of bytes in the file is returned or
31 -1 on error, in which case errno is valid
32 ----*/
33 long
34 name_file_size(char *file)
36 struct stat buffer;
38 if(our_stat(file, &buffer) != 0)
39 return(-1L);
41 return((long)buffer.st_size);
45 /*----------------------------------------------------------------------
46 Return the number of bytes in given file
48 Args: fp -- FILE * for open file
50 Result: the number of bytes in the file is returned or
51 -1 on error, in which case errno is valid
52 ----*/
53 long
54 fp_file_size(FILE *fp)
56 struct stat buffer;
58 if(fstat(fileno(fp), &buffer) != 0)
59 return(-1L);
61 return((long)buffer.st_size);
65 /*----------------------------------------------------------------------
66 Return the modification time of given file
68 Args: file -- file name
70 Result: the time of last modification (mtime) of the file is returned or
71 -1 on error, in which case errno is valid
72 ----*/
73 time_t
74 name_file_mtime(char *file)
76 struct stat buffer;
78 if(our_stat(file, &buffer) != 0)
79 return((time_t)(-1));
81 return(buffer.st_mtime);
85 /*----------------------------------------------------------------------
86 Return the modification time of given file
88 Args: fp -- FILE * for open file
90 Result: the time of last modification (mtime) of the file is returned or
91 -1 on error, in which case errno is valid
92 ----*/
93 time_t
94 fp_file_mtime(FILE *fp)
96 struct stat buffer;
98 if(fstat(fileno(fp), &buffer) != 0)
99 return((time_t)(-1));
101 return(buffer.st_mtime);
105 /*----------------------------------------------------------------------
106 Copy the mode, owner, and group of sourcefile to targetfile.
108 Args: targetfile --
109 sourcefile --
111 We don't bother keeping track of success or failure because we don't care.
112 ----*/
113 void
114 file_attrib_copy(char *targetfile, char *sourcefile)
116 struct stat buffer;
118 if(our_stat(sourcefile, &buffer) == 0){
119 our_chmod(targetfile, buffer.st_mode);
120 #if HAVE_CHOWN
121 our_chown(targetfile, buffer.st_uid, buffer.st_gid);
122 #endif