ZDR: remove dependency on zdr.h from the examples and nfs-ls
[deb_libnfs.git] / examples / nfs-io.c
1 /*
2 Copyright (C) by Peter Lieven <pl@kamp.de> 2013
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define _FILE_OFFSET_BITS 64
19 #define _GNU_SOURCE
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef AROS
26 #include "aros_compat.h"
27 #endif
28
29 #ifdef WIN32
30 #include "win32_compat.h"
31 #pragma comment(lib, "ws2_32.lib")
32 WSADATA wsaData;
33 #define PRId64 "ll"
34 #else
35 #include <inttypes.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <sys/statvfs.h>
39 #ifndef AROS
40 #include <sys/statvfs.h>
41 #endif
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdint.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include "libnfs.h"
55 #include "libnfs-raw.h"
56 #include "libnfs-raw-mount.h"
57
58 void print_usage(void)
59 {
60 fprintf(stderr, "Usage: nfs-io [-?|--help|--usage] [stat|creat|unlink|mkdir|rmdir] <url>\n");
61 }
62
63 int main(int argc, char *argv[])
64 {
65 int ret = 1;
66 struct nfs_context *nfs = NULL;
67 struct nfsfh *nfsfh = NULL;
68 struct nfs_url *url = NULL;
69
70 #ifdef WIN32
71 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
72 printf("Failed to start Winsock2\n");
73 exit(10);
74 }
75 #endif
76
77 #ifdef AROS
78 aros_init_socket();
79 #endif
80
81 if (argc < 3) {
82 fprintf(stderr, "No URL specified.\n");
83 goto finished;
84 }
85
86 nfs = nfs_init_context();
87 if (nfs == NULL) {
88 printf("failed to init context\n");
89 goto finished;
90 }
91
92 url = nfs_parse_url_full(nfs, argv[argc - 1]);
93 if (url == NULL) {
94 fprintf(stderr, "%s\n", nfs_get_error(nfs));
95 goto finished;
96 }
97
98 if (nfs_mount(nfs, url->server, url->path) != 0) {
99 fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
100 goto finished;
101 }
102
103 if (!strncmp(argv[1], "creat", 5)) {
104 ret = nfs_creat(nfs, url->file, 0600, &nfsfh);
105 } else if (!strncmp(argv[1], "unlink", 6)) {
106 ret = nfs_unlink(nfs, url->file);
107 } else if (!strncmp(argv[1], "mkdir", 5)) {
108 ret = nfs_mkdir(nfs, url->file);
109 } else if (!strncmp(argv[1], "rmdir", 5)) {
110 ret = nfs_rmdir(nfs, url->file);
111 } else if (!strncmp(argv[1], "stat", 4)) {
112 struct stat st;
113 ret = nfs_stat(nfs, url->file, &st);
114 if (!ret) {
115 switch (st.st_mode & S_IFMT) {
116 #ifndef WIN32
117 case S_IFLNK:
118 printf("l");
119 break;
120 #endif
121 case S_IFREG:
122 printf("-");
123 break;
124 case S_IFDIR:
125 printf("d");
126 break;
127 case S_IFCHR:
128 printf("c");
129 break;
130 case S_IFBLK:
131 printf("b");
132 break;
133 }
134 printf("%c%c%c",
135 "-r"[!!(st.st_mode & S_IRUSR)],
136 "-w"[!!(st.st_mode & S_IWUSR)],
137 "-x"[!!(st.st_mode & S_IXUSR)]
138 );
139 printf("%c%c%c",
140 "-r"[!!(st.st_mode & S_IRGRP)],
141 "-w"[!!(st.st_mode & S_IWGRP)],
142 "-x"[!!(st.st_mode & S_IXGRP)]
143 );
144 printf("%c%c%c",
145 "-r"[!!(st.st_mode & S_IROTH)],
146 "-w"[!!(st.st_mode & S_IWOTH)],
147 "-x"[!!(st.st_mode & S_IXOTH)]
148 );
149 printf(" %2d", (int) st.st_nlink);
150 printf(" %5d", st.st_uid);
151 printf(" %5d", st.st_gid);
152 printf(" %12" PRId64, st.st_size);
153 printf("\n");
154 }
155 } else {
156 goto finished;
157 }
158
159 if (ret) {
160 fprintf(stderr, "ERROR: %s\n", nfs_get_error(nfs));
161 }
162
163 finished:
164 if (ret > 0) {
165 print_usage();
166 }
167 nfs_destroy_url(url);
168 if (nfs != NULL) {
169 if (nfsfh) {
170 nfs_close(nfs, nfsfh);
171 }
172 nfs_destroy_context(nfs);
173 }
174 return !!ret;
175 }
176