Stanislav Kholmanskikh | 1d5d484 | 2013-08-14 10:01:46 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | * |
| 18 | * Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com> |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "test.h" |
| 30 | |
| 31 | int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount) |
| 32 | { |
| 33 | int fd, counter; |
| 34 | char *buf; |
| 35 | |
| 36 | fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR); |
| 37 | if (fd < 0) |
| 38 | return -1; |
| 39 | |
| 40 | /* Filling a memory buffer with provided pattern */ |
| 41 | buf = malloc(bs); |
| 42 | if (buf == NULL) { |
| 43 | close(fd); |
| 44 | |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | for (counter = 0; counter < bs; counter++) |
| 49 | buf[counter] = pattern; |
| 50 | |
| 51 | /* Filling the file */ |
| 52 | for (counter = 0; counter < bcount; counter++) { |
| 53 | if (write(fd, buf, bs) != bs) { |
| 54 | free(buf); |
| 55 | close(fd); |
| 56 | unlink(path); |
| 57 | |
| 58 | return -1; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | free(buf); |
| 63 | |
| 64 | if (close(fd) < 0) { |
| 65 | unlink(path); |
| 66 | |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |