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