* New version 2.26
[alpine.git] / pith / osdep / creatdir.c
blobccddbcb3db00157d2930edef8b1d6c07b4543caa
1 /*
2 * ========================================================================
3 * Copyright 2006-2007 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include <system.h>
16 #include "../charconv/utf8.h"
17 #include "../charconv/filesys.h"
18 #include "creatdir.h"
21 #ifdef S_IRWXU
22 #define MAILDIR_MODE S_IRWXU
23 #else
24 #define MAILDIR_MODE 0700
25 #endif
29 /*----------------------------------------------------------------------
30 Create the mail subdirectory.
32 Args: dir -- Name of the directory to create
34 Result: Directory is created. Returns 0 on success, else -1 on error
35 and errno is valid.
36 ----*/
37 int
38 create_mail_dir(char *dir)
40 if(our_mkdir(dir, MAILDIR_MODE) < 0)
41 return(-1);
43 #ifndef _WINDOWS
44 our_chmod(dir, MAILDIR_MODE);
46 /* Some systems need this, on others we don't care if it fails */
47 our_chown(dir, getuid(), getgid());
48 #endif /* !_WINDOWS */
50 return(0);
54 /*----------------------------------------------------------------------
55 Create random directory
57 Args: dir -- Name of the directory that contains the random directory
58 len -- size of dir.
60 Result: Directory is created. Returns 0 on success, else -1 on error
61 and errno is valid.
62 ----*/
63 int
64 create_random_dir(char *dir, size_t len)
66 size_t olen, dlen = strlen(dir);
68 olen = dlen; /* save original length */
70 if(dir[dlen-1] != C_FILESEP){
71 dir[dlen++] = C_FILESEP;
72 dir[dlen] = '\0';
75 if(dlen + 6 < len)
76 strcat(dir, "XXXXXX");
77 else{
78 dir[olen] = '\0';
79 return -1;
82 #ifndef _WINDOWS
83 dir = mkdtemp(dir);
84 our_chmod(dir, MAILDIR_MODE);
86 /* Some systems need this, on others we don't care if it fails */
87 our_chown(dir, getuid(), getgid());
88 #else
89 { int i;
90 char *s = &dir[strlen(dir) - 6];
91 for(i = 0; i < 10; i++){
92 sprintf(s, "%02x%02x%02x", (unsigned int)(random() % 256), (unsigned int)(random() % 256),
93 (unsigned int)(random() % 256));
94 if(our_mkdir(dir, 0700) == 0) return dir;
96 *dir = '\0'; /* if we are here, we failed! */
98 #endif /* !_WINDOWS */
100 return(0);