libnfs.pc.in: fix pkg-config --cflags
[deb_libnfs.git] / examples / nfs-ls.c
CommitLineData
a6bc1707
RS
1/*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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")
32WSADATA wsaData;
67a9f57e 33#define PRId64 "ll"
a6bc1707 34#else
67a9f57e 35#include <inttypes.h>
a6bc1707
RS
36#include <string.h>
37#include <sys/stat.h>
38#ifndef AROS
39#include <sys/statvfs.h>
40#endif
41#endif
42
43#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <stdint.h>
a6bc1707
RS
50#include <sys/types.h>
51#include <sys/stat.h>
52#include <fcntl.h>
53#include "libnfs-zdr.h"
54#include "libnfs.h"
55#include "libnfs-raw.h"
56#include "libnfs-raw-mount.h"
57
58struct client {
59 char *server;
60 char *export;
61 uint32_t mount_port;
62 int is_finished;
63};
64
65void print_usage(void)
66{
67 fprintf(stderr, "Usage: nfs-ls [-?|--help] [--usage] <url>\n");
68}
69
70int main(int argc, char *argv[])
71{
72 struct nfs_context *nfs = NULL;
73 int i, ret, res;
74 uint64_t offset;
75 struct client client;
76 struct stat st;
77 struct nfsfh *nfsfh;
78 struct nfsdir *nfsdir;
79 struct nfsdirent *nfsdirent;
80 struct statvfs svfs;
81 exports export, tmp;
82 const char *url = NULL;
83 char *server = NULL, *path = NULL, *strp;
84
85#ifdef WIN32
86 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
87 printf("Failed to start Winsock2\n");
88 exit(10);
89 }
90#endif
91
92#ifdef AROS
93 aros_init_socket();
94#endif
95
96 url = argv[1];
97
98 if (url == NULL) {
99 fprintf(stderr, "No URL specified.\n");
100 print_usage();
101 exit(0);
102 }
103
104 if (strncmp(url, "nfs://", 6)) {
105 fprintf(stderr, "Invalid URL specified.\n");
106 print_usage();
107 exit(0);
108 }
109
110 server = strdup(url + 6);
111 if (server == NULL) {
112 fprintf(stderr, "Failed to strdup server string\n");
113 exit(10);
114 }
115 if (server[0] == '/' || server[0] == '\0') {
116 fprintf(stderr, "Invalid server string.\n");
117 free(server);
118 exit(10);
119 }
120 strp = strchr(server, '/');
121 if (strp == NULL) {
122 fprintf(stderr, "Invalid URL specified.\n");
123 print_usage();
124 free(server);
125 exit(0);
126 }
127 path = strdup(strp);
128 if (path == NULL) {
129 fprintf(stderr, "Failed to strdup server string\n");
130 free(server);
131 exit(10);
132 }
133 if (path[0] != '/') {
134 fprintf(stderr, "Invalid path.\n");
135 free(server);
136 free(path);
137 exit(10);
138 }
139 *strp = 0;
140
141 client.server = server;
142 client.export = path;
143 client.is_finished = 0;
144
145
146 nfs = nfs_init_context();
147 if (nfs == NULL) {
148 printf("failed to init context\n");
149 goto finished;
150 }
151
152 ret = nfs_mount(nfs, client.server, client.export);
153 if (ret != 0) {
154 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
155 goto finished;
156 }
157
158
159 ret = nfs_opendir(nfs, "/", &nfsdir);
160 if (ret != 0) {
161 printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs));
162 exit(10);
163 }
164 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
165 char path[1024];
166
167 if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
168 continue;
169 }
170
67a9f57e 171 sprintf(path, "%s/%s", "/", nfsdirent->name);
a6bc1707
RS
172 ret = nfs_stat(nfs, path, &st);
173 if (ret != 0) {
174 fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs));
175 continue;
176 }
177
178 switch (st.st_mode & S_IFMT) {
67a9f57e 179#ifndef WIN32
a6bc1707 180 case S_IFLNK:
67a9f57e 181#endif
a6bc1707
RS
182 case S_IFREG:
183 printf("-");
184 break;
185 case S_IFDIR:
186 printf("d");
187 break;
188 case S_IFCHR:
189 printf("c");
190 break;
191 case S_IFBLK:
192 printf("b");
193 break;
194 }
195 printf("%c%c%c",
196 "-r"[!!(st.st_mode & S_IRUSR)],
197 "-w"[!!(st.st_mode & S_IWUSR)],
198 "-x"[!!(st.st_mode & S_IXUSR)]
199 );
200 printf("%c%c%c",
201 "-r"[!!(st.st_mode & S_IRGRP)],
202 "-w"[!!(st.st_mode & S_IWGRP)],
203 "-x"[!!(st.st_mode & S_IXGRP)]
204 );
205 printf("%c%c%c",
206 "-r"[!!(st.st_mode & S_IROTH)],
207 "-w"[!!(st.st_mode & S_IWOTH)],
208 "-x"[!!(st.st_mode & S_IXOTH)]
209 );
210 printf(" %2d", st.st_nlink);
211 printf(" %5d", st.st_uid);
212 printf(" %5d", st.st_gid);
213 printf(" %12" PRId64, st.st_size);
214
215 printf(" %s\n", nfsdirent->name);
216 }
217 nfs_closedir(nfs, nfsdir);
218
219
220finished:
221 free(server);
222 free(path);
223 if (nfs != NULL) {
224 nfs_destroy_context(nfs);
225 }
226 return 0;
227}
228