"Fossies" - the Fresh Open Source Software Archive 
Member "littleutils-1.2.5/littleutils/filedate.c" (29 Oct 2021, 6649 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "filedate.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
1.2.4_vs_1.2.5.
1 /* filedate: Prints out the modification time for the specified files.
2
3 Copyright (C) 2004-2021 by Brian Lindholm.
4 This file is part of the littleutils utility set.
5
6 The filedate utility is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 The filedate utility is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 the littleutils. If not, see <https://www.gnu.org/licenses/>. */
18
19
20 #include <config.h>
21
22 #include <limits.h>
23 #ifdef HAVE_STDIO_H
24 # include <stdio.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 # include <string.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #include <time.h>
33
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 # define OPTEND -1
37 #else
38 # define OPTEND EOF
39 #endif
40 #ifdef HAVE_GETOPT_H
41 # include <getopt.h>
42 #endif
43
44 #ifdef __MINGW32__
45 extern int getopt (int argc, char * const *argv, const char *optstring);
46 extern char *optarg;
47 extern int optind;
48 #endif
49
50 #ifdef DJGPP
51 /* speed up stat command for DJGPP */
52 unsigned short _djstat_flags = 63;
53 #endif
54
55 #ifndef PATH_MAX
56 # define PATH_MAX 256
57 #endif
58
59 #define FORMAT_DEFAULT 0
60 #define FORMAT_LOCALE 1
61 #define FORMAT_TOUCH 2
62
63
64 /* help function */
65
66 static void
67 help (FILE *where)
68 {
69 fprintf (where,
70 "filedate " PACKAGE_VERSION "\n"
71 "usage: filedate [-a(ccess_time)] [-f file_list] [-h(elp)] [-l(ocal_format)]\n"
72 " [-p(ipe)] [-q(uiet)] [-t(ouch_format)] [-v(erbose)] file...\n");
73 }
74
75
76 /* print filedate function */
77
78 static void
79 printdate (char *filename, int verbose, int use_access_time, int format)
80 {
81 struct stat file_stats;
82 char date_string[256], nano_string[16], *format_string;
83 time_t time_val;
84 int use_nsec;
85
86 if (stat (filename, &file_stats))
87 fprintf (stderr, "filedate error: can't determine date of %s\n", filename);
88 else {
89 if (format == FORMAT_TOUCH)
90 {
91 format_string = "%Y%m%d%H%M.%S";
92 use_nsec = 0;
93 }
94 else if (format == FORMAT_LOCALE)
95 {
96 format_string = "%c";
97 use_nsec = 0;
98 }
99 else
100 {
101 format_string = "%Y-%m-%d %H:%M:%S";
102 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
103 use_nsec = 1;
104 #elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
105 use_nsec = 1;
106 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
107 use_nsec = 1;
108 #else
109 use_nsec = 0;
110 #endif
111 }
112 if (use_access_time)
113 time_val = file_stats.st_atime;
114 else
115 time_val = file_stats.st_mtime;
116 (void) strftime (date_string, 255, format_string, localtime (&time_val));
117 if (use_nsec == 0)
118 nano_string[0] = '\0';
119 else if (use_access_time)
120 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
121 (void) sprintf(nano_string, ".%09u", (unsigned int) file_stats.st_atim.tv_nsec);
122 #elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
123 (void) sprintf(nano_string, ".%09u", (unsigned int) file_stats.st_atimespec.tv_nsec);
124 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
125 (void) sprintf(nano_string, ".%09u", (unsigned int) file_stats.st_atimensec);
126 #else
127 nano_string[0] = '\0';
128 #endif
129 else
130 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
131 (void) sprintf(nano_string, ".%09u", (unsigned int) file_stats.st_mtim.tv_nsec);
132 #elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
133 (void) sprintf(nano_string, ".%09u", (unsigned int) file_stats.st_mtimespec.tv_nsec);
134 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
135 (void) sprintf(nano_string, ".%09u", (unsigned int) file_stats.stat.st_mtimensec);
136 #else
137 nano_string[0] = '\0';
138 #endif
139 if (verbose == 1)
140 fprintf (stdout, "%s\t%s%s\n", filename, date_string, nano_string);
141 else
142 fprintf (stdout, "%s%s\n", date_string, nano_string);
143 }
144 }
145
146
147 /* main program */
148
149 int
150 main (int argc, char *argv[])
151 {
152 FILE *infile;
153 char filename[PATH_MAX], *listname, *newline, *rc;
154 int argn, opt, use_access_time, use_file, use_format, use_pipe, verbose;
155
156 /* parse options */
157
158 listname = "";
159 verbose = 0;
160 use_access_time = 0;
161 use_file = 0;
162 use_format = FORMAT_DEFAULT;
163 use_pipe = 0;
164 while ((opt = getopt (argc, argv, "af:hlpqtv")) != OPTEND)
165 switch (opt)
166 {
167 case 'a':
168 use_access_time = 1;
169 break;
170 case 'f':
171 use_file = 1;
172 listname = optarg;
173 break;
174 case 'h':
175 help (stdout);
176 return (0);
177 case 'l':
178 use_format = FORMAT_LOCALE;
179 break;
180 case 'p':
181 use_pipe = 1;
182 break;
183 case 'q':
184 verbose = -1;
185 break;
186 case 't':
187 use_format = FORMAT_TOUCH;
188 break;
189 case 'v':
190 verbose = 1;
191 break;
192 case '?':
193 help (stderr);
194 return (1);
195 }
196
197 /* finalize options */
198
199 if ((optind == argc) && (use_file == 0) && (use_pipe == 0))
200 {
201 help (stdout);
202 return (0);
203 }
204 if (verbose == 0)
205 {
206 if (((argc - optind) != 1) || use_file || use_pipe)
207 verbose = 1;
208 else
209 verbose = -1;
210 }
211
212 /* process files in listed in file specified by -f option */
213
214 if (use_file)
215 {
216 infile = fopen (listname, "r");
217 if (infile == NULL)
218 fprintf (stderr, "filedate error: can't open %s!\n", listname);
219 else
220 {
221 while (!feof (infile))
222 {
223 rc = fgets (filename, PATH_MAX - 1, infile);
224 if (rc != NULL)
225 {
226 newline = strchr (filename, '\n');
227 if (newline != NULL)
228 *newline = '\0';
229 if (strlen (filename) != 0)
230 printdate (filename, verbose, use_access_time, use_format);
231 }
232 }
233 (void) fclose (infile);
234 }
235 }
236
237 /* process files listed on stdin (i.e., the -p option) */
238
239 if (use_pipe)
240 while (!feof (stdin))
241 {
242 rc = fgets (filename, PATH_MAX - 1, stdin);
243 if (rc != NULL)
244 {
245 newline = strchr (filename, '\n');
246 if (newline != NULL)
247 *newline = '\0';
248 if (strlen (filename) != 0)
249 printdate (filename, verbose, use_access_time, use_format);
250 }
251 }
252
253 /* process files given in the argument list */
254
255 for (argn = optind; argn < argc; argn++)
256 printdate (argv[argn], verbose, use_access_time, use_format);
257
258 /* indicate successful finish */
259
260 return (0);
261 }