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

📄 utility.c

📁 一个简单的操作系统minix的核心代码
💻 C
字号:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				src/fs/utility.c	 	 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

27400	/* This file contains a few general purpose utility routines.
27401	 *
27402	 * The entry points into this file are
27403	 *   clock_time:  ask the clock task for the real time
27404	 *   copy:        copy a block of data
27405	 *   fetch_name:  go get a path name from user space
27406	 *   no_sys:      reject a system call that FS does not handle
27407	 *   panic:       something awful has occurred;  MINIX cannot continue
27408	 *   conv2:       do byte swapping on a 16-bit int
27409	 *   conv4:       do byte swapping on a 32-bit long
27410	 */
27411	
27412	#include "fs.h"
27413	#include <minix/com.h>
27414	#include <minix/boot.h>
27415	#include <unistd.h>
27416	#include "buf.h"
27417	#include "file.h"
27418	#include "fproc.h"
27419	#include "inode.h"
27420	#include "param.h"
27421	
27422	PRIVATE int panicking;          /* inhibits recursive panics during sync */
27423	PRIVATE message clock_mess;
27424	
27425	/*===========================================================================*
27426	 *                              clock_time                                   *
27427	 *===========================================================================*/
27428	PUBLIC time_t clock_time()
27429	{
27430	/* This routine returns the time in seconds since 1.1.1970.  MINIX is an
27431	 * astrophysically naive system that assumes the earth rotates at a constant
27432	 * rate and that such things as leap seconds do not exist.
27433	 */
27434	
27435	  register int k;
27436	
27437	  clock_mess.m_type = GET_TIME;
27438	  if ( (k = sendrec(CLOCK, &clock_mess)) != OK) panic("clock_time err", k);
27439	
27440	  return( (time_t) clock_mess.NEW_TIME);
27441	}
	
	
27444	/*===========================================================================*
27445	 *                              fetch_name                                   *
27446	 *===========================================================================*/
27447	PUBLIC int fetch_name(path, len, flag)
27448	char *path;                     /* pointer to the path in user space */
27449	int len;                        /* path length, including 0 byte */
27450	int flag;                       /* M3 means path may be in message */
27451	{
27452	/* Go get path and put it in 'user_path'.
27453	 * If 'flag' = M3 and 'len' <= M3_STRING, the path is present in 'message'.
27454	 * If it is not, go copy it from user space.
27455	 */
27456	
27457	  register char *rpu, *rpm;
27458	  int r;
27459	
27460	  /* Check name length for validity. */
27461	  if (len <= 0) {
27462	        err_code = EINVAL;
27463	        return(EGENERIC);
27464	  }
27465	
27466	  if (len > PATH_MAX) {
27467	        err_code = ENAMETOOLONG;
27468	        return(EGENERIC);
27469	  }
27470	
27471	  if (flag == M3 && len <= M3_STRING) {
27472	        /* Just copy the path from the message to 'user_path'. */
27473	        rpu = &user_path[0];
27474	        rpm = pathname;         /* contained in input message */
27475	        do { *rpu++ = *rpm++; } while (--len);
27476	        r = OK;
27477	  } else {
27478	        /* String is not contained in the message.  Get it from user space. */
27479	        r = sys_copy(who, D, (phys_bytes) path,
27480	                FS_PROC_NR, D, (phys_bytes) user_path, (phys_bytes) len);
27481	  }
27482	  return(r);
27483	}
	
	
27486	/*===========================================================================*
27487	 *                              no_sys                                       *
27488	 *===========================================================================*/
27489	PUBLIC int no_sys()
27490	{
27491	/* Somebody has used an illegal system call number */
27492	
27493	  return(EINVAL);
27494	}
	
	
27497	/*===========================================================================*
27498	 *                              panic                                        *
27499	 *===========================================================================*/
27500	PUBLIC void panic(format, num)
27501	char *format;                   /* format string */
27502	int num;                        /* number to go with format string */
27503	{
27504	/* Something awful has happened.  Panics are caused when an internal
27505	 * inconsistency is detected, e.g., a programming error or illegal value of a
27506	 * defined constant.
27507	 */
27508	
27509	  if (panicking) return;        /* do not panic during a sync */
27510	  panicking = TRUE;             /* prevent another panic during the sync */
27511	  printf("File system panic: %s ", format);
27512	  if (num != NO_NUM) printf("%d",num); 
27513	  printf("\n");
27514	  (void) do_sync();             /* flush everything to the disk */
27515	  sys_abort(RBT_PANIC);
27516	}
	
	
27519	/*===========================================================================*
27520	 *                              conv2                                        *
27521	 *===========================================================================*/
27522	PUBLIC unsigned conv2(norm, w)
27523	int norm;                       /* TRUE if no swap, FALSE for byte swap */
27524	int w;                          /* promotion of 16-bit word to be swapped */
27525	{
27526	/* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
27527	
27528	  if (norm) return( (unsigned) w & 0xFFFF);
27529	  return( ((w&BYTE) << 8) | ( (w>>8) & BYTE));
27530	}
	
	
27533	/*===========================================================================*
27534	 *                              conv4                                        *
27535	 *===========================================================================*/
27536	PUBLIC long conv4(norm, x)
27537	int norm;                       /* TRUE if no swap, FALSE for byte swap */
27538	long x;                         /* 32-bit long to be byte swapped */
27539	{
27540	/* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
27541	
27542	  unsigned lo, hi;
27543	  long l;
27544	  
27545	  if (norm) return(x);                  /* byte order was already ok */
27546	  lo = conv2(FALSE, (int) x & 0xFFFF);  /* low-order half, byte swapped */
27547	  hi = conv2(FALSE, (int) (x>>16) & 0xFFFF);    /* high-order half, swapped */
27548	  l = ( (long) lo <<16) | hi;
27549	  return(l);
27550	}

⌨️ 快捷键说明

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