nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1--- origin/main.c 2016-12-12 12:53:38.344285376 +0100
2+++ main.c 2016-12-12 13:01:41.134548824 +0100
3@@ -26,11 +26,13 @@
4 #include <string.h>
5
6 #ifdef USE_UNIX_REDIRECTION
7-#define DEVNULL ">/dev/null 2>&1"
8+#define DEVNULL "/dev/null"
9 #else
10-#define DEVNULL ">NUL 2>&1"
11+#define DEVNULL "NUL"
12 #endif
13
14+#include <errno.h>
15+
16 #include "crack.h"
17
18 int use_unzip;
19@@ -47,21 +49,77 @@
20 int REGPARAM
21 check_unzip (const char *pw)
22 {
23- char buff[1024];
24- int status;
25+pid_t cpid;
26+cpid = fork ();
27+if (cpid == -1)
28+ {
29+ perror ("fork");
30+ exit (EXIT_FAILURE);
31+ }
32+
33+if (cpid == 0)
34+ {
35+ // Redirect STDERR/STDOUT to /dev/null
36+ int oldfd_stderr, oldfd_stdout;
37+ oldfd_stdout = dup (fileno (stdout));
38+ if (oldfd_stdout == -1)
39+ {
40+ perror ("dup for stdout");
41+ _exit (127);
42+ }
43+ oldfd_stderr = dup (fileno (stderr));
44+ if (oldfd_stderr == -1)
45+ {
46+ perror ("dup for stderr");
47+ _exit (127);
48+ }
49+ if (freopen (DEVNULL, "w", stdout) == NULL)
50+ {
51+ perror ("freopen " DEVNULL " for stdout");
52+ _exit (127);
53+ }
54+ if (freopen (DEVNULL, "w", stderr) == NULL)
55+ {
56+ perror ("freopen " DEVNULL " for stderr");
57+ _exit (127);
58+ }
59+ execlp ("unzip", "unzip", "-qqtP", pw, file_path[0], NULL);
60+
61+ // When execlp failed.
62+ // Restores the stderr/stdout redirection to print an error.
63+ int errno_saved = errno;
64+ dup2 (oldfd_stderr, fileno (stderr));
65+ dup2 (oldfd_stdout, fileno (stdout));
66+ close (oldfd_stderr);
67+ close (oldfd_stdout);
68+ errno = errno_saved;
69+ perror ("execlp for unzip");
70+ _exit (127); // Returns 127 on error as system(3) does
71+ }
72
73- sprintf (buff, "unzip -qqtP \"%s\" %s " DEVNULL, pw, file_path[0]);
74- status = system (buff);
75-
76-#undef REDIR
77+ int status;
78
79- if (status == EXIT_SUCCESS)
80+ if (waitpid (cpid, &status, 0) == -1)
81 {
82- printf("\n\nPASSWORD FOUND!!!!: pw == %s\n", pw);
83+ perror ("waitpid");
84+ exit (EXIT_FAILURE);
85+ }
86+
87+ // The child process does not terminated normally, OR returns the exit status 127.
88+ if (!WIFEXITED (status)
89+ || (WIFEXITED (status) && (WEXITSTATUS (status) == 127)))
90+ {
91+ fprintf (stderr, "Executing unzip failed.\n");
92+ exit (EXIT_FAILURE);
93+ }
94+// unzip exited normally with the exit status 0 then...
95+ if (WIFEXITED (status) && (WEXITSTATUS (status) == EXIT_SUCCESS))
96+ {
97+ printf ("\n\nPASSWORD FOUND!!!!: pw == %s\n", pw);
98 exit (EXIT_SUCCESS);
99 }
100
101- return !status;
102+ return 0;
103 }
104
105 /* misc. callbacks. */