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

📄 utilunix.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
    sigemptyset (&ignore.sa_mask);
    ignore.sa_flags = 0;

    sigaction (SIGINT, &ignore, &save_intr);
    sigaction (SIGQUIT, &ignore, &save_quit);
    sigaction (SIGTSTP, &startup_handler, &save_stop);

    switch (pid = fork ()) {
    case -1:
            closepipes ();
	    return -1;
    case 0: {
	    sigaction (SIGINT, &save_intr, NULL);
	    sigaction (SIGQUIT, &save_quit, NULL);

	    switch (pid = fork ()) {
	    	case -1:
	    	    closepipes ();
	    	    exit (1);
	    	case 0: {
#define MAXARGS 16
		     int argno;
		     char *args[MAXARGS];
		     va_list ap;
		     int nulldevice;

		     port_shutdown_extra_fds ();

		     nulldevice = open ("/dev/null", O_WRONLY);
		     close (0);
		     dup (pipe0 [0]);
		     close (1);
		     dup (pipe1 [1]);
		     close (2);
		     dup (nulldevice);
		     close (nulldevice);
		     closepipes ();
		     va_start (ap, command);
		     argno = 0;
		     while ((args[argno++] = va_arg(ap, char *)) != NULL)
		         if (argno == (MAXARGS - 1)) {
			     args[argno] = NULL;
			     break;
		         }
		     va_end (ap);
		     execvp (command, args);
		     exit (0);
		}
	    	default:
	    	    {
	    	    	char buffer [8192];
	    	    	int i;

	    	    	close (pipe0 [0]);
	    	    	close (pipe1 [0]);
	    	    	close (pipe1 [1]);
	    	    	while ((i = mc_read (inhandle, buffer,
                                             (inlen == -1 || inlen > 8192)
                                             ? 8192 : inlen)) > 0) {
	    	    	    write (pipe0 [1], buffer, i);
	    	    	    if (inlen != -1) {
			        inlen -= i;
			        if (!inlen)
				    break;
			    }
	    	    	}
	    	    	close (pipe0 [1]);
	   		while (waitpid (pid, &i, 0) < 0)
			    if (errno != EINTR)
				break;

			port_shutdown_extra_fds ();
	   		exit (i);
	    	    }
	    }
    }
    default:
	*the_pid = pid;
	break;
    }
    close (pipe0 [0]);
    close (pipe0 [1]);
    close (pipe1 [1]);
    return pipe1 [0];
}

int mc_doublepclose (int pipe, pid_t pid)
{
    int status = 0;

    close (pipe);
    waitpid (pid, &status, 0);
#ifdef SCO_FLAVOR
    waitpid (-1, NULL, WNOHANG);
#endif /* SCO_FLAVOR */
    sigaction (SIGINT, &save_intr, NULL);
    sigaction (SIGQUIT, &save_quit, NULL);
    sigaction (SIGTSTP, &save_stop, NULL);

    return status;
}

/* 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;

#if defined(__QNX__)
		/*
		** QNX accesses the directories of nodes on its peer-to-peer
		** network by prefixing their directories with "//[nid]".
		** We don't want to collapse two '/'s if they're at the start
		** of the path, followed by digits, and ending with a '/'.
		*/
		if (start == 0 && i == 1)
		{
			char *p = path + 2;
			char *q = strchr(p, PATH_SEP);

			if (q > p)
			{
				*q = 0;
				if (!strcspn(p, "0123456789"))
				{
					start = q - path;
					i = start + 1;
				}
				*q = PATH_SEP;
			}
		}
#endif

        /* 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;
}

#ifdef SCO_FLAVOR
int gettimeofday( struct timeval * tv, struct timezone * tz)
{
    struct timeb tb;
    struct tm * l;

    ftime( &tb );
    if (errno == EFAULT)
	return -1;
    l = localtime(&tb.time);
    tv->tv_sec = l->tm_sec;
    tv->tv_usec = (long) tb.millitm;
    return 0;
}
#endif /* SCO_FLAVOR */

