ru.po: Heavily updated translation
[midnight-commander.git] / nt / utilnt.c
blob3e227b33d7ff304198178d964609d0ae6065bc43
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., 675 Mass Ave, Cambridge, MA 02139, 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/time.h> /* select: timeout */
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <stdarg.h>
36 #include <process.h>
37 #include <fs.h>
38 #include <util.h>
39 #include "util.Win32.h"
41 #ifdef __BORLANDC__
42 #define ENOTEMPTY ERROR_DIR_NOT_EMPTY
43 #endif
45 char *get_owner (int uid)
47 return "none";
50 char *get_group (int gid)
52 return "none";
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 ");
70 old_error = dup (2);
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)
81 char *title;
82 char msg[MAX_PIPE_SIZE];
83 int len = 0;
85 if (error)
86 title = " Error ";
87 else
88 title = " Warning ";
89 if (old_error >= 0){
90 close (2);
91 dup (old_error);
92 close (old_error);
93 len = read (error_pipe[0], msg, MAX_PIPE_SIZE);
95 if (len >= 0)
96 msg[len] = 0;
97 close (error_pipe[0]);
99 if (error < 0)
100 return; /* Just ignore error message */
101 if (text == NULL){
102 if (len == 0) return; /* Nothing to show */
104 /* Show message from pipe */
105 message (error, title, msg);
106 } else {
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];
115 int len = 0;
116 if (old_error >= 0){
117 while (len < MAX_PIPE_SIZE)
119 int rvalue;
121 rvalue = read (error_pipe[0], error + len, 1);
122 len ++;
123 if (rvalue <= 0)
124 break;
126 error[len] = 0;
127 close (error_pipe[0]);
129 if (len > 0)
130 message (0, " Warning ", error);
133 int my_system (int as_shell_command, const char *shell, const char *command)
135 int status = 0;
137 #if 0
138 /* .ado: temp. turn out */
139 if (as_shell_command) {
140 /* It is only the shell, /c will not work */
141 if (command)
142 spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
143 else
144 spawnlp (P_WAIT, shell, (char *) 0);
145 } else
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... */
151 #endif
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 */
158 break;
159 case EXE_otherCUI: /* DOS COM, MZ, ZM, Phar Lap */
160 case EXE_win32CUI: /* NT or Chicago Console API, also OS/2 */
161 case EXE_Unknown:
162 default:
163 spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
164 break;
167 else
168 spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
170 else
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... */
177 return status;
180 /* get_default_shell
181 Get the default shell for the current hardware platform
183 char* get_default_shell()
185 if (win32_GetPlatform() == OS_WinNT)
186 return "cmd.exe";
187 else
188 return "command.com";
191 char *tilde_expand (char *directory)
193 return strdup (directory);
196 /* sleep: Call Windows API.
197 Can't do simple define. That would need <windows.h> in every source
199 void sleep(unsigned long dwMiliSecs)
201 Sleep(dwMiliSecs);
205 /* Canonicalize path, and return a new path. Do everything in situ.
206 * [call OS API]
208 char *canonicalize_pathname (char *path)
210 /* This holds an unused pointer to the start of file name in path */
211 char *pName;
212 char pCanonical[MC_MAXPATHLEN];
214 GetFullPathName (path, MC_MAXPATHLEN, pCanonical, &pName);
216 /* FIXME: buffer large enough? */
217 strcpy (path, pCanonical);
218 return path;
220 /* Do 'manual' canonicalizing, needed for some VFS functions
222 Canonicalize path, and return a new path. Do everything in situ.
223 The new path differs from path in:
224 Multiple `/'s are collapsed to a single `/'.
225 Leading `./'s and trailing `/.'s are removed.
226 Trailing `/'s are removed.
227 Non-leading `../'s and trailing `..'s are handled by removing
228 portions of the path. */
229 char *unixlike_canonicalize_pathname (char *path)
231 int i, start;
232 char stub_char;
234 stub_char = (*path == PATH_SEP) ? PATH_SEP : '.';
236 /* Walk along path looking for things to compact. */
237 i = 0;
238 for (;;) {
239 if (!path[i])
240 break;
242 while (path[i] && path[i] != PATH_SEP)
243 i++;
245 start = i++;
247 /* If we didn't find any slashes, then there is nothing left to do. */
248 if (!path[start])
249 break;
251 /* Handle multiple `/'s in a row. */
252 while (path[i] == PATH_SEP)
253 i++;
255 if ((start + 1) != i) {
256 strcpy (path + start + 1, path + i);
257 i = start + 1;
260 /* Handle backquoted `/'. */
261 if (start > 0 && path[start - 1] == '\\')
262 continue;
264 /* Check for trailing `/'. */
265 if (start && !path[i]) {
266 zero_last:
267 path[--i] = '\0';
268 break;
271 /* Check for `../', `./' or trailing `.' by itself. */
272 if (path[i] == '.') {
273 /* Handle trailing `.' by itself. */
274 if (!path[i + 1])
275 goto zero_last;
277 /* Handle `./'. */
278 if (path[i + 1] == PATH_SEP) {
279 strcpy (path + i, path + i + 1);
280 i = start;
281 continue;
284 /* Handle `../' or trailing `..' by itself.
285 Remove the previous ?/ part with the exception of
286 ../, which we should leave intact. */
287 if (path[i + 1] == '.' && (path[i + 2] == PATH_SEP || !path[i + 2])) {
288 while (--start > -1 && path[start] != PATH_SEP);
289 if (!strncmp (path + start + 1, "../", 3))
290 continue;
291 strcpy (path + start + 1, path + i + 2);
292 i = start;
293 continue;
298 if (!*path) {
299 *path = stub_char;
300 path[1] = '\0';
302 return path;
305 #ifndef USE_VFS
307 int mc_rmdir (char *path);
308 Fix for Win95 UGLY BUG in rmdir: it will return ENOACCESS instead
309 of ENOTEMPTY.
311 int mc_rmdir (char *path)
313 if (win32_GetPlatform() == OS_Win95) {
314 if (rmdir(path)) {
315 SetLastError (ERROR_DIR_NOT_EMPTY);
316 _doserrno = ERROR_DIR_NOT_EMPTY; /* FIXME: We are always saying the same thing! */
317 errno = ENOTEMPTY;
318 return -1;
319 } else
320 return 0;
322 else
323 return rmdir(path); /* No trouble in Windows NT */
326 static int conv_nt_unx_rc(int rc)
328 int errCode;
329 switch (rc) {
330 case ERROR_FILE_NOT_FOUND:
331 case ERROR_PATH_NOT_FOUND:
332 case ERROR_TOO_MANY_OPEN_FILES:
333 errCode = ENOENT;
334 break;
335 case ERROR_INVALID_HANDLE:
336 case ERROR_ARENA_TRASHED:
337 case ERROR_ACCESS_DENIED:
338 case ERROR_INVALID_ACCESS:
339 case ERROR_WRITE_PROTECT:
340 case ERROR_WRITE_FAULT:
341 case ERROR_READ_FAULT:
342 case ERROR_SHARING_VIOLATION:
343 errCode = EACCES;
344 break;
345 case ERROR_NOT_ENOUGH_MEMORY:
346 errCode = ENOMEM;
347 break;
348 case ERROR_INVALID_BLOCK:
349 case ERROR_INVALID_FUNCTION:
350 case ERROR_INVALID_DRIVE:
351 errCode = ENODEV;
352 break;
353 case ERROR_CURRENT_DIRECTORY:
354 errCode = ENOTDIR;
355 break;
356 case ERROR_NOT_READY:
357 errCode = EINVAL;
358 break;
359 default:
360 errCode = EINVAL;
361 break;
362 } /* endswitch */
363 return errCode;
367 int mc_unlink (char *pathName)
368 For Windows 95 and NT, files should be able to be deleted even
369 if they don't have write-protection. We should build a question box
370 like: Delete anyway? Yes <No> All
372 int mc_unlink (char *pathName)
374 char *fileName;
375 char *trunced_name;
376 static int erase_all = 0;
377 BOOL rc;
378 DWORD returnError;
380 rc = DeleteFile(pathName);
381 returnError = GetLastError();
382 if ((rc == FALSE) && (returnError == ERROR_ACCESS_DENIED)) {
383 int result;
384 if (!erase_all) {
385 errno = conv_nt_unx_rc(returnError);
386 trunced_name = name_trunc(pathName, 30);
387 fileName = (char *) malloc(strlen(trunced_name) + 16);
388 strcpy(fileName, "File ");
389 strcat(fileName, trunced_name);
390 strcat(fileName, " protected");
391 result = query_dialog(fileName, "Delete anyway?", 3, 3, " No ", " Yes ", " All in the future!");
392 free(fileName);
394 switch (result) {
395 case 0:
396 do_refresh ();
397 return -1;
398 case 1:
399 do_refresh ();
400 break;
401 case 2:
402 do_refresh ();
403 erase_all = 1;
404 break;
405 default:
406 do_refresh ();
407 return -1;
408 break;
412 chmod(pathName, S_IWRITE); /* make it writable */
413 rc = DeleteFile(pathName);
414 returnError = GetLastError();
415 if (rc == FALSE) {
416 errno = conv_nt_unx_rc(returnError);
417 return -1;
420 if (rc == TRUE) return 0;
421 else
422 return -1;
427 int mc_rename (char *original, char *target)
428 Fix for Win95 and WinNT BUG in rename: they will return ENOACCESS instead
429 of EXDEV.
431 int mc_rename (char* original, char *target)
433 /* check same device: easy with FAT/NTFS, first letter is drive
434 FIXME: network paths will fail this test */
435 if (*original != *target) {
436 SetLastError (ERROR_NOT_SAME_DEVICE);
437 _doserrno = ERROR_NOT_SAME_DEVICE;
438 errno = EXDEV;
439 return -1;
441 else
442 return rename(original,target);
444 #endif /*USE_VFS*/
446 void my_statfs (struct my_statfs *myfs_stats, char *path)
448 int i, len = 0;
449 DWORD lpSectorsPerCluster, lpBytesPerSector, lpFreeClusters, lpClusters;
450 DWORD lpMaximumComponentLength, dw, lpFileSystemFlags;
451 static char lpVolumeNameBuffer[256], lpFileSystemNameBuffer[30];
453 GetDiskFreeSpace(NULL, &lpSectorsPerCluster, &lpBytesPerSector,
454 &lpFreeClusters, &lpClusters);
456 /* KBytes available */
457 myfs_stats->avail = lpSectorsPerCluster * lpBytesPerSector * lpFreeClusters / 1024;
459 /* KBytes total */
460 myfs_stats->total = lpSectorsPerCluster * lpBytesPerSector * lpClusters / 1024;
461 myfs_stats->nfree = lpFreeClusters;
462 myfs_stats->nodes = lpClusters;
464 GetVolumeInformation(NULL, lpVolumeNameBuffer, 255, NULL,
465 &lpMaximumComponentLength, &lpFileSystemFlags,
466 lpFileSystemNameBuffer, 30);
468 myfs_stats->mpoint = lpFileSystemNameBuffer;
469 myfs_stats->device = lpVolumeNameBuffer;
472 myfs_stats->type = GetDriveType(NULL);
473 switch (myfs_stats->type) {
475 * mmm. DeviceIoControl may fail if you are not root case
476 * F5_1Pt2_512, 5.25", 1.2MB, 512 bytes/sector
477 * myfs_stats->typename = "5.25\" 1.2MB"; break; case
478 * F3_1Pt44_512, 3.5", 1.44MB, 512 bytes/sector
479 * myfs_stats->typename = "3.5\" 1.44MB"; break; case
480 * F3_2Pt88_512, 3.5", 2.88MB, 512 bytes/sector
481 * myfs_stats->typename = "3.5\" 2.88MB"; break; case
482 * F3_20Pt8_512, 3.5", 20.8MB, 512 bytes/sector
483 * myfs_stats->typename = "3.5\" 20.8MB"; break; case
484 * F3_720_512, 3.5", 720KB, 512 bytes/sector
485 * myfs_stats->typename = "3.5\" 720MB"; break; case
486 * F5_360_512, 5.25", 360KB, 512 bytes/sector
487 * myfs_stats->typename = "5.25\" 360KB"; break; case
488 * F5_320_512, 5.25", 320KB, 512 bytes/sector
489 * case F5_320_1024, 5.25", 320KB, 1024
490 * bytes/sector myfs_stats->typename = "5.25\" 320KB"; break;
491 * case F5_180_512, 5.25", 180KB, 512
492 * bytes/sector myfs_stats->typename = "5.25\" 180KB"; break;
493 * case F5_160_512, 5.25", 160KB, 512
494 * bytes/sector myfs_stats->typename = "5.25\" 160KB"; break;
495 * case RemovableMedia, Removable media other than
496 * floppy myfs_stats->typename = "Removable"; break; case
497 * FixedMedia Fixed hard disk media
498 * myfs_stats->typename = "Hard Disk"; break; case Unknown:
499 * Format is unknown
501 case DRIVE_REMOVABLE:
502 myfs_stats->typename = "Removable";
503 break;
504 case DRIVE_FIXED:
505 myfs_stats->typename = "Hard Disk";
506 break;
507 case DRIVE_REMOTE:
508 myfs_stats->typename = "Networked";
509 break;
510 case DRIVE_CDROM:
511 myfs_stats->typename = "CD-ROM";
512 break;
513 case DRIVE_RAMDISK:
514 myfs_stats->typename = "RAM disk";
515 break;
516 default:
517 myfs_stats->typename = "unknown";
518 break;
522 int gettimeofday (struct timeval* tvp, void *p)
524 if (p != NULL)
525 return 0;
527 /* Since MC only calls this func from get_random_hint we return
528 some value, not exactly the "correct" one */
529 tvp->tv_sec = GetTickCount()/1000; /* Number of milliseconds since Windows started*/
530 tvp->tv_usec = GetTickCount();
533 // FAKE funcs
535 int
536 look_for_exe(const char* pathname)
538 int j;
539 char *p;
540 int lgh = strlen(pathname);
542 if (lgh < 4) {
543 return 0;
544 } else {
545 p = (char *) pathname;
546 for (j=0; j<lgh-4; j++) {
547 p++;
548 } /* endfor */
549 if (!stricmp(p, ".exe") ||
550 !stricmp(p, ".bat") ||
551 !stricmp(p, ".com") ||
552 !stricmp(p, ".cmd")) {
553 return 1;
556 return 0;
559 int
560 lstat (const char* pathname, struct stat *buffer)
562 int rc = stat (pathname, buffer);
563 #ifdef __BORLANDC__
564 if (rc == 0) {
565 if (!(buffer->st_mode & S_IFDIR)) {
566 if (!look_for_exe(pathname)) {
567 buffer->st_mode &= !S_IXUSR & !S_IXGRP & !S_IXOTH;
571 #endif
572 return rc;
575 int getuid ()
577 /* SID sid;
578 LookupAccountName (NULL, &sid...
579 return 0;
581 return 0;
584 int getgid ()
586 return 0;
589 int readlink (char* path, char* buf, int size)
591 return -1;
593 int symlink (char *n1, char *n2)
595 return -1;
597 int link (char *p1, char *p2)
599 return -1;
601 int chown (char *path, int owner, int group)
603 return -1;
605 int mknod (char *path, int mode, int dev)
607 return -1;
610 void init_uid_gid_cache (void)
612 return;
615 int mc_doublepopen (int inhandle, int inlen, pid_t *the_pid, char *command, ...)
617 return 0;
620 int mc_doublepclose (int pipe, pid_t pid)
622 /* win32Trace(("mc_doublepclose called")); */
623 return 0;
626 /*hacks to get it compile, remove these after vfs works */
627 #ifndef USE_VFS
628 char *vfs_get_current_dir (void)
630 return NULL;
633 int vfs_current_is_extfs (void)
635 return 0;
638 int vfs_file_is_ftp (char *filename)
640 return 0;
643 int mc_utime (char *path, struct utimbuf *times)
645 return 0;
649 void extfs_run (char *file)
651 return;
653 #endif
655 int strncasecmp (char *s, char *d, int count)
657 register char result;
659 while (count > 0){
660 if (result = (0x20 | *s) - (0x20 | *d))
661 break;
662 if (!*s)
663 return 0;
664 s++;
665 d++;
666 count--;
668 return result;
671 char *
672 get_default_editor (void)
674 return "vi.exe";
679 errno_dir_not_empty (int err)
681 if (err == ENOTEMPTY || err == EEXIST || err == EACCES)
682 return 1;
683 return 0;
686 /* The MC library directory is by default the directory where mc.exe
687 is situated. It is possible to specify this directory via MCHOME
688 environment variable */
689 char *
690 get_mc_lib_dir ()
692 char *cur;
693 char *mchome = getenv("MCHOME");
695 if (mchome && *mchome)
696 return mchome;
697 mchome = malloc(MC_MAXPATHLEN);
698 GetModuleFileName(NULL, mchome, MC_MAXPATHLEN);
699 for (cur = mchome + strlen(mchome); \
700 (cur > mchome) && (*cur != PATH_SEP); cur--);
701 *cur = 0;
702 cur = strdup(mchome);
703 free(mchome);
704 if (!cur || !*cur) {
705 free(cur);
706 return "C:\\MC";
708 return cur;
710 int get_user_rights (struct stat *buf)
712 return 2;
714 void init_groups (void)
717 void delete_groups (void)