Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Synchronet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Main
Synchronet
Commits
ceddc46e
Commit
ceddc46e
authored
9 years ago
by
deuce
Browse files
Options
Downloads
Patches
Plain Diff
Add raw_read() and raw_write() which access the underlying descriptor,
not the file object itself.
parent
759c9021
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/sbbs3/js_file.c
+110
-0
110 additions, 0 deletions
src/sbbs3/js_file.c
with
110 additions
and
0 deletions
src/sbbs3/js_file.c
+
110
−
0
View file @
ceddc46e
...
...
@@ -284,6 +284,61 @@ js_close(JSContext *cx, uintN argc, jsval *arglist)
return
(
JS_TRUE
);
}
static
JSBool
js_raw_read
(
JSContext
*
cx
,
uintN
argc
,
jsval
*
arglist
)
{
JSObject
*
obj
=
JS_THIS_OBJECT
(
cx
,
arglist
);
jsval
*
argv
=
JS_ARGV
(
cx
,
arglist
);
char
*
buf
;
int32
len
;
JSString
*
str
;
private_t
*
p
;
jsrefcount
rc
;
JS_SET_RVAL
(
cx
,
arglist
,
JSVAL_NULL
);
if
((
p
=
(
private_t
*
)
JS_GetPrivate
(
cx
,
obj
))
==
NULL
)
{
JS_ReportError
(
cx
,
getprivate_failure
,
WHERE
);
return
(
JS_FALSE
);
}
if
(
p
->
fp
==
NULL
)
return
(
JS_TRUE
);
if
(
argc
)
{
if
(
!
JS_ValueToInt32
(
cx
,
argv
[
0
],
&
len
))
return
(
JS_FALSE
);
}
else
len
=
1
;
if
(
len
<
0
)
len
=
1
;
if
((
buf
=
malloc
(
len
))
==
NULL
)
return
(
JS_TRUE
);
rc
=
JS_SUSPENDREQUEST
(
cx
);
len
=
read
(
fileno
(
p
->
fp
),
buf
,
len
);
if
(
len
<
0
)
len
=
0
;
JS_RESUMEREQUEST
(
cx
,
rc
);
str
=
JS_NewStringCopyN
(
cx
,
buf
,
len
);
free
(
buf
);
if
(
str
==
NULL
)
return
(
JS_FALSE
);
JS_SET_RVAL
(
cx
,
arglist
,
STRING_TO_JSVAL
(
str
));
rc
=
JS_SUSPENDREQUEST
(
cx
);
dbprintf
(
FALSE
,
p
,
"read %u raw bytes"
,
len
);
JS_RESUMEREQUEST
(
cx
,
rc
);
return
(
JS_TRUE
);
}
static
JSBool
js_read
(
JSContext
*
cx
,
uintN
argc
,
jsval
*
arglist
)
{
...
...
@@ -1439,6 +1494,50 @@ js_iniSetAllObjects(JSContext *cx, uintN argc, jsval *arglist)
return
(
JS_TRUE
);
}
static
JSBool
js_raw_write
(
JSContext
*
cx
,
uintN
argc
,
jsval
*
arglist
)
{
JSObject
*
obj
=
JS_THIS_OBJECT
(
cx
,
arglist
);
jsval
*
argv
=
JS_ARGV
(
cx
,
arglist
);
char
*
cp
;
size_t
len
;
/* string length */
JSString
*
str
;
private_t
*
p
;
jsrefcount
rc
;
JS_SET_RVAL
(
cx
,
arglist
,
JSVAL_FALSE
);
if
((
p
=
(
private_t
*
)
JS_GetPrivate
(
cx
,
obj
))
==
NULL
)
{
JS_ReportError
(
cx
,
getprivate_failure
,
WHERE
);
return
(
JS_FALSE
);
}
if
(
p
->
fp
==
NULL
)
return
(
JS_TRUE
);
if
((
str
=
JS_ValueToString
(
cx
,
argv
[
0
]))
==
NULL
)
return
(
JS_FALSE
);
JSSTRING_TO_MSTRING
(
cx
,
str
,
cp
,
&
len
);
HANDLE_PENDING
(
cx
);
if
(
cp
==
NULL
)
return
JS_TRUE
;
rc
=
JS_SUSPENDREQUEST
(
cx
);
if
(
write
(
fileno
(
p
->
fp
),
cp
,
len
)
==
(
size_t
)
len
)
{
free
(
cp
);
dbprintf
(
FALSE
,
p
,
"wrote %u raw bytes"
,
len
);
JS_SET_RVAL
(
cx
,
arglist
,
JSVAL_TRUE
);
}
else
{
free
(
cp
);
dbprintf
(
TRUE
,
p
,
"raw write of %u bytes failed"
,
len
);
}
JS_RESUMEREQUEST
(
cx
,
rc
);
return
(
JS_TRUE
);
}
static
JSBool
js_write
(
JSContext
*
cx
,
uintN
argc
,
jsval
*
arglist
)
{
...
...
@@ -2486,6 +2585,12 @@ static jsSyncMethodSpec js_file_functions[] = {
,
JSDOCSTR
(
"read all lines into an array of strings, <i>maxlen</i> defaults to 512 characters"
)
,
310
},
{
"raw_read"
,
js_raw_read
,
0
,
JSTYPE_STRING
,
JSDOCSTR
(
"[maxlen=<i>1</i>]"
)
,
JSDOCSTR
(
"read a string from underlying file descriptor. "
"Undefined results when mixed with any other read/write methods except raw_write, including indirect ones. "
"<i>maxlen</i> defaults to one"
)
,
317
},
{
"write"
,
js_write
,
1
,
JSTYPE_BOOLEAN
,
JSDOCSTR
(
"text [,length=<i>text_length</i>]"
)
,
JSDOCSTR
(
"write a string to the file (optionally unix-to-unix or base64 decoding in the process)"
)
,
310
...
...
@@ -2503,6 +2608,11 @@ static jsSyncMethodSpec js_file_functions[] = {
,
JSDOCSTR
(
"write an array of strings to file"
)
,
310
},
{
"raw_write"
,
js_raw_write
,
1
,
JSTYPE_BOOLEAN
,
JSDOCSTR
(
"text"
)
,
JSDOCSTR
(
"write a string to the underlying file descriptor. "
"Undefined results when mixed with any other read/write methods except raw_read, including indirect ones."
)
,
317
},
{
"printf"
,
js_fprintf
,
0
,
JSTYPE_NUMBER
,
JSDOCSTR
(
"format [,args]"
)
,
JSDOCSTR
(
"write a formatted string to the file (ala fprintf) - "
"<small>CAUTION: for experienced C programmers ONLY</small>"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment