support for Geforce FX5500 based on patch by Pascal Yu <yu_pascal at hotmail.com>
[mplayer/greg.git] / libmpdemux / stream_smb.c
blobc92b1da43263462c12d94f495900bccc97dbbfa1
2 #include "config.h"
4 #ifdef LIBSMBCLIENT
6 #include <libsmbclient.h>
7 #include <unistd.h>
9 #include "mp_msg.h"
10 #include "stream.h"
11 #include "help_mp.h"
12 #include "m_option.h"
13 #include "m_struct.h"
15 static struct stream_priv_s {
16 } stream_priv_dflts = {
19 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
20 // URL definition
21 static m_option_t stream_opts_fields[] = {
22 { NULL, NULL, 0, 0, 0, 0, NULL }
25 static struct m_struct_st stream_opts = {
26 "smb",
27 sizeof(struct stream_priv_s),
28 &stream_priv_dflts,
29 stream_opts_fields
32 static char smb_password[15];
33 static char smb_username[15];
35 static void smb_auth_fn(const char *server, const char *share,
36 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
37 char *password, int pwmaxlen)
39 char temp[128];
41 strcpy(temp, "LAN");
42 if (temp[strlen(temp) - 1] == 0x0a)
43 temp[strlen(temp) - 1] = 0x00;
45 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
47 strcpy(temp, smb_username);
48 if (temp[strlen(temp) - 1] == 0x0a)
49 temp[strlen(temp) - 1] = 0x00;
51 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
53 strcpy(temp, smb_password);
54 if (temp[strlen(temp) - 1] == 0x0a)
55 temp[strlen(temp) - 1] = 0x00;
57 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
60 static int seek(stream_t *s,off_t newpos) {
61 s->pos = newpos;
62 if(smbc_lseek(s->fd,s->pos,SEEK_SET)<0) {
63 s->eof=1;
64 return 0;
66 return 1;
69 static int fill_buffer(stream_t *s, char* buffer, int max_len){
70 int r = smbc_read(s->fd,buffer,max_len);
71 return (r <= 0) ? -1 : r;
74 static int write_buffer(stream_t *s, char* buffer, int len) {
75 int r = smbc_write(s->fd,buffer,len);
76 return (r <= 0) ? -1 : r;
79 static void close_f(stream_t *s){
80 smbc_close(s->fd);
83 static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
84 struct stream_priv_s *p = (struct stream_priv_s*)opts;
85 char *filename;
86 mode_t m = 0;
87 off_t len;
88 int fd, err;
90 filename = stream->url;
92 if(mode == STREAM_READ)
93 m = O_RDONLY;
94 else if (mode == STREAM_WRITE) //who's gonna do that ?
95 m = O_WRONLY;
96 else {
97 mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
98 m_struct_free (&stream_opts, opts);
99 return STREAM_UNSUPORTED;
102 if(!filename) {
103 mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
104 m_struct_free(&stream_opts, opts);
105 return STREAM_ERROR;
108 err = smbc_init(smb_auth_fn, 1);
109 if (err < 0) {
110 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBInitError,err);
111 m_struct_free(&stream_opts, opts);
112 return STREAM_ERROR;
115 fd = smbc_open(filename, m,0644);
116 if (fd < 0) {
117 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBFileNotFound, filename);
118 m_struct_free(&stream_opts, opts);
119 return STREAM_ERROR;
122 len = smbc_lseek(fd,0,SEEK_END);
123 smbc_lseek (fd, 0, SEEK_SET);
124 if (len <= 0)
125 stream->flags = 0;
126 else {
127 stream->flags = STREAM_READ | STREAM_SEEK;
128 stream->end_pos = len;
129 stream->seek = seek;
131 stream->type = STREAMTYPE_SMB;
132 stream->fd = fd;
133 stream->fill_buffer = fill_buffer;
134 stream->write_buffer = write_buffer;
135 stream->close = close_f;
137 m_struct_free(&stream_opts, opts);
138 return STREAM_OK;
141 stream_info_t stream_info_smb = {
142 "Server Message Block",
143 "smb",
144 "M. Tourne",
145 "based on the code from 'a bulgarian' (one says)",
146 open_f,
147 {"smb", NULL},
148 &stream_opts,
149 0 //Url is an option string
152 #endif