chmod.nt.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Chmod command for Windows NT operating system
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. Notes:
  14. - Preliminar. Work to be done here. (Maybe should write a command to
  15. change attributes and another to change rwx permissions.
  16. */
  17. #include <config.h>
  18. #ifndef _OS_NT
  19. #error This file is for the NT operating system.
  20. #else
  21. #include <windows.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "tty.h"
  25. #include "mad.h"
  26. #include "util.h"
  27. #include "win.h"
  28. #include "color.h"
  29. #include "dlg.h"
  30. #include "widget.h"
  31. #include "dialog.h" /* For do_refresh() */
  32. #include "dir.h"
  33. #include "panel.h" /* Needed for the externs */
  34. #include "file.h"
  35. #include "main.h"
  36. #include "chmod.h"
  37. #include "achown.h"
  38. #include "chown.h"
  39. static int single_set;
  40. struct Dlg_head *ch_dlg;
  41. #define PX 5
  42. #define PY 2
  43. #define FX 40
  44. #define FY 2
  45. #define BX 6
  46. #define BY 17
  47. #define TX 40
  48. #define TY 12
  49. #define PERMISSIONS 4
  50. #define BUTTONS 6
  51. #define B_MARKED B_USER
  52. #define B_ALL B_USER+1
  53. #define B_SETMRK B_USER+2
  54. #define B_CLRMRK B_USER+3
  55. int mode_change, need_update;
  56. int c_file, end_chmod;
  57. umode_t and_mask, or_mask, c_stat;
  58. char *c_fname, *c_fown, *c_fgrp, *c_fperm;
  59. int c_fsize;
  60. static WLabel *statl;
  61. static int normal_color;
  62. static int title_color;
  63. static int selection_color;
  64. struct {
  65. mode_t mode;
  66. char *text;
  67. int selected;
  68. WCheck *check;
  69. } check_perm[PERMISSIONS] = {
  70. {
  71. FILE_ATTRIBUTE_ARCHIVE, "Archive", 0, 0,
  72. },
  73. {
  74. FILE_ATTRIBUTE_READONLY, "Read Only", 0, 0,
  75. },
  76. {
  77. FILE_ATTRIBUTE_HIDDEN, "Hidden", 0, 0,
  78. },
  79. {
  80. FILE_ATTRIBUTE_SYSTEM, "System", 0, 0,
  81. },
  82. /* {
  83. S_IWGRP, "write by group", 0, 0,
  84. },
  85. {
  86. S_IRGRP, "read by group", 0, 0,
  87. },
  88. {
  89. S_IXUSR, "execute/search by owner", 0, 0,
  90. },
  91. {
  92. S_IWUSR, "write by owner", 0, 0,
  93. },
  94. {
  95. S_IRUSR, "read by owner", 0, 0,
  96. },
  97. */
  98. };
  99. struct {
  100. int ret_cmd, flags, y, x;
  101. char *text;
  102. } chmod_but[BUTTONS] = {
  103. {
  104. B_CANCEL, NORMAL_BUTTON, 2, 33, "&Cancel",
  105. },
  106. {
  107. B_ENTER, DEFPUSH_BUTTON, 2, 17, "&Set",
  108. },
  109. {
  110. B_CLRMRK, NORMAL_BUTTON, 0, 42, "C&lear marked",
  111. },
  112. {
  113. B_SETMRK, NORMAL_BUTTON, 0, 27, "S&et marked",
  114. },
  115. {
  116. B_MARKED, NORMAL_BUTTON, 0, 12, "&Marked all",
  117. },
  118. {
  119. B_ALL, NORMAL_BUTTON, 0, 0, "Set &all",
  120. },
  121. };
  122. static void chmod_toggle_select (void)
  123. {
  124. int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
  125. attrset (normal_color);
  126. check_perm[Id].selected ^= 1;
  127. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
  128. addch ((check_perm[Id].selected) ? '*' : ' ');
  129. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
  130. }
  131. static void chmod_refresh (void)
  132. {
  133. attrset (normal_color);
  134. dlg_erase (ch_dlg);
  135. draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
  136. draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
  137. draw_box (ch_dlg, FY, FX, 10, 25);
  138. dlg_move (ch_dlg, FY + 1, FX + 2);
  139. addstr ("Name");
  140. dlg_move (ch_dlg, FY + 3, FX + 2);
  141. addstr ("Permissions (Octal)");
  142. dlg_move (ch_dlg, FY + 5, FX + 2);
  143. addstr ("Owner name");
  144. dlg_move (ch_dlg, FY + 7, FX + 2);
  145. addstr ("Group name");
  146. attrset (title_color);
  147. dlg_move (ch_dlg, 1, 28);
  148. addstr (" Chmod command ");
  149. dlg_move (ch_dlg, PY, PX + 1);
  150. addstr (" Permission ");
  151. dlg_move (ch_dlg, FY, FX + 1);
  152. addstr (" File ");
  153. attrset (selection_color);
  154. dlg_move (ch_dlg, TY, TX);
  155. addstr ("Use SPACE to change");
  156. dlg_move (ch_dlg, TY + 1, TX);
  157. addstr ("an option, ARROW KEYS");
  158. dlg_move (ch_dlg, TY + 2, TX);
  159. addstr ("to move between options");
  160. dlg_move (ch_dlg, TY + 3, TX);
  161. addstr ("and T or INS to mark");
  162. }
  163. static int chmod_callback (Dlg_head *h, int Par, int Msg)
  164. {
  165. char buffer [10];
  166. switch (Msg) {
  167. case DLG_ACTION:
  168. if (Par >= BUTTONS - single_set * 2){
  169. c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
  170. sprintf (buffer, "%o", c_stat);
  171. label_set_text (statl, buffer);
  172. chmod_toggle_select ();
  173. mode_change = 1;
  174. }
  175. break;
  176. case DLG_KEY:
  177. if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
  178. ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
  179. chmod_toggle_select ();
  180. if (Par == KEY_IC)
  181. dlg_one_down (ch_dlg);
  182. return 1;
  183. }
  184. break;
  185. #ifndef HAVE_X
  186. case DLG_DRAW:
  187. chmod_refresh ();
  188. break;
  189. #endif
  190. }
  191. return 0;
  192. }
  193. static void init_chmod (void)
  194. {
  195. int i;
  196. do_refresh ();
  197. end_chmod = c_file = need_update = 0;
  198. single_set = (cpanel->marked < 2) ? 2 : 0;
  199. if (use_colors){
  200. normal_color = COLOR_NORMAL;
  201. title_color = COLOR_HOT_NORMAL;
  202. selection_color = COLOR_NORMAL;
  203. } else {
  204. normal_color = NORMAL_COLOR;
  205. title_color = SELECTED_COLOR;
  206. selection_color = SELECTED_COLOR;
  207. }
  208. ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
  209. chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
  210. x_set_dialog_title (ch_dlg, "Chmod command");
  211. #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
  212. chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
  213. tk_new_frame (ch_dlg, "b.");
  214. for (i = 0; i < BUTTONS; i++) {
  215. if (i == 2 && single_set)
  216. break;
  217. else
  218. add_widgetl (ch_dlg, button_new (XTRACT (i)), XV_WLAY_RIGHTOF);
  219. }
  220. #define XTRACT2(i) 0, check_perm [i].text, NULL
  221. tk_new_frame (ch_dlg, "c.");
  222. for (i = 0; i < PERMISSIONS; i++) {
  223. check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
  224. XTRACT2 (i));
  225. add_widget (ch_dlg, check_perm[i].check);
  226. }
  227. }
  228. static int stat_file (char *filename, struct stat *st)
  229. {
  230. // Note: - we assume attribute values fit in st.st_mode (a word, not a dword)
  231. // - we change st_mode value with attributes, not RWX permissions
  232. if (stat (filename, st))
  233. return 0;
  234. st->st_mode = GetFileAttributes (filename);
  235. if (st->st_mode & FILE_ATTRIBUTE_DIRECTORY)
  236. return 0;
  237. return 1;
  238. }
  239. static void chmod_done (void)
  240. {
  241. if (need_update)
  242. update_panels (UP_OPTIMIZE, UP_KEEPSEL, UP_KEEPSEL);
  243. repaint_screen ();
  244. }
  245. char *next_file (void)
  246. {
  247. while (!cpanel->dir.list[c_file].f.marked)
  248. c_file++;
  249. return cpanel->dir.list[c_file].fname;
  250. }
  251. static void do_chmod (struct stat *sf)
  252. {
  253. sf->st_mode &= and_mask;
  254. sf->st_mode |= or_mask;
  255. SetFileAttributes (cpanel->dir.list [c_file].fname, sf->st_mode);
  256. do_file_mark (cpanel, c_file, 0);
  257. }
  258. static void apply_mask (struct stat *sf)
  259. {
  260. char *fname;
  261. need_update = end_chmod = 1;
  262. do_chmod (sf);
  263. do {
  264. fname = next_file ();
  265. if (!stat_file (fname, sf))
  266. return;
  267. c_stat = sf->st_mode;
  268. do_chmod (sf);
  269. } while (cpanel->marked);
  270. }
  271. static int my_chmod (char *path, int mode)
  272. {
  273. SetFileAttributes(path, mode);
  274. return (GetFileAttributes(path) != mode);
  275. }
  276. void chmod_cmd (void)
  277. {
  278. char buffer [10];
  279. char *fname;
  280. int i;
  281. struct stat sf_stat;
  282. do { /* do while any files remaining */
  283. init_chmod ();
  284. if (cpanel->marked)
  285. fname = next_file (); /* next marked file */
  286. else
  287. fname = selection (cpanel)->fname; /* single file */
  288. if (!stat_file (fname, &sf_stat)) /* get status of file */
  289. break;
  290. c_stat = sf_stat.st_mode;
  291. mode_change = 0; /* clear changes flag */
  292. /* set check buttons */
  293. for (i = 0; i < PERMISSIONS; i++){
  294. check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
  295. check_perm[i].selected = 0;
  296. }
  297. tk_new_frame (ch_dlg, "l.");
  298. /* Set the labels */
  299. c_fname = name_trunc (fname, 21);
  300. add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
  301. c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
  302. add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
  303. c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
  304. add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
  305. sprintf (buffer, "%o", c_stat);
  306. statl = label_new (FY+4, FX+2, buffer, NULL);
  307. add_widget (ch_dlg, statl);
  308. tk_end_frame ();
  309. run_dlg (ch_dlg); /* retrieve an action */
  310. /* do action */
  311. switch (ch_dlg->ret_value){
  312. case B_ENTER:
  313. if (mode_change)
  314. if (my_chmod (fname, c_stat))
  315. message(1, "Chmod", "Change mode failed!");
  316. need_update = 1;
  317. break;
  318. case B_CANCEL:
  319. end_chmod = 1;
  320. break;
  321. case B_ALL:
  322. case B_MARKED:
  323. and_mask = or_mask = 0;
  324. and_mask = ~and_mask;
  325. for (i = 0; i < PERMISSIONS; i++) {
  326. if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
  327. if (check_perm[i].check->state & C_BOOL)
  328. or_mask |= check_perm[i].mode;
  329. else
  330. and_mask &= ~check_perm[i].mode;
  331. }
  332. apply_mask (&sf_stat);
  333. break;
  334. case B_SETMRK:
  335. and_mask = or_mask = 0;
  336. and_mask = ~and_mask;
  337. for (i = 0; i < PERMISSIONS; i++) {
  338. if (check_perm[i].selected)
  339. or_mask |= check_perm[i].mode;
  340. }
  341. apply_mask (&sf_stat);
  342. break;
  343. case B_CLRMRK:
  344. and_mask = or_mask = 0;
  345. and_mask = ~and_mask;
  346. for (i = 0; i < PERMISSIONS; i++) {
  347. if (check_perm[i].selected)
  348. and_mask &= ~check_perm[i].mode;
  349. }
  350. apply_mask (&sf_stat);
  351. break;
  352. }
  353. if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
  354. do_file_mark (cpanel, c_file, 0);
  355. need_update = 1;
  356. }
  357. destroy_dlg (ch_dlg);
  358. } while (cpanel->marked && !end_chmod);
  359. chmod_done ();
  360. }
  361. #endif /*_OS_NT*/