1 /* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
2 /*
3    Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2017
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #define _FILE_OFFSET_BITS 64
20 #define _GNU_SOURCE
21 
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 
32 #include "libnfs.h"
33 
usage(void)34 void usage(void)
35 {
36 	fprintf(stderr, "Usage: prog_mkdir <url> <cwd> <path>\n");
37 	exit(1);
38 }
39 
main(int argc,char * argv[])40 int main(int argc, char *argv[])
41 {
42 	struct nfs_context *nfs = NULL;
43 	struct nfs_url *url = NULL;
44 	int ret = 0;
45 
46 	if (argc != 4) {
47 		usage();
48 	}
49 
50 	nfs = nfs_init_context();
51 	if (nfs == NULL) {
52 		printf("failed to init context\n");
53 		exit(1);
54 	}
55 
56 	url = nfs_parse_url_full(nfs, argv[1]);
57 	if (url == NULL) {
58 		fprintf(stderr, "%s\n", nfs_get_error(nfs));
59 		exit(1);
60 	}
61 
62 	if (nfs_mount(nfs, url->server, url->path) != 0) {
63  		fprintf(stderr, "Failed to mount nfs share : %s\n",
64 			nfs_get_error(nfs));
65 		ret = 1;
66 		goto finished;
67 	}
68 
69 	if (nfs_chdir(nfs, argv[2]) != 0) {
70  		fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
71 			argv[2], nfs_get_error(nfs));
72 		ret = 1;
73 		goto finished;
74 	}
75 
76 	if (nfs_mkdir(nfs, argv[3])) {
77  		fprintf(stderr, "Failed to mkdir: %s\n",
78 			nfs_get_error(nfs));
79 		ret = 1;
80 		goto finished;
81 	}
82 
83 finished:
84 	nfs_destroy_url(url);
85 	nfs_destroy_context(nfs);
86 
87 	return ret;
88 }
89