s3/docs: Fix typo in man mount.cifs.
[Samba.git] / examples / libsmbclient / testtruncate.c
blob8882acd85db2b44dbc7a07f2933f152b13e4673a
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <time.h>
5 #include <errno.h>
6 #include <libsmbclient.h>
7 #include "get_auth_data_fn.h"
10 int main(int argc, char * argv[])
12 int fd;
13 int ret;
14 int debug = 0;
15 int savedErrno;
16 char buffer[128];
17 char * pSmbPath = NULL;
18 char * pLocalPath = NULL;
19 struct stat st;
21 if (argc != 2)
23 printf("usage: "
24 "%s smb://path/to/file\n",
25 argv[0]);
26 return 1;
29 smbc_init(get_auth_data_fn, debug);
31 if ((fd = smbc_open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0)) < 0)
33 perror("smbc_open");
34 return 1;
37 strcpy(buffer, "Hello world.\nThis is a test.\n");
39 ret = smbc_write(fd, buffer, strlen(buffer));
40 savedErrno = errno;
41 smbc_close(fd);
43 if (ret < 0)
45 errno = savedErrno;
46 perror("write");
49 if (smbc_stat(argv[1], &st) < 0)
51 perror("smbc_stat");
52 return 1;
55 printf("Original size: %lu\n", (unsigned long) st.st_size);
57 if ((fd = smbc_open(argv[1], O_WRONLY, 0)) < 0)
59 perror("smbc_open");
60 return 1;
63 ret = smbc_ftruncate(fd, 13);
64 savedErrno = errno;
65 smbc_close(fd);
66 if (ret < 0)
68 errno = savedErrno;
69 perror("smbc_ftruncate");
70 return 1;
73 if (smbc_stat(argv[1], &st) < 0)
75 perror("smbc_stat");
76 return 1;
79 printf("New size: %lu\n", (unsigned long) st.st_size);
81 return 0;