]> git.proxmox.com Git - libgit2.git/blob - src/posix.c
Merge pull request #411 from boyski/gcc4
[libgit2.git] / src / posix.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #include "common.h"
8 #include "posix.h"
9 #include "path.h"
10 #include <stdio.h>
11 #include <ctype.h>
12
13 int p_open(const char *path, int flags)
14 {
15 return open(path, flags | O_BINARY);
16 }
17
18 int p_creat(const char *path, int mode)
19 {
20 return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
21 }
22
23 int p_read(git_file fd, void *buf, size_t cnt)
24 {
25 char *b = buf;
26 while (cnt) {
27 ssize_t r = read(fd, b, cnt);
28 if (r < 0) {
29 if (errno == EINTR || errno == EAGAIN)
30 continue;
31 return GIT_EOSERR;
32 }
33 if (!r)
34 break;
35 cnt -= r;
36 b += r;
37 }
38 return (int)(b - (char *)buf);
39 }
40
41 int p_write(git_file fd, const void *buf, size_t cnt)
42 {
43 const char *b = buf;
44 while (cnt) {
45 ssize_t r = write(fd, b, cnt);
46 if (r < 0) {
47 if (errno == EINTR || errno == EAGAIN)
48 continue;
49 return GIT_EOSERR;
50 }
51 if (!r) {
52 errno = EPIPE;
53 return GIT_EOSERR;
54 }
55 cnt -= r;
56 b += r;
57 }
58 return GIT_SUCCESS;
59 }
60
61 int p_getcwd(char *buffer_out, size_t size)
62 {
63 char *cwd_buffer;
64
65 assert(buffer_out && size > 0);
66
67 #ifdef GIT_WIN32
68 cwd_buffer = _getcwd(buffer_out, size);
69 #else
70 cwd_buffer = getcwd(buffer_out, size);
71 #endif
72
73 if (cwd_buffer == NULL)
74 return git__throw(GIT_EOSERR, "Failed to retrieve current working directory");
75
76 git_path_mkposix(buffer_out);
77
78 git_path_join(buffer_out, buffer_out, ""); //Ensure the path ends with a trailing slash
79 return GIT_SUCCESS;
80 }