diff options
Diffstat (limited to 'tests/compat/mkstemp.c')
| -rw-r--r-- | tests/compat/mkstemp.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/compat/mkstemp.c b/tests/compat/mkstemp.c new file mode 100644 index 0000000..fe3d15a --- /dev/null +++ b/tests/compat/mkstemp.c | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | /* | ||
| 2 | musl as a whole is licensed under the following standard MIT license: | ||
| 3 | |||
| 4 | ---------------------------------------------------------------------- | ||
| 5 | Copyright © 2005-2020 Rich Felker, et al. | ||
| 6 | |||
| 7 | Permission is hereby granted, free of charge, to any person obtaining | ||
| 8 | a copy of this software and associated documentation files (the | ||
| 9 | "Software"), to deal in the Software without restriction, including | ||
| 10 | without limitation the rights to use, copy, modify, merge, publish, | ||
| 11 | distribute, sublicense, and/or sell copies of the Software, and to | ||
| 12 | permit persons to whom the Software is furnished to do so, subject to | ||
| 13 | the following conditions: | ||
| 14 | |||
| 15 | The above copyright notice and this permission notice shall be | ||
| 16 | included in all copies or substantial portions of the Software. | ||
| 17 | |||
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| 21 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
| 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
| 23 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
| 24 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 25 | ---------------------------------------------------------------------- | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <string.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | #include <fcntl.h> | ||
| 32 | #include <unistd.h> | ||
| 33 | #include <limits.h> | ||
| 34 | #include <errno.h> | ||
| 35 | |||
| 36 | #include <io.h> | ||
| 37 | |||
| 38 | int mkstemp(char *template) | ||
| 39 | { | ||
| 40 | int fd; | ||
| 41 | retry: | ||
| 42 | if (!_mktemp(template)) return -1; | ||
| 43 | fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600); | ||
| 44 | if (fd >= 0) return fd; | ||
| 45 | if (errno == EEXIST) { | ||
| 46 | /* this is safe because mktemp verified | ||
| 47 | * that we have a valid template string */ | ||
| 48 | strcpy(template+strlen(template)-6, "XXXXXX"); | ||
| 49 | goto retry; | ||
| 50 | } | ||
| 51 | return -1; | ||
| 52 | } | ||
