/[baselayout]/trunk/src/checkown.c
Gentoo

Diff of /trunk/src/checkown.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2898 Revision 2899
1/* 1/*
2 checkown.c 2 checkown.c
3 Checks for the existance of a directory and creates it 3 Checks for the existance of a file or directory and creates it
4 if necessary. It can also correct its ownership. 4 if necessary. It can also correct its ownership.
5 5
6 Copyright 2007 Gentoo Foundation 6 Copyright 2007 Gentoo Foundation
7 */ 7 */
8 8
9#define APPLET "checkown" 9#define APPLET "checkown"
10 10
11#include <sys/types.h> 11#include <sys/types.h>
12#include <sys/stat.h> 12#include <sys/stat.h>
13#include <errno.h> 13#include <errno.h>
14#include <fcntl.h>
14#include <getopt.h> 15#include <getopt.h>
15#include <grp.h> 16#include <grp.h>
16#include <pwd.h> 17#include <pwd.h>
17#include <stdio.h> 18#include <stdio.h>
18#include <stdlib.h> 19#include <stdlib.h>
22#include "builtins.h" 23#include "builtins.h"
23#include "einfo.h" 24#include "einfo.h"
24 25
25static char *applet = NULL; 26static char *applet = NULL;
26 27
27static int do_check (char *path, uid_t uid, gid_t gid, mode_t mode) 28static int do_check (char *path, uid_t uid, gid_t gid, mode_t mode, bool file)
28{ 29{
29 struct stat dirstat; 30 struct stat dirstat;
30 31
31 memset (&dirstat, 0, sizeof (dirstat)); 32 memset (&dirstat, 0, sizeof (dirstat));
32 33
33 if (stat (path, &dirstat)) { 34 if (stat (path, &dirstat)) {
35 if (file) {
36 int fd;
37 einfo ("%s: creating file", path);
38 if ((fd = open (path, O_CREAT)) == -1) {
39 eerror ("%s: open: %s", applet, strerror (errno));
40 return (-1);
41 }
42 close (fd);
43 } else {
34 einfo ("%s: creating directory", path); 44 einfo ("%s: creating directory", path);
35 if (! mode) 45 if (! mode)
36 mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 46 mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
37 if (mkdir (path, mode)) { 47 if (mkdir (path, mode)) {
38 eerror ("%s: mkdir: %s", applet, strerror (errno)); 48 eerror ("%s: mkdir: %s", applet, strerror (errno));
39 return (-1); 49 return (-1);
50 }
51 mode = 0;
40 } 52 }
53 }
54
41 } else if (mode && (dirstat.st_mode & 0777) != mode) { 55 if (mode && (dirstat.st_mode & 0777) != mode) {
42 einfo ("%s: correcting mode", applet); 56 einfo ("%s: correcting mode", applet);
43 if (chmod (path, mode)) { 57 if (chmod (path, mode)) {
44 eerror ("%s: chmod: %s", applet, strerror (errno)); 58 eerror ("%s: chmod: %s", applet, strerror (errno));
45 return (-1); 59 return (-1);
46 } 60 }
106 else 120 else
107 return (getgrgid (tid)); 121 return (getgrgid (tid));
108} 122}
109 123
110#include "_usage.h" 124#include "_usage.h"
111#define getoptstring "m:g:u:" getoptstring_COMMON 125#define getoptstring "fm:g:u:" getoptstring_COMMON
112static struct option longopts[] = { 126static struct option longopts[] = {
127 { "directory", 0, NULL, 'd'},
128 { "file", 0, NULL, 'f'},
113 { "mode", 1, NULL, 'm'}, 129 { "mode", 1, NULL, 'm'},
114 { "user", 1, NULL, 'u'}, 130 { "user", 1, NULL, 'u'},
115 { "group", 1, NULL, 'g'}, 131 { "group", 1, NULL, 'g'},
116 longopts_COMMON 132 longopts_COMMON
117 { NULL, 0, NULL, 0} 133 { NULL, 0, NULL, 0}
122{ 138{
123 int opt; 139 int opt;
124 uid_t uid = geteuid(); 140 uid_t uid = geteuid();
125 gid_t gid = getgid(); 141 gid_t gid = getgid();
126 mode_t mode = 0; 142 mode_t mode = 0;
127
128 struct passwd *pw = NULL; 143 struct passwd *pw = NULL;
129 struct group *gr = NULL; 144 struct group *gr = NULL;
145 bool file = 0;
146
130 char *p; 147 char *p;
131 int retval = EXIT_SUCCESS; 148 int retval = EXIT_SUCCESS;
132 149
133 applet = argv[0]; 150 applet = argv[0];
134 151
135 while ((opt = getopt_long (argc, argv, getoptstring, 152 while ((opt = getopt_long (argc, argv, getoptstring,
136 longopts, (int *) 0)) != -1) 153 longopts, (int *) 0)) != -1)
137 { 154 {
138 switch (opt) { 155 switch (opt) {
156 case 'd':
157 file = 0;
158 break;
159 case 'f':
160 file = 1;
161 break;
139 case 'm': 162 case 'm':
140 if (parse_mode (&mode, optarg)) 163 if (parse_mode (&mode, optarg))
141 eerrorx ("%s: invalid mode `%s'", applet, optarg); 164 eerrorx ("%s: invalid mode `%s'", applet, optarg);
142 break; 165 break;
143 case 'u': 166 case 'u':
163 } 186 }
164 if (gr) 187 if (gr)
165 gid = gr->gr_gid; 188 gid = gr->gr_gid;
166 189
167 while (optind < argc) { 190 while (optind < argc) {
168 if (do_check (argv[optind], uid, gid, mode)) 191 if (do_check (argv[optind], uid, gid, mode, file))
169 retval = EXIT_FAILURE; 192 retval = EXIT_FAILURE;
170 optind++; 193 optind++;
171 } 194 }
172 195
173 exit (retval); 196 exit (retval);

Legend:
Removed from v.2898  
changed lines
  Added in v.2899

  ViewVC Help
Powered by ViewVC 1.1.20