#ifndef HAVE_PUTENV

/* The following piece of code was copied from the GNU C Library */
/* And is provided here for nextstep who lacks putenv */

extern char **environ;

#ifndef	HAVE_GNU_LD
#define	__environ	environ
#endif


/* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
int
putenv (const char *string)
{
    const char *const name_end = strchr (string, '=');
    register size_t size;
    register char **ep;

    if (name_end == NULL){
	/* Remove the variable from the environment.  */
	size = strlen (string);
	for (ep = __environ; *ep != NULL; ++ep)
	    if (!strncmp (*ep, string, size) && (*ep)[size] == '='){
		while (ep[1] != NULL){
		    ep[0] = ep[1];
		    ++ep;
		}
		*ep = NULL;
		return 0;
	    }
    }

    size = 0;
    for (ep = __environ; *ep != NULL; ++ep)
	if (!strncmp (*ep, string, name_end - string) &&
	    (*ep)[name_end - string] == '=')
	    break;
	else
	    ++size;

    if (*ep == NULL){
	static char **last_environ = NULL;
	char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
	if (new_environ == NULL)
	    return -1;
	(void) memcpy ((void *) new_environ, (void *) __environ,
		       size * sizeof (char *));
	new_environ[size] = (char *) string;
	new_environ[size + 1] = NULL;
	if (last_environ != NULL)
	    free ((void *) last_environ);
	last_environ = new_environ;
	__environ = new_environ;
    }
    else
	*ep = (char *) string;

    return 0;
}
#endif /* !HAVE_PUTENV */

void my_statfs (struct my_statfs *myfs_stats, char *path)
{
    int i, len = 0;

#ifndef NO_INFOMOUNT
    struct mount_entry *entry = NULL;
    struct mount_entry *temp = mount_list;
    struct fs_usage fs_use;

    while (temp){
	i = strlen (temp->me_mountdir);
	if (i > len && (strncmp (path, temp->me_mountdir, i) == 0))
	    if (!entry || (path [i] == PATH_SEP || path [i] == 0)){
		len = i;
		entry = temp;
	    }
	temp = temp->me_next;
    }

    if (entry){
	get_fs_usage (entry->me_mountdir, &fs_use);

	myfs_stats->type = entry->me_dev;
	myfs_stats->typename = entry->me_type;
	myfs_stats->mpoint = entry->me_mountdir;
	myfs_stats->device = entry->me_devname;
	myfs_stats->avail = getuid () ? fs_use.fsu_bavail/2 : fs_use.fsu_bfree/2;
	myfs_stats->total = fs_use.fsu_blocks/2;
	myfs_stats->nfree = fs_use.fsu_ffree;
	myfs_stats->nodes = fs_use.fsu_files;
    } else
#endif
#if defined(NO_INFOMOUNT) && defined(__QNX__)
/*
** This is the "other side" of the hack to read_filesystem_list() in
** mountlist.c.
** It's not the most efficient approach, but consumes less memory. It
** also accomodates QNX's ability to mount filesystems on the fly.
*/
	struct mount_entry	*entry;
    struct fs_usage		fs_use;

	if ((entry = read_filesystem_list(0, 0)) != NULL)
	{
		get_fs_usage(entry->me_mountdir, &fs_use);

		myfs_stats->type = entry->me_dev;
		myfs_stats->typename = entry->me_type;
		myfs_stats->mpoint = entry->me_mountdir;
		myfs_stats->device = entry->me_devname;

		myfs_stats->avail = fs_use.fsu_bfree / 2;
		myfs_stats->total = fs_use.fsu_blocks / 2;
		myfs_stats->nfree = fs_use.fsu_ffree;
		myfs_stats->nodes = fs_use.fsu_files;
	}
	else
#endif
    {
	myfs_stats->type = 0;
	myfs_stats->mpoint = "unknown";
	myfs_stats->device = "unknown";
	myfs_stats->avail = 0;
	myfs_stats->total = 0;
	myfs_stats->nfree = 0;
	myfs_stats->nodes = 0;
    }
}

