"Fossies" - the Fresh Open Source Software Archive 
Member "littleutils-1.2.5/littleutils/fileown.c" (29 Oct 2021, 6030 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 "fileown.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 /* fileown: Prints out the owner or group of the specified files.
2
3 Copyright (C) 2009-2021 by Brian Lindholm.
4 This file is part of the littleutils utility set.
5
6 The fileown 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 fileown 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 <grp.h>
23 #if HAVE_INTTYPES_H
24 # include <inttypes.h>
25 #endif
26 #include <limits.h>
27 #include <pwd.h>
28 #ifdef HAVE_STDIO_H
29 # include <stdio.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #endif
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
39 #endif
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 # define OPTEND -1
47 #else
48 # define OPTEND EOF
49 #endif
50 #ifdef HAVE_GETOPT_H
51 # include <getopt.h>
52 #endif
53
54 #ifdef __MINGW32__
55 extern int getopt (int argc, char * const *argv, const char *optstring);
56 extern char *optarg;
57 extern int optind;
58 #endif
59
60 #ifdef DJGPP
61 unsigned short _djstat_flags = 63; /* speed up stat command for DJGPP */
62 #endif
63
64 #ifndef PATH_MAX
65 #define PATH_MAX 256
66 #endif
67
68 char *uid_fmt = (sizeof (uid_t) <= sizeof (int) ? "%u" : "%lu");
69 char *gid_fmt = (sizeof (gid_t) <= sizeof (int) ? "%u" : "%lu");
70
71
72 /* help function */
73
74 static void
75 help (FILE *where)
76 {
77 fprintf (where,
78 "fileown " PACKAGE_VERSION "\n"
79 "usage: fileown [-b(oth_ids)] [-f file_list] [-g(roup_id)] [-h(elp)]\n"
80 " [-n(umbers)] [-p(ipe)] [-q(uiet)] [-v(erbose)] file...\n");
81 }
82
83
84 /* print file owner function */
85
86 static void
87 printowner (char *filename, int verbose, int print_numbers, int print_gid, int print_uid)
88 {
89 struct stat file_stats;
90 struct passwd *file_passwd;
91 struct group *file_group;
92
93 if (stat (filename, &file_stats))
94 fprintf (stderr, "fileown error: can't determine uid/gid of %s\n", filename);
95 else {
96 if (verbose == 1) {
97 fprintf (stdout, "%s\t", filename);
98 }
99 if (print_uid == 1) {
100 if (print_numbers == 1) {
101 fprintf (stdout, uid_fmt, file_stats.st_uid);
102 }
103 else {
104 file_passwd = getpwuid(file_stats.st_uid);
105 if (file_passwd == NULL) {
106 fprintf (stdout, uid_fmt, file_stats.st_uid);
107 }
108 else {
109 fprintf (stdout, "%s", file_passwd->pw_name);
110 }
111 }
112 }
113 if ((print_uid == 1) && (print_gid == 1)) {
114 fprintf (stdout, ".");
115 }
116 if (print_gid == 1) {
117 if (print_numbers == 1) {
118 fprintf (stdout, gid_fmt, file_stats.st_gid);
119 }
120 else {
121 file_group = getgrgid(file_stats.st_gid);
122 if (file_group == NULL) {
123 fprintf (stdout, gid_fmt, file_stats.st_gid);
124 }
125 else {
126 fprintf (stdout, "%s", file_group ->gr_name);
127 }
128 }
129 }
130 fprintf (stdout, "\n");
131 }
132 }
133
134
135 /* main program */
136
137 int
138 main (int argc, char *argv[])
139 {
140 FILE *infile;
141 char filename[PATH_MAX], *listname, *newline, *rc;
142 int argn, print_gid, print_numbers, print_uid, opt, use_file, use_pipe, verbose;
143
144 /* parse options */
145
146 listname = "";
147 verbose = 0;
148 use_file = 0;
149 use_pipe = 0;
150 print_numbers = 0;
151 print_uid = 1;
152 print_gid = 0;
153 while ((opt = getopt (argc, argv, "bf:ghnpqv")) != OPTEND)
154 switch (opt)
155 {
156 case 'b':
157 print_uid = 1;
158 print_gid = 1;
159 break;
160 case 'f':
161 use_file = 1;
162 listname = optarg;
163 break;
164 case 'g':
165 print_uid = 0;
166 print_gid = 1;
167 break;
168 case 'h':
169 help (stdout);
170 return (0);
171 case 'n':
172 print_numbers = 1;
173 break;
174 case 'p':
175 use_pipe = 1;
176 break;
177 case 'q':
178 verbose = -1;
179 break;
180 case 'v':
181 verbose = 1;
182 break;
183 case '?':
184 help (stderr);
185 return (1);
186 }
187
188 /* finalize options */
189
190 if ((optind == argc) && (use_file == 0) && (use_pipe == 0))
191 {
192 help (stdout);
193 return (0);
194 }
195 if (verbose == 0)
196 {
197 if (((argc - optind) != 1) || use_file || use_pipe)
198 verbose = 1;
199 else
200 verbose = -1;
201 }
202
203 /* process files in listed in file specified by -f option */
204
205 if (use_file)
206 {
207 infile = fopen (listname, "r");
208 if (infile == NULL)
209 fprintf (stderr, "fileown error: can't open %s!\n", listname);
210 else
211 {
212 while (!feof (infile))
213 {
214 rc = fgets (filename, PATH_MAX - 1, infile);
215 if (rc != NULL)
216 {
217 newline = strchr (filename, '\n');
218 if (newline != NULL)
219 *newline = '\0';
220 if (strlen (filename) != 0)
221 printowner (filename, verbose, print_numbers, print_gid, print_uid);
222 }
223 }
224 (void) fclose (infile);
225 }
226 }
227
228 /* process files listed on stdin (i.e., the -p option) */
229
230 if (use_pipe)
231 while (!feof (stdin))
232 {
233 rc = fgets (filename, PATH_MAX - 1, stdin);
234 if (rc != NULL)
235 {
236 newline = strchr (filename, '\n');
237 if (newline != NULL)
238 *newline = '\0';
239 if (strlen (filename) != 0)
240 printowner (filename, verbose, print_numbers, print_gid, print_uid);
241 }
242 }
243
244 /* process files given in the argument list */
245
246 for (argn = optind; argn < argc; argn++)
247 printowner (argv[argn], verbose, print_numbers, print_gid, print_uid);
248
249 /* indicate successful finish */
250
251 return (0);
252 }