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 * ========================================================================
16 #include "../charconv/utf8.h"
17 #include "../charconv/filesys.h"
22 #define MAILDIR_MODE S_IRWXU
24 #define MAILDIR_MODE 0700
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
38 create_mail_dir(char *dir
)
40 if(our_mkdir(dir
, MAILDIR_MODE
) < 0)
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 */
54 /*----------------------------------------------------------------------
55 Create random directory
57 Args: dir -- Name of the directory that contains the random directory
60 Result: Directory is created. Returns 0 on success, else -1 on error
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
;
76 strcat(dir
, "XXXXXX");
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());
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 */