#ifdef HAVE_GET_PROCESS_STATS
#    include <sys/procstats.h>

int gettimeofday (struct timeval *tp, void *tzp)
{
  return get_process_stats(tp, PS_SELF, 0, 0);
}
#endif

#ifdef SCO_FLAVOR
/* Define this only for SCO */
#ifdef USE_NETCODE
#ifndef HAVE_SOCKETPAIR

/*
 The code for s_pipe function is adapted from Section 7.9
 of the "UNIX Network Programming" by W. Richard Stevens,
 published by Prentice Hall, ISBN 0-13-949876-1
 (c) 1990 by P T R Prentice Hall

 It is used to implement socketpair function for SVR3 systems
 that lack it.
*/

#include <sys/types.h>
#include <sys/stream.h>  /* defines queue_t */
#include <stropts.h>     /* defines struct strtdinsert */
#include <fcntl.h>

#define SPX_DEVICE "/dev/spx"
#define S_PIPE_HANDLE_ERRNO 1
/* if the above is defined to 1, we will attempt to
   save and restore errno to indicate failure
   reason to the caller;
   Please note that this will not work in environments
   where errno is not just an integer
*/

#if S_PIPE_HANDLE_ERRNO
#include <errno.h>
/* This is for "extern int errno;" */
#endif

 /* s_pipe returns 0 if OK, -1 on error */
 /* two file descriptors are returned   */
int s_pipe(int fd[2])
{
   struct strfdinsert  ins;  /* stream I_FDINSERT ioctl format */
   queue_t             *pointer;
   #if S_PIPE_HANDLE_ERRNO
   int err_save;
   #endif
   /*
    * First open the stream clone device "dev/spx" twice,
    * obtaining the two file descriptors
    */

   if ( (fd[0] = open(SPX_DEVICE, O_RDWR)) < 0)
      return -1;
   if ( (fd[1] = open(SPX_DEVICE, O_RDWR)) < 0) {
      #if S_PIPE_HANDLE_ERRNO
      err_save = errno;
      #endif
      close(fd[0]);
      #if S_PIPE_HANDLE_ERRNO
      errno = err_save;
      #endif
      return -1;
   }

   /*
    * Now link these two stream together with an I_FDINSERT ioctl.
    */

   ins.ctlbuf.buf     = (char *) &pointer;   /* no control information, just the pointer */
   ins.ctlbuf.maxlen  = sizeof pointer;
   ins.ctlbuf.len     = sizeof pointer;
   ins.databuf.buf    = (char *) 0;   /* no data to be sent */
   ins.databuf.maxlen = 0;
   ins.databuf.len    = -1;  /* magic: must be -1 rather than 0 for stream pipes */

   ins.fildes = fd[1];  /* the fd to connect with fd[0] */
   ins.flags  = 0;      /* nonpriority message */
   ins.offset = 0;      /* offset of pointer in control buffer */

   if (ioctl(fd[0], I_FDINSERT, (char *) &ins) < 0) {
      #if S_PIPE_HANDLE_ERRNO
      err_save = errno;
      #endif
      close(fd[0]);
      close(fd[1]);
      #if S_PIPE_HANDLE_ERRNO
      errno = err_save;
      #endif
      return -1;
   }
   /* all is OK if we came here, indicate success */
   return 0;
}

int socketpair(int dummy1, int dummy2, int dummy3, int fd[2])
{
   return s_pipe(fd);
}

#endif /* ifndef HAVE_SOCKETPAIR */
#endif /* ifdef USE_NETCODE */
#endif /* SCO_FLAVOR */

⌨️ 快捷键说明

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