⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util_nt.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Various utilities - NT versions
   Copyright (C) 1994, 1995, 1996 the Free Software Foundation.

   Written 1994, 1995, 1996 by:
   Juan Grigera, Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
   Jakub Jelinek, Mauricio Plaza.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <signal.h>		/* my_system */
#include <limits.h>		/* INT_MAX */
#include <errno.h>
#if defined(_MSC_VER)
#include <sys/time.h___>		/* select: timeout */
#else
#include <sys/time.h>		/* select: timeout */
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <process.h>
#include "../src/fs.h"
#include "../src/util.h"
#include "util_win32.h"

#ifdef __BORLANDC__
#define ENOTEMPTY ERROR_DIR_NOT_EMPTY
#endif

char *get_owner (int uid)
{
    return "none";
}

char *get_group (int gid)
{
    return "none";
}

/* Pipes are guaranteed to be able to hold at least 4096 bytes */
/* More than that would be unportable */
#define MAX_PIPE_SIZE 4096

static int error_pipe[2];	/* File descriptors of error pipe */
static int old_error;		/* File descriptor of old standard error */

/* Creates a pipe to hold standard error for a later analysis. */
/* The pipe can hold 4096 bytes. Make sure no more is written */
/* or a deadlock might occur. */
void open_error_pipe (void)
{
    if (pipe (error_pipe) < 0){
	message (0, _(" Warning "), _(" Pipe failed ") );
    }
    old_error = dup (2);
    if(old_error < 0 || close(2) || dup (error_pipe[1]) != 2){
	message (0, _(" Warning "), _(" Dup failed ") );
	close (error_pipe[0]);
	close (error_pipe[1]);
    }
    close (error_pipe[1]);
}

void close_error_pipe (int error, char *text)
{
    char *title;
    char msg[MAX_PIPE_SIZE];
    int len = 0;

    if (error)
	title = _(" Error ");
    else
	title = _(" Warning ");
    if (old_error >= 0){
	close (2);
	dup (old_error);
	close (old_error);
	len = read (error_pipe[0], msg, MAX_PIPE_SIZE);

	if (len >= 0)
	    msg[len] = 0;
	close (error_pipe[0]);
    }
    if (error < 0)
	return;		/* Just ignore error message */
    if (text == NULL){
	if (len == 0) return;	/* Nothing to show */

	/* Show message from pipe */
	message (error, title, msg);
    } else {
	/* Show given text and possible message from pipe */
	message (error, title, " %s \n %s ", text, msg);
    }
}

void check_error_pipe (void)
{
    char error[MAX_PIPE_SIZE];
    int len = 0;
    if (old_error >= 0){
	while (len < MAX_PIPE_SIZE)
	{
	    int rvalue;

	    rvalue = -1; // read (error_pipe[0], error + len, 1);
	    if (rvalue <= 0)
		break;
	    len ++;
	}
	error[len] = 0;
	close (error_pipe[0]);
    }
    if (len > 0)
        message (0, _(" Warning "), error);
}

int my_system (int as_shell_command, const char *shell, const char *command)
{
    int status = 0;

#if 0
/* .ado: temp. turn out */
    if (as_shell_command) {
		/* It is only the shell, /c will not work */
		if (command)
			spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
		else
			spawnlp (P_WAIT, shell, (char *) 0);
	} else
       spawnl (P_WAIT, shell, shell, command, (char *) 0);

    if (win32_GetPlatform() == OS_Win95) {
	    SetConsoleTitle ("GNU Midnight Commander");		/* title is gone after spawn... */
    }
#endif
    if (as_shell_command) {
	if (!access(command, 0)) {
	    switch(win32_GetEXEType (shell)) {
		case EXE_win16:			/* Windows 3.x archive or OS/2 */
		case EXE_win32GUI:		/* NT or Chicago GUI API */
		    spawnlp (P_NOWAIT, shell, shell, "/c", command, (char *) 0);   /* don't wait for GUI programs to end */
		    break;
		case EXE_otherCUI:		/* DOS COM, MZ, ZM, Phar Lap */
		case EXE_win32CUI:		/* NT or Chicago Console API, also OS/2 */
		case EXE_Unknown:
		default:
		    spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
		    break;
	    }
	}
	else
	    spawnlp (P_WAIT, shell, shell, "/c", command, (char *) 0);
    }
    else
       spawnl (P_WAIT, shell, shell, command, (char *) 0);

    if (win32_GetPlatform() == OS_Win95) {
	    SetConsoleTitle ("GNU Midnight Commander");		/* title is gone after spawn... */
    }

    return status;
}

/* get_default_shell
   Get the default shell for the current hardware platform
*/
char* get_default_shell()
{
    if (win32_GetPlatform() == OS_WinNT)
	return "cmd.exe";
    else
	return "command.com";
}

char *tilde_expand (char *directory)
{
    return strdup (directory);
}

/* sleep: Call Windows API.
	  Can't do simple define. That would need <windows.h> in every source
*/
#ifndef __EMX__
void sleep(unsigned long dwMiliSecs)
{
    Sleep(dwMiliSecs);
}
#endif

/* Canonicalize path, and return a new path. Do everything in situ.
   The new path differs from path in:
	Multiple `/'s are collapsed to a single `/'.
	Leading `./'s and trailing `/.'s are removed.
	Trailing `/'s are removed.
	Non-leading `../'s and trailing `..'s are handled by removing
	portions of the path. */
char *canonicalize_pathname (char *path)
{
    int i, start;
    char stub_char;

    stub_char = (*path == PATH_SEP) ? PATH_SEP : '.';

    /* Walk along path looking for things to compact. */
    i = 0;
    for (;;) {
        if (!path[i])
	    break;

      	while (path[i] && path[i] != PATH_SEP)
	    i++;

      	start = i++;

      	/* If we didn't find any slashes, then there is nothing left to do. */
      	if (!path[start])
	    break;

        /* Handle multiple `/'s in a row. */
        while (path[i] == PATH_SEP)
	    i++;

        if ((start + 1) != i) {
	    strcpy (path + start + 1, path + i);
	    i = start + 1;
	}

        /* Handle backquoted `/'. */
        if (start > 0 && path[start - 1] == '\\')
	    continue;

        /* Check for trailing `/'. */
        if (start && !path[i]) {
	zero_last:
	    path[--i] = '\0';
	    break;
	}

        /* Check for `../', `./' or trailing `.' by itself. */
        if (path[i] == '.') {
	    /* Handle trailing `.' by itself. */
	    if (!path[i + 1])
	        goto zero_last;

	    /* Handle `./'. */
	    if (path[i + 1] == PATH_SEP) {
	        strcpy (path + i, path + i + 1);
	        i = start;
	        continue;
	    }

	    /* Handle `../' or trailing `..' by itself.
	       Remove the previous ?/ part with the exception of
	       ../, which we should leave intact. */
	    if (path[i + 1] == '.' && (path[i + 2] == PATH_SEP || !path[i + 2])) {
	        while (--start > -1 && path[start] != PATH_SEP);
	        if (!strncmp (path + start + 1, "../", 3))
	            continue;
	        strcpy (path + start + 1, path + i + 2);
	        i = start;
	        continue;
	    }
	}
    }

    if (!*path) {
        *path = stub_char;
        path[1] = '\0';
    }
    return path;
}

#ifndef USE_VFS
/*
   int mc_rmdir (char *path);
   Fix for Win95 UGLY BUG in rmdir: it will return ENOACCESS instead
   of ENOTEMPTY.
 */
int mc_rmdir (char *path)
{
    if (win32_GetPlatform() == OS_Win95) {
		if (rmdir(path)) {
			SetLastError (ERROR_DIR_NOT_EMPTY);
#ifndef __EMX__
			/* FIXME: We are always saying the same thing! */
			_doserrno = ERROR_DIR_NOT_EMPTY;
#endif
			errno = ENOTEMPTY;
			return -1;
		} else
			return 0;
    }
    else
		return rmdir(path);		/* No trouble in Windows NT */
}

static int conv_nt_unx_rc(int rc)
{
   int errCode;
   switch (rc) {
      case ERROR_FILE_NOT_FOUND:
      case ERROR_PATH_NOT_FOUND:
      case ERROR_TOO_MANY_OPEN_FILES:
         errCode = ENOENT;
         break;
      case ERROR_INVALID_HANDLE:
      case ERROR_ARENA_TRASHED:
      case ERROR_ACCESS_DENIED:
  	  case ERROR_INVALID_ACCESS:
  	  case ERROR_WRITE_PROTECT:
  	  case ERROR_WRITE_FAULT:
      case ERROR_READ_FAULT:
  	  case ERROR_SHARING_VIOLATION:
	     errCode = EACCES;
         break;
      case ERROR_NOT_ENOUGH_MEMORY:
		  errCode = ENOMEM;
		  break;
      case ERROR_INVALID_BLOCK:
	  case ERROR_INVALID_FUNCTION:
	  case ERROR_INVALID_DRIVE:
		  errCode = ENODEV;
		  break;
	  case ERROR_CURRENT_DIRECTORY:
		  errCode = ENOTDIR;
		  break;
	  case ERROR_NOT_READY:
         errCode = EINVAL;
         break;
      default:
         errCode = EINVAL;
        break;
   } /* endswitch */
   return errCode;
}

/*
   int mc_unlink (char *pathName)
   For Windows 95 and NT, files should be able to be deleted even
   if they don't have write-protection. We should build a question box
   like: Delete anyway? Yes <No> All
*/
int mc_unlink (char *pathName)
{
	char		*fileName;
	char		*trunced_name;
	static int  erase_all = 0;
	BOOL        rc;
	DWORD       returnError;

	rc = DeleteFile(pathName);
	returnError = GetLastError();
	if ((rc == FALSE) && (returnError == ERROR_ACCESS_DENIED)) {
		int result;
		if (!erase_all) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -