cleanup: avoid various GCC warnings
[mplayer/greg.git] / stream / stream_smb.c
blob14791a2995f9e849fe55e5758b1a2da3c9ad7b1a
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 = smbc_write(s->fd,buffer,len);
104 return (r <= 0) ? -1 : r;
107 static void close_f(stream_t *s){
108 smbc_close(s->fd);
111 static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
112 char *filename;
113 mode_t m = 0;
114 off_t len;
115 int fd, err;
117 filename = stream->url;
119 if(mode == STREAM_READ)
120 m = O_RDONLY;
121 else if (mode == STREAM_WRITE) //who's gonna do that ?
122 m = O_RDWR|O_CREAT|O_TRUNC;
123 else {
124 mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
125 m_struct_free (&stream_opts, opts);
126 return STREAM_UNSUPPORTED;
129 if(!filename) {
130 mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
131 m_struct_free(&stream_opts, opts);
132 return STREAM_ERROR;
135 err = smbc_init(smb_auth_fn, 1);
136 if (err < 0) {
137 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Cannot init the libsmbclient library: %d\n",err);
138 m_struct_free(&stream_opts, opts);
139 return STREAM_ERROR;
142 fd = smbc_open(filename, m,0644);
143 if (fd < 0) {
144 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Could not open from LAN: '%s'\n", filename);
145 m_struct_free(&stream_opts, opts);
146 return STREAM_ERROR;
149 stream->flags = mode;
150 len = 0;
151 if(mode == STREAM_READ) {
152 len = smbc_lseek(fd,0,SEEK_END);
153 smbc_lseek (fd, 0, SEEK_SET);
155 if(len > 0 || mode == STREAM_WRITE) {
156 stream->flags |= MP_STREAM_SEEK;
157 stream->seek = seek;
158 if(mode == STREAM_READ) stream->end_pos = len;
160 stream->type = STREAMTYPE_SMB;
161 stream->fd = fd;
162 stream->fill_buffer = fill_buffer;
163 stream->write_buffer = write_buffer;
164 stream->close = close_f;
165 stream->control = control;
167 m_struct_free(&stream_opts, opts);
168 return STREAM_OK;
171 const stream_info_t stream_info_smb = {
172 "Server Message Block",
173 "smb",
174 "M. Tourne",
175 "based on the code from 'a bulgarian' (one says)",
176 open_f,
177 {"smb", NULL},
178 &stream_opts,
179 0 //Url is an option string