changeset 1408:f1fbe4cdd75c

[project @ 1995-09-15 06:39:28 by jwe] Initial revision
author jwe
date Fri, 15 Sep 1995 06:39:28 +0000
parents 9517c453c9ab
children e282214c41a5
files liboctave/mkdir.c liboctave/rename.c liboctave/rmdir.c liboctave/safe-xstat.cin liboctave/safe-xstat.hin
diffstat 5 files changed, 403 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/mkdir.c	Fri Sep 15 06:39:28 1995 +0000
@@ -0,0 +1,103 @@
+/* mkdir.c -- BSD compatible make directory function for System V
+   Copyright (C) 1988, 1990 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifndef HAVE_MKDIR
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#ifdef STAT_MACROS_BROKEN
+#undef S_ISDIR
+#endif
+
+#if !defined(S_ISDIR) && defined(S_IFDIR)
+#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
+#endif
+
+#include "safe-stat.h"
+
+/* mkdir adapted from GNU tar.  */
+
+/* Make directory DPATH, with permission mode DMODE.
+
+   Written by Robert Rother, Mariah Corporation, August 1985
+   (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
+
+   Severely hacked over by John Gilmore to make a 4.2BSD compatible
+   subroutine.	11Mar86; hoptoad!gnu
+
+   Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
+   subroutine didn't return EEXIST.  It does now.  */
+
+int
+mkdir (dpath, dmode)
+     char *dpath;
+     int dmode;
+{
+  int cpid, status;
+  struct stat statbuf;
+
+  if (SAFE_STAT (dpath, &statbuf) == 0)
+    {
+      errno = EEXIST;		/* stat worked, so it already exists.  */
+      return -1;
+    }
+
+  /* If stat fails for a reason other than non-existence, return error.  */
+  if (errno != ENOENT)
+    return -1;
+
+  cpid = fork ();
+  switch (cpid)
+    {
+    case -1:			/* Cannot fork.  */
+      return -1;		/* errno is already set.  */
+
+    case 0:			/* Child process.  */
+      /* Cheap hack to set mode of new directory.  Since this child
+	 process is going away anyway, we zap its umask.
+	 This won't suffice to set SUID, SGID, etc. on this
+	 directory, so the parent process calls chmod afterward.  */
+      status = umask (0);	/* Get current umask.  */
+      umask (status | (0777 & ~dmode));	/* Set for mkdir.  */
+      execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
+      _exit (1);
+
+    default:			/* Parent process.  */
+      /* Wait for kid to finish.  */
+      while (wait (&status) != cpid)
+	/* Do nothing.  */ ;
+
+      if (status & 0xFFFF)
+	{
+	  /* /bin/mkdir failed.  */
+	  errno = EIO;
+	  return -1;
+	}
+      return chmod (dpath, dmode);
+    }
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/rename.c	Fri Sep 15 06:39:28 1995 +0000
@@ -0,0 +1,109 @@
+/* rename.c -- BSD compatible directory function for System V
+   Copyright (C) 1988, 1990 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifndef HAVE_RENAME
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#ifdef STAT_MACROS_BROKEN
+#undef S_ISDIR
+#endif /* STAT_MACROS_BROKEN.  */
+
+#if !defined(S_ISDIR) && defined(S_IFDIR)
+#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
+#endif
+
+#include "safe-stat.h"
+
+/* Rename file FROM to file TO.
+   Return 0 if successful, -1 if not. */
+
+int
+rename (from, to)
+     char *from;
+     char *to;
+{
+  struct stat from_stats, to_stats;
+  int pid, status;
+
+  if (SAFE_STAT (from, &from_stats))
+    return -1;
+
+  /* Be careful not to unlink `from' if it happens to be equal to `to' or
+     (on filesystems that silently truncate filenames after 14 characters)
+     if `from' and `to' share the significant characters. */
+  if (SAFE_STAT (to, &to_stats))
+    {
+      if (errno != ENOENT)
+        return -1;
+    }
+  else
+    {
+      if ((from_stats.st_dev == to_stats.st_dev)
+          && (from_stats.st_ino == to_stats.st_dev))
+        /* `from' and `to' designate the same file on that filesystem. */
+        return 0;
+
+      if (unlink (to) && errno != ENOENT)
+        return -1;
+    }
+
+  if (S_ISDIR (from_stats.st_mode))
+    {
+      /* Need a setuid root process to link and unlink directories. */
+      pid = fork ();
+      switch (pid)
+	{
+	case -1:		/* Error. */
+	  error (1, errno, "cannot fork");
+
+	case 0:			/* Child. */
+	  execl (MVDIR, "mvdir", from, to, (char *) 0);
+	  error (255, errno, "cannot run `%s'", MVDIR);
+
+	default:		/* Parent. */
+	  while (wait (&status) != pid)
+	    /* Do nothing. */ ;
+
+	  errno = 0;		/* mvdir printed the system error message. */
+	  if (status)
+	    return -1;
+	}
+    }
+  else
+    {
+      if (link (from, to))
+	return -1;
+      if (unlink (from) && errno != ENOENT)
+	{
+	  unlink (to);
+	  return -1;
+	}
+    }
+  return 0;
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/rmdir.c	Fri Sep 15 06:39:28 1995 +0000
@@ -0,0 +1,92 @@
+/* rmdir.c -- BSD compatible remove directory function for System V
+   Copyright (C) 1988, 1990 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifndef HAVE_RMDIR
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#ifdef STAT_MACROS_BROKEN
+#undef S_ISDIR
+#endif
+
+#if !defined(S_ISDIR) && defined(S_IFDIR)
+#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
+#endif
+
+#include "safe-stat.h"
+
+/* rmdir adapted from GNU tar.  */
+
+/* Remove directory DPATH.
+   Return 0 if successful, -1 if not.  */
+
+int
+rmdir (dpath)
+     char *dpath;
+{
+  int cpid, status;
+  struct stat statbuf;
+
+  if (SAFE_STAT (dpath, &statbuf) != 0)
+    return -1;			/* errno already set */
+
+  if (!S_ISDIR (statbuf.st_mode))
+    {
+      errno = ENOTDIR;
+      return -1;
+    }
+
+  cpid = fork ();
+  switch (cpid)
+    {
+    case -1:			/* cannot fork */
+      return -1;		/* errno already set */
+
+    case 0:			/* child process */
+      execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
+      _exit (1);
+
+    default:			/* parent process */
+
+      /* Wait for kid to finish.  */
+
+      while (wait (&status) != cpid)
+	/* Do nothing.  */ ;
+
+      if (status & 0xFFFF)
+	{
+
+	  /* /bin/rmdir failed.  */
+
+	  errno = EIO;
+	  return -1;
+	}
+      return 0;
+    }
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/safe-xstat.cin	Fri Sep 15 06:39:28 1995 +0000
@@ -0,0 +1,28 @@
+/* safe-@l@stat.c -- EINTR-safe interface to @l@stat
+   Copyright (C) 1994 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   
+/* Written by Jim Meyering <meyering@comco.com>.  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#define __static /* empty */
+#include "safe-@l@stat.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/safe-xstat.hin	Fri Sep 15 06:39:28 1995 +0000
@@ -0,0 +1,71 @@
+/* safe-@l@stat.h -- EINTR-safe interface to @l@stat
+   Copyright (C) 1994 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   
+/* Written by Jim Meyering <meyering@comco.com>.  */
+
+#ifndef _safe_@l@stat_h_
+#define _safe_@l@stat_h_ 1
+
+/* NOTE: you must include the following headers (in the listed order)
+   before this one: <sys/types.h>, <sys/stat.h>.  */
+
+#if !defined(S_ISLNK) && defined(S_IFLNK)			@LSTAT_ONLY@
+#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)			@LSTAT_ONLY@
+#endif								@LSTAT_ONLY@
+								@LSTAT_ONLY@
+#ifndef S_ISLNK							@LSTAT_ONLY@
+#include "safe-stat.h"						@LSTAT_ONLY@
+#define SAFE_LSTAT SAFE_STAT					@LSTAT_ONLY@
+#define safe_lstat safe_stat					@LSTAT_ONLY@
+#else								@LSTAT_ONLY@
+								@LSTAT_ONLY@
+#include <errno.h>
+
+#ifndef errno
+extern int errno;
+#endif
+
+#ifndef __GNUC__
+#define __inline /* empty */
+#endif
+
+/* On some systems, @l@stat can return EINTR.  */
+
+#ifndef EINTR
+# define SAFE_@L@STAT(name, buf) @l@stat (name, buf)
+#else
+# ifndef __static
+#   define __static static
+# endif
+# define SAFE_@L@STAT(name, buf) safe_@l@stat (name, buf)
+__static __inline int
+safe_@l@stat (name, buf)
+     const char *name;
+     struct stat *buf;
+{
+  int ret;
+
+  do
+    ret = @l@stat (name, buf);
+  while (ret < 0 && errno == EINTR);
+
+  return ret;
+}
+#endif
+
+#endif /* S_ISLNK */						@LSTAT_ONLY@
+#endif /* _safe_@l@stat_h_ */