Pledge archivers/p7zip main executables

--- CPP/7zip/UI/Console/Main.cpp.orig	Sat Oct 17 11:20:22 2015
+++ CPP/7zip/UI/Console/Main.cpp	Sun Jan 24 21:51:01 2016
@@ -484,6 +484,20 @@ int Main2(
   #endif
 )
 {
+
+#ifndef EXTERNAL_CODECS
+
+#ifdef PLEDGE_DEBUG
+  printf("initial pledge: 7za 7zr\n");
+#endif
+
+  if (pledge("stdio rpath wpath cpath fattr tty ps", NULL) == -1) {
+    perror("pledge");
+    exit(2);
+  }
+
+#endif
+
   #if defined(_WIN32) && !defined(UNDER_CE)
   SetFileApisToOEM();
   #endif
@@ -580,6 +594,19 @@ int Main2(
   codecs->CaseSensitive = options.CaseSensitive;
   ThrowException_if_Error(codecs->Load());
 
+#ifdef EXTERNAL_CODECS
+
+#ifdef PLEDGE_DEBUG
+  printf("initial pledge: 7z\n");
+#endif
+
+  if (pledge("stdio rpath wpath cpath fattr tty ps", NULL) == -1) {
+    perror("pledge");
+    exit(2);
+  }
+
+#endif
+
   bool isExtractGroupCommand = options.Command.IsFromExtractGroup();
 
   if (codecs->Formats.Size() == 0 &&
@@ -636,6 +663,16 @@ int Main2(
 
   if (options.Command.CommandType == NCommandType::kInfo)
   {
+
+#ifdef PLEDGE_DEBUG
+  printf("reduced pledge: info\n");
+#endif
+
+  if (pledge("stdio", NULL) == -1) {
+    perror("pledge");
+    exit(2);
+  }
+
     CStdOutStream &so = (g_StdStream ? *g_StdStream : g_StdOut);
     unsigned i;
 
@@ -802,6 +839,16 @@ int Main2(
   }
   else if (options.Command.CommandType == NCommandType::kBenchmark)
   {
+
+#ifdef PLEDGE_DEBUG
+  printf("reduced pledge: benchmark\n");
+#endif
+
+  if (pledge("stdio ps", NULL) == -1) {
+    perror("pledge");
+    exit(2);
+  }
+
     CStdOutStream &so = (g_StdStream ? *g_StdStream : g_StdOut);
     hresultMain = BenchCon(EXTERNAL_CODECS_VARS_L
         options.Properties, options.NumIterations, (FILE *)so);
@@ -869,6 +916,17 @@ int Main2(
     if (hresultMain == S_OK)
     if (isExtractGroupCommand)
     {
+
+      if (options.Command.IsTestCommand() || options.StdOutMode) {
+#ifdef PLEDGE_DEBUG
+          printf("reduced pledge: test stdout\n");
+#endif
+          if (pledge("stdio rpath tty", NULL) == -1) {
+              perror("pledge");
+              exit(2);
+	  }
+      }
+
       CExtractCallbackConsole *ecs = new CExtractCallbackConsole;
       CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
 
@@ -1029,6 +1087,16 @@ int Main2(
     }
     else
     {
+
+#ifdef PLEDGE_DEBUG
+  printf("reduced pledge: list\n");
+#endif
+
+  if (pledge("stdio rpath tty", NULL) == -1) {
+    perror("pledge");
+    exit(2);
+  }
+
       UInt64 numErrors = 0;
       UInt64 numWarnings = 0;
       
@@ -1139,6 +1207,16 @@ int Main2(
   }
   else if (options.Command.CommandType == NCommandType::kHash)
   {
+
+#ifdef PLEDGE_DEBUG
+  printf("reduced pledge: hash\n");
+#endif
+
+  if (pledge("stdio rpath", NULL) == -1) {
+    perror("pledge");
+    exit(2);
+  }
+
     const CHashOptions &uo = options.HashOptions;
 
     CHashCallbackConsole callback;
