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

📄 syscalls.cc

📁 cygwin, 著名的在win32下模拟unix操作系统的东东
💻 CC
📖 第 1 页 / 共 4 页
字号:
      (lnk_suffix = strrchr (real_new.get_win32 (), '.')))     *lnk_suffix = '\0';  if (!MoveFile (real_old, real_new))    res = -1;  if (res == 0 || (GetLastError () != ERROR_ALREADY_EXISTS		   && GetLastError () != ERROR_FILE_EXISTS))    goto done;  if (wincap.has_move_file_ex ())    {      if (MoveFileEx (real_old.get_win32 (), real_new.get_win32 (),		      MOVEFILE_REPLACE_EXISTING))	res = 0;    }  else    {      syscall_printf ("try win95 hack");      for (int i = 0; i < 2; i++)	{	  if (!DeleteFileA (real_new.get_win32 ()) &&	      GetLastError () != ERROR_FILE_NOT_FOUND)	    {	      syscall_printf ("deleting %s to be paranoid",			      real_new.get_win32 ());	      break;	    }	  else if (MoveFile (real_old.get_win32 (), real_new.get_win32 ()))	    {	      res = 0;	      break;	    }	}    }done:  if (res)    {      __seterrno ();      /* Reset R/O attributes if neccessary. */      if (real_new.has_attribute (FILE_ATTRIBUTE_READONLY))	SetFileAttributes (real_new, real_new);    }  else    {      /* make the new file have the permissions of the old one */      DWORD attr = real_old;#ifdef HIDDEN_DOT_FILES      char *c = strrchr (real_old.get_win32 (), '\\');      if ((c && c[1] == '.') || *real_old.get_win32 () == '.')	attr &= ~FILE_ATTRIBUTE_HIDDEN;      c = strrchr (real_new.get_win32 (), '\\');      if ((c && c[1] == '.') || *real_new.get_win32 () == '.')	attr |= FILE_ATTRIBUTE_HIDDEN;#endif      SetFileAttributes (real_new, attr);      /* Shortcut hack, No. 2, part 2 */      /* if the new filename was an existing shortcut, remove it now if the	 new filename is equal to the shortcut name without .lnk suffix. */      if (lnk_suffix)	{	  *lnk_suffix = '.';	  DeleteFile (real_new);	}    }  syscall_printf ("%d = rename (%s, %s)", res, (char *) real_old,		  (char *) real_new);  return res;}extern "C" intsystem (const char *cmdstring){  if (check_null_empty_str_errno (cmdstring))    return -1;  sigframe thisframe (mainthread);  int res;  const char* command[4];  _sig_func_ptr oldint, oldquit;  sigset_t child_block, old_mask;  if (cmdstring == (const char *) NULL)	return 1;  oldint = signal (SIGINT, SIG_IGN);  oldquit = signal (SIGQUIT, SIG_IGN);  sigemptyset (&child_block);  sigaddset (&child_block, SIGCHLD);  (void) sigprocmask (SIG_BLOCK, &child_block, &old_mask);  command[0] = "sh";  command[1] = "-c";  command[2] = cmdstring;  command[3] = (const char *) NULL;  if ((res = spawnvp (_P_WAIT, "sh", command)) == -1)    {      // when exec fails, return value should be as if shell      // executed exit (127)      res = 127;    }  signal (SIGINT, oldint);  signal (SIGQUIT, oldquit);  (void) sigprocmask (SIG_SETMASK, &old_mask, 0);  return res;}extern "C" intsetdtablesize (int size){  if (size <= (int)cygheap->fdtab.size || cygheap->fdtab.extend (size - cygheap->fdtab.size))    return 0;  return -1;}extern "C" intgetdtablesize (){  return cygheap->fdtab.size > OPEN_MAX ? cygheap->fdtab.size : OPEN_MAX;}extern "C" size_tgetpagesize (){  if (!system_info.dwPageSize)    GetSystemInfo (&system_info);  return (int) system_info.dwPageSize;}static intcheck_posix_perm (const char *fname, int v){  extern int allow_ntea, allow_ntsec, allow_smbntsec;  /* Windows 95/98/ME don't support file system security at all. */  if (!wincap.has_security ())    return 0;  /* ntea is ok for supporting permission bits but it doesn't support     full POSIX security settings. */  if (v == _PC_POSIX_PERMISSIONS && allow_ntea)    return 1;  if (!allow_ntsec)    return 0;  char *root = rootdir (strcpy ((char *)alloca (strlen (fname)), fname));  if (!allow_smbntsec      && ((root[0] == '\\' && root[1] == '\\')	  || GetDriveType (root) == DRIVE_REMOTE))    return 0;  DWORD vsn, len, flags;  if (!GetVolumeInformation (root, NULL, 0, &vsn, &len, &flags, NULL, 16))    {      __seterrno ();      return 0;    }  return (flags & FS_PERSISTENT_ACLS) ? 1 : 0;}/* FIXME: not all values are correct... */extern "C" long intfpathconf (int fd, int v){  cygheap_fdget cfd (fd);  if (cfd < 0)    return -1;  switch (v)    {    case _PC_LINK_MAX:      return _POSIX_LINK_MAX;    case _PC_MAX_CANON:    case _PC_MAX_INPUT:      if (isatty (fd))	return _POSIX_MAX_CANON;      else	{	  set_errno (EBADF);	  return -1;	}    case _PC_NAME_MAX:    case _PC_PATH_MAX:      return PATH_MAX;    case _PC_PIPE_BUF:      return PIPE_BUF;    case _PC_CHOWN_RESTRICTED:    case _PC_NO_TRUNC:      return -1;    case _PC_VDISABLE:      if (cfd->is_tty ())	return -1;      else	{	  set_errno (EBADF);	  return -1;	}    case _PC_POSIX_PERMISSIONS:    case _PC_POSIX_SECURITY:      if (cfd->get_device () == FH_DISK)	return check_posix_perm (cfd->get_win32_name (), v);      set_errno (EINVAL);      return -1;    default:      set_errno (EINVAL);      return -1;    }}extern "C" long intpathconf (const char *file, int v){  switch (v)    {    case _PC_PATH_MAX:      if (check_null_empty_str_errno (file))	  return -1;      return PATH_MAX - strlen (file);    case _PC_NAME_MAX:      return PATH_MAX;    case _PC_LINK_MAX:      return _POSIX_LINK_MAX;    case _PC_MAX_CANON:    case _PC_MAX_INPUT:      return _POSIX_MAX_CANON;    case _PC_PIPE_BUF:      return PIPE_BUF;    case _PC_CHOWN_RESTRICTED:    case _PC_NO_TRUNC:      return -1;    case _PC_VDISABLE:      return -1;    case _PC_POSIX_PERMISSIONS:    case _PC_POSIX_SECURITY:      {	path_conv full_path (file, PC_SYM_FOLLOW | PC_FULL);	if (full_path.error)	  {	    set_errno (full_path.error);	    return -1;	  }	if (full_path.is_device ())	  {	    set_errno (EINVAL);	    return -1;	  }	return check_posix_perm (full_path, v);      }    default:      set_errno (EINVAL);      return -1;    }}extern "C" char *ttyname (int fd){  cygheap_fdget cfd (fd);  if (cfd < 0 || !cfd->is_tty ())    {      return 0;    }  return (char *) (cfd->ttyname ());}extern "C" char *ctermid (char *str){  static NO_COPY char buf[16];  if (str == NULL)    str = buf;  if (!real_tty_attached (myself))    strcpy (str, "/dev/conin");  else    __small_sprintf (str, "/dev/tty%d", myself->ctty);  return str;}/* Tells stdio if it should do the cr/lf conversion for this file */extern "C" int_cygwin_istext_for_stdio (int fd){  syscall_printf ("_cygwin_istext_for_stdio (%d)", fd);  if (CYGWIN_VERSION_OLD_STDIO_CRLF_HANDLING)    {      syscall_printf (" _cifs: old API");      return 0; /* we do it for old apps, due to getc/putc macros */    }  cygheap_fdget cfd (fd, false, false);  if (cfd < 0)    {      syscall_printf (" _cifs: fd not open");      return 0;    }  if (cfd->get_device () != FH_DISK)    {      syscall_printf (" _cifs: fd not disk file");      return 0;    }  if (cfd->get_w_binary () || cfd->get_r_binary ())    {      syscall_printf (" _cifs: get_*_binary");      return 0;    }  syscall_printf ("_cygwin_istext_for_stdio says yes");  return 1;}/* internal newlib function */extern "C" int _fwalk (struct _reent *ptr, int (*function) (FILE *));static int setmode_mode;static int setmode_file;static intsetmode_helper (FILE *f){  if (fileno (f) != setmode_file)    return 0;  syscall_printf ("setmode: file was %s now %s",		 f->_flags & __SCLE ? "text" : "raw",		 setmode_mode & O_TEXT ? "text" : "raw");  if (setmode_mode & O_TEXT)    f->_flags |= __SCLE;  else    f->_flags &= ~__SCLE;  return 0;}extern "C" intgetmode (int fd){  cygheap_fdget cfd (fd);  if (cfd < 0)    return -1;  return cfd->get_flags () & (O_BINARY | O_TEXT);}/* Set a file descriptor into text or binary mode, returning the   previous mode.  */extern "C" intsetmode (int fd, int mode){  cygheap_fdget cfd (fd);  if (cfd < 0)    return -1;  if (mode != O_BINARY  && mode != O_TEXT && mode != 0)    {      set_errno (EINVAL);      return -1;    }  /* Note that we have no way to indicate the case that writes are     binary but not reads, or vice-versa.  These cases can arise when     using the tty or console interface.  People using those     interfaces should not use setmode.  */  int res;  if (cfd->get_w_binary () && cfd->get_r_binary ())    res = O_BINARY;  else if (cfd->get_w_binset () && cfd->get_r_binset ())    res = O_TEXT;	/* Specifically set O_TEXT */  else    res = 0;  if (!mode)    cfd->reset_to_open_binmode ();  else    cfd->set_flags ((cfd->get_flags () & ~(O_TEXT | O_BINARY)) | mode);  if (_cygwin_istext_for_stdio (fd))    setmode_mode = O_TEXT;  else    setmode_mode = O_BINARY;  setmode_file = fd;  _fwalk (_REENT, setmode_helper);  syscall_printf ("setmode (%d<%s>, %p) returns %s", fd, cfd->get_name (),		  mode, res & O_TEXT ? "text" : "binary");  return res;}extern "C" intftruncate64 (int fd, __off64_t length){  sigframe thisframe (mainthread);  int res = -1;  if (length < 0)    set_errno (EINVAL);  else    {      cygheap_fdget cfd (fd);      if (cfd >= 0)	{	  HANDLE h = cygheap->fdtab[fd]->get_handle ();	  if (cfd->get_handle ())	    {	      /* remember curr file pointer location */	      __off64_t prev_loc = cfd->lseek (0, SEEK_CUR);	      cfd->lseek (length, SEEK_SET);	      if (!SetEndOfFile (h))		__seterrno ();	      else		res = 0;	      /* restore original file pointer location */	      cfd->lseek (prev_loc, SEEK_SET);	    }	}    }  syscall_printf ("%d = ftruncate (%d, %d)", res, fd, length);  return res;}/* ftruncate: P96 5.6.7.1 */extern "C" intftruncate (int fd, __off32_t length){  return ftruncate64 (fd, (__off64_t)length);}/* truncate: Provided by SVR4 and 4.3+BSD.  Not part of POSIX.1 or XPG3 */extern "C" inttruncate64 (const char *pathname, __off64_t length){  sigframe thisframe (mainthread);  int fd;  int res = -1;  fd = open (pathname, O_RDWR);  if (fd == -1)    set_errno (EBADF);  else    {      res = ftruncate64 (fd, length);      close (fd);    }  syscall_printf ("%d = truncate (%s, %d)", res, pathname, length);  return res;}/* truncate: Provided by SVR4 and 4.3+BSD.  Not part of POSIX.1 or XPG3 */extern "C" inttruncate (const char *pathname, __off32_t length){  return truncate64 (pathname, (__off64_t)length);}extern "C" longget_osfhandle (int fd){  long res;  cygheap_fdget cfd (fd);  if (cfd >= 0)    res = (long) cfd->get_handle ();  else    res = -1;  syscall_printf ("%d = get_osfhandle (%d)", res, fd);  return res;}extern "C" intstatfs (const char *fname, struct statfs *sfs){  sigframe thisframe (mainthread);  if (!sfs)    {      set_errno (EFAULT);      return -1;    }  path_conv full_path (fname, PC_SYM_FOLLOW | PC_FULL);  char *root = rootdir (full_path);  syscall_printf ("statfs %s", root);  DWORD spc, bps, freec, totalc;  if (!GetDiskFreeSpace (root, &spc, &bps, &freec, &totalc))    {      __seterrno ();      return -1;    }  DWORD vsn, maxlen, flags;  if (!GetVolumeInformation (root, NULL, 0, &vsn, &maxlen, &flags, NULL, 0))    {      __seterrno ();      return -1;    }  sfs->f_type = flags;  sfs->f_bsize = spc*bps;  sfs->f_blocks = totalc;  sfs->f_bfree = sfs->f_bavail = freec;  sfs->f_files = -1;  sfs->f_ffree = -1;  sfs->f_fsid = vsn;  sfs->f_namelen = maxlen;  return 0;}extern "C" intfstatfs (int fd, struct statfs *sfs){  sigframe thisframe (mainthread);  cygheap_fdget cfd (fd);  if (cfd < 0)    return -1;  return statfs (cfd->get_name (), sfs);}/* setpgid: POSIX 4.3.3.1 */extern "C" intsetpgid (pid_t pid, pid_t pgid){  sigframe thisframe (mainthread);  int res = -1;  if (pid == 0)    pid = getpid ();  if (pgid == 0)    pgid = pid;  if (pgid < 0)    {      set_errno (EINVAL);      goto out;    }  else    {      pinfo p (pid);      if (!p)	{	  set_errno (ESRCH);	  goto out;	}      /* A process may only change the process group of itself and its children */      if (p == myself || p->ppid == myself->pid)	{	  p->pgid = pgid;	  if (p->pid != p->pgid)	    p->set_has_pgid_children (0);	  res = 0;	}      else	{	  set_errno (EPERM);	  goto out;	}    }out:  syscall_printf ("pid %d, pgid %d, res %d", pid, pgid, res);  return res;}extern "C" pid_tgetpgid (pid_t pid){  sigframe thisframe (mainthread);  if (pid == 0)    pid = getpid ();  pinfo p (pid);  if (p == 0)    {      set_errno (ESRCH);      return -1;    }  return p->pgid;}extern "C" intsetpgrp (void){  sigframe thisframe (mainthread);  return setpgid (0, 0);}extern "C" pid_tgetpgrp (void){  sigframe thisframe (mainthread);  return getpgid (0);}extern "C" char *ptsname (int fd){  sigframe thisframe (mainthread);  cygheap_fdget cfd (fd);  if (cfd < 0)    return 0;  return (char *) (cfd->ptsname ());}/* FIXME: what is this? */extern "C" int __declspec(dllexport)regfree (){  return 0;}/* mknod was the call to create directories before the introduction   of mkdir in 4.2BSD and SVR3.  Use of mknod required superuser privs   so the mkdir command had to be setuid root.   Although mknod hasn't been implemented yet, some GNU tools (e.g. the   fileutils) assume its existence so we must provide a stub that always   fails. */extern "C" intmknod (const char *_path, mode_t mode, dev_t dev){  set_errno (ENOSYS);  return -1;}extern "C" intmkfifo (const char *_path, mode_t mode){  set_errno (ENOSYS);  return -1;}/* seteuid: standards? */extern "C" intseteuid32 (__uid32_t uid){

⌨️ 快捷键说明

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