wmbattery: avoid truncating ACPI file paths
[dockapps.git] / wmcapshare / obex_io.c
blob012ce1afabdc6ef52ac470d8a618ac8a4521ae6c
1 /*********************************************************************
3 * Filename: obex_io.c
4 * Version: 0.3
5 * Description: Some useful disk-IO functions
6 * Status: Experimental.
7 * Author: Pontus Fuchs <pontus.fuchs@tactel.se>
8 * Created at: Mon Aug 07 19:32:14 2000
9 * Modified at: Mon Aug 07 19:32:14 2000
10 * Modified by: Pontus Fuchs <pontus.fuchs@tactel.se>
11 * Modified at: Sun Oct 06 10:00:00 2001
12 * Modified by: Ben Moore <ben@netjunki.org>
14 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
31 ********************************************************************/
33 #include <stdio.h>
35 #ifdef _WIN32
36 #include <io.h>
37 #include <windows.h>
38 #else
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #endif /*_WIN32 */
43 #include <fcntl.h>
44 #include <string.h>
46 #include <glib.h>
47 #include <openobex/obex.h>
49 #include "obex_io.h"
51 extern obex_t *handle;
52 int obex_protocol_type = OBEX_PROTOCOL_GENERIC;
55 // Get the filesize in a "portable" way
57 gint get_filesize(const char *filename)
59 #ifdef _WIN32
60 HANDLE fh;
61 gint size;
62 fh = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
63 if(fh == INVALID_HANDLE_VALUE) {
64 g_print("Cannot open %s\n", filename);
65 return -1;
67 size = GetFileSize(fh, NULL);
68 g_print("fize size was %d\n", size);
69 CloseHandle(fh);
70 return size;
72 #else
73 struct stat stats;
74 /* Need to know the file length */
75 stat(filename, &stats);
76 return (gint) stats.st_size;
77 #endif
82 // Read a file and alloc a buffer for it
84 guint8* easy_readfile(const char *filename, int *file_size)
86 int actual;
87 int fd;
88 guint8 *buf;
90 *file_size = get_filesize(filename);
91 g_print("name=%s, size=%d\n", filename, *file_size);
93 #ifdef _WIN32
94 fd = open(filename, O_RDONLY | O_BINARY, 0);
95 #else
96 fd = open(filename, O_RDONLY, 0);
97 #endif
99 if (fd == -1) {
100 return NULL;
103 if(! (buf = g_malloc(*file_size)) ) {
104 return NULL;
107 actual = read(fd, buf, *file_size);
108 close(fd);
110 #ifdef _WIN32
111 if(actual != *file_size) {
112 g_free(buf);
113 buf = NULL;
115 #else
116 *file_size = actual;
117 #endif
118 return buf;
125 obex_object_t *build_object_from_file(obex_t *handle, const char *filename)
127 obex_headerdata_t hdd;
128 guint8 unicode_buf[200];
129 gint namebuf_len;
130 obex_object_t *object;
131 guint32 creator_id;
132 int file_size;
133 char *name = NULL;
134 guint8 *buf;
137 buf = easy_readfile(filename, &file_size);
138 if(buf == NULL)
139 return NULL;
141 /* Set Memopad as the default creator ID */
142 creator_id = MEMO_PAD;
144 /* Find the . in the filename */
145 name = strchr(filename, '.');
146 if (name) {
147 name++;
148 if (strcmp(name, "vcf") == 0) {
149 printf( "This is a Address Book file\n");
150 creator_id = ADDRESS_BOOK;
151 } else if (strcmp(name, "vcs") == 0) {
152 printf( "This is a Date Book file\n");
153 creator_id = DATE_BOOK;
154 } else if (strcmp(name, "txt") == 0) {
155 printf("This is a Memo pad file\n");
156 creator_id = MEMO_PAD;
157 } else if (strcmp(name, "prc") == 0) {
158 printf("This is a Pilot resource file\n");
159 creator_id = PILOT_RESOURCE;
162 /* Build object */
163 object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
165 namebuf_len = OBEX_CharToUnicode(unicode_buf, filename, sizeof(unicode_buf));
167 hdd.bs = unicode_buf;
168 OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME,
169 hdd, namebuf_len, 0);
171 hdd.bq4 = file_size;
172 OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH,
173 hdd, sizeof(guint32), 0);
175 #if 0
176 /* Optional header for win95 irxfer, allows date to be set on file */
177 OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME2,
178 (obex_headerdata_t) (guint32) stats.st_mtime,
179 sizeof(guint32), 0);
180 #endif
181 if (obex_protocol_type != 1) {
182 /* Optional header for Palm Pilot */
183 /* win95 irxfer does not seem to like this one */
184 hdd.bq4 = creator_id;
185 OBEX_ObjectAddHeader(handle, object, HEADER_CREATOR_ID,
186 hdd, sizeof(guint32), 0);
189 hdd.bs = buf;
190 OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY,
191 hdd, file_size, 0);
193 g_free(buf);
194 return object;
199 * Function safe_save_file ()
201 * First remove path and add "/tmp/". Then save.
204 gint safe_save_file(gchar *name, const guint8 *buf, gint len, gchar *savedir)
206 gchar *s = NULL;
207 gchar filename[255] = {0,};
208 gint fd;
209 gint actual;
212 g_print("Filename = %s\n", name);
214 #ifndef _WIN32
215 sprintf( filename, "%s", savedir);
216 #endif
217 s = strrchr(name, '/');
218 if (s == NULL)
219 s = name;
220 else
221 s++;
223 strncat(filename, s, 250);
224 #ifdef _WIN32
225 fd = open(filename, O_RDWR | O_CREAT, 0);
226 #else
227 fd = open(filename, O_RDWR | O_CREAT, DEFFILEMODE);
228 #endif
230 if ( fd < 0) {
231 perror( filename);
232 return -1;
235 actual = write(fd, buf, len);
236 close(fd);
238 g_print( "Wrote %s (%d bytes)\n", filename, actual);
240 return actual;