]> git.proxmox.com Git - libgit2.git/blame - src/posix.c
Merge pull request #968 from arrbee/diff-support-typechange
[libgit2.git] / src / posix.c
CommitLineData
bb742ede 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
bb742ede
VM
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 */
f79026b4
VM
7#include "common.h"
8#include "posix.h"
9#include "path.h"
10#include <stdio.h>
11#include <ctype.h>
12
7998ae5a
PB
13#ifndef GIT_WIN32
14
798e4d53
VM
15#ifdef NO_ADDRINFO
16int p_getaddrinfo(
17 const char *host,
18 const char *port,
19 struct addrinfo *hints,
20 struct addrinfo **info)
21{
22 GIT_UNUSED(hints);
23
24 struct addrinfo *ainfo, *ai;
25 int p = 0;
26
27 if ((ainfo = malloc(sizeof(struct addrinfo))) == NULL)
28 return -1;
29
30 if ((ainfo->ai_hostent = gethostbyname(host)) == NULL)
31 return -2;
32
33 ainfo->ai_servent = getservbyname(port, 0);
34
35 if (ainfo->ai_servent)
36 ainfo->ai_port = ainfo->ai_servent->s_port;
37 else
38 ainfo->ai_port = atol(port);
39
40 memcpy(&ainfo->ai_addr_in.sin_addr,
41 ainfo->ai_hostent->h_addr_list[0],
42 ainfo->ai_hostent->h_length);
43
44 ainfo->ai_protocol = 0;
45 ainfo->ai_socktype = hints->ai_socktype;
46 ainfo->ai_family = ainfo->ai_hostent->h_addrtype;
47 ainfo->ai_addr_in.sin_family = ainfo->ai_family;
48 ainfo->ai_addr_in.sin_port = ainfo->ai_port;
49 ainfo->ai_addr = (struct addrinfo *)&ainfo->ai_addr_in;
50 ainfo->ai_addrlen = sizeof(struct sockaddr_in);
51
52 *info = ainfo;
53
54 if (ainfo->ai_hostent->h_addr_list[1] == NULL) {
55 ainfo->ai_next = NULL;
56 return 0;
57 }
58
59 ai = ainfo;
60
61 for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
62 ai->ai_next = malloc(sizeof(struct addrinfo));
63 memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo));
64 memcpy(&ai->ai_next->ai_addr_in.sin_addr,
65 ainfo->ai_hostent->h_addr_list[p],
66 ainfo->ai_hostent->h_length);
67 ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in;
68 ai = ai->ai_next;
69 }
70
71 ai->ai_next = NULL;
72 return 0;
73}
74
75void p_freeaddrinfo(struct addrinfo *info)
76{
77 struct addrinfo *p, *next;
78
79 p = info;
80
81 while(p != NULL) {
82 next = p->ai_next;
83 free(p);
84 p = next;
85 }
86}
87
88const char *p_gai_strerror(int ret)
89{
90 switch(ret) {
91 case -1:
92 return "Out of memory";
93 break;
94
95 case -2:
96 return "Address lookup failed";
97 break;
98
99 default:
100 return "Unknown error";
101 break;
102 }
103}
104#endif /* NO_ADDRINFO */
105
3191ae89 106int p_open(const char *path, int flags, ...)
f79026b4 107{
3191ae89 108 mode_t mode = 0;
109
110 if (flags & O_CREAT)
111 {
112 va_list arg_list;
113
114 va_start(arg_list, flags);
19579847 115 mode = (mode_t)va_arg(arg_list, int);
3191ae89 116 va_end(arg_list);
117 }
118
119 return open(path, flags | O_BINARY, mode);
f79026b4
VM
120}
121
33127043 122int p_creat(const char *path, mode_t mode)
f79026b4
VM
123{
124 return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
125}
126
7998ae5a
PB
127int p_getcwd(char *buffer_out, size_t size)
128{
129 char *cwd_buffer;
130
131 assert(buffer_out && size > 0);
132
133 cwd_buffer = getcwd(buffer_out, size);
134
135 if (cwd_buffer == NULL)
dda708e7 136 return -1;
7998ae5a
PB
137
138 git_path_mkposix(buffer_out);
deafee7b 139 git_path_string_to_dir(buffer_out, size); /* append trailing slash */
97769280 140
deafee7b 141 return 0;
7998ae5a
PB
142}
143
0c49ec2d
CMN
144int p_rename(const char *from, const char *to)
145{
146 if (!link(from, to)) {
147 p_unlink(from);
dda708e7 148 return 0;
0c49ec2d
CMN
149 }
150
151 if (!rename(from, to))
dda708e7 152 return 0;
0c49ec2d 153
dda708e7 154 return -1;
0c49ec2d
CMN
155}
156
798e4d53 157#endif /* GIT_WIN32 */
7998ae5a 158
f79026b4
VM
159int p_read(git_file fd, void *buf, size_t cnt)
160{
161 char *b = buf;
162 while (cnt) {
44ef8b1b
RB
163 ssize_t r;
164#ifdef GIT_WIN32
165 assert((size_t)((unsigned int)cnt) == cnt);
166 r = read(fd, b, (unsigned int)cnt);
167#else
168 r = read(fd, b, cnt);
169#endif
f79026b4
VM
170 if (r < 0) {
171 if (errno == EINTR || errno == EAGAIN)
172 continue;
dda708e7 173 return -1;
f79026b4
VM
174 }
175 if (!r)
176 break;
177 cnt -= r;
178 b += r;
179 }
180 return (int)(b - (char *)buf);
181}
182
2ba222c5 183int p_write(git_file fd, const void *buf, size_t cnt)
f79026b4 184{
2ba222c5 185 const char *b = buf;
f79026b4 186 while (cnt) {
44ef8b1b
RB
187 ssize_t r;
188#ifdef GIT_WIN32
189 assert((size_t)((unsigned int)cnt) == cnt);
190 r = write(fd, b, (unsigned int)cnt);
191#else
192 r = write(fd, b, cnt);
193#endif
f79026b4
VM
194 if (r < 0) {
195 if (errno == EINTR || errno == EAGAIN)
196 continue;
dda708e7 197 return -1;
f79026b4
VM
198 }
199 if (!r) {
200 errno = EPIPE;
dda708e7 201 return -1;
f79026b4
VM
202 }
203 cnt -= r;
204 b += r;
205 }
dda708e7 206 return 0;
f79026b4 207}