mpw.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 1,011 行 · 第 1/2 页

C
1,011
字号
/* Bits in ioFlAttrib: */#define LOCKBIT	(1<<0)		/* File locked */#define DIRBIT	(1<<4)		/* It's a directory *//* Macified "stat" in which filename is given relative to a directory,   specified by long DirID.  */static int_stat (char *name, long dirid, struct stat *buf){  CInfoPBRec cipbr;  HFileInfo *fpb = (HFileInfo*) &cipbr;  DirInfo *dpb = (DirInfo*) &cipbr;  Str255 pname;  short err;  /* Make a temp copy of the name and pascalize. */  strcpy ((char *) pname, name);  c2pstr (pname);    cipbr.dirInfo.ioDrDirID = dirid;  cipbr.hFileInfo.ioNamePtr = pname;  cipbr.hFileInfo.ioVRefNum = 0;  cipbr.hFileInfo.ioFDirIndex = 0;  cipbr.hFileInfo.ioFVersNum = 0;  err = PBGetCatInfo (&cipbr, 0);  if (err != noErr)    {      errno = ENOENT;      return -1;    }  /* Mac files are readable if they can be accessed at all. */  buf->st_mode = 0444;  /* Mark unlocked files as writeable. */  if (!(fpb->ioFlAttrib & LOCKBIT))    buf->st_mode |= 0222;  if (fpb->ioFlAttrib & DIRBIT)    {      /* Mark directories as "executable". */      buf->st_mode |= 0111 | S_IFDIR;      buf->st_size = dpb->ioDrNmFls;      buf->st_rsize = 0;    }  else    {      buf->st_mode |= S_IFREG;      /* Mark apps as "executable". */      if (fpb->ioFlFndrInfo.fdType == 'APPL')	buf->st_mode |= 0111;      /* Fill in the sizes of data and resource forks. */      buf->st_size = fpb->ioFlLgLen;      buf->st_rsize = fpb->ioFlRLgLen;    }  /* Fill in various times. */  buf->st_atime = fpb->ioFlCrDat;  buf->st_mtime = fpb->ioFlMdDat;  buf->st_ctime = fpb->ioFlCrDat;  /* Set up an imitation inode number. */  buf->st_ino = (unsigned short) fpb->ioDirID;  /* Set up an imitation device. */  GetVRefNum (buf->st_ino, &buf->st_dev);  buf->st_uid = __uid;  buf->st_gid = __gid;/*  buf->st_FlFndrInfo = fpb->ioFlFndrInfo;  */  return 0;}/* stat() sets up an empty dirid. */intstat (char *path, struct stat *buf){  long rslt, errnum;  char tmpname[256];  mpwify_filename (path, tmpname);  if (DebugPI)    fprintf (stderr, "# stat (%s, %x)", tmpname, buf);  PROGRESS (1);  rslt = _stat (tmpname, 0L, buf);  errnum = errno;  if (DebugPI)    {      fprintf (stderr, " -> %d", rslt);      if (rslt != 0)	fprintf (stderr, " (errno is %d)", errnum);      fprintf (stderr, "\n");      fflush (stderr);    }  if (rslt != 0)    errno = errnum;  return rslt;}intfstat (int fd, struct stat *buf){  FCBPBRec fcb;  FILE *fp;  Str255 pathname;  long dirid = 0L, temp;  long rslt, errnum;  short err;  if (DebugPI < 0)    DebugPI = (*(getenv ("DEBUG_PATHNAMES")) == '1' ? 1 : 0);  if (DebugPI)    fprintf (stderr, "# fstat (%d, %x)", fd, buf);  PROGRESS (1);  pathname[0] = 0;#ifdef FIOFNAME  /* Use an MPW-specific ioctl to get the pathname associated with     the file descriptor.  */  ioctl (fd, FIOFNAME, (long *) pathname); #else  you lose#endif  if (DebugPI)    fprintf (stderr, " (name is %s)", pathname);  dirid = 0L /* fcb.ioFCBParID */ ;  rslt = _stat ((char *) pathname, dirid, buf);  errnum = errno;  if (DebugPI)    {      fprintf (stderr, " -> %d", rslt);      if (rslt != 0)	fprintf (stderr, " (errno is %d)", errnum);      fprintf (stderr, "\n");      fflush (stderr);    }  if (rslt != 0)    errno = errnum;  return rslt;}#endif /* n USE_MW_HEADERS */chdir (){  errno = ENOSYS;  return (-1);}char *getcwd (char *buf, int size){  if (buf == NULL)    buf = (char *) malloc (size);  strcpy(buf, ":");  return buf;}/* This should probably be more elaborate for MPW. */char *getpwd (){  return ":";}intmpw_open (char *filename, int arg2, int arg3){#undef open  int fd, errnum = 0;  char tmpname[256];  mpwify_filename (filename, tmpname);  fd = open (tmpname, arg2);  errnum = errno;  if (DebugPI)    {      fprintf (stderr, "# open (%s, %d, %d)", tmpname, arg2, arg3);      fprintf (stderr, " -> %d", fd);      if (fd == -1)	fprintf (stderr, " (errno is %d)", errnum);      fprintf (stderr, "\n");    }  if (fd == -1)    errno = errnum;  return fd;}intmpw_access (char *filename, unsigned int cmd){#undef access  int rslt, errnum = 0;  struct stat st;  char tmpname[256];  mpwify_filename (filename, tmpname);  if (cmd & R_OK || cmd & X_OK)    {      rslt = stat (tmpname, &st);      errnum = errno;      if (rslt >= 0)	{	  if ((((st.st_mode & 004) == 0) && (cmd & R_OK))	      || (((st.st_mode & 002) == 0) && (cmd & W_OK))	      || (((st.st_mode & 001) == 0) && (cmd & X_OK)))	    {	      rslt = -1;	      errnum = EACCES;	    }	}    }  if (DebugPI)    {      fprintf (stderr, "# mpw_access (%s, %d)", tmpname, cmd);      fprintf (stderr, " -> %d", rslt);      if (rslt != 0)	fprintf (stderr, " (errno is %d)", errnum);      fprintf (stderr, "\n");    }  if (rslt != 0)    errno = errnum;  return rslt;}/* The MPW library creat() has no mode argument. */intmpw_creat (char *path, /* mode_t */ int mode){#undef creat#ifdef USE_MW_HEADERS  return creat (path, mode);#else  return creat (path);#endif}/* This is a hack to get control in an MPW tool before it crashes the   machine.  */mpw_special_init (name)     char *name;{  if (strstr (name, "DEBUG"))    DebugStr("\pat beginning of program");}static int current_umask;intumask(int mask){  int oldmask = current_umask;  current_umask = mask;  return oldmask;}/* Cursor-spinning stuff that includes metering of spin rate and delays.  *//* Nonzero when cursor spinning has been set up properly.  */int cursor_inited;/* Nonzero if spin should be measured and excessive delays reported.  */int measure_spin;/* Nonzero if spin histogram and rate data should be written out.  */int dump_spin_data;long warning_threshold = 400000;long bucket_size = 1024;long bucket_power = 10;long numbuckets = 300;int *delay_counts;int overflow_count;char *current_progress;static UnsignedWide last_microseconds;static char *last_spin_file = "";static int last_spin_line;voidwarn_if_spin_delay (char *file, int line){  long diff, ix;  UnsignedWide now;  Microseconds(&now);  diff = now.lo - last_microseconds.lo;  if (diff > warning_threshold)    fprintf (stderr, "# %s: %ld.%06ld sec delay getting from %s:%d to %s:%d\n",	     (current_progress ? current_progress : ""),	     diff / 1000000, diff % 1000000,	     last_spin_file, last_spin_line, file, line);  if (dump_spin_data)    {      if (diff >= 0)	{	  ix = diff >> bucket_power;	  if (ix >= 0 && ix < numbuckets && delay_counts != NULL)	    ++delay_counts[ix];	  else	    ++overflow_count;	}      else	fprintf (stderr, "raw diff is %ld (?)\n", diff);    }}voidrecord_for_spin_delay (char *file, int line){  Microseconds (&last_microseconds);  last_spin_file = file;  last_spin_line = line;}voidmpw_start_progress (char *str, int n, char *file, int line){  int i;  char *measure, *threshold;  if (!cursor_inited)    {      InitCursorCtl (nil);      cursor_inited = 1;      record_for_spin_delay (file, line);      measure = getenv ("MEASURE_SPIN");      if (measure != NULL && measure[0] != '\0')	{	  measure_spin = 1;	  if (strcmp (measure, "all") == 0)	    dump_spin_data = 1;	}      threshold = getenv ("SPIN_WARN_THRESHOLD");      if (threshold != NULL && threshold[0] != '\0')	warning_threshold = atol (threshold);      if (dump_spin_data)	{	  if (delay_counts == NULL)	    delay_counts = (int *) malloc (numbuckets * sizeof (int));	  for (i = 0; i < numbuckets; ++i)	    delay_counts[i] = 0;	  overflow_count = 0;	}    }  current_progress = str;  sys_nerr = errno_max ();  mpw_special_init (str);}voidmpw_progress (int n){  SpinCursor (32);}voidmpw_progress_measured (int n, char *file, int line){  if (measure_spin)    warn_if_spin_delay (file, line);  SpinCursor (32);  if (measure_spin)    record_for_spin_delay (file, line);}voidmpw_end_progress (char *str, char *file, int line){  long i, delay, count = 0, sum = 0, avgdelay, spinrate;  long curpower = 0, curgroup = 0;  /* Warn if it's been a while since the last spin.  */  if (measure_spin)    warn_if_spin_delay (file, line);  /* Dump all the nonzero delay counts and an approximation of the delay.  */  if (dump_spin_data && delay_counts != NULL)    {      for (i = 0; i < numbuckets; ++i)	{	  delay = (i + 1) * bucket_size;	  sum += delay_counts[i] * (i + 1);	  count += delay_counts[i];	  if (delay <= (1 << curpower))	    {	      curgroup += delay_counts[i];	    }	  else	    {	      if (curgroup > 0)		fprintf (stderr,			 "# %s: %d delays between %ld.%06ld and %ld.%06ld sec\n",			 (str ? str : ""),			 curgroup,			 (1 << curpower) / 1000000,			 (1 << curpower) % 1000000,			 (1 << (curpower + 1)) / 1000000,			 (1 << (curpower + 1)) % 1000000);	      ++curpower;	      curgroup = 0;	    }	}      if (count > 0)	{	  avgdelay = (sum * bucket_size) / count;	  spinrate = 1000000 / avgdelay;	  fprintf (stderr, "# %s: Average spin rate is %d times/sec\n",		   (str ? str : ""), spinrate);	}    }}#ifdef PROGRESS_TEST/* Test program.  */main (){  int i, j;  double x = 1.0, y = 2.4;  long start = Microseconds (), tm;  FIXME  START_PROGRESS ("hi", 0);  for (i = 0; i < 1000; ++i)    {      PROGRESS (1);      for (j = 0; j < (i * 100); ++j)	{	  x += (x * y) / j;	}    }    END_PROGRESS ("hi");    tm = Microseconds () - start;  printf ("Total time is %d.%d secs\n", tm / 1000000, tm % 1000000);}#endif#ifdef USE_MW_HEADERS/* Empty definitions for Metrowerks' SIOUX console library. */#ifndef __CONSOLE__#include <console.h>#endifshortInstallConsole(short fd){#pragma unused (fd)	return 0;}voidRemoveConsole(void){}longWriteCharsToConsole(char *buf, long n){#pragma unused (buf, n)	return 0;}long ReadCharsFromConsole(char *buf, long n){#pragma unused (buf, n)	return 0;}extern char *__ttyname(long fd){	static char *__devicename = "null device";	if (fd >= 0 && fd <= 2)	  return (__devicename);	return NULL;}#endif

⌨️ 快捷键说明

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