1b4298db26ceae68f311d666a513c65df8c55af9
[nivanova/samba-autobuild/.git] / examples / libsmbclient / testtruncate.c
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"
8
9
10 int main(int argc, char * argv[]) 
11
12     int             fd;
13     int             ret;
14     int             debug = 0;
15     int             savedErrno;
16     char            buffer[128];
17     struct stat     st; 
18     
19     if (argc != 2)
20     {
21         printf("usage: "
22                "%s smb://path/to/file\n",
23                argv[0]);
24         return 1;
25     }
26
27     smbc_init(get_auth_data_fn, debug); 
28     
29     if ((fd = smbc_open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0)) < 0)
30     {
31         perror("smbc_open");
32         return 1;
33     }
34
35     snprintf(buffer, sizeof(buffer), "%s", "Hello world.\nThis is a test.\n");
36
37     ret = smbc_write(fd, buffer, strlen(buffer));
38     savedErrno = errno;
39     smbc_close(fd);
40
41     if (ret < 0)
42     {
43         errno = savedErrno;
44         perror("write");
45     }
46
47     if (smbc_stat(argv[1], &st) < 0)
48     {
49         perror("smbc_stat");
50         return 1;
51     }
52     
53     printf("Original size: %lu\n", (unsigned long) st.st_size);
54     
55     if ((fd = smbc_open(argv[1], O_WRONLY, 0)) < 0)
56     {
57         perror("smbc_open");
58         return 1;
59     }
60
61     ret = smbc_ftruncate(fd, 13);
62     savedErrno = errno;
63     smbc_close(fd);
64     if (ret < 0)
65     {
66         errno = savedErrno;
67         perror("smbc_ftruncate");
68         return 1;
69     }
70     
71     if (smbc_stat(argv[1], &st) < 0)
72     {
73         perror("smbc_stat");
74         return 1;
75     }
76     
77     printf("New size: %lu\n", (unsigned long) st.st_size);
78     
79     return 0; 
80 }