WSLUA_CONSTRUCTOR Dir_make(lua_State* L) { /* Creates a directory. The created directory is set for permission mode 0755 (octal), meaning it is read+write+execute by owner, but only read+execute by group and others. IF the directory was created successfully, a boolean `true` is returned. If the directory cannot be made because it already exists, `false` is returned. If the directory cannot be made because an error occurred, `nil` is returned. @since 1.11.3 */ #define WSLUA_ARG_Dir_make_NAME 1 /* The name of the directory, possibly including path. */ const char *dir_path = luaL_checkstring(L, WSLUA_ARG_Dir_make_NAME); ws_statb64 s_buf; int ret; if (ws_stat64(dir_path, &s_buf) != 0 && errno == ENOENT) { ret = ws_mkdir(dir_path, 0755); if (ret == -1) { lua_pushnil(L); } else { lua_pushboolean(L, 1); } } else { lua_pushboolean(L, 0); } WSLUA_RETURN(1); /* Boolean `true` on success, `false` if already exists, `nil` on error. */ }
/* Generate a unique temporary directory name from TEMPLATE. The last six characters of TEMPLATE must be TMP_FILE_SUFFIX; they are replaced with a string that makes the filename unique. Returns 0 on success or -1 on error (from mkdir(2)). */ char * mkdtemp (char *path_template) { static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; size_t len; size_t i; len = strlen (path_template); if (len < 6 || strcmp (&path_template[len - 6], TMP_FILE_SUFFIX)) { __set_errno (EINVAL); return NULL; } if (g_snprintf (&path_template[len - 5], 6, "%.5u", (unsigned int) ws_getpid () % 100000) != 5) /* Inconceivable lossage. */ return NULL; for (i = 0; i < sizeof (letters); ++i) { int ret; path_template[len - 6] = letters[i]; ret = ws_mkdir(path_template, 0700); if (ret >= 0) return path_template; } /* We return the null string if we can't find a unique file name. */ path_template[0] = '\0'; return NULL; }