docs: document --quvi-format
[mplayer.git] / stream / stream_smb.c
blobf176bc75188c826400a25bd57d3df88e8664d63d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <libsmbclient.h>
22 #include <unistd.h>
24 #include "mp_msg.h"
25 #include "stream.h"
26 #include "m_option.h"
27 #include "m_struct.h"
29 static struct stream_priv_s {
30 } stream_priv_dflts = {
33 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
34 // URL definition
35 static const m_option_t stream_opts_fields[] = {
36 { NULL, NULL, 0, 0, 0, 0, NULL }
39 static const struct m_struct_st stream_opts = {
40 "smb",
41 sizeof(struct stream_priv_s),
42 &stream_priv_dflts,
43 stream_opts_fields
46 static char smb_password[15];
47 static char smb_username[15];
49 static void smb_auth_fn(const char *server, const char *share,
50 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
51 char *password, int pwmaxlen)
53 char temp[128];
55 strcpy(temp, "LAN");
56 if (temp[strlen(temp) - 1] == 0x0a)
57 temp[strlen(temp) - 1] = 0x00;
59 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
61 strcpy(temp, smb_username);
62 if (temp[strlen(temp) - 1] == 0x0a)
63 temp[strlen(temp) - 1] = 0x00;
65 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
67 strcpy(temp, smb_password);
68 if (temp[strlen(temp) - 1] == 0x0a)
69 temp[strlen(temp) - 1] = 0x00;
71 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
74 static int control(stream_t *s, int cmd, void *arg) {
75 switch(cmd) {
76 case STREAM_CTRL_GET_SIZE: {
77 off_t size = smbc_lseek(s->fd,0,SEEK_END);
78 smbc_lseek(s->fd,s->pos,SEEK_SET);
79 if(size != (off_t)-1) {
80 *((off_t*)arg) = size;
81 return 1;
85 return STREAM_UNSUPPORTED;
88 static int seek(stream_t *s,off_t newpos) {
89 s->pos = newpos;
90 if(smbc_lseek(s->fd,s->pos,SEEK_SET)<0) {
91 s->eof=1;
92 return 0;
94 return 1;
97 static int fill_buffer(stream_t *s, char* buffer, int max_len){
98 int r = smbc_read(s->fd,buffer,max_len);
99 return (r <= 0) ? -1 : r;
102 static int write_buffer(stream_t *s, char* buffer, int len) {
103 int r;
104 int wr = 0;
105 while (wr < len) {
106 r = smbc_write(s->fd,buffer,len);
107 if (r <= 0)
108 return -1;
109 wr += r;
110 buffer += r;
112 return len;
115 static void close_f(stream_t *s){
116 smbc_close(s->fd);
119 static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
120 char *filename;
121 mode_t m = 0;
122 off_t len;
123 int fd, err;
125 filename = stream->url;
127 if(mode == STREAM_READ)
128 m = O_RDONLY;
129 else if (mode == STREAM_WRITE) //who's gonna do that ?
130 m = O_RDWR|O_CREAT|O_TRUNC;
131 else {
132 mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
133 m_struct_free (&stream_opts, opts);
134 return STREAM_UNSUPPORTED;
137 if(!filename) {
138 mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
139 m_struct_free(&stream_opts, opts);
140 return STREAM_ERROR;
143 err = smbc_init(smb_auth_fn, 1);
144 if (err < 0) {
145 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Cannot init the libsmbclient library: %d\n",err);
146 m_struct_free(&stream_opts, opts);
147 return STREAM_ERROR;
150 fd = smbc_open(filename, m,0644);
151 if (fd < 0) {
152 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Could not open from LAN: '%s'\n", filename);
153 m_struct_free(&stream_opts, opts);
154 return STREAM_ERROR;
157 stream->flags = mode;
158 len = 0;
159 if(mode == STREAM_READ) {
160 len = smbc_lseek(fd,0,SEEK_END);
161 smbc_lseek (fd, 0, SEEK_SET);
163 if(len > 0 || mode == STREAM_WRITE) {
164 stream->flags |= MP_STREAM_SEEK;
165 stream->seek = seek;
166 if(mode == STREAM_READ) stream->end_pos = len;
168 stream->type = STREAMTYPE_SMB;
169 stream->fd = fd;
170 stream->fill_buffer = fill_buffer;
171 stream->write_buffer = write_buffer;
172 stream->close = close_f;
173 stream->control = control;
175 m_struct_free(&stream_opts, opts);
176 return STREAM_OK;
179 const stream_info_t stream_info_smb = {
180 "Server Message Block",
181 "smb",
182 "M. Tourne",
183 "based on the code from 'a bulgarian' (one says)",
184 open_f,
185 {"smb", NULL},
186 &stream_opts,
187 0 //Url is an option string