Fix compilation: #undef standard library functions that are
[mplayer/glamo.git] / stream / stream_smb.c
blob17ba02768745d233cf2d2e66940a75cef466d5cf
2 #include "config.h"
4 #include <libsmbclient.h>
5 #include <unistd.h>
7 #include "mp_msg.h"
8 #include "stream.h"
9 #include "help_mp.h"
10 #include "m_option.h"
11 #include "m_struct.h"
13 static struct stream_priv_s {
14 } stream_priv_dflts = {
17 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
18 // URL definition
19 static const m_option_t stream_opts_fields[] = {
20 { NULL, NULL, 0, 0, 0, 0, NULL }
23 static const struct m_struct_st stream_opts = {
24 "smb",
25 sizeof(struct stream_priv_s),
26 &stream_priv_dflts,
27 stream_opts_fields
30 static char smb_password[15];
31 static char smb_username[15];
33 static void smb_auth_fn(const char *server, const char *share,
34 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
35 char *password, int pwmaxlen)
37 char temp[128];
39 strcpy(temp, "LAN");
40 if (temp[strlen(temp) - 1] == 0x0a)
41 temp[strlen(temp) - 1] = 0x00;
43 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
45 strcpy(temp, smb_username);
46 if (temp[strlen(temp) - 1] == 0x0a)
47 temp[strlen(temp) - 1] = 0x00;
49 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
51 strcpy(temp, smb_password);
52 if (temp[strlen(temp) - 1] == 0x0a)
53 temp[strlen(temp) - 1] = 0x00;
55 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
58 static int control(stream_t *s, int cmd, void *arg) {
59 switch(cmd) {
60 case STREAM_CTRL_GET_SIZE: {
61 off_t size = smbc_lseek(s->fd,0,SEEK_END);
62 smbc_lseek(s->fd,s->pos,SEEK_SET);
63 if(size != (off_t)-1) {
64 *((off_t*)arg) = size;
65 return 1;
69 return STREAM_UNSUPPORTED;
72 static int seek(stream_t *s,off_t newpos) {
73 s->pos = newpos;
74 if(smbc_lseek(s->fd,s->pos,SEEK_SET)<0) {
75 s->eof=1;
76 return 0;
78 return 1;
81 static int fill_buffer(stream_t *s, char* buffer, int max_len){
82 int r = smbc_read(s->fd,buffer,max_len);
83 return (r <= 0) ? -1 : r;
86 static int write_buffer(stream_t *s, char* buffer, int len) {
87 int r = smbc_write(s->fd,buffer,len);
88 return (r <= 0) ? -1 : r;
91 static void close_f(stream_t *s){
92 smbc_close(s->fd);
95 static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
96 struct stream_priv_s *p = (struct stream_priv_s*)opts;
97 char *filename;
98 mode_t m = 0;
99 off_t len;
100 int fd, err;
102 filename = stream->url;
104 if(mode == STREAM_READ)
105 m = O_RDONLY;
106 else if (mode == STREAM_WRITE) //who's gonna do that ?
107 m = O_RDWR|O_CREAT|O_TRUNC;
108 else {
109 mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
110 m_struct_free (&stream_opts, opts);
111 return STREAM_UNSUPPORTED;
114 if(!filename) {
115 mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
116 m_struct_free(&stream_opts, opts);
117 return STREAM_ERROR;
120 err = smbc_init(smb_auth_fn, 1);
121 if (err < 0) {
122 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBInitError,err);
123 m_struct_free(&stream_opts, opts);
124 return STREAM_ERROR;
127 fd = smbc_open(filename, m,0644);
128 if (fd < 0) {
129 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBFileNotFound, filename);
130 m_struct_free(&stream_opts, opts);
131 return STREAM_ERROR;
134 stream->flags = mode;
135 len = 0;
136 if(mode == STREAM_READ) {
137 len = smbc_lseek(fd,0,SEEK_END);
138 smbc_lseek (fd, 0, SEEK_SET);
140 if(len > 0 || mode == STREAM_WRITE) {
141 stream->flags |= STREAM_SEEK;
142 stream->seek = seek;
143 if(mode == STREAM_READ) stream->end_pos = len;
145 stream->type = STREAMTYPE_SMB;
146 stream->fd = fd;
147 stream->fill_buffer = fill_buffer;
148 stream->write_buffer = write_buffer;
149 stream->close = close_f;
150 stream->control = control;
152 m_struct_free(&stream_opts, opts);
153 return STREAM_OK;
156 const stream_info_t stream_info_smb = {
157 "Server Message Block",
158 "smb",
159 "M. Tourne",
160 "based on the code from 'a bulgarian' (one says)",
161 open_f,
162 {"smb", NULL},
163 &stream_opts,
164 0 //Url is an option string