source: EcnlProtoTool/trunk/prototool/src/shellif.c@ 286

Last change on this file since 286 was 286, checked in by coas-nagasima, 6 years ago

各ソフトウェアのライセンスを整理

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 17.4 KB
Line 
1/*
2 * TOPPERS ECHONET Lite Communication Middleware
3 *
4 * Copyright (C) 2017 Cores Co., Ltd. Japan
5 *
6 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
7 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
8 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
9 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
10 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
11 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
12 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
13 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
14ƒƒ“ƒgi—˜—p
15 * ŽÒƒ}ƒjƒ…
16ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
17 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
18 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
19 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
20 * ‚ƁD
21 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
22ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
23ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
24 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
25 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
26 * •ñ‚·‚邱‚ƁD
27 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
28 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
29 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
30 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
31 * –Ɛӂ·‚邱‚ƁD
32 *
33 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
34 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
35 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
36 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
37 * ‚̐ӔC‚𕉂í‚È‚¢D
38 *
39 * @(#) $Id: shellif.c 286 2017-05-02 15:25:33Z coas-nagasima $
40 */
41#include <setjmp.h>
42#include <sys/socket.h>
43#include <sys/time.h>
44#include <sys/select.h>
45#include <poll.h>
46#include <stdarg.h>
47#include "shellif.h"
48#include "analogin_api.h"
49#include "gpio_api.h"
50#include "i2c_api.h"
51#include "pwmout_api.h"
52#include "rtc_api.h"
53#include "serial_api.h"
54#include "us_ticker_api.h"
55#include "wait_api.h"
56#include "arduino.h"
57#include "usrcmd.h"
58#include <sys/mman.h>
59#include <signal.h>
60
61typedef struct __dirstream DIR;
62
63typedef void (*analogin_init_t)(analogin_t *obj, PinName pin);
64typedef uint16_t (*analogin_read_u16_t)(analogin_t *obj);
65typedef void (*gpio_init_t)(gpio_t *obj, PinName pin);
66typedef void (*gpio_dir_t)(gpio_t *obj, PinDirection direction);
67typedef int (*i2c_start_t)(i2c_t *obj);
68typedef int (*i2c_stop_t)(i2c_t *obj);
69typedef void (*i2c_init_t)(i2c_t *obj, PinName sda, PinName scl);
70typedef int (*i2c_read_t)(i2c_t *obj, int address, char *data, int length, int stop);
71typedef int (*i2c_write_t)(i2c_t *obj, int address, const char *data, int length, int stop);
72typedef int (*i2c_byte_read_t)(i2c_t *obj, int last);
73typedef int (*i2c_byte_write_t)(i2c_t *obj, int data);
74typedef void (*pwmout_free_t)(pwmout_t* obj);
75typedef void (*pwmout_init_t)(pwmout_t* obj, PinName pin);
76typedef void (*pwmout_pulsewidth_us_t)(pwmout_t* obj, int us);
77typedef void (*rtc_init_t)(void);
78typedef void (*rtc_free_t)(void);
79typedef int (*rtc_isenabled_t)(void);
80typedef time_t (*rtc_read_t)(void);
81typedef void (*rtc_write_t)(time_t t);
82typedef void (*serial_baud_t)(serial_t *obj, int baudrate);
83typedef void (*serial_init_t)(serial_t *obj, PinName tx, PinName rx);
84typedef int (*serial_readable_t)(serial_t *obj);
85typedef int (*serial_getc_t)(serial_t *obj);
86typedef void (*serial_putc_t)(serial_t *obj, int c);
87typedef uint32_t (*us_ticker_read_t)(void);
88typedef void (*wait_ms_t)(int ms);
89typedef bool (*SD_begin_t)();
90typedef int (*usrcmd_cd_t)(int argc, char **argv);
91typedef int (*usrcmd_ls_t)(int argc, char **argv);
92typedef int (*usrcmd_cp_t)(int argc, char **argv);
93typedef int (*usrcmd_rm_t)(int argc, char **argv);
94typedef int (*usrcmd_mv_t)(int argc, char **argv);
95typedef int (*usrcmd_mkdir_t)(int argc, char **argv);
96typedef int (*usrcmd_hexdump_t)(int argc, char **argv);
97typedef int (*usrcmd_info_t)(int argc, char **argv);
98typedef int (*usrcmd_exit_t)(int argc, char **argv);
99typedef void(*shell_abort_t)();
100typedef void(*shell_exit_t)(int exitcd);
101typedef int (*shell_kill_t)(int pid, int sig);
102typedef int (*shell_gettimeofday_t)(struct timeval * tp, void * tzvp);
103typedef void (*vsyslog_t)(int priority, const char *format, va_list ap);
104typedef int (*shell_open_t)(const char * path, int flags);
105typedef int (*shell_close_t)(int fd);
106typedef int (*shell_read_t)(int fd, char *data, int len);
107typedef int (*shell_write_t)(int fd, char *data, int len);
108typedef int (*shell_lseek_t)(int fd, int ptr, int dir);
109typedef int (*shell_fstat_t)(int fd, struct stat * st);
110typedef int (*shell_stat_t)(const char *fname, struct stat *st);
111typedef int (*shell_link_t)(void);
112typedef int (*shell_unlink_t)(const char *path);
113typedef int (*shell_isatty_t)(int fd);
114typedef int (*fsync_t)(int fd);
115typedef int (*ftruncate_t)(int fd, off_t length);
116typedef int (*ioctl_t)(int fd, int request, va_list ap);
117typedef int (*tcgetattr_t)(int fd, struct termios *termios_p);
118typedef int (*tcsetattr_t)(int fd, int optional_actions, const struct termios *termios_p);
119typedef int (*shell_rename_t)(const char *oldpath, const char *newpath);
120typedef int (*rmdir_t)(const char *path);
121typedef int (*mkdir_t)(const char *path, mode_t mode);
122typedef int (*chmod_t)(const char *path, mode_t mode);
123typedef char *(*getcwd_t)(char *buf, size_t size);
124typedef int (*chdir_t)(const char *path);
125typedef int (*chroot_t)(const char *path);
126typedef int (*closedir_t)(DIR *dir);
127typedef DIR *(*opendir_t)(const char *path);
128typedef struct dirent *(*readdir_t)(DIR *dir);
129typedef void (*rewinddir_t)(DIR *dir);
130typedef void (*seekdir_t)(DIR *dir, long pos);
131typedef long (*telldir_t)(DIR *dir);
132typedef int (*shell_getpid_t)();
133typedef int (*socket_t)(int family, int type, int protocol);
134typedef int (*bind_t)(int fd, const struct sockaddr *addr, socklen_t len);
135typedef int (*listen_t)(int fd, int backlog);
136typedef int (*connect_t)(int fd, const struct sockaddr *addr, socklen_t len);
137typedef int (*accept_t)(int fd, struct sockaddr *addr, socklen_t *len);
138typedef ssize_t (*send_t)(int fd, const void *buf, size_t len, int flags);
139typedef ssize_t (*sendto_t)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen);
140typedef ssize_t (*recv_t)(int fd, void *buf, size_t len, int flags);
141typedef ssize_t (*recvfrom_t)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *alen);
142typedef int (*shutdown_t)(int fd, int how);
143typedef int (*getsockopt_t)(int fd, int level, int optname, void *optval, socklen_t *optlen);
144typedef int (*setsockopt_t)(int fd, int level, int optname, const void *optval, socklen_t optlen);
145typedef int (*select_t)(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
146typedef int (*poll_t)(struct pollfd *fds, nfds_t nfds, int timeout);
147typedef int (*gethostname_t)(char *name, size_t len);
148typedef int (*getnameinfo_t)(const struct sockaddr *sa, socklen_t sl, char *node, socklen_t nodelen, char *serv, socklen_t servlen, int flags);
149typedef int (*getpeername_t)(int fd, struct sockaddr *addr, socklen_t *len);
150typedef int (*getsockname_t)(int fd, struct sockaddr *addr, socklen_t *len);
151typedef int (*socketpair_t)(int family, int type, int protocol, int fd[2]);
152
153void analogin_init(analogin_t *obj, PinName pin)
154{
155 __imp_analogin_init(obj, pin);
156}
157
158uint16_t analogin_read_u16(analogin_t *obj)
159{
160 return __imp_analogin_read_u16(obj);
161}
162
163void gpio_init(gpio_t *obj, PinName pin)
164{
165 __imp_gpio_init(obj, pin);
166}
167
168void gpio_dir(gpio_t *obj, PinDirection direction)
169{
170 __imp_gpio_dir(obj, direction);
171}
172
173int i2c_start(i2c_t *obj)
174{
175 return __imp_i2c_start(obj);
176}
177
178int i2c_stop(i2c_t *obj)
179{
180 return __imp_i2c_stop(obj);
181}
182
183void i2c_init(i2c_t *obj, PinName sda, PinName scl)
184{
185 __imp_i2c_init(obj, sda, scl);
186}
187
188int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
189{
190 return __imp_i2c_read(obj, address, data, length, stop);
191}
192
193int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
194{
195 return __imp_i2c_write(obj, address, data, length, stop);
196}
197
198int i2c_byte_read(i2c_t *obj, int last)
199{
200 return __imp_i2c_byte_read(obj, last);
201}
202
203int i2c_byte_write(i2c_t *obj, int data)
204{
205 return __imp_i2c_byte_write(obj, data);
206}
207
208void pwmout_free(pwmout_t* obj)
209{
210 __imp_pwmout_free(obj);
211}
212
213void pwmout_init(pwmout_t* obj, PinName pin)
214{
215 __imp_pwmout_init(obj, pin);
216}
217
218void pwmout_pulsewidth_us(pwmout_t* obj, int us)
219{
220 __imp_pwmout_pulsewidth_us(obj, us);
221}
222
223void rtc_init(void)
224{
225 __imp_rtc_init();
226}
227
228void rtc_free(void)
229{
230 __imp_rtc_free();
231}
232
233int rtc_isenabled(void)
234{
235 return __imp_rtc_isenabled();
236}
237
238time_t rtc_read(void)
239{
240 return __imp_rtc_read();
241}
242
243void rtc_write(time_t t)
244{
245 __imp_rtc_write(t);
246}
247
248void serial_baud(serial_t *obj, int baudrate)
249{
250 __imp_serial_baud(obj, baudrate);
251}
252
253void serial_init(serial_t *obj, PinName tx, PinName rx)
254{
255 __imp_serial_init(obj, tx, rx);
256}
257
258int serial_readable(serial_t *obj)
259{
260 return __imp_serial_readable(obj);
261}
262
263int serial_getc(serial_t *obj)
264{
265 return __imp_serial_getc(obj);
266}
267
268void serial_putc(serial_t *obj, int c)
269{
270 __imp_serial_putc(obj, c);
271}
272
273uint32_t us_ticker_read(void)
274{
275 return __imp_us_ticker_read();
276}
277
278void wait_ms(int ms)
279{
280 __imp_wait_ms(ms);
281}
282
283bool SD_begin()
284{
285 return __imp_SD_begin();
286}
287
288int usrcmd_cd(int argc, char **argv)
289{
290 return __imp_usrcmd_cd(argc, argv);
291}
292
293int usrcmd_ls(int argc, char **argv)
294{
295 return __imp_usrcmd_ls(argc, argv);
296}
297
298int usrcmd_cp(int argc, char **argv)
299{
300 return __imp_usrcmd_cp(argc, argv);
301}
302
303int usrcmd_rm(int argc, char **argv)
304{
305 return __imp_usrcmd_rm(argc, argv);
306}
307
308int usrcmd_mv(int argc, char **argv)
309{
310 return __imp_usrcmd_mv(argc, argv);
311}
312
313int usrcmd_mkdir(int argc, char **argv)
314{
315 return __imp_usrcmd_mkdir(argc, argv);
316}
317
318int usrcmd_hexdump(int argc, char **argv)
319{
320 return __imp_usrcmd_hexdump(argc, argv);
321}
322
323int usrcmd_info(int argc, char **argv)
324{
325 return __imp_usrcmd_info(argc, argv);
326}
327
328int usrcmd_exit(int argc, char **argv)
329{
330 return __imp_usrcmd_exit(argc, argv);
331}
332
333void abort()
334{
335 __imp_shell_abort();
336}
337
338void _exit(int exitcd)
339{
340 __imp_shell_exit(exitcd);
341}
342
343int _kill(int pid, int sig)
344{
345 return __imp_shell_kill(pid, sig);
346}
347
348int _gettimeofday(struct timeval * tp, void * tzvp)
349{
350 return __imp_shell_gettimeofday(tp, tzvp);
351}
352
353void vsyslog(int priority, const char *format, va_list ap)
354{
355 __imp_vsyslog(priority, format, ap);
356}
357
358void syslog(int priority, const char *format, ...)
359{
360 va_list ap;
361 va_start(ap, format);
362 vsyslog(priority, format, ap);
363 va_end(ap);
364}
365
366int _open(const char * path, int flags)
367{
368 return __imp_shell_open(path, flags);
369}
370
371int _close(int fd)
372{
373 return __imp_shell_close(fd);
374}
375
376int _read(int fd, char *data, int len)
377{
378 return __imp_shell_read(fd, data, len);
379}
380
381int _write(int fd, char *data, int len)
382{
383 return __imp_shell_write(fd, data, len);
384}
385
386int _lseek(int fd, int ptr, int dir)
387{
388 return __imp_shell_lseek(fd, ptr, dir);
389}
390
391int _fstat(int fd, struct stat * st)
392{
393 return __imp_shell_fstat(fd, st);
394}
395
396int _stat(const char *fname, struct stat *st)
397{
398 return __imp_shell_stat(fname, st);
399}
400
401int _link(void)
402{
403 return __imp_shell_link();
404}
405
406int _unlink(const char *path)
407{
408 return __imp_shell_unlink(path);
409}
410
411int _isatty(int fd)
412{
413 return __imp_shell_isatty(fd);
414}
415
416int fsync(int fd)
417{
418 return __imp_fsync(fd);
419}
420
421int ftruncate(int fd, off_t length)
422{
423 return __imp_ftruncate(fd, length);
424}
425
426int ioctl(int fd, int request, ...)
427{
428 int result;
429 va_list ap;
430 va_start(ap, request);
431 result = __imp_ioctl(fd, request, ap);
432 va_end(ap);
433 return result;
434}
435
436int tcgetattr(int fd, struct termios *termios_p)
437{
438 return __imp_tcgetattr(fd, termios_p);
439}
440
441int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
442{
443 return __imp_tcsetattr(fd, optional_actions, termios_p);
444}
445
446int _rename(const char *oldpath, const char *newpath)
447{
448 return __imp_shell_rename(oldpath, newpath);
449}
450
451int rmdir(const char *path)
452{
453 return __imp_rmdir(path);
454}
455
456int mkdir(const char *path, mode_t mode)
457{
458 return __imp_mkdir(path, mode);
459}
460
461int chmod(const char *path, mode_t mode)
462{
463 return __imp_chmod(path, mode);
464}
465
466char *getcwd(char *buf, size_t size)
467{
468 return __imp_getcwd(buf, size);
469}
470
471int chdir(const char *path)
472{
473 return __imp_chdir(path);
474}
475
476int chroot(const char *path)
477{
478 return __imp_chroot(path);
479}
480
481int closedir(DIR *dir)
482{
483 return __imp_closedir(dir);
484}
485
486DIR *opendir(const char *path)
487{
488 return __imp_opendir(path);
489}
490
491struct dirent *readdir(DIR *dir)
492{
493 return __imp_readdir(dir);
494}
495
496void rewinddir(DIR *dir)
497{
498 __imp_rewinddir(dir);
499}
500
501void seekdir(DIR *dir, long pos)
502{
503 __imp_seekdir(dir, pos);
504}
505
506long telldir(DIR *dir)
507{
508 return __imp_telldir(dir);
509}
510
511int _getpid()
512{
513 return __imp_shell_getpid();
514}
515
516int socket(int family, int type, int protocol)
517{
518 return __imp_socket(family, type, protocol);
519}
520
521int bind(int fd, const struct sockaddr *addr, socklen_t len)
522{
523 return __imp_bind(fd, addr, len);
524}
525
526int listen(int fd, int backlog)
527{
528 return __imp_listen(fd, backlog);
529}
530
531int connect(int fd, const struct sockaddr *addr, socklen_t len)
532{
533 return __imp_connect(fd, addr, len);
534}
535
536int accept(int fd, struct sockaddr *addr, socklen_t *len)
537{
538 return __imp_accept(fd, addr, len);
539}
540
541ssize_t send(int fd, const void *buf, size_t len, int flags)
542{
543 return __imp_send(fd, buf, len, flags);
544}
545
546ssize_t sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
547{
548 return __imp_sendto(fd, buf, len, flags, addr, alen);
549}
550
551ssize_t recv(int fd, void *buf, size_t len, int flags)
552{
553 return __imp_recv(fd, buf, len, flags);
554}
555
556ssize_t recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *alen)
557{
558 return __imp_recvfrom(fd, buf, len, flags, addr, alen);
559}
560
561int shutdown(int fd, int how)
562{
563 return __imp_shutdown(fd, how);
564}
565
566int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
567{
568 return __imp_getsockopt(fd, level, optname, optval, optlen);
569}
570
571int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
572{
573 return __imp_setsockopt(fd, level, optname, optval, optlen);
574}
575
576int select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
577{
578 return __imp_select(n, rfds, wfds, efds, tv);
579}
580
581int poll(struct pollfd *fds, nfds_t nfds, int timeout)
582{
583 return __imp_poll(fds, nfds, timeout);
584}
585
586unsigned if_nametoindex(const char *name)
587{
588 return 1;
589}
590
591int gethostname(char *name, size_t len)
592{
593 return __imp_gethostname(name, len);
594}
595
596int getnameinfo(const struct sockaddr *sa, socklen_t sl,
597 char *node, socklen_t nodelen,
598 char *serv, socklen_t servlen,
599 int flags)
600{
601 return __imp_getnameinfo(sa, sl, node, nodelen, serv, servlen, flags);
602}
603
604int getpeername(int fd, struct sockaddr *addr, socklen_t *len)
605{
606 return __imp_getpeername(fd, addr, len);
607}
608
609int getsockname(int fd, struct sockaddr *addr, socklen_t *len)
610{
611 return __imp_getsockname(fd, addr, len);
612}
613
614int socketpair(int family, int type, int protocol, int fd[2])
615{
616 return __imp_socketpair(family, type, protocol, fd);
617}
618
619char *basename(char *s)
620{
621 size_t i, j;
622 if (!s || !*s) return ".";
623 i = strlen(s) - 1;
624 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
625 for (; i&&s[i] == '/'; i--) s[i] = 0;
626 for (; i&&s[i - 1] != '/'; i--);
627 return s + i;
628}
629
630char *dirname(char *_s)
631{
632 char *s = _s;
633 size_t i, j;
634 if (!s || !*s) return ".";
635 i = strlen(s) - 1;
636 for (j = 0; j <= i; j++) if (s[j] == ':') { s = &s[j + 1]; i -= j; break; }
637 for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s;
638 for (; s[i] != '/'; i--) if (!i) return (s == _s) ? "." : _s;
639 for (; s[i] == '/'; i--) if (!i) if (s == _s) return "/"; else break;
640 s[i + 1] = 0;
641 return _s;
642}
643
644char *realpath(const char *filename, char *resolved)
645{
646 char *path = getcwd(resolved, 256);
647
648 if (path != NULL)
649 strcat(path, filename);
650
651 return path;
652}
653
654uint32_t htonl(uint32_t n)
655{
656 return (n >> 24) | ((n >> 8) & 0xFF00) | ((n << 8) & 0xFF0000) | (n << 24);
657}
658
659uint32_t ntohl(uint32_t n)
660{
661 return (n >> 24) | ((n >> 8) & 0xFF00) | ((n << 8) & 0xFF0000) | (n << 24);
662}
663
664uint16_t htons(uint16_t n)
665{
666 return ((n >> 8) & 0xFF) | ((n << 8) & 0xFF00);
667}
668
669uint16_t ntohs(uint16_t n)
670{
671 return ((n >> 8) & 0xFF) | ((n << 8) & 0xFF00);
672}
673
674int mprotect(void *addr, size_t len, int prot)
675{
676 return 0;
677}
678
679int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
680{
681 return 0;
682}
683
684unsigned int Utf8_to_Utf16(const char *src, int *code_size)
685{
686 int i;
687 unsigned int uc = 0;
688 unsigned char len = 0;
689
690 len = 0;
691 if ((src[0] & 0x80) == 0) { uc = src[0] & 0x7F; len = 0; }
692 else if ((src[0] & 0xE0) == 0xC0) { uc = src[0] & 0x1F; len = 1; }
693 else if ((src[0] & 0xF0) == 0xE0) { uc = src[0] & 0x0F; len = 2; }
694 else if ((src[0] & 0xF8) == 0xF0) { uc = src[0] & 0x07; len = 3; }
695 else if ((src[0] & 0xFC) == 0xF8) { uc = src[0] & 0x03; len = 4; }
696 else if ((src[0] & 0xFE) == 0xFC) { uc = src[0] & 0x01; len = 5; }
697
698 i = 1;
699 while ((i <= len) && ((src[i] & 0xC0) == 0x80)) {
700 uc = (uc << 6) | (src[i] & 0x3F);
701 i++;
702 }
703
704 //Á”Žš”Ý’è
705 *code_size = i;
706
707 //Œ»óA2ƒoƒCƒgŒÀ’è
708 return uc;
709}
710
711int bs_char_count(char *str, int pos)
712{
713 int num = 1, local_pos = pos;
714 char *c;
715
716 while (1) {
717 c = str + local_pos;
718
719 if ((*c & 0x80) != 0x80)
720 return num;
721
722 if (((*c & 0xE0) == 0xC0) && (num == 2)) {
723 return num;
724 }
725 else if (((*c & 0xF0) == 0xE0) && (num == 3)) {
726 return num;
727 }
728 else if (((*c & 0xF8) == 0xF0) && (num == 4)) {
729 return num;
730 }
731 else if (((*c & 0xFC) == 0xF8) && (num == 5)) {
732 return num;
733 }
734 else if (((*c & 0xFE) == 0xFC) && (num == 6)) {
735 return num;
736 }
737 else {
738 if (local_pos > 0) {
739 local_pos--;
740 num++;
741 }
742 else
743 return 0;
744 }
745 }
746
747 return 0;
748}
Note: See TracBrowser for help on using the repository browser.