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. */
23 #include <sys/types.h>
29 #include <signal.h> /* my_system */
30 #include <limits.h> /* INT_MAX */
32 #include <sys/time.h> /* select: timeout */
33 #include <sys/param.h>
37 #include "../src/fs.h"
38 #include "../src/util.h"
39 #include "util_win32.h"
42 #define ENOTEMPTY ERROR_DIR_NOT_EMPTY
45 char *get_owner (int uid
)
50 char *get_group (int gid
)
55 /* Pipes are guaranteed to be able to hold at least 4096 bytes */
56 /* More than that would be unportable */
57 #define MAX_PIPE_SIZE 4096
59 static int error_pipe
[2]; /* File descriptors of error pipe */
60 static int old_error
; /* File descriptor of old standard error */
62 /* Creates a pipe to hold standard error for a later analysis. */
63 /* The pipe can hold 4096 bytes. Make sure no more is written */
64 /* or a deadlock might occur. */
65 void open_error_pipe (void)
67 if (pipe (error_pipe
) < 0){
68 message (0, " Warning ", " Pipe failed ");
71 if(old_error
< 0 || close(2) || dup (error_pipe
[1]) != 2){
72 message (0, " Warning ", " Dup failed ");
73 close (error_pipe
[0]);
74 close (error_pipe
[1]);
76 close (error_pipe
[1]);
79 void close_error_pipe (int error
, char *text
)
82 char msg
[MAX_PIPE_SIZE
];
93 len
= read (error_pipe
[0], msg
, MAX_PIPE_SIZE
);
97 close (error_pipe
[0]);
100 return; /* Just ignore error message */
102 if (len
== 0) return; /* Nothing to show */
104 /* Show message from pipe */
105 message (error
, title
, msg
);
107 /* Show given text and possible message from pipe */
108 message (error
, title
, " %s \n %s ", text
, msg
);
112 void check_error_pipe (void)
114 char error
[MAX_PIPE_SIZE
];
117 while (len
< MAX_PIPE_SIZE
)
121 rvalue
= -1; // read (error_pipe[0], error + len, 1);
127 close (error_pipe
[0]);
130 message (0, " Warning ", error
);
133 int my_system (int as_shell_command
, const char *shell
, const char *command
)
138 /* .ado: temp. turn out */
139 if (as_shell_command
) {
140 /* It is only the shell, /c will not work */
142 spawnlp (P_WAIT
, shell
, shell
, "/c", command
, (char *) 0);
144 spawnlp (P_WAIT
, shell
, (char *) 0);
146 spawnl (P_WAIT
, shell
, shell
, command
, (char *) 0);
148 if (win32_GetPlatform() == OS_Win95
) {
149 SetConsoleTitle ("GNU Midnight Commander"); /* title is gone after spawn... */
152 if (as_shell_command
) {
153 if (!access(command
, 0)) {
154 switch(win32_GetEXEType (shell
)) {
155 case EXE_win16
: /* Windows 3.x archive or OS/2 */
156 case EXE_win32GUI
: /* NT or Chicago GUI API */
157 spawnlp (P_NOWAIT
, shell
, shell
, "/c", command
, (char *) 0); /* don't wait for GUI programs to end */
159 case EXE_otherCUI
: /* DOS COM, MZ, ZM, Phar Lap */
160 case EXE_win32CUI
: /* NT or Chicago Console API, also OS/2 */
163 spawnlp (P_WAIT
, shell
, shell
, "/c", command
, (char *) 0);
168 spawnlp (P_WAIT
, shell
, shell
, "/c", command
, (char *) 0);
171 spawnl (P_WAIT
, shell
, shell
, command
, (char *) 0);
173 if (win32_GetPlatform() == OS_Win95
) {
174 SetConsoleTitle ("GNU Midnight Commander"); /* title is gone after spawn... */
181 Get the default shell for the current hardware platform
183 char* get_default_shell()
185 if (win32_GetPlatform() == OS_WinNT
)
188 return "command.com";
191 char *tilde_expand (char *directory
)
193 return strdup (directory
);
196 /* sleep: Call Windows API.
197 Cannot do simple define. That would need <windows.h> in every source
200 void sleep(unsigned long dwMiliSecs
)
206 /* Canonicalize path, and return a new path. Do everything in situ.
207 The new path differs from path in:
208 Multiple `/'s are collapsed to a single `/'.
209 Leading `./'s and trailing `/.'s are removed.
210 Trailing `/'s are removed.
211 Non-leading `../'s and trailing `..'s are handled by removing
212 portions of the path. */
213 char *canonicalize_pathname (char *path
)
218 stub_char
= (*path
== PATH_SEP
) ? PATH_SEP
: '.';
220 /* Walk along path looking for things to compact. */
226 while (path
[i
] && path
[i
] != PATH_SEP
)
231 /* If we didn't find any slashes, then there is nothing left to do. */
235 /* Handle multiple `/'s in a row. */
236 while (path
[i
] == PATH_SEP
)
239 if ((start
+ 1) != i
) {
240 strcpy (path
+ start
+ 1, path
+ i
);
244 /* Check for trailing `/'. */
245 if (start
&& !path
[i
]) {
251 /* Check for `../', `./' or trailing `.' by itself. */
252 if (path
[i
] == '.') {
253 /* Handle trailing `.' by itself. */
258 if (path
[i
+ 1] == PATH_SEP
) {
259 strcpy (path
+ i
, path
+ i
+ 1);
264 /* Handle `../' or trailing `..' by itself.
265 Remove the previous ?/ part with the exception of
266 ../, which we should leave intact. */
267 if (path
[i
+ 1] == '.' && (path
[i
+ 2] == PATH_SEP
|| !path
[i
+ 2])) {
268 while (--start
> -1 && path
[start
] != PATH_SEP
);
269 if (!strncmp (path
+ start
+ 1, "../", 3))
271 strcpy (path
+ start
+ 1, path
+ i
+ 2);
287 int mc_rmdir (char *path);
288 Fix for Win95 UGLY BUG in rmdir: it will return ENOACCESS instead
291 int mc_rmdir (char *path
)
293 if (win32_GetPlatform() == OS_Win95
) {
295 SetLastError (ERROR_DIR_NOT_EMPTY
);
297 /* FIXME: We are always saying the same thing! */
298 _doserrno
= ERROR_DIR_NOT_EMPTY
;
306 return rmdir(path
); /* No trouble in Windows NT */
309 static int conv_nt_unx_rc(int rc
)
313 case ERROR_FILE_NOT_FOUND
:
314 case ERROR_PATH_NOT_FOUND
:
315 case ERROR_TOO_MANY_OPEN_FILES
:
318 case ERROR_INVALID_HANDLE
:
319 case ERROR_ARENA_TRASHED
:
320 case ERROR_ACCESS_DENIED
:
321 case ERROR_INVALID_ACCESS
:
322 case ERROR_WRITE_PROTECT
:
323 case ERROR_WRITE_FAULT
:
324 case ERROR_READ_FAULT
:
325 case ERROR_SHARING_VIOLATION
:
328 case ERROR_NOT_ENOUGH_MEMORY
:
331 case ERROR_INVALID_BLOCK
:
332 case ERROR_INVALID_FUNCTION
:
333 case ERROR_INVALID_DRIVE
:
336 case ERROR_CURRENT_DIRECTORY
:
339 case ERROR_NOT_READY
:
350 int mc_unlink (char *pathName)
351 For Windows 95 and NT, files should be able to be deleted even
352 if they don't have write-protection. We should build a question box
353 like: Delete anyway? Yes <No> All
355 int mc_unlink (char *pathName
)
359 static int erase_all
= 0;
363 rc
= DeleteFile(pathName
);
364 returnError
= GetLastError();
365 if ((rc
== FALSE
) && (returnError
== ERROR_ACCESS_DENIED
)) {
368 errno
= conv_nt_unx_rc(returnError
);
369 trunced_name
= name_trunc(pathName
, 30);
370 fileName
= (char *) malloc(strlen(trunced_name
) + 16);
371 strcpy(fileName
, "File ");
372 strcat(fileName
, trunced_name
);
373 strcat(fileName
, " protected");
374 result
= query_dialog(fileName
, "Delete anyway?", 3, 3, " No ", " Yes ", " All in the future!");
395 chmod(pathName
, S_IWRITE
); /* make it writable */
396 rc
= DeleteFile(pathName
);
397 returnError
= GetLastError();
399 errno
= conv_nt_unx_rc(returnError
);
403 if (rc
== TRUE
) return 0;
409 void my_statfs (struct my_statfs
*myfs_stats
, char *path
)
412 DWORD lpSectorsPerCluster
, lpBytesPerSector
, lpFreeClusters
, lpClusters
;
413 DWORD lpMaximumComponentLength
, lpFileSystemFlags
;
414 static char lpVolumeNameBuffer
[256], lpFileSystemNameBuffer
[30];
416 GetDiskFreeSpace(NULL
, &lpSectorsPerCluster
, &lpBytesPerSector
,
417 &lpFreeClusters
, &lpClusters
);
419 /* KBytes available */
420 myfs_stats
->avail
= lpSectorsPerCluster
* lpBytesPerSector
* lpFreeClusters
/ 1024;
423 myfs_stats
->total
= lpSectorsPerCluster
* lpBytesPerSector
* lpClusters
/ 1024;
424 myfs_stats
->nfree
= lpFreeClusters
;
425 myfs_stats
->nodes
= lpClusters
;
427 GetVolumeInformation(NULL
, lpVolumeNameBuffer
, 255, NULL
,
428 &lpMaximumComponentLength
, &lpFileSystemFlags
,
429 lpFileSystemNameBuffer
, 30);
431 myfs_stats
->mpoint
= lpFileSystemNameBuffer
;
432 myfs_stats
->device
= lpVolumeNameBuffer
;
435 myfs_stats
->type
= GetDriveType(NULL
);
436 switch (myfs_stats
->type
) {
438 * mmm. DeviceIoControl may fail if you are not root case
439 * F5_1Pt2_512, 5.25", 1.2MB, 512 bytes/sector
440 * myfs_stats->typename = "5.25\" 1.2MB"; break; case
441 * F3_1Pt44_512, 3.5", 1.44MB, 512 bytes/sector
442 * myfs_stats->typename = "3.5\" 1.44MB"; break; case
443 * F3_2Pt88_512, 3.5", 2.88MB, 512 bytes/sector
444 * myfs_stats->typename = "3.5\" 2.88MB"; break; case
445 * F3_20Pt8_512, 3.5", 20.8MB, 512 bytes/sector
446 * myfs_stats->typename = "3.5\" 20.8MB"; break; case
447 * F3_720_512, 3.5", 720KB, 512 bytes/sector
448 * myfs_stats->typename = "3.5\" 720MB"; break; case
449 * F5_360_512, 5.25", 360KB, 512 bytes/sector
450 * myfs_stats->typename = "5.25\" 360KB"; break; case
451 * F5_320_512, 5.25", 320KB, 512 bytes/sector
452 * case F5_320_1024, 5.25", 320KB, 1024
453 * bytes/sector myfs_stats->typename = "5.25\" 320KB"; break;
454 * case F5_180_512, 5.25", 180KB, 512
455 * bytes/sector myfs_stats->typename = "5.25\" 180KB"; break;
456 * case F5_160_512, 5.25", 160KB, 512
457 * bytes/sector myfs_stats->typename = "5.25\" 160KB"; break;
458 * case RemovableMedia, Removable media other than
459 * floppy myfs_stats->typename = "Removable"; break; case
460 * FixedMedia Fixed hard disk media
461 * myfs_stats->typename = "Hard Disk"; break; case Unknown:
464 case DRIVE_REMOVABLE
:
465 myfs_stats
->typename
= "Removable";
468 myfs_stats
->typename
= "Hard Disk";
471 myfs_stats
->typename
= "Networked";
474 myfs_stats
->typename
= "CD-ROM";
477 myfs_stats
->typename
= "RAM disk";
480 myfs_stats
->typename
= "unknown";
485 int gettimeofday (struct timeval
* tvp
, void *p
)
490 /* Since MC only calls this func from get_random_hint we return
491 some value, not exactly the "correct" one */
492 tvp
->tv_sec
= GetTickCount()/1000; /* Number of milliseconds since Windows started*/
493 tvp
->tv_usec
= GetTickCount();
499 look_for_exe(const char* pathname
)
503 int lgh
= strlen(pathname
);
508 p
= (char *) pathname
;
509 for (j
=0; j
<lgh
-4; j
++) {
512 if (!stricmp(p
, ".exe") ||
513 !stricmp(p
, ".bat") ||
514 !stricmp(p
, ".com") ||
515 !stricmp(p
, ".cmd")) {
523 lstat (const char* pathname
, struct stat
*buffer
)
525 int rc
= stat (pathname
, buffer
);
528 if (!(buffer
->st_mode
& S_IFDIR
)) {
529 if (!look_for_exe(pathname
)) {
530 buffer
->st_mode
&= !S_IXUSR
& !S_IXGRP
& !S_IXOTH
;
541 LookupAccountName (NULL, &sid...
552 int readlink (char* path
, char* buf
, int size
)
556 int symlink (char *n1
, char *n2
)
560 int link (char *p1
, char *p2
)
564 int chown (char *path
, int owner
, int group
)
568 int mknod (char *path
, int mode
, int dev
)
573 void init_uid_gid_cache (void)
578 /* INHANDLE is a result of some mc_open call to any vfs, this function
579 returns a normal handle (to be used with read) of a pipe for reading
580 of the output of COMMAND with arguments ... (must include argv[0] as
581 well) which gets as its input at most INLEN bytes from the INHANDLE
582 using mc_read. You have to call mc_doublepclose to close the returned
583 handle afterwards. If INLEN is -1, we read as much as we can :) */
584 int mc_doublepopen (int inhandle
, int inlen
, pid_t
*the_pid
, char *command
, ...)
586 int pipe0
[2], pipe1
[2], std_sav
[2];
597 if(_pipe(pipe0
, 8192, O_BINARY
| O_NOINHERIT
) == -1)
599 if(_pipe(pipe1
, 8192, O_BINARY
| O_NOINHERIT
) == -1)
601 // Duplicate stdin/stdout handles (next line will close original)
602 std_sav
[0] = _dup(_fileno(stdin
));
603 std_sav
[1] = _dup(_fileno(stdout
));
604 // Duplicate read end of pipe0 to stdin handle
605 if(_dup2(pipe0
[0], _fileno(stdin
)) != 0)
607 // Duplicate write end of pipe1 to stdout handle
608 if(_dup2(pipe1
[1], _fileno(stdout
)) != 0)
610 // Close original read end of pipe0
612 // Close original write end of pipe1
615 va_start (ap
, command
);
617 while ((args
[argno
++] = va_arg(ap
, char *)) != NULL
)
618 if (argno
== (MAXARGS
- 1)) {
624 pid
= spawnvp(P_NOWAIT
,command
, args
);// argv[1], (const char* const*)&argv[1]);
627 // Duplicate copy of original stdin back into stdin
628 if(_dup2(std_sav
[0], _fileno(stdin
)) != 0)
630 // Duplicate copy of original stdout back into stdout
631 if(_dup2(std_sav
[1], _fileno(stdout
)) != 0)
633 // Close duplicate copy of original stdout and stdin
638 while ((i
= _read (inhandle
, buffer
,
639 (inlen
== -1 || inlen
> 8192)
640 ? 8192 : inlen
)) > 0) {
641 write (pipe0
[1], buffer
, i
);
654 int mc_doublepclose (int pipe
, pid_t pid
)
659 _cwait ( &status
, pid
, 0);
663 /*hacks to get it compile, remove these after vfs works */
665 /*hacks to get it compile, remove these after vfs works */
667 char *vfs_get_current_dir (void)
672 int vfs_current_is_extfs (void)
677 int vfs_file_is_ftp (char *filename
)
682 int mc_utime (char *path
, void *times
)
688 void extfs_run (char *file
)
695 get_default_editor (void)
697 return "notepad.exe";
701 errno_dir_not_empty (int err
)
703 if (err
== ENOTEMPTY
|| err
== EEXIST
|| err
== EACCES
)
708 /* The MC library directory is by default the directory where mc.exe
709 is situated. It is possible to specify this directory via MCHOME
710 environment variable */
715 char *mchome
= getenv("MCHOME");
717 if (mchome
&& *mchome
)
719 mchome
= malloc(MC_MAXPATHLEN
);
720 GetModuleFileName(NULL
, mchome
, MC_MAXPATHLEN
);
721 for (cur
= mchome
+ strlen(mchome
); \
722 (cur
> mchome
) && (*cur
!= PATH_SEP
); cur
--);
724 cur
= strdup(mchome
);
732 int get_user_rights (struct stat
*buf
)
736 void init_groups (void)
739 void delete_groups (void)