b641a08a1ce07164514b43e04f422f4467bf13cf
[samba.git] / examples / libsmbclient / testwrite.c
1 #include <sys/types.h>
2 #include <stdio.h> 
3 #include <unistd.h>
4 #include <string.h> 
5 #include <time.h> 
6 #include <errno.h>
7 #include <libsmbclient.h> 
8 #include "get_auth_data_fn.h"
9
10
11 int main(int argc, char * argv[]) 
12
13     int             fd;
14     int             ret;
15     int             debug = 0;
16     int             savedErrno;
17     char            buffer[2048]; 
18     char            path[2048];
19     char *          p;
20     
21     smbc_init(get_auth_data_fn, debug); 
22     
23     printf("CAUTION: This program will overwrite a file.  "
24            "Press ENTER to continue.");
25     fgets(buffer, sizeof(buffer), stdin);
26            
27
28     for (;;)
29     {
30         fprintf(stdout, "\nPath: ");
31         *path = '\0';
32         fgets(path, sizeof(path) - 1, stdin);
33         if (strlen(path) == 0)
34         {
35             return 0;
36         }
37
38         p = path + strlen(path) - 1;
39         if (*p == '\n')
40         {
41             *p = '\0';
42         }
43     
44         if ((fd = smbc_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0)) < 0)
45         {
46             perror("smbc_open");
47             continue;
48         }
49
50         strcpy(buffer, "Hello world\n");
51
52         ret = smbc_write(fd, buffer, strlen(buffer));
53         savedErrno = errno;
54         smbc_close(fd);
55
56         if (ret < 0)
57         {
58             errno = savedErrno;
59             perror("write");
60         }
61     }
62
63     return 0; 
64 }