chmod.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* Chmod command for Windows NT and OS/2
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. */
  14. #include <config.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. /* for chmod and stat */
  18. #include <io.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include "../src/global.h"
  22. #include "../src/tty.h"
  23. #include "../src/mad.h"
  24. #include "../src/util.h"
  25. #include "../src/win.h"
  26. #include "../src/color.h"
  27. #include "../src/dlg.h"
  28. #include "../src/widget.h"
  29. #include "../src/dialog.h" /* For do_refresh() */
  30. #include "../src/dir.h"
  31. #include "../src/panel.h" /* Needed for the externs */
  32. #include "../src/file.h"
  33. #include "../src/main.h"
  34. #include "../src/chmod.h"
  35. #include "../src/achown.h"
  36. #include "../src/chown.h"
  37. #define FILE_ARCHIVED FILE_ATTRIBUTE_ARCHIVE
  38. #define FILE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY
  39. #define FILE_HIDDEN FILE_ATTRIBUTE_HIDDEN
  40. #define FILE_READONLY FILE_ATTRIBUTE_READONLY
  41. #define FILE_SYSTEM FILE_ATTRIBUTE_SYSTEM
  42. #define mk_chmod(fname,st) SetFileAttributes(fname,st)
  43. static int single_set;
  44. struct Dlg_head *ch_dlg;
  45. #define PX 5
  46. #define PY 2
  47. #define FX 40
  48. #define FY 2
  49. #define BX 6
  50. #define BY 17
  51. #define TX 40
  52. #define TY 12
  53. #define PERMISSIONS 4
  54. #define BUTTONS 6
  55. #define B_MARKED B_USER
  56. #define B_ALL B_USER+1
  57. #define B_SETMRK B_USER+2
  58. #define B_CLRMRK B_USER+3
  59. int mode_change, need_update;
  60. int c_file, end_chmod;
  61. umode_t and_mask, or_mask, c_stat;
  62. char *c_fname, *c_fown, *c_fgrp, *c_fperm;
  63. int c_fsize;
  64. static WLabel *statl;
  65. static int normal_color;
  66. static int title_color;
  67. static int selection_color;
  68. /* bsedos.h */
  69. struct {
  70. mode_t mode;
  71. char *text;
  72. int selected;
  73. WCheck *check;
  74. } check_perm[PERMISSIONS] = {
  75. {
  76. FILE_ARCHIVED, N_("Archive"), 0, 0,
  77. },
  78. {
  79. FILE_READONLY, N_("Read Only"), 0, 0,
  80. },
  81. {
  82. FILE_HIDDEN, N_("Hidden"), 0, 0,
  83. },
  84. {
  85. FILE_SYSTEM, N_("System"), 0, 0,
  86. },
  87. };
  88. struct {
  89. int ret_cmd, flags, y, x;
  90. char *text;
  91. } chmod_but[BUTTONS] = {
  92. {
  93. B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel"),
  94. },
  95. {
  96. B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set"),
  97. },
  98. {
  99. B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked"),
  100. },
  101. {
  102. B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked"),
  103. },
  104. {
  105. B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all"),
  106. },
  107. {
  108. B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all"),
  109. },
  110. };
  111. static void chmod_toggle_select (void)
  112. {
  113. int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
  114. attrset (normal_color);
  115. check_perm[Id].selected ^= 1;
  116. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
  117. addch ((check_perm[Id].selected) ? '*' : ' ');
  118. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
  119. }
  120. static void chmod_refresh (void)
  121. {
  122. attrset (normal_color);
  123. dlg_erase (ch_dlg);
  124. draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
  125. draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
  126. draw_box (ch_dlg, FY, FX, 10, 25);
  127. dlg_move (ch_dlg, FY + 1, FX + 2);
  128. addstr (_("Name"));
  129. dlg_move (ch_dlg, FY + 3, FX + 2);
  130. addstr (_("Permissions (Octal)"));
  131. dlg_move (ch_dlg, FY + 5, FX + 2);
  132. addstr (_("Owner name"));
  133. dlg_move (ch_dlg, FY + 7, FX + 2);
  134. addstr (_("Group name"));
  135. attrset (title_color);
  136. dlg_move (ch_dlg, 1, 28);
  137. addstr (_(" Chmod command "));
  138. dlg_move (ch_dlg, PY, PX + 1);
  139. addstr (_(" Permission "));
  140. dlg_move (ch_dlg, FY, FX + 1);
  141. addstr (_(" File "));
  142. attrset (selection_color);
  143. dlg_move (ch_dlg, TY, TX);
  144. addstr (_("Use SPACE to change"));
  145. dlg_move (ch_dlg, TY + 1, TX);
  146. addstr (_("an option, ARROW KEYS"));
  147. dlg_move (ch_dlg, TY + 2, TX);
  148. addstr (_("to move between options"));
  149. dlg_move (ch_dlg, TY + 3, TX);
  150. addstr (_("and T or INS to mark"));
  151. }
  152. static int chmod_callback (Dlg_head *h, int Par, int Msg)
  153. {
  154. char buffer [10];
  155. switch (Msg) {
  156. case DLG_ACTION:
  157. if (Par >= BUTTONS - single_set * 2){
  158. c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
  159. sprintf (buffer, "%o", c_stat);
  160. label_set_text (statl, buffer);
  161. chmod_toggle_select ();
  162. mode_change = 1;
  163. }
  164. break;
  165. case DLG_KEY:
  166. if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
  167. ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
  168. chmod_toggle_select ();
  169. if (Par == KEY_IC)
  170. dlg_one_down (ch_dlg);
  171. return 1;
  172. }
  173. break;
  174. case DLG_DRAW:
  175. chmod_refresh ();
  176. break;
  177. }
  178. return 0;
  179. }
  180. static void init_chmod (void)
  181. {
  182. int i;
  183. do_refresh ();
  184. end_chmod = c_file = need_update = 0;
  185. single_set = (cpanel->marked < 2) ? 2 : 0;
  186. if (use_colors){
  187. normal_color = COLOR_NORMAL;
  188. title_color = COLOR_HOT_NORMAL;
  189. selection_color = COLOR_NORMAL;
  190. } else {
  191. normal_color = NORMAL_COLOR;
  192. title_color = SELECTED_COLOR;
  193. selection_color = SELECTED_COLOR;
  194. }
  195. ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
  196. chmod_callback, _("[Chmod]"), _("chmod"), DLG_CENTER);
  197. x_set_dialog_title (ch_dlg, _("Chmod command"));
  198. #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
  199. chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
  200. for (i = 0; i < BUTTONS; i++) {
  201. if (i == 2 && single_set)
  202. break;
  203. else
  204. add_widget (ch_dlg, button_new (XTRACT (i)));
  205. }
  206. #define XTRACT2(i) 0, check_perm [i].text, NULL
  207. for (i = 0; i < PERMISSIONS; i++) {
  208. check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
  209. XTRACT2 (i));
  210. add_widget (ch_dlg, check_perm[i].check);
  211. }
  212. }
  213. int pc_stat_file (char *filename)
  214. {
  215. mode_t st;
  216. st = GetFileAttributes (filename);
  217. if (st & FILE_DIRECTORY)
  218. st = -1;
  219. return st;
  220. }
  221. static void chmod_done (void)
  222. {
  223. if (need_update)
  224. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  225. repaint_screen ();
  226. }
  227. char *next_file (void)
  228. {
  229. while (!cpanel->dir.list[c_file].f.marked)
  230. c_file++;
  231. return cpanel->dir.list[c_file].fname;
  232. }
  233. static void do_chmod (mode_t sf)
  234. {
  235. sf &= and_mask;
  236. sf |= or_mask;
  237. mk_chmod(cpanel->dir.list[c_file].fname, sf);
  238. do_file_mark (cpanel, c_file, 0);
  239. }
  240. static void apply_mask (mode_t sf)
  241. {
  242. char *fname;
  243. mode_t sf_stat;
  244. need_update = end_chmod = 1;
  245. do_chmod (sf);
  246. do {
  247. fname = next_file ();
  248. if ((sf_stat = pc_stat_file (fname)) < 0)
  249. break;
  250. c_stat = sf_stat;
  251. do_chmod (c_stat);
  252. } while (cpanel->marked);
  253. }
  254. void chmod_cmd (void)
  255. {
  256. char buffer [10];
  257. char *fname;
  258. int i;
  259. mode_t sf_stat;
  260. do { /* do while any files remaining */
  261. init_chmod ();
  262. if (cpanel->marked)
  263. fname = next_file (); /* next marked file */
  264. else
  265. fname = selection (cpanel)->fname; /* single file */
  266. if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
  267. break;
  268. c_stat = sf_stat;
  269. mode_change = 0; /* clear changes flag */
  270. /* set check buttons */
  271. for (i = 0; i < PERMISSIONS; i++){
  272. check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
  273. check_perm[i].selected = 0;
  274. }
  275. /* Set the labels */
  276. c_fname = name_trunc (fname, 21);
  277. add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
  278. c_fown = _("unknown");
  279. add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
  280. c_fgrp = _("unknown");
  281. add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
  282. sprintf (buffer, "%o", c_stat);
  283. statl = label_new (FY+4, FX+2, buffer, NULL);
  284. add_widget (ch_dlg, statl);
  285. run_dlg (ch_dlg); /* retrieve an action */
  286. /* do action */
  287. switch (ch_dlg->ret_value){
  288. case B_ENTER:
  289. if (mode_change)
  290. mk_chmod (fname, c_stat); /*.ado */
  291. need_update = 1;
  292. break;
  293. case B_CANCEL:
  294. end_chmod = 1;
  295. break;
  296. case B_ALL:
  297. case B_MARKED:
  298. and_mask = or_mask = 0;
  299. and_mask = ~and_mask;
  300. for (i = 0; i < PERMISSIONS; i++) {
  301. if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
  302. if (check_perm[i].check->state & C_BOOL)
  303. or_mask |= check_perm[i].mode;
  304. else
  305. and_mask &= ~check_perm[i].mode;
  306. }
  307. apply_mask (sf_stat);
  308. break;
  309. case B_SETMRK:
  310. and_mask = or_mask = 0;
  311. and_mask = ~and_mask;
  312. for (i = 0; i < PERMISSIONS; i++) {
  313. if (check_perm[i].selected)
  314. or_mask |= check_perm[i].mode;
  315. }
  316. apply_mask (sf_stat);
  317. break;
  318. case B_CLRMRK:
  319. and_mask = or_mask = 0;
  320. and_mask = ~and_mask;
  321. for (i = 0; i < PERMISSIONS; i++) {
  322. if (check_perm[i].selected)
  323. and_mask &= ~check_perm[i].mode;
  324. }
  325. apply_mask (sf_stat);
  326. break;
  327. }
  328. if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
  329. do_file_mark (cpanel, c_file, 0);
  330. need_update = 1;
  331. }
  332. destroy_dlg (ch_dlg);
  333. } while (cpanel->marked && !end_chmod);
  334. chmod_done ();
  335. }