pyldb: avoid segfault when adding an element with no name
[nivanova/samba-autobuild/.git] / examples / libsmbclient / testread.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     for (;;)
24     {
25         fprintf(stdout, "Path: ");
26         *path = '\0';
27         p = fgets(path, sizeof(path) - 1, stdin);
28         if (p == NULL) {
29                 fprintf(stderr, "failed to read from stdin\n");
30                 return 1;
31         }
32         if (strlen(path) == 0)
33         {
34             return 0;
35         }
36
37         p = path + strlen(path) - 1;
38         if (*p == '\n')
39         {
40             *p = '\0';
41         }
42     
43         if ((fd = smbc_open(path, O_RDONLY, 0)) < 0)
44         {
45             perror("smbc_open");
46             continue;
47         }
48
49         do
50         {
51             ret = smbc_read(fd, buffer, sizeof(buffer));
52             savedErrno = errno;
53             if (ret > 0) fwrite(buffer, 1, ret, stdout);
54         } while (ret > 0);
55
56         smbc_close(fd);
57
58         if (ret < 0)
59         {
60             errno = savedErrno;
61             perror("read");
62         }
63     }
64
65     return 0; 
66 }