1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
30 #define ENABLE_CURLX_PRINTF
31 /* use our own printf() functions */
34 #include "tool_dirhie.h"
36 #include "memdebug.h" /* keep this as LAST include */
39 # ifndef __NOVELL_LIBC__
40 # define mkdir mkdir_510
45 # define mkdir(x,y) (mkdir)((x))
51 static void show_dir_errno(FILE *errors, const char *name)
56 fprintf(errors, "You don't have permission to create %s.\n", name);
61 fprintf(errors, "The directory name %s is too long.\n", name);
66 fprintf(errors, "%s resides on a read-only file system.\n", name);
71 fprintf(errors, "No space left on the file system that will "
72 "contain the directory %s.\n", name);
77 fprintf(errors, "Cannot create directory %s because you "
78 "exceeded your quota.\n", name);
82 fprintf(errors, "Error creating directory %s.\n", name);
88 * Create the needed directory hierarchy recursively in order to save
89 * multi-GETs in file output, ie:
90 * curl "http://my.site/dir[1-5]/file[1-5].txt" -o "dir#1/file#2.txt"
91 * should create all the dir* automagically
95 /* systems that may use either or when specifying a path */
96 #define PATH_DELIMITERS "\\/"
98 #define PATH_DELIMITERS DIR_CHAR
102 CURLcode create_dir_hierarchy(const char *outfile, FILE *errors)
108 CURLcode result = CURLE_OK;
111 outlen = strlen(outfile);
112 outdup = strdup(outfile);
114 return CURLE_OUT_OF_MEMORY;
116 dirbuildup = malloc(outlen + 1);
118 Curl_safefree(outdup);
119 return CURLE_OUT_OF_MEMORY;
121 dirbuildup[0] = '\0';
123 /* Allow strtok() here since this isn't used threaded */
124 /* !checksrc! disable BANNEDFUNC 2 */
125 tempdir = strtok(outdup, PATH_DELIMITERS);
127 while(tempdir != NULL) {
128 tempdir2 = strtok(NULL, PATH_DELIMITERS);
129 /* since strtok returns a token for the last word even
130 if not ending with DIR_CHAR, we need to prune it */
131 if(tempdir2 != NULL) {
132 size_t dlen = strlen(dirbuildup);
134 snprintf(&dirbuildup[dlen], outlen - dlen, "%s%s", DIR_CHAR, tempdir);
136 if(outdup == tempdir)
137 /* the output string doesn't start with a separator */
138 strcpy(dirbuildup, tempdir);
140 snprintf(dirbuildup, outlen, "%s%s", DIR_CHAR, tempdir);
142 if(access(dirbuildup, F_OK) == -1) {
143 if(-1 == mkdir(dirbuildup, (mode_t)0000750)) {
144 show_dir_errno(errors, dirbuildup);
145 result = CURLE_WRITE_ERROR;
146 break; /* get out of loop */
153 Curl_safefree(dirbuildup);
154 Curl_safefree(outdup);