Added cs to the list of languages
[midnight-commander.git] / src / chown.c
blob1e6181d804a29f2242b6f463f6f31f4301e11924
1 /* Chown command -- for the Midnight Commander
2 Copyright (C) 1994 Radek Doulik
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <config.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <errno.h> /* For errno on SunOS systems */
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <grp.h>
28 #include <pwd.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include "tty.h"
34 #include "global.h"
35 #include "win.h"
36 #include "color.h"
37 #include "dlg.h"
38 #include "widget.h"
39 #include "dialog.h" /* For do_refresh() */
41 /* Needed for the extern declarations of integer parameters */
42 #include "dir.h"
43 #include "panel.h" /* Needed for the externs */
44 #include "file.h"
45 #include "chmod.h"
46 #include "main.h"
47 #include "chown.h"
48 #include "wtools.h" /* For init_box_colors */
49 #include "../vfs/vfs.h"
51 #define UX 5
52 #define UY 2
54 #define GX 27
55 #define GY 2
57 #define BX 5
58 #define BY 15
60 #define TX 50
61 #define TY 2
63 #define BUTTONS 5
65 #define B_SETALL B_USER
66 #define B_SETUSR B_USER + 1
67 #define B_SETGRP B_USER + 2
69 /* struct stat *sf_stat; */
70 static int need_update, end_chown;
71 static int current_file;
72 static int single_set;
73 static WListbox *l_user, *l_group;
75 static struct {
76 int ret_cmd, flags, y, x;
77 char *text;
78 } chown_but[BUTTONS] = {
79 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
80 { B_ENTER, DEFPUSH_BUTTON, 0, 40, N_("&Set") },
81 { B_SETUSR, NORMAL_BUTTON, 0, 25, N_("Set &users") },
82 { B_SETGRP, NORMAL_BUTTON, 0, 11, N_("Set &groups") },
83 { B_SETALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
86 #define LABELS 5
87 static struct {
88 int y, x;
89 WLabel *l;
90 } chown_label [LABELS] = {
91 { TY+2, TX+2 },
92 { TY+4, TX+2 },
93 { TY+6, TX+2 },
94 { TY+8, TX+2 },
95 { TY+10,TX+2 }
98 #ifndef HAVE_X
99 static void
100 chown_refresh (void)
102 attrset (COLOR_NORMAL);
103 dlg_erase (ch_dlg);
105 draw_box (ch_dlg, 1, 2, 16, 70);
106 draw_box (ch_dlg, UY, UX, 12, 21);
107 draw_box (ch_dlg, GY, GX, 12, 21);
108 draw_box (ch_dlg, TY, TX, 12, 19);
110 attrset (COLOR_NORMAL);
111 dlg_move (ch_dlg, TY + 1, TX + 1);
112 addstr (_(" Name "));
113 dlg_move (ch_dlg, TY + 3, TX + 1);
114 addstr (_(" Owner name "));
115 dlg_move (ch_dlg, TY + 5, TX + 1);
116 addstr (_(" Group name "));
117 dlg_move (ch_dlg, TY + 7, TX + 1);
118 addstr (_(" Size "));
119 dlg_move (ch_dlg, TY + 9, TX + 1);
120 addstr (_(" Permission "));
122 attrset (COLOR_HOT_NORMAL);
123 dlg_move (ch_dlg, 1, 28);
124 addstr (_(" Chown command "));
125 dlg_move (ch_dlg, UY, UX + 1);
126 addstr (_(" User name "));
127 dlg_move (ch_dlg, GY, GX + 1);
128 addstr (_(" Group name "));
129 dlg_move (ch_dlg, TY, TX + 1);
130 addstr (_(" File "));
132 #endif
134 static char *
135 next_file (void)
137 while (!cpanel->dir.list[current_file].f.marked)
138 current_file++;
140 return cpanel->dir.list[current_file].fname;
143 static int
144 chown_callback (Dlg_head * h, int Par, int Msg)
146 switch (Msg) {
147 #ifndef HAVE_X
148 case DLG_DRAW:
149 chown_refresh ();
150 break;
151 #endif
153 return 0;
156 static int
157 l_call (void *data)
159 return 1;
162 static void
163 init_chown (void)
165 int i;
166 struct passwd *l_pass;
167 struct group *l_grp;
169 do_refresh ();
170 end_chown = need_update = current_file = 0;
171 single_set = (cpanel->marked < 2) ? 3 : 0;
173 ch_dlg = create_dlg (0, 0, 18, 74, dialog_colors, chown_callback,
174 "[Chown]", "chown", DLG_CENTER);
176 #define XTRACT(i) BY+chown_but[i].y, BX+chown_but[i].x, chown_but[i].ret_cmd, chown_but[i].flags, _(chown_but[i].text), 0, 0, NULL
178 for (i = 0; i < BUTTONS-single_set; i++)
179 add_widget (ch_dlg, button_new (XTRACT (i)));
181 /* Add the widgets for the file information */
182 #define LX(i) chown_label [i].y, chown_label [i].x, "", NULL
183 for (i = 0; i < LABELS; i++){
184 chown_label [i].l = label_new (LX (i));
185 add_widget (ch_dlg, chown_label [i].l);
188 /* get new listboxes */
189 l_user = listbox_new (UY + 1, UX + 1, 19, 10, 0, l_call, NULL);
190 l_group = listbox_new (GY + 1, GX + 1, 19, 10, 0, l_call, NULL);
192 listbox_add_item (l_user, 0, 0, _("<Unknown user>"), NULL); /* add fields for unknown names (numbers) */
193 listbox_add_item (l_group, 0, 0, _("<Unknown group>"), NULL);
195 setpwent (); /* get and put user names in the listbox */
196 while ((l_pass = getpwent ())) {
197 listbox_add_item (l_user, 0, 0, l_pass->pw_name, NULL);
199 endpwent ();
201 setgrent (); /* get and put group names in the listbox */
202 while ((l_grp = getgrent ())) {
203 listbox_add_item (l_group, 0, 0, l_grp->gr_name, NULL);
205 endgrent ();
207 add_widget (ch_dlg, l_group);
208 add_widget (ch_dlg, l_user); /* add listboxes to the dialogs */
211 static void
212 chown_done (void)
214 if (need_update)
215 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
216 repaint_screen ();
219 static inline void
220 do_chown (uid_t u, gid_t g)
222 if (mc_chown (cpanel->dir.list [current_file].fname, u, g) == -1)
223 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
224 cpanel->dir.list [current_file].fname, unix_error_string (errno));
226 do_file_mark (cpanel, current_file, 0);
229 static void
230 apply_chowns (uid_t u, gid_t g)
232 char *fname;
234 need_update = end_chown = 1;
235 do_chown (u,g);
237 do {
238 fname = next_file ();
240 do_chown (u,g);
241 } while (cpanel->marked);
244 #define chown_label(n,txt) label_set_text (chown_label [n].l, txt)
246 void
247 chown_cmd (void)
249 char *fname;
250 struct stat sf_stat;
251 WLEntry *fe;
252 uid_t new_user;
253 gid_t new_group;
254 char buffer [BUF_TINY];
256 #if 0
257 /* Please no */
258 if (!vfs_current_is_local ()) {
259 if (vfs_current_is_extfs ()) {
260 message (1, _(" Oops... "),
261 _(" I can't run the Chown command on an extfs "));
262 return;
263 } else if (vfs_current_is_tarfs ()) {
264 message (1, _(" Oops... "),
265 _(" I can't run the Chown command on a tarfs "));
266 return;
269 #endif
271 do { /* do while any files remaining */
272 init_chown ();
273 new_user = new_group = -1;
275 if (cpanel->marked)
276 fname = next_file (); /* next marked file */
277 else
278 fname = selection (cpanel)->fname; /* single file */
280 if (!stat_file (fname, &sf_stat)){ /* get status of file */
281 destroy_dlg (ch_dlg);
282 break;
285 /* select in listboxes */
286 fe = listbox_search_text (l_user, get_owner(sf_stat.st_uid));
287 if (fe)
288 listbox_select_entry (l_user, fe);
290 fe = listbox_search_text (l_group, get_group(sf_stat.st_gid));
291 if (fe)
292 listbox_select_entry (l_group, fe);
294 chown_label (0, name_trunc (fname, 15));
295 chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15));
296 chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15));
297 g_snprintf (buffer, sizeof (buffer), "%d", c_fsize);
298 chown_label (3, buffer);
299 chown_label (4, string_perm (sf_stat.st_mode));
301 run_dlg (ch_dlg);
303 switch (ch_dlg->ret_value) {
304 case B_CANCEL:
305 end_chown = 1;
306 break;
308 case B_SETUSR:
310 struct passwd *user;
312 user = getpwnam (l_user->current->text);
313 if (user){
314 new_user = user->pw_uid;
315 apply_chowns (new_user, new_group);
317 break;
319 case B_SETGRP:
321 struct group *grp;
323 grp = getgrnam (l_group->current->text);
324 if (grp){
325 new_group = grp->gr_gid;
326 apply_chowns (new_user, new_group);
328 break;
330 case B_SETALL:
331 case B_ENTER:
333 struct group *grp;
334 struct passwd *user;
336 grp = getgrnam (l_group->current->text);
337 if (grp)
338 new_group = grp->gr_gid;
339 user = getpwnam (l_user->current->text);
340 if (user)
341 new_user = user->pw_uid;
342 if (ch_dlg->ret_value==B_ENTER) {
343 need_update = 1;
344 if (mc_chown (fname, new_user, new_group) == -1)
345 message (1, MSG_ERROR, _(" Couldn't chown \"%s\" \n %s "),
346 fname, unix_error_string (errno));
347 } else
348 apply_chowns (new_user, new_group);
349 break;
353 if (cpanel->marked && ch_dlg->ret_value != B_CANCEL){
354 do_file_mark (cpanel, current_file, 0);
355 need_update = 1;
357 destroy_dlg (ch_dlg);
358 } while (cpanel->marked && !end_chown);
360 chown_done ();