chmod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #ifdef __os2__
  16. #define INCL_DOSFILEMGR
  17. #include <os2.h>
  18. #endif
  19. #ifdef _OS_NT
  20. #include <windows.h>
  21. #endif
  22. #include <string.h>
  23. #include <stdio.h>
  24. /* for chmod and stat */
  25. #include <io.h>
  26. #include <sys\types.h>
  27. #include <sys\stat.h>
  28. #include "../src/tty.h"
  29. #include "../src/mad.h"
  30. #include "../src/util.h"
  31. #include "../src/win.h"
  32. #include "../src/color.h"
  33. #include "../src/dlg.h"
  34. #include "../src/widget.h"
  35. #include "../src/dialog.h" /* For do_refresh() */
  36. #include "../src/dir.h"
  37. #include "../src/panel.h" /* Needed for the externs */
  38. #include "../src/file.h"
  39. #include "../src/main.h"
  40. #include "../src/chmod.h"
  41. #include "../src/achown.h"
  42. #include "../src/chown.h"
  43. #ifdef _OS_NT
  44. #define FILE_ARCHIVED FILE_ATTRIBUTE_ARCHIVE
  45. #define FILE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY
  46. #define FILE_HIDDEN FILE_ATTRIBUTE_HIDDEN
  47. #define FILE_READONLY FILE_ATTRIBUTE_READONLY
  48. #define FILE_SYSTEM FILE_ATTRIBUTE_SYSTEM
  49. #define mk_chmod(fname,st) SetFileAttributes(fname,st)
  50. #endif
  51. static int single_set;
  52. struct Dlg_head *ch_dlg;
  53. #define PX 5
  54. #define PY 2
  55. #define FX 40
  56. #define FY 2
  57. #define BX 6
  58. #define BY 17
  59. #define TX 40
  60. #define TY 12
  61. #define PERMISSIONS 4
  62. #define BUTTONS 6
  63. #define B_MARKED B_USER
  64. #define B_ALL B_USER+1
  65. #define B_SETMRK B_USER+2
  66. #define B_CLRMRK B_USER+3
  67. int mode_change, need_update;
  68. int c_file, end_chmod;
  69. umode_t and_mask, or_mask, c_stat;
  70. char *c_fname, *c_fown, *c_fgrp, *c_fperm;
  71. int c_fsize;
  72. static WLabel *statl;
  73. static int normal_color;
  74. static int title_color;
  75. static int selection_color;
  76. /* bsedos.h */
  77. struct {
  78. mode_t mode;
  79. char *text;
  80. int selected;
  81. WCheck *check;
  82. } check_perm[PERMISSIONS] = {
  83. {
  84. FILE_ARCHIVED, "Archive", 0, 0,
  85. },
  86. {
  87. FILE_READONLY, "Read Only", 0, 0,
  88. },
  89. {
  90. FILE_HIDDEN, "Hidden", 0, 0,
  91. },
  92. {
  93. FILE_SYSTEM, "System", 0, 0,
  94. },
  95. };
  96. struct {
  97. int ret_cmd, flags, y, x;
  98. char *text;
  99. } chmod_but[BUTTONS] = {
  100. {
  101. B_CANCEL, NORMAL_BUTTON, 2, 33, "&Cancel",
  102. },
  103. {
  104. B_ENTER, DEFPUSH_BUTTON, 2, 17, "&Set",
  105. },
  106. {
  107. B_CLRMRK, NORMAL_BUTTON, 0, 42, "C&lear marked",
  108. },
  109. {
  110. B_SETMRK, NORMAL_BUTTON, 0, 27, "S&et marked",
  111. },
  112. {
  113. B_MARKED, NORMAL_BUTTON, 0, 12, "&Marked all",
  114. },
  115. {
  116. B_ALL, NORMAL_BUTTON, 0, 0, "Set &all",
  117. },
  118. };
  119. static void chmod_toggle_select (void)
  120. {
  121. int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
  122. attrset (normal_color);
  123. check_perm[Id].selected ^= 1;
  124. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
  125. addch ((check_perm[Id].selected) ? '*' : ' ');
  126. dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
  127. }
  128. static void chmod_refresh (void)
  129. {
  130. attrset (normal_color);
  131. dlg_erase (ch_dlg);
  132. draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
  133. draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
  134. draw_box (ch_dlg, FY, FX, 10, 25);
  135. dlg_move (ch_dlg, FY + 1, FX + 2);
  136. addstr ("Name");
  137. dlg_move (ch_dlg, FY + 3, FX + 2);
  138. addstr ("Permissions (Octal)");
  139. dlg_move (ch_dlg, FY + 5, FX + 2);
  140. addstr ("Owner name");
  141. dlg_move (ch_dlg, FY + 7, FX + 2);
  142. addstr ("Group name");
  143. attrset (title_color);
  144. dlg_move (ch_dlg, 1, 28);
  145. addstr (" Chmod command ");
  146. dlg_move (ch_dlg, PY, PX + 1);
  147. addstr (" Permission ");
  148. dlg_move (ch_dlg, FY, FX + 1);
  149. addstr (" File ");
  150. attrset (selection_color);
  151. dlg_move (ch_dlg, TY, TX);
  152. addstr ("Use SPACE to change");
  153. dlg_move (ch_dlg, TY + 1, TX);
  154. addstr ("an option, ARROW KEYS");
  155. dlg_move (ch_dlg, TY + 2, TX);
  156. addstr ("to move between options");
  157. dlg_move (ch_dlg, TY + 3, TX);
  158. addstr ("and T or INS to mark");
  159. }
  160. static int chmod_callback (Dlg_head *h, int Par, int Msg)
  161. {
  162. char buffer [10];
  163. switch (Msg) {
  164. case DLG_ACTION:
  165. if (Par >= BUTTONS - single_set * 2){
  166. c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
  167. sprintf (buffer, "%o", c_stat);
  168. label_set_text (statl, buffer);
  169. chmod_toggle_select ();
  170. mode_change = 1;
  171. }
  172. break;
  173. case DLG_KEY:
  174. if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
  175. ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
  176. chmod_toggle_select ();
  177. if (Par == KEY_IC)
  178. dlg_one_down (ch_dlg);
  179. return 1;
  180. }
  181. break;
  182. #ifndef HAVE_X
  183. case DLG_DRAW:
  184. chmod_refresh ();
  185. break;
  186. #endif
  187. }
  188. return 0;
  189. }
  190. static void init_chmod (void)
  191. {
  192. int i;
  193. do_refresh ();
  194. end_chmod = c_file = need_update = 0;
  195. single_set = (cpanel->marked < 2) ? 2 : 0;
  196. if (use_colors){
  197. normal_color = COLOR_NORMAL;
  198. title_color = COLOR_HOT_NORMAL;
  199. selection_color = COLOR_NORMAL;
  200. } else {
  201. normal_color = NORMAL_COLOR;
  202. title_color = SELECTED_COLOR;
  203. selection_color = SELECTED_COLOR;
  204. }
  205. ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
  206. chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
  207. x_set_dialog_title (ch_dlg, "Chmod command");
  208. #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
  209. chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
  210. for (i = 0; i < BUTTONS; i++) {
  211. if (i == 2 && single_set)
  212. break;
  213. else
  214. add_widgetl (ch_dlg, button_new (XTRACT (i)), XV_WLAY_RIGHTOF);
  215. }
  216. #define XTRACT2(i) 0, check_perm [i].text, NULL
  217. for (i = 0; i < PERMISSIONS; i++) {
  218. check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
  219. XTRACT2 (i));
  220. add_widget (ch_dlg, check_perm[i].check);
  221. }
  222. }
  223. int pc_stat_file (char *filename)
  224. {
  225. mode_t st;
  226. #ifdef _OS_NT
  227. st = GetFileAttributes (filename);
  228. #endif /* _OS_NT */
  229. #ifdef __os2__
  230. HFILE fHandle = 0L;
  231. ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
  232. FILESTATUS3 fInfoBuf;
  233. ULONG fInfoBufSize;
  234. ULONG fAction = 0;
  235. APIRET rc;
  236. fInfoBufSize = sizeof(FILESTATUS3);
  237. rc = DosOpen((PSZ) filename,
  238. &fHandle,
  239. &fAction,
  240. (ULONG) 0,
  241. FILE_NORMAL,
  242. OPEN_ACTION_OPEN_IF_EXISTS,
  243. (OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE),
  244. (PEAOP2) NULL);
  245. if (rc != 0) {
  246. return -1;
  247. }
  248. rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
  249. DosClose(fHandle);
  250. if (rc != 0) {
  251. return -1; /* error ! */
  252. } else {
  253. st = fInfoBuf.attrFile;
  254. }
  255. #endif /* __os2__ */
  256. if (st & FILE_DIRECTORY)
  257. st = -1;
  258. return st;
  259. }
  260. static void chmod_done (void)
  261. {
  262. if (need_update)
  263. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  264. repaint_screen ();
  265. }
  266. char *next_file (void)
  267. {
  268. while (!cpanel->dir.list[c_file].f.marked)
  269. c_file++;
  270. return cpanel->dir.list[c_file].fname;
  271. }
  272. #ifdef __os2__
  273. static int mk_chmod (char *filename, ULONG st)
  274. {
  275. HFILE fHandle = 0L;
  276. ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
  277. FILESTATUS3 fInfoBuf;
  278. ULONG fInfoBufSize;
  279. ULONG fAction = 0L;
  280. APIRET rc;
  281. if (!(st & FILE_READONLY))
  282. chmod(filename, (S_IWRITE | S_IREAD));
  283. fInfoBufSize = sizeof(FILESTATUS3);
  284. rc = DosOpen((PSZ) filename,
  285. &fHandle,
  286. &fAction,
  287. (ULONG) 0,
  288. FILE_NORMAL,
  289. OPEN_ACTION_OPEN_IF_EXISTS,
  290. (OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE),
  291. 0L);
  292. if (rc != 0) {
  293. return rc;
  294. }
  295. rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
  296. if (rc!=0) {
  297. DosClose(fHandle);
  298. return rc;
  299. }
  300. fInfoBuf.attrFile = st;
  301. rc = DosSetFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
  302. rc = DosClose(fHandle);
  303. return rc;
  304. }
  305. #endif /* __os2__ */
  306. static void do_chmod (mode_t sf)
  307. {
  308. sf &= and_mask;
  309. sf |= or_mask;
  310. mk_chmod(cpanel->dir.list[c_file].fname, sf);
  311. do_file_mark (cpanel, c_file, 0);
  312. }
  313. static void apply_mask (mode_t sf)
  314. {
  315. char *fname;
  316. mode_t sf_stat;
  317. need_update = end_chmod = 1;
  318. do_chmod (sf);
  319. do {
  320. fname = next_file ();
  321. if ((sf_stat = pc_stat_file (fname)) < 0)
  322. break;
  323. c_stat = sf_stat;
  324. do_chmod (c_stat);
  325. } while (cpanel->marked);
  326. }
  327. void chmod_cmd (void)
  328. {
  329. char buffer [10];
  330. char *fname;
  331. int i;
  332. mode_t sf_stat;
  333. do { /* do while any files remaining */
  334. init_chmod ();
  335. if (cpanel->marked)
  336. fname = next_file (); /* next marked file */
  337. else
  338. fname = selection (cpanel)->fname; /* single file */
  339. if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
  340. break;
  341. c_stat = sf_stat;
  342. mode_change = 0; /* clear changes flag */
  343. /* set check buttons */
  344. for (i = 0; i < PERMISSIONS; i++){
  345. check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
  346. check_perm[i].selected = 0;
  347. }
  348. /* Set the labels */
  349. c_fname = name_trunc (fname, 21);
  350. add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
  351. c_fown = "unknown";
  352. add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
  353. c_fgrp = "unknown";
  354. add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
  355. sprintf (buffer, "%o", c_stat);
  356. statl = label_new (FY+4, FX+2, buffer, NULL);
  357. add_widget (ch_dlg, statl);
  358. run_dlg (ch_dlg); /* retrieve an action */
  359. /* do action */
  360. switch (ch_dlg->ret_value){
  361. case B_ENTER:
  362. if (mode_change)
  363. mk_chmod (fname, c_stat); /*.ado */
  364. need_update = 1;
  365. break;
  366. case B_CANCEL:
  367. end_chmod = 1;
  368. break;
  369. case B_ALL:
  370. case B_MARKED:
  371. and_mask = or_mask = 0;
  372. and_mask = ~and_mask;
  373. for (i = 0; i < PERMISSIONS; i++) {
  374. if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
  375. if (check_perm[i].check->state & C_BOOL)
  376. or_mask |= check_perm[i].mode;
  377. else
  378. and_mask &= ~check_perm[i].mode;
  379. }
  380. apply_mask (sf_stat);
  381. break;
  382. case B_SETMRK:
  383. and_mask = or_mask = 0;
  384. and_mask = ~and_mask;
  385. for (i = 0; i < PERMISSIONS; i++) {
  386. if (check_perm[i].selected)
  387. or_mask |= check_perm[i].mode;
  388. }
  389. apply_mask (sf_stat);
  390. break;
  391. case B_CLRMRK:
  392. and_mask = or_mask = 0;
  393. and_mask = ~and_mask;
  394. for (i = 0; i < PERMISSIONS; i++) {
  395. if (check_perm[i].selected)
  396. and_mask &= ~check_perm[i].mode;
  397. }
  398. apply_mask (sf_stat);
  399. break;
  400. }
  401. if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
  402. do_file_mark (cpanel, c_file, 0);
  403. need_update = 1;
  404. }
  405. destroy_dlg (ch_dlg);
  406. } while (cpanel->marked && !end_chmod);
  407. chmod_done ();
  408. }