mpw.c

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

C
1,011
字号
/* MPW-Unix compatibility library.   Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.This file is part of the libiberty library.Libiberty is free software; you can redistribute it and/ormodify it under the terms of the GNU Library General PublicLicense as published by the Free Software Foundation; eitherversion 2 of the License, or (at your option) any later version.Libiberty is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULibrary General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with libiberty; see the file COPYING.LIB.  Ifnot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA.  *//* This should only be compiled and linked under MPW. */#include "mpw.h"#include <stdlib.h>#ifndef USE_MW_HEADERS#include <sys/time.h>#include <sys/resource.h>#endif#include <Types.h>#include <Files.h>#include <Timer.h>/* Initialize to 0 at first, then set to errno_max() later.  */int sys_nerr = 0;/* Debug flag for pathname hacking.  Set this to one and rebuild. */int DebugPI = -1;voidmpwify_filename(char *unixname, char *macname){  int i, j;  /* (should truncate 255 chars from end of name, not beginning) */  if (strlen (unixname) > 255)    {      fprintf (stderr, "Pathname \"%s\" is too long for Macs, truncating\n",	       unixname);    }  j = 0;  /* If you're going to end up with one or more colons in the middle of a     path after an all-Unix relative path is translated, you must add a     colon on the front, so that the first component is not thought to be     a disk name.  */  if (unixname[0] != '/' && ! strchr (unixname, ':') && strchr (unixname, '/'))    {      macname[j++] = ':';    }  for (i = 0; unixname[i] != '\0' && i < 255; ++i)    {      if (i == 0 && unixname[i] == '/')	{	  if (strncmp (unixname, "/tmp/", 5) == 0)	    {	      /* A temporary name, make a more Mac-flavored tmpname. */	      /* A better choice would be {Boot}Trash:foo, but		 that would require being able to identify the		 boot disk's and trashcan's name.  Another option		 would be to have an env var, so user can point it		 at a ramdisk. */	      macname[j++] = ':';	      macname[j++] = 't';	      macname[j++] = 'm';	      macname[j++] = 'p';	      macname[j++] = '_';	      i += 4;	    }	  else	    {	      /* Don't copy the leading slash. */	    }	}      else if (unixname[i] == ':' && unixname[i+1] == '/')	{	  macname[j++] = ':';	  i += 1;	}      else if (unixname[i] == '.' && unixname[i+1] == '/')	{	  macname[j++] = ':';	  i += 1;	}      else if (unixname[i] == '.' && unixname[i+1] == '.' && unixname[i+2] == '/')	{	  macname[j++] = ':';	  macname[j++] = ':';	  i += 2;	}      else if (unixname[i] == '/')	{	  macname[j++] = ':';	}      else	{	  macname[j++] = unixname[i];	}    }  macname[j] = '\0';  /* Allow for getting the debug flag from an env var; quite useful. */  if (DebugPI < 0)    DebugPI = (*(getenv ("DEBUG_PATHNAMES")) == '1' ? 1 : 0);  if (DebugPI)    {      fprintf (stderr, "# Made \"%s\"\n", unixname);      fprintf (stderr, "# into \"%s\"\n", macname);    }}/* MPW-flavored basename finder. */char *mpw_basename (name)  char *name;{  char *base = name;  while (*name)    {      if (*name++ == ':')	{	  base = name;	}    }  return base;}/* Mixed MPW/Unix basename finder.  This can be led astray by   filenames with slashes in them and come up with a basename that   either corresponds to no file or (worse) to some other file, so   should only be tried if other methods of finding a file via a   basename have failed.  */char *mpw_mixed_basename (name)  char *name;{  char *base = name;  while (*name)    {      if (*name == '/' || *name == ':')	{	  base = name + 1;	}      ++name;    }  return base;}/* This function is fopen() modified to create files that are type TEXT   or 'BIN ', and always of type 'MPS '.  */FILE *mpw_fopen (char *name, char *mode){#undef fopen  int errnum;  FILE *fp;  char tmpname[256];  mpwify_filename (name, tmpname);  PROGRESS (1);  fp = fopen (tmpname, mode);  errnum = errno;  /* If writing, need to set type and creator usefully. */  if (strchr (mode, 'w'))    {      char *pname = (char *) malloc (strlen (tmpname) + 2);      OSErr e;      struct FInfo fi;      pname[0] = strlen (tmpname);      strcpy (pname+1, tmpname);	      e = GetFInfo ((ConstStr255Param) pname, 0, &fi);      /* should do spiffier error handling */      if (e != 0)	fprintf(stderr, "GetFInfo returns %d\n", e);      if (strchr (mode, 'b'))	{	  fi.fdType = (OSType) 'BIN ';	}      else	{	  fi.fdType = (OSType) 'TEXT';	}      fi.fdCreator = (OSType) 'MPS ';      e = SetFInfo ((ConstStr255Param) pname, 0, &fi);      if (e != 0)	fprintf(stderr, "SetFInfo returns %d\n", e);      free (pname);    }  if (fp == NULL)    errno = errnum;  return fp;}/* This is a version of fseek() modified to fill the file with zeros   if seeking past the end of it.  */#define ZEROBLKSIZE 4096char zeros[ZEROBLKSIZE];intmpw_fseek (FILE *fp, int offset, int whence){#undef fseek  int cursize, numleft;  PROGRESS (1);  if (whence == SEEK_SET)    {      fseek (fp, 0, SEEK_END);      cursize = ftell (fp);      if (offset > cursize)	{	  numleft = offset - cursize;	  while (numleft > ZEROBLKSIZE)	    {	      /* This might fail, should check for that. */	      PROGRESS (1);	      fwrite (zeros, 1, ZEROBLKSIZE, fp);	      numleft -= ZEROBLKSIZE;	    }	  PROGRESS (1);	  fwrite (zeros, 1, numleft, fp);	  fflush (fp);	}    }  return fseek (fp, offset, whence);}intmpw_fread (char *ptr, int size, int nitems, FILE *stream){#undef fread  int rslt;  PROGRESS (1);  rslt = fread (ptr, size, nitems, stream);  PROGRESS (1);  return rslt;}intmpw_fwrite (char *ptr, int size, int nitems, FILE *stream){#undef fwrite  int rslt;  PROGRESS (1);  rslt = fwrite (ptr, size, nitems, stream);  PROGRESS (1);  return rslt;}intlink (){  fprintf (stderr, "link not available!\n");  mpw_abort ();}intfork (){  fprintf (stderr, "fork not available!\n");  mpw_abort ();}intvfork (){  fprintf (stderr, "vfork not available!\n");  mpw_abort ();  return (-1);}intpipe (int *fd){  fprintf (stderr, "pipe not available!\n");  mpw_abort ();  return (-1);}#ifndef USE_MW_HEADERSintexecvp (char *file, char **argv){  fprintf (stderr, "execvp not available!\n");  mpw_abort ();  return (-1);}intexecv (char *path, char **argv){  fprintf (stderr, "execv not available!\n");  mpw_abort ();  return (-1);}#endifintkill (int pid, int sig){  fprintf (stderr, "kill not available!\n");  mpw_abort ();  return (-1);}intwait (int *status){  *status = 0;  return 0;}#ifndef USE_MW_HEADERSintsleep (int seconds){  unsigned long start_time, now;  time (&start_time);  while (1)    {      PROGRESS (1);      time (&now);      if (now > start_time + seconds)	return 0;    }}#endifvoidputenv (char *str){  /* The GCC driver calls this to do things for collect2, but we     don't care about collect2. */}intchmod (char *path, int mode){  /* Pretend it was all OK. */  return 0;}#ifndef USE_MW_HEADERSintgetuid (){  /* One value is as good as another... */  return 0;}intgetgid (){  /* One value is as good as another... */  return 0;}#endif/* Instead of coredumping, which is not a normal Mac facility, we   drop into Macsbug.  If we then "g" from Macsbug, the program will   exit cleanly. */voidmpw_abort (){  /* Make sure no output still buffered up, then zap into MacsBug. */  fflush(stdout);  fflush(stderr);  printf("## Abort! ##\n");#ifdef MPW_SADE  SysError(8005);#else   Debugger();#endif  /* "g" in MacsBug will then cause a regular error exit. */  exit (1);}/* Imitation getrusage based on the ANSI clock() function. */intgetrusage (int who, struct rusage *rusage){  int clk = clock ();#if 0  rusage->ru_utime.tv_sec = clk / CLOCKS_PER_SEC;  rusage->ru_utime.tv_usec = ((clk * 1000) / CLOCKS_PER_SEC) * 1000;  rusage->ru_stime.tv_sec = 0;  rusage->ru_stime.tv_usec = 0;#endif}intsbrk (){  return 0;}#ifndef USE_MW_HEADERSintisatty (int fd){  return 0;}/* This is inherited from Timothy Murray's Posix library. */#include "utime.h"intutime (char *filename, struct utimbuf *times){  CInfoPBRec cipbr;  HFileInfo *fpb = (HFileInfo *) &cipbr;  DirInfo *dpb = (DirInfo *) &cipbr;  unsigned char pname[256];  short err;    strcpy ((char *) pname, filename);  c2pstr (pname);  dpb->ioDrDirID = 0L;  fpb->ioNamePtr = pname;  fpb->ioVRefNum = 0;  fpb->ioFDirIndex = 0;  fpb->ioFVersNum = 0;  err = PBGetCatInfo (&cipbr, 0);  if (err != noErr) {    errno = ENOENT;    return -1;  }  dpb->ioDrDirID = 0L;  fpb->ioFlMdDat = times->modtime;  fpb->ioFlCrDat = times->actime;  err = PBSetCatInfo (&cipbr, 0);  if (err != noErr) {    errno = EACCES;    return -1;  }  return 0;}intmkdir (char *path, int mode){  errno = ENOSYS;  return -1;}intrmdir (){  errno = ENOSYS;  return -1;}#endifchown (){  errno = ENOSYS;  return -1;}char *myenviron[] = {NULL};char **environ = myenviron;#ifndef USE_MW_HEADERS/* Minimal 'stat' emulation: tells directories from files and   gives length and mtime.   Derived from code written by Guido van Rossum, CWI, Amsterdam   and placed by him in the public domain.  */extern int __uid, __gid;int __uid = 0;int __gid = 0;

⌨️ 快捷键说明

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