summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libunix/mkdir.c
blob: 2546cbff1e4618e8a86d8bad49b5413691d28a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* mkdir.c
   Create a directory.  We must go through a subsidiary program to
   force our real uid to be the uucp owner before invoking the setuid
   /bin/mkdir program.  */

#include "uucp.h"

#include "sysdep.h"

#include <errno.h>

int
mkdir (zdir, imode)
     const char *zdir;
     int imode;
{
  struct stat s;
  const char *azargs[3];
  int aidescs[3];
  pid_t ipid;

  /* Make sure the directory does not exist, since we will otherwise
     get the wrong errno value.  */
  if (stat (zdir, &s) == 0)
    {
      errno = EEXIST;
      return -1;
    }

  /* /bin/mkdir will create the directory with mode 777, so we set our
     umask to get the mode we want.  */
  (void) umask ((~ imode) & (S_IRWXU | S_IRWXG | S_IRWXO));

  azargs[0] = UUDIR_PROGRAM;
  azargs[1] = zdir;
  azargs[2] = NULL;
  aidescs[0] = SPAWN_NULL;
  aidescs[1] = SPAWN_NULL;
  aidescs[2] = SPAWN_NULL;

  ipid = ixsspawn (azargs, aidescs, TRUE, FALSE, (const char *) NULL,
		   TRUE, FALSE, (const char *) NULL,
		   (const char *) NULL, (const char *) NULL);

  (void) umask (0);

  if (ipid < 0)
    return -1;

  if (ixswait ((unsigned long) ipid, (const char *) NULL) != 0)
    {
      /* Make up an errno value.  */
      errno = EACCES;
      return -1;
    }

  return 0;
}
OpenPOWER on IntegriCloud