Traslated some chmod mgs related (for win32 support? not shure)
[midnight-commander.git] / pc / util_nt.c
blob4a069126c2114a445e745d84dafe9d752e244de3
1 /* Various utilities - NT versions
2 Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
4 Written 1994, 1995, 1996 by:
5 Juan Grigera, Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
6 Jakub Jelinek, Mauricio Plaza.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <sys/types.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <windows.h>
27 #include <io.h>
28 #include <fcntl.h>
29 #include <signal.h> /* my_system */
30 #include <limits.h> /* INT_MAX */
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <stdarg.h>
34 #include <process.h>
35 #include "../src/global.h"
36 #include "../src/util.h"
37 #include "util_win32.h"
39 #ifdef __BORLANDC__
40 #define ENOTEMPTY ERROR_DIR_NOT_EMPTY
41 #endif
43 char *get_owner (int uid)
45 return "none";
48 char *get_group (int gid)
50 return "none";
53 /* Pipes are guaranteed to be able to hold at least 4096 bytes */
54 /* More than that would be unportable */
55 #define MAX_PIPE_SIZE 4096
57 static int error_pipe[2]; /* File descriptors of error pipe */
58 static int old_error; /* File descriptor of old standard error */
60 /* Creates a pipe to hold standard error for a later analysis. */
61 /* The pipe can hold 4096 bytes. Make sure no more is written */
62 /* or a deadlock might occur. */
63 void open_error_pipe (void)
65 if (pipe (error_pipe) < 0){
66 message (0, " Warning ", " Pipe failed ");
68 old_error = dup (2);
69 if(old_error < 0 || close(2) || dup (error_pipe[1]) != 2){
70 message (0, " Warning ", " Dup failed ");
71 close (error_pipe[0]);
72 close (error_pipe[1]);
74 close (error_pipe[1]);
77 void close_error_pipe (int error, char *text)
79 char *title;
80 char msg[MAX_PIPE_SIZE];
81 int len = 0;
83 if (error)
84 title = " Error ";
85 else
86 title = " Warning ";
87 if (old_error >= 0){
88 close (2);
89 dup (old_error);
90 close (old_error);
91 len = read (error_pipe[0], msg, MAX_PIPE_SIZE);
93 if (len >= 0)
94 msg[len] = 0;
95 close (error_pipe[0]);
97 if (error < 0)
98 return; /* Just ignore error message */
99 if (text == NULL){
100 if (len == 0) return; /* Nothing to show */
102 /* Show message from pipe */
103 message (error, title, msg);
104 } else {
105 /* Show given text and possible message from pipe */
106 message (error, title, " %s \n %s ", text, msg);
110 void check_error_pipe (void)
112 char error[MAX_PIPE_SIZE];
113 int len = 0;
114 if (old_error >= 0){
115 while (len < MAX_PIPE_SIZE)
117 int rvalue;
119 rvalue = -1; // read (error_pipe[0], error + len, 1);
120 if (rvalue <= 0)
121 break;
122 len ++;
124 error[len] = 0;
125 close (error_pipe[0]);
127 if (len > 0)
128 message (0, " Warning ", error);
131 int my_system (int as_shell_command, const char *shell, const char *command)
133 int status = 0;
135 #if 0
136 /* .ado: temp. turn out */
137 if (as_shell_command) {
138 /* It is only the shell, /c will not work */
139 if (command)
140 spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
141 else
142 spawnlp (P_WAIT, shell, (char *) 0);
143 } else
144 spawnl (P_WAIT, shell, shell, command, (char *) 0);
146 if (win32_GetPlatform() == OS_Win95) {
147 SetConsoleTitle ("GNU Midnight Commander"); /* title is gone after spawn... */
149 #endif
150 if (as_shell_command) {
151 if (!access(command, 0)) {
152 switch(win32_GetEXEType (shell)) {
153 case EXE_win16: /* Windows 3.x archive or OS/2 */
154 case EXE_win32GUI: /* NT or Chicago GUI API */
155 spawnlp (P_NOWAIT, shell, shell, "/c", command, (char *) 0); /* don't wait for GUI programs to end */
156 break;
157 case EXE_otherCUI: /* DOS COM, MZ, ZM, Phar Lap */
158 case EXE_win32CUI: /* NT or Chicago Console API, also OS/2 */
159 case EXE_Unknown:
160 default:
161 spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
162 break;
165 else
166 spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
168 else
169 spawnl (P_WAIT, shell, shell, command, (char *) 0);
171 if (win32_GetPlatform() == OS_Win95) {
172 SetConsoleTitle ("GNU Midnight Commander"); /* title is gone after spawn... */
175 return status;
178 /* get_default_shell
179 Get the default shell for the current hardware platform
181 char* get_default_shell()
183 if (win32_GetPlatform() == OS_WinNT)
184 return "cmd.exe";
185 else
186 return "command.com";
189 char *tilde_expand (char *directory)
191 return strdup (directory);
194 /* sleep: Call Windows API.
195 Cannot do simple define. That would need <windows.h> in every source
197 #ifndef __EMX__
198 void sleep(unsigned long dwMiliSecs)
200 Sleep(dwMiliSecs);
202 #endif
204 /* Canonicalize path, and return a new path. Do everything in situ.
205 The new path differs from path in:
206 Multiple `/'s are collapsed to a single `/'.
207 Leading `./'s and trailing `/.'s are removed.
208 Trailing `/'s are removed.
209 Non-leading `../'s and trailing `..'s are handled by removing
210 portions of the path. */
211 char *canonicalize_pathname (char *path)
213 int i, start;
214 char stub_char;
216 stub_char = (*path == PATH_SEP) ? PATH_SEP : '.';
218 /* Walk along path looking for things to compact. */
219 i = 0;
220 for (;;) {
221 if (!path[i])
222 break;
224 while (path[i] && path[i] != PATH_SEP)
225 i++;
227 start = i++;
229 /* If we didn't find any slashes, then there is nothing left to do. */
230 if (!path[start])
231 break;
233 /* Handle multiple `/'s in a row. */
234 while (path[i] == PATH_SEP)
235 i++;
237 if ((start + 1) != i) {
238 strcpy (path + start + 1, path + i);
239 i = start + 1;
242 /* Check for trailing `/'. */
243 if (start && !path[i]) {
244 zero_last:
245 path[--i] = '\0';
246 break;
249 /* Check for `../', `./' or trailing `.' by itself. */
250 if (path[i] == '.') {
251 /* Handle trailing `.' by itself. */
252 if (!path[i + 1])
253 goto zero_last;
255 /* Handle `./'. */
256 if (path[i + 1] == PATH_SEP) {
257 strcpy (path + i, path + i + 1);
258 i = start;
259 continue;
262 /* Handle `../' or trailing `..' by itself.
263 Remove the previous ?/ part with the exception of
264 ../, which we should leave intact. */
265 if (path[i + 1] == '.' && (path[i + 2] == PATH_SEP || !path[i + 2])) {
266 while (--start > -1 && path[start] != PATH_SEP);
267 if (!strncmp (path + start + 1, "../", 3))
268 continue;
269 strcpy (path + start + 1, path + i + 2);
270 i = start;
271 continue;
276 if (!*path) {
277 *path = stub_char;
278 path[1] = '\0';
280 return path;
283 #ifndef USE_VFS
285 int mc_rmdir (char *path);
286 Fix for Win95 UGLY BUG in rmdir: it will return ENOACCESS instead
287 of ENOTEMPTY.
289 int mc_rmdir (char *path)
291 if (win32_GetPlatform() == OS_Win95) {
292 if (rmdir(path)) {
293 SetLastError (ERROR_DIR_NOT_EMPTY);
294 #ifndef __EMX__
295 /* FIXME: We are always saying the same thing! */
296 _doserrno = ERROR_DIR_NOT_EMPTY;
297 #endif
298 errno = ENOTEMPTY;
299 return -1;
300 } else
301 return 0;
303 else
304 return rmdir(path); /* No trouble in Windows NT */
307 static int conv_nt_unx_rc(int rc)
309 int errCode;
310 switch (rc) {
311 case ERROR_FILE_NOT_FOUND:
312 case ERROR_PATH_NOT_FOUND:
313 case ERROR_TOO_MANY_OPEN_FILES:
314 errCode = ENOENT;
315 break;
316 case ERROR_INVALID_HANDLE:
317 case ERROR_ARENA_TRASHED:
318 case ERROR_ACCESS_DENIED:
319 case ERROR_INVALID_ACCESS:
320 case ERROR_WRITE_PROTECT:
321 case ERROR_WRITE_FAULT:
322 case ERROR_READ_FAULT:
323 case ERROR_SHARING_VIOLATION:
324 errCode = EACCES;
325 break;
326 case ERROR_NOT_ENOUGH_MEMORY:
327 errCode = ENOMEM;
328 break;
329 case ERROR_INVALID_BLOCK:
330 case ERROR_INVALID_FUNCTION:
331 case ERROR_INVALID_DRIVE:
332 errCode = ENODEV;
333 break;
334 case ERROR_CURRENT_DIRECTORY:
335 errCode = ENOTDIR;
336 break;
337 case ERROR_NOT_READY:
338 errCode = EINVAL;
339 break;
340 default:
341 errCode = EINVAL;
342 break;
343 } /* endswitch */
344 return errCode;
348 int mc_unlink (char *pathName)
349 For Windows 95 and NT, files should be able to be deleted even
350 if they don't have write-protection. We should build a question box
351 like: Delete anyway? Yes <No> All
353 int mc_unlink (char *pathName)
355 char *fileName;
356 char *trunced_name;
357 static int erase_all = 0;
358 BOOL rc;
359 DWORD returnError;
361 rc = DeleteFile(pathName);
362 returnError = GetLastError();
363 if ((rc == FALSE) && (returnError == ERROR_ACCESS_DENIED)) {
364 int result;
365 if (!erase_all) {
366 errno = conv_nt_unx_rc(returnError);
367 trunced_name = name_trunc(pathName, 30);
368 fileName = (char *) malloc(strlen(trunced_name) + 16);
369 strcpy(fileName, "File ");
370 strcat(fileName, trunced_name);
371 strcat(fileName, " protected");
372 result = query_dialog(fileName, "Delete anyway?", 3, 3, " No ", " Yes ", " All in the future!");
373 free(fileName);
375 switch (result) {
376 case 0:
377 do_refresh ();
378 return -1;
379 case 1:
380 do_refresh ();
381 break;
382 case 2:
383 do_refresh ();
384 erase_all = 1;
385 break;
386 default:
387 do_refresh ();
388 return -1;
389 break;
393 chmod(pathName, S_IWRITE); /* make it writable */
394 rc = DeleteFile(pathName);
395 returnError = GetLastError();
396 if (rc == FALSE) {
397 errno = conv_nt_unx_rc(returnError);
398 return -1;
401 if (rc == TRUE) return 0;
402 else
403 return -1;
405 #endif /*USE_VFS*/
407 void my_statfs (struct my_statfs *myfs_stats, char *path)
409 int len = 0;
410 DWORD lpSectorsPerCluster, lpBytesPerSector, lpFreeClusters, lpClusters;
411 DWORD lpMaximumComponentLength, lpFileSystemFlags;
412 static char lpVolumeNameBuffer[256], lpFileSystemNameBuffer[30];
414 GetDiskFreeSpace(NULL, &lpSectorsPerCluster, &lpBytesPerSector,
415 &lpFreeClusters, &lpClusters);
417 /* KBytes available */
418 myfs_stats->avail = lpSectorsPerCluster * lpBytesPerSector * lpFreeClusters / 1024;
420 /* KBytes total */
421 myfs_stats->total = lpSectorsPerCluster * lpBytesPerSector * lpClusters / 1024;
422 myfs_stats->nfree = lpFreeClusters;
423 myfs_stats->nodes = lpClusters;
425 GetVolumeInformation(NULL, lpVolumeNameBuffer, 255, NULL,
426 &lpMaximumComponentLength, &lpFileSystemFlags,
427 lpFileSystemNameBuffer, 30);
429 myfs_stats->mpoint = lpFileSystemNameBuffer;
430 myfs_stats->device = lpVolumeNameBuffer;
433 myfs_stats->type = GetDriveType(NULL);
434 switch (myfs_stats->type) {
436 * mmm. DeviceIoControl may fail if you are not root case
437 * F5_1Pt2_512, 5.25", 1.2MB, 512 bytes/sector
438 * myfs_stats->typename = "5.25\" 1.2MB"; break; case
439 * F3_1Pt44_512, 3.5", 1.44MB, 512 bytes/sector
440 * myfs_stats->typename = "3.5\" 1.44MB"; break; case
441 * F3_2Pt88_512, 3.5", 2.88MB, 512 bytes/sector
442 * myfs_stats->typename = "3.5\" 2.88MB"; break; case
443 * F3_20Pt8_512, 3.5", 20.8MB, 512 bytes/sector
444 * myfs_stats->typename = "3.5\" 20.8MB"; break; case
445 * F3_720_512, 3.5", 720KB, 512 bytes/sector
446 * myfs_stats->typename = "3.5\" 720MB"; break; case
447 * F5_360_512, 5.25", 360KB, 512 bytes/sector
448 * myfs_stats->typename = "5.25\" 360KB"; break; case
449 * F5_320_512, 5.25", 320KB, 512 bytes/sector
450 * case F5_320_1024, 5.25", 320KB, 1024
451 * bytes/sector myfs_stats->typename = "5.25\" 320KB"; break;
452 * case F5_180_512, 5.25", 180KB, 512
453 * bytes/sector myfs_stats->typename = "5.25\" 180KB"; break;
454 * case F5_160_512, 5.25", 160KB, 512
455 * bytes/sector myfs_stats->typename = "5.25\" 160KB"; break;
456 * case RemovableMedia, Removable media other than
457 * floppy myfs_stats->typename = "Removable"; break; case
458 * FixedMedia Fixed hard disk media
459 * myfs_stats->typename = "Hard Disk"; break; case Unknown:
460 * Format is unknown
462 case DRIVE_REMOVABLE:
463 myfs_stats->typename = "Removable";
464 break;
465 case DRIVE_FIXED:
466 myfs_stats->typename = "Hard Disk";
467 break;
468 case DRIVE_REMOTE:
469 myfs_stats->typename = "Networked";
470 break;
471 case DRIVE_CDROM:
472 myfs_stats->typename = "CD-ROM";
473 break;
474 case DRIVE_RAMDISK:
475 myfs_stats->typename = "RAM disk";
476 break;
477 default:
478 myfs_stats->typename = "unknown";
479 break;
483 int gettimeofday (struct timeval* tvp, void *p)
485 if (p != NULL)
486 return 0;
488 /* Since MC only calls this func from get_random_hint we return
489 some value, not exactly the "correct" one */
490 tvp->tv_sec = GetTickCount()/1000; /* Number of milliseconds since Windows started*/
491 tvp->tv_usec = GetTickCount();
494 /* FAKE functions */
496 int
497 look_for_exe(const char* pathname)
499 int j;
500 char *p;
501 int lgh = strlen(pathname);
503 if (lgh < 4) {
504 return 0;
505 } else {
506 p = (char *) pathname;
507 for (j=0; j<lgh-4; j++) {
508 p++;
509 } /* endfor */
510 if (!stricmp(p, ".exe") ||
511 !stricmp(p, ".bat") ||
512 !stricmp(p, ".com") ||
513 !stricmp(p, ".cmd")) {
514 return 1;
517 return 0;
520 int
521 lstat (const char* pathname, struct stat *buffer)
523 int rc = stat (pathname, buffer);
524 #ifdef __BORLANDC__
525 if (rc == 0) {
526 if (!(buffer->st_mode & S_IFDIR)) {
527 if (!look_for_exe(pathname)) {
528 buffer->st_mode &= !S_IXUSR & !S_IXGRP & !S_IXOTH;
532 #endif
533 return rc;
536 int getuid ()
538 /* SID sid;
539 LookupAccountName (NULL, &sid...
540 return 0;
542 return 0;
545 int getgid ()
547 return 0;
550 int readlink (char* path, char* buf, int size)
552 return -1;
554 int symlink (char *n1, char *n2)
556 return -1;
558 int link (char *p1, char *p2)
560 return -1;
562 int chown (char *path, int owner, int group)
564 return -1;
566 int mknod (char *path, int mode, int dev)
568 return -1;
571 void init_uid_gid_cache (void)
573 return;
576 /* INHANDLE is a result of some mc_open call to any vfs, this function
577 returns a normal handle (to be used with read) of a pipe for reading
578 of the output of COMMAND with arguments ... (must include argv[0] as
579 well) which gets as its input at most INLEN bytes from the INHANDLE
580 using mc_read. You have to call mc_doublepclose to close the returned
581 handle afterwards. If INLEN is -1, we read as much as we can :) */
582 int mc_doublepopen (int inhandle, int inlen, pid_t *the_pid, char *command, ...)
584 int pipe0 [2], pipe1 [2], std_sav [2];
585 #define MAXARGS 16
586 int argno;
587 char *args[MAXARGS];
588 char buffer [8192];
589 int i;
590 va_list ap;
592 pid_t pid;
594 // Create the pipes
595 if(_pipe(pipe0, 8192, O_BINARY | O_NOINHERIT) == -1)
596 exit (1);
597 if(_pipe(pipe1, 8192, O_BINARY | O_NOINHERIT) == -1)
598 exit (1);
599 // Duplicate stdin/stdout handles (next line will close original)
600 std_sav[0] = _dup(_fileno(stdin));
601 std_sav[1] = _dup(_fileno(stdout));
602 // Duplicate read end of pipe0 to stdin handle
603 if(_dup2(pipe0[0], _fileno(stdin)) != 0)
604 exit (1);
605 // Duplicate write end of pipe1 to stdout handle
606 if(_dup2(pipe1[1], _fileno(stdout)) != 0)
607 exit (1);
608 // Close original read end of pipe0
609 close(pipe0[0]);
610 // Close original write end of pipe1
611 close(pipe1[1]);
613 va_start (ap, command);
614 argno = 0;
615 while ((args[argno++] = va_arg(ap, char *)) != NULL)
616 if (argno == (MAXARGS - 1)) {
617 args[argno] = NULL;
618 break;
620 va_end (ap);
621 // Spawn process
622 pid = spawnvp(P_NOWAIT,command, args);// argv[1], (const char* const*)&argv[1]);
623 if(!pid)
624 exit (1);
625 // Duplicate copy of original stdin back into stdin
626 if(_dup2(std_sav[0], _fileno(stdin)) != 0)
627 exit (1);
628 // Duplicate copy of original stdout back into stdout
629 if(_dup2(std_sav[1], _fileno(stdout)) != 0)
630 exit (1);
631 // Close duplicate copy of original stdout and stdin
632 close(std_sav[0]);
633 close(std_sav[1]);
636 while ((i = _read (inhandle, buffer,
637 (inlen == -1 || inlen > 8192)
638 ? 8192 : inlen)) > 0) {
639 write (pipe0 [1], buffer, i);
640 if (inlen != -1) {
641 inlen -= i;
642 if (!inlen)
643 break;
646 close (pipe0 [1]);
647 *the_pid = pid;
648 return pipe1 [0];
652 int mc_doublepclose (int pipe, pid_t pid)
654 int status = 0;
656 close (pipe);
657 _cwait ( &status, pid, 0);
658 return status;
661 /*hacks to get it compile, remove these after vfs works */
663 /*hacks to get it compile, remove these after vfs works */
664 #ifndef USE_VFS
665 char *vfs_get_current_dir (void)
667 return NULL;
670 int vfs_current_is_extfs (void)
672 return 0;
675 int vfs_file_is_ftp (char *filename)
677 return 0;
680 int mc_utime (char *path, void *times)
682 return 0;
686 void extfs_run (char *file)
688 return;
690 #endif
692 char *
693 get_default_editor (void)
695 return "notepad.exe";
699 errno_dir_not_empty (int err)
701 if (err == ENOTEMPTY || err == EEXIST || err == EACCES)
702 return 1;
703 return 0;
706 /* The MC library directory is by default the directory where mc.exe
707 is situated. It is possible to specify this directory via MCHOME
708 environment variable */
709 char *
710 get_mc_lib_dir ()
712 char *cur;
713 char *mchome = getenv("MCHOME");
715 if (mchome && *mchome)
716 return mchome;
717 mchome = malloc(MC_MAXPATHLEN);
718 GetModuleFileName(NULL, mchome, MC_MAXPATHLEN);
719 for (cur = mchome + strlen(mchome); \
720 (cur > mchome) && (*cur != PATH_SEP); cur--);
721 *cur = 0;
722 cur = strdup(mchome);
723 free(mchome);
724 if (!cur || !*cur) {
725 free(cur);
726 return "C:\\MC";
728 return cur;
730 int get_user_rights (struct stat *buf)
732 return 2;
734 void init_groups (void)
737 void delete_groups (void)