aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Touhey <thomas@touhey.fr>2020-09-20 18:42:40 +0200
committerThomas Touhey <thomas@touhey.fr>2020-09-20 18:42:40 +0200
commitea17794786c4ef1ddf1dbb503f5d385e9bc1f81b (patch)
tree75c2cbf52f61a3bd6e5e8d04b2a2928a145dfcbd
parentb58a5dac1ff27797d6f74010127e0e202084fa66 (diff)
many things (i have to review most)HEADmaster
-rw-r--r--docs/Pipfile1
-rw-r--r--docs/Pipfile.lock174
-rw-r--r--docs/favicon.pngbin8563 -> 1245 bytes
-rw-r--r--docs/logo.pngbin12064 -> 4825 bytes
-rw-r--r--docs/user.rst5
-rw-r--r--docs/user/chrono.rst (renamed from docs/user/time.rst)4
-rw-r--r--docs/user/fs.rst33
-rw-r--r--docs/user/objects.rst4
-rw-r--r--docs/user/system.rst105
-rw-r--r--include/libtio.h2
-rw-r--r--include/libtio/error.h2
-rw-r--r--include/libtio/fs.h182
-rw-r--r--include/libtio/io/usb.h2
-rw-r--r--include/libtio/system.h8
-rw-r--r--include/libtio/time.h111
-rw-r--r--lib/error/names.c5
-rw-r--r--lib/internals.h35
-rw-r--r--lib/system/unix/fs.c177
-rw-r--r--lib/system/unix/internals.h22
-rw-r--r--lib/system/unix/open.c54
-rw-r--r--lib/system/unix/stream.c183
-rw-r--r--lib/system/unix/time.c.draft29
-rw-r--r--lib/system/unix/utils.c91
-rw-r--r--lib/system/windows/stream.c4
-rw-r--r--lib/time/internals.h62
25 files changed, 970 insertions, 325 deletions
diff --git a/docs/Pipfile b/docs/Pipfile
index 57a81a3..09c87a2 100644
--- a/docs/Pipfile
+++ b/docs/Pipfile
@@ -8,6 +8,7 @@ verify_ssl = true
[packages]
sphinx = "*"
sphinx-rtd-theme = "*"
+sphinx-autobuild = "*"
[requires]
python_version = "3.7"
diff --git a/docs/Pipfile.lock b/docs/Pipfile.lock
index 46bae79..f41193c 100644
--- a/docs/Pipfile.lock
+++ b/docs/Pipfile.lock
@@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
- "sha256": "ad49cdffc75c09b6abfb00f80a96f9c4967858dfc7153f34febb993592308482"
+ "sha256": "6d8826054098dd25b2b5d62c6a903410a1e1777e50e73c379e6b48d4b6be3fb9"
},
"pipfile-spec": 6,
"requires": {
@@ -23,19 +23,26 @@
],
"version": "==0.7.12"
},
+ "argh": {
+ "hashes": [
+ "sha256:a9b3aaa1904eeb78e32394cd46c6f37ac0fb4af6dc488daa58971bdc7d7fcaf3",
+ "sha256:e9535b8c84dc9571a48999094fda7f33e63c3f1b74f3e5f3ac0105a58405bb65"
+ ],
+ "version": "==0.26.2"
+ },
"babel": {
"hashes": [
- "sha256:6778d85147d5d85345c14a26aada5e478ab04e39b078b0745ee6870c2b5cf669",
- "sha256:8cba50f48c529ca3fa18cf81fa9403be176d374ac4d60738b839122dfaaa3d23"
+ "sha256:1aac2ae2d0d8ea368fa90906567f5c08463d98ade155c0c4bfedd6a0f7160e38",
+ "sha256:d670ea0b10f8b723672d3a6abeb87b565b244da220d76b4dba1b66269ec152d4"
],
- "version": "==2.6.0"
+ "version": "==2.8.0"
},
"certifi": {
"hashes": [
- "sha256:59b7658e26ca9c7339e00f8f4636cdfe59d34fa37b9b04f6f9e9926b3cece1a5",
- "sha256:b26104d6835d1f5e49452a26eb2ff87fe7090b89dfcaee5ea2212697e1e1d7ae"
+ "sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3",
+ "sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"
],
- "version": "==2019.3.9"
+ "version": "==2019.11.28"
},
"chardet": {
"hashes": [
@@ -46,32 +53,38 @@
},
"docutils": {
"hashes": [
- "sha256:02aec4bd92ab067f6ff27a38a38a41173bf01bed8f89157768c1573f53e474a6",
- "sha256:51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274",
- "sha256:7a4bd47eaf6596e1295ecb11361139febe29b084a87bf005bf899f9a42edc3c6"
+ "sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af",
+ "sha256:c2de3a60e9e7d07be26b7f2b00ca0309c207e06c100f9cc2a94931fc75a478fc"
],
- "version": "==0.14"
+ "version": "==0.16"
},
"idna": {
"hashes": [
- "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",
- "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"
+ "sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb",
+ "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa"
],
- "version": "==2.8"
+ "version": "==2.9"
},
"imagesize": {
"hashes": [
- "sha256:3f349de3eb99145973fefb7dbe38554414e5c30abd0c8e4b970a7c9d09f3a1d8",
- "sha256:f3832918bc3c66617f92e35f5d70729187676313caa60c187eb0f28b8fe5e3b5"
+ "sha256:6965f19a6a2039c7d48bca7dba2473069ff854c36ae6f19d2cde309d998228a1",
+ "sha256:b1f6b5a4eab1f73479a50fb79fcf729514a900c341d8503d62a62dbc4127a2b1"
],
- "version": "==1.1.0"
+ "version": "==1.2.0"
},
"jinja2": {
"hashes": [
- "sha256:065c4f02ebe7f7cf559e49ee5a95fb800a9e4528727aec6f24402a5374c65013",
- "sha256:14dd6caf1527abb21f08f86c784eac40853ba93edb79552aa1e4b8aef1b61c7b"
+ "sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250",
+ "sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49"
],
- "version": "==2.10.1"
+ "version": "==2.11.1"
+ },
+ "livereload": {
+ "hashes": [
+ "sha256:78d55f2c268a8823ba499305dcac64e28ddeb9a92571e12d543cd304faf5817b",
+ "sha256:89254f78d7529d7ea0a3417d224c34287ebfe266b05e67e51facaf82c27f0f66"
+ ],
+ "version": "==2.6.1"
},
"markupsafe": {
"hashes": [
@@ -79,13 +92,16 @@
"sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161",
"sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235",
"sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5",
+ "sha256:13d3144e1e340870b25e7b10b98d779608c02016d5184cfb9927a9f10c689f42",
"sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff",
"sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b",
"sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1",
"sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e",
"sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183",
"sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66",
+ "sha256:596510de112c685489095da617b5bcbbac7dd6384aeebeda4df6025d0256a81b",
"sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1",
+ "sha256:6788b695d50a51edb699cb55e35487e430fa21f1ed838122d722e0ff0ac5ba15",
"sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1",
"sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e",
"sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b",
@@ -102,66 +118,104 @@
"sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6",
"sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f",
"sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f",
- "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7"
+ "sha256:cdb132fc825c38e1aeec2c8aa9338310d29d337bebbd7baa06889d09a60a1fa2",
+ "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7",
+ "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be"
],
"version": "==1.1.1"
},
"packaging": {
"hashes": [
- "sha256:0c98a5d0be38ed775798ece1b9727178c4469d9c3b4ada66e8e6b7849f8732af",
- "sha256:9e1cbf8c12b1f1ce0bb5344b8d7ecf66a6f8a6e91bcb0c84593ed6d3ab5c4ab3"
+ "sha256:170748228214b70b672c581a3dd610ee51f733018650740e98c7df862a583f73",
+ "sha256:e665345f9eef0c621aa0bf2f8d78cf6d21904eef16a93f020240b704a57f1334"
],
- "version": "==19.0"
+ "version": "==20.1"
+ },
+ "pathtools": {
+ "hashes": [
+ "sha256:7c35c5421a39bb82e58018febd90e3b6e5db34c5443aaaf742b3f33d4655f1c0"
+ ],
+ "version": "==0.1.2"
+ },
+ "port-for": {
+ "hashes": [
+ "sha256:b16a84bb29c2954db44c29be38b17c659c9c27e33918dec16b90d375cc596f1c"
+ ],
+ "version": "==0.3.1"
},
"pygments": {
"hashes": [
- "sha256:31cba6ffb739f099a85e243eff8cb717089fdd3c7300767d9fc34cb8e1b065f5",
- "sha256:5ad302949b3c98dd73f8d9fcdc7e9cb592f120e32a18e23efd7f3dc51194472b"
+ "sha256:2a3fe295e54a20164a9df49c75fa58526d3be48e14aceba6d6b1e8ac0bfd6f1b",
+ "sha256:98c8aa5a9f778fcd1026a17361ddaf7330d1b7c62ae97c3bb0ae73e0b9b6b0fe"
],
- "version": "==2.4.0"
+ "version": "==2.5.2"
},
"pyparsing": {
"hashes": [
- "sha256:1873c03321fc118f4e9746baf201ff990ceb915f433f23b395f5580d1840cb2a",
- "sha256:9b6323ef4ab914af344ba97510e966d64ba91055d6b9afa6b30799340e89cc03"
+ "sha256:4c830582a84fb022400b85429791bc551f1f4871c33f23e44f353119e92f969f",
+ "sha256:c342dccb5250c08d45fd6f8b4a559613ca603b57498511740e65cd11a2e7dcec"
],
- "version": "==2.4.0"
+ "version": "==2.4.6"
},
"pytz": {
"hashes": [
- "sha256:303879e36b721603cc54604edcac9d20401bdbe31e1e4fdee5b9f98d5d31dfda",
- "sha256:d747dd3d23d77ef44c6a3526e274af6efeb0a6f1afd5a69ba4d5be4098c8e141"
+ "sha256:1c557d7d0e871de1f5ccd5833f60fb2550652da6be2693c1e02300743d21500d",
+ "sha256:b02c06db6cf09c12dd25137e563b31700d3b80fcc4ad23abb7a315f2789819be"
+ ],
+ "version": "==2019.3"
+ },
+ "pyyaml": {
+ "hashes": [
+ "sha256:059b2ee3194d718896c0ad077dd8c043e5e909d9180f387ce42012662a4946d6",
+ "sha256:1cf708e2ac57f3aabc87405f04b86354f66799c8e62c28c5fc5f88b5521b2dbf",
+ "sha256:24521fa2890642614558b492b473bee0ac1f8057a7263156b02e8b14c88ce6f5",
+ "sha256:4fee71aa5bc6ed9d5f116327c04273e25ae31a3020386916905767ec4fc5317e",
+ "sha256:70024e02197337533eef7b85b068212420f950319cc8c580261963aefc75f811",
+ "sha256:74782fbd4d4f87ff04159e986886931456a1894c61229be9eaf4de6f6e44b99e",
+ "sha256:940532b111b1952befd7db542c370887a8611660d2b9becff75d39355303d82d",
+ "sha256:cb1f2f5e426dc9f07a7681419fe39cee823bb74f723f36f70399123f439e9b20",
+ "sha256:dbbb2379c19ed6042e8f11f2a2c66d39cceb8aeace421bfc29d085d93eda3689",
+ "sha256:e3a057b7a64f1222b56e47bcff5e4b94c4f61faac04c7c4ecb1985e18caa3994",
+ "sha256:e9f45bd5b92c7974e59bcd2dcc8631a6b6cc380a904725fce7bc08872e691615"
],
- "version": "==2019.1"
+ "version": "==5.3"
},
"requests": {
"hashes": [
- "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",
- "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31"
+ "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee",
+ "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"
],
- "version": "==2.22.0"
+ "version": "==2.23.0"
},
"six": {
"hashes": [
- "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c",
- "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73"
+ "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a",
+ "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c"
],
- "version": "==1.12.0"
+ "version": "==1.14.0"
},
"snowballstemmer": {
"hashes": [
- "sha256:919f26a68b2c17a7634da993d91339e288964f93c274f1343e3bbbe2096e1128",
- "sha256:9f3bcd3c401c3e862ec0ebe6d2c069ebc012ce142cce209c098ccb5b09136e89"
+ "sha256:209f257d7533fdb3cb73bdbd24f436239ca3b2fa67d56f6ff88e86be08cc5ef0",
+ "sha256:df3bac3df4c2c01363f3dd2cfa78cce2840a79b9f1c2d2de9ce8d31683992f52"
],
- "version": "==1.2.1"
+ "version": "==2.0.0"
},
"sphinx": {
"hashes": [
- "sha256:423280646fb37944dd3c85c58fb92a20d745793a9f6c511f59da82fa97cd404b",
- "sha256:de930f42600a4fef993587633984cc5027dedba2464bcf00ddace26b40f8d9ce"
+ "sha256:776ff8333181138fae52df65be733127539623bb46cc692e7fa0fcfc80d7aa88",
+ "sha256:ca762da97c3b5107cbf0ab9e11d3ec7ab8d3c31377266fd613b962ed971df709"
],
"index": "pypi",
- "version": "==2.0.1"
+ "version": "==2.4.3"
+ },
+ "sphinx-autobuild": {
+ "hashes": [
+ "sha256:66388f81884666e3821edbe05dd53a0cfb68093873d17320d0610de8db28c74e",
+ "sha256:e60aea0789cab02fa32ee63c7acae5ef41c06f1434d9fd0a74250a61f5994692"
+ ],
+ "index": "pypi",
+ "version": "==0.7.1"
},
"sphinx-rtd-theme": {
"hashes": [
@@ -187,10 +241,10 @@
},
"sphinxcontrib-htmlhelp": {
"hashes": [
- "sha256:4670f99f8951bd78cd4ad2ab962f798f5618b17675c35c5ac3b2132a14ea8422",
- "sha256:d4fd39a65a625c9df86d7fa8a2d9f3cd8299a3a4b15db63b50aac9e161d8eff7"
+ "sha256:3c0bc24a2c41e340ac37c85ced6dafc879ab485c095b1d65d2461ac2f7cca86f",
+ "sha256:e8f5bb7e31b2dbb25b9cc435c8ab7a79787ebf7f906155729338f3156d93659b"
],
- "version": "==1.0.2"
+ "version": "==1.0.3"
},
"sphinxcontrib-jsmath": {
"hashes": [
@@ -213,12 +267,30 @@
],
"version": "==1.1.3"
},
+ "tornado": {
+ "hashes": [
+ "sha256:349884248c36801afa19e342a77cc4458caca694b0eda633f5878e458a44cb2c",
+ "sha256:398e0d35e086ba38a0427c3b37f4337327231942e731edaa6e9fd1865bbd6f60",
+ "sha256:4e73ef678b1a859f0cb29e1d895526a20ea64b5ffd510a2307b5998c7df24281",
+ "sha256:559bce3d31484b665259f50cd94c5c28b961b09315ccd838f284687245f416e5",
+ "sha256:abbe53a39734ef4aba061fca54e30c6b4639d3e1f59653f0da37a0003de148c7",
+ "sha256:c845db36ba616912074c5b1ee897f8e0124df269468f25e4fe21fe72f6edd7a9",
+ "sha256:c9399267c926a4e7c418baa5cbe91c7d1cf362d505a1ef898fde44a07c9dd8a5"
+ ],
+ "version": "==6.0.3"
+ },
"urllib3": {
"hashes": [
- "sha256:a53063d8b9210a7bdec15e7b272776b9d42b2fd6816401a0d43006ad2f9902db",
- "sha256:d363e3607d8de0c220d31950a8f38b18d5ba7c0830facd71a1c6b1036b7ce06c"
+ "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc",
+ "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"
+ ],
+ "version": "==1.25.8"
+ },
+ "watchdog": {
+ "hashes": [
+ "sha256:c560efb643faed5ef28784b2245cf8874f939569717a4a12826a173ac644456b"
],
- "version": "==1.25.2"
+ "version": "==0.10.2"
}
},
"develop": {}
diff --git a/docs/favicon.png b/docs/favicon.png
index eb13967..ba716a8 100644
--- a/docs/favicon.png
+++ b/docs/favicon.png
Binary files differ
diff --git a/docs/logo.png b/docs/logo.png
index ef64985..21584ff 100644
--- a/docs/logo.png
+++ b/docs/logo.png
Binary files differ
diff --git a/docs/user.rst b/docs/user.rst
index cbc660c..aa38343 100644
--- a/docs/user.rst
+++ b/docs/user.rst
@@ -10,7 +10,10 @@ developers must also know in order to contribute to the project.
:maxdepth: 2
user/concepts
+ user/objects
user/iterators
- user/streams
user/time
+ user/system
+ user/chrono
+ user/streams
user/logging
diff --git a/docs/user/time.rst b/docs/user/chrono.rst
index c0cd546..45bd9ec 100644
--- a/docs/user/time.rst
+++ b/docs/user/chrono.rst
@@ -1,5 +1,5 @@
-Time management
-===============
+Timers and sleeps
+=================
Sleeping for some time
----------------------
diff --git a/docs/user/fs.rst b/docs/user/fs.rst
new file mode 100644
index 0000000..e508a8d
--- /dev/null
+++ b/docs/user/fs.rst
@@ -0,0 +1,33 @@
+Filesystems management
+======================
+
+Filesystems are represented through cursors, which represent a view of
+a filesystem through a current directory.
+
+The filesystem object is usable both by the library and the user. It is
+usually opened through a system.
+
+.. type:: tio_file_cursor_t
+
+ A filesystem cursor object.
+
+Changing the current directory
+------------------------------
+
+You can change the current directory on which a file cursor is pointing
+to with the following function:
+
+.. c:function:: int tio_move(tio_file_cursor_t *cursor, char const *name)
+
+ Changes the current directory on which a file cursor is pointing
+ to to the one within the current directory that uses the name
+ given by ``name``, or the parent directory if ``name`` is ``".."``.
+
+Gathering information on the current folder
+-------------------------------------------
+
+You can list files through the following file:
+
+.. c:function:: int tio_list(tio_file_cursor_t *cursor, tio_iter_t **iterp)
+
+ Create a file iterator which gives things. TODO
diff --git a/docs/user/objects.rst b/docs/user/objects.rst
new file mode 100644
index 0000000..0e3764f
--- /dev/null
+++ b/docs/user/objects.rst
@@ -0,0 +1,4 @@
+Objects in libtio
+=================
+
+Everything in libtio is regrouped in objects. Objects are
diff --git a/docs/user/system.rst b/docs/user/system.rst
new file mode 100644
index 0000000..c067d99
--- /dev/null
+++ b/docs/user/system.rst
@@ -0,0 +1,105 @@
+Systems
+=======
+
+Systems are the abstractions for an operating system, or if you prefer,
+an “external world” on which you can open streams, access filesystems,
+and so on.
+
+The system object is usable both by the library and the user. One global
+object of this kind is the local system, which is the immediately
+accessible system, e.g. the win32 interface for Windows, the Linux
+system interface for Linux distributions, and so on.
+
+.. type:: tio_system_t
+
+ An object representing a system.
+
+Managing serial connections on a system
+---------------------------------------
+
+To open a serial stream on a given system, you can use the following
+function:
+
+.. c:function:: int tio_open_serial_stream(tio_system_t *system, \
+ tio_stream_t **streamp, char const *name, \
+ tio_serial_attrs_t const *attrs)
+
+ Open a serial stream on a system, using the given device name.
+
+Device names can vary depending on the systems: on Microsoft Windows,
+it might be simple names such as ``COM4``, where on UNIX-like systems,
+it can be full paths to the serial device, e.g. ``/dev/ttyS0``.
+
+For listing the available serial devices, you can use the following
+functions:
+
+.. c:function:: int tio_list_serial_ports(tio_system_t *system, \
+ tio_iter_t **iterp)
+
+ List serial devices, with just the name.
+
+.. c:function:: int tio_next_serial_port(tio_iter_t *iter, \
+ char const **namep)
+
+ Get the next serial port name on the system.
+
+Managing USB devices on a system
+--------------------------------
+
+To open a USB stream on a given system, you can use the following
+function:
+
+.. c:function:: int tio_open_usb_stream(tio_system_t *system, \
+ tio_stream_t **streamp, int bus, int addr)
+
+ Open a USB stream on a system, using the given bus and address numbers.
+
+For listing the available USB devices, you can use the following
+functions:
+
+.. c:function:: int tio_list_usb_devices(tio_system_t **system, \
+ tio_iter_t **iterp)
+
+ List USB devices, with information represented as a
+ :c:type:`tio_usb_device_t` entry. For getting an entry in the
+ iterator, use :c:func:`tio_next_usb_device`.
+
+.. c:function:: int tio_next_usb_device(tio_iter_t *iter, \
+ tio_usb_device_t * const *devicep)
+
+ Get the next entry of an iterator opened with
+ :c:func:`tio_list_usb_devices`.
+
+A USB device is represented by the following type:
+
+.. c:type:: tio_usb_device_t
+
+ A USB device entry, for iterating.
+
+ .. c:member:: int tio_usb_device_bus
+
+ The bus number for the given USB device.
+
+ .. c:member:: int tio_usb_device_address
+
+ The address number on the bus for the given USB device.
+
+ .. c:member:: int tio_usb_device_class
+
+ An integer corresponding to the ``bDeviceClass`` USB property.
+
+ .. c:member:: int tio_usb_device_subclass
+
+ An integer corresponding to the ``bDeviceSubClass`` USB property.
+
+ .. c:member:: int tio_usb_device_protocol
+
+ An integer corresponding to the ``bDeviceProtocol`` USB property.
+
+ .. c:member:: unsigned int tio_usb_device_vendor_id
+
+ An integer corresponding to the ``idVendor`` USB property.
+
+ .. c:member:: unsigned int tio_usb_device_product_id
+
+ An integer corresponding to the ``idProduct`` USB property.
diff --git a/include/libtio.h b/include/libtio.h
index 52071c2..8468842 100644
--- a/include/libtio.h
+++ b/include/libtio.h
@@ -8,9 +8,11 @@
# include "libtio/log.h"
# include "libtio/iter.h"
+# include "libtio/time.h"
# include "libtio/stream.h"
# include "libtio/chrono.h"
# include "libtio/mutex.h"
+# include "libtio/fs.h"
# include "libtio/system.h"
/* Define what is required to use the library facilities. */
diff --git a/include/libtio/error.h b/include/libtio/error.h
index 67856d6..d5f0541 100644
--- a/include/libtio/error.h
+++ b/include/libtio/error.h
@@ -34,6 +34,8 @@ typedef int tio_error_t;
# define tio_error_notfound 0x06
# define tio_error_access 0x07
# define tio_error_lock 0x08
+# define tio_error_nedata 0x09
+# define tio_error_tmdata 0x0A
/* Stream errors.
* `stream`: invalid stream or stream in erroneous state (parent, …).
diff --git a/include/libtio/fs.h b/include/libtio/fs.h
index e3c3c32..11265f6 100644
--- a/include/libtio/fs.h
+++ b/include/libtio/fs.h
@@ -4,90 +4,92 @@
# include "stream.h"
TIO_BEGIN_NAMESPACE
-TIO_STRUCT(tio_fs, tio_fs_t)
-TIO_STRUCT(tio_fs_functions, tio_fs_functions_t)
-TIO_STRUCT(tio_fs_create_info, tio_fs_create_info_t)
-TIO_STRUCT(tio_path, tio_path_t)
-TIO_STRUCT(tio_stat, tio_stat_t)
+TIO_STRUCT(tio_file_cursor, tio_file_cursor_t)
+TIO_STRUCT(tio_file_cursor_functions, tio_file_cursor_functions_t)
+TIO_STRUCT(tio_stat, tio_stat_t)
/* File type. */
-typedef int tio_filetype_t;
+typedef int tio_file_type_t;
# define TIO_FILETYPE_OTH 0
# define TIO_FILETYPE_REG 1
# define TIO_FILETYPE_DIR 2
-/* Get file information. */
+/* File information. */
-# define TIO_STAT_FLAG_BTIME 1
-# define TIO_STAT_FLAG_ATIME 2
-# define TIO_STAT_FLAG_MTIME 4
-
-# define TIO_STAT_FLAG_ALL 7
+# define TIO_STAT_FLAG_TIMES 1
+# define TIO_STAT_FLAG_ALL 1
struct tio_stat {
- tio_filetype_t tio_stat_type;
+ tio_file_type_t tio_stat_type;
- tio_off_t tio_stat_size;
- time_t tio_stat_btime;
- time_t tio_stat_atime;
- time_t tio_stat_mtime;
+ tio_off_t tio_stat_size;
+ tio_time_t *tio_stat_btime;
+ tio_time_t *tio_stat_atime;
+ tio_time_t *tio_stat_mtime;
};
-/* Create information. */
-
-struct tio_fs_create_info {
- tio_filetype_t tio_fs_create_info_type;
+/* Flags for opening a file.
+ *
+ * `WRITE`: allow writes on the file.
+ * `CREATE`: create the regular file if it doesn't exist yet.
+ * `END`: instead of setting the cursor at the beginning of the file,
+ * set it at its end (e.g. for appending). */
- tio_off_t tio_fs_create_info_size;
-};
+# define TIO_FILE_OPEN_FLAG_WRITE 1
+# define TIO_FILE_OPEN_FLAG_CREATE 2
+# define TIO_FILE_OPEN_FLAG_END 4
/* Filesystem functions. */
-typedef TIO_HOOK_TYPE(int) tio_fs_close_t
+typedef TIO_HOOK_TYPE(void) tio_file_cursor_close_t
OF((void *tio__cookie));
-
-typedef TIO_HOOK_TYPE(int) tio_fs_create_t
- OF((void *tio__cookie, tio_fs_create_info_t const *tio__infop));
-typedef TIO_HOOK_TYPE(int) tio_fs_remove_t
- OF((void *tio__cookie, tio_path_t const *tio__path));
-typedef TIO_HOOK_TYPE(int) tio_fs_move_t
- OF((void *tio__cookie, tio_path_t const *tio__srcpath,
- tio_path_t const *tio__destpath));
-
-typedef TIO_HOOK_TYPE(int) tio_fs_stat_t
- OF((void *tio__cookie, tio_stat_t *tio__stat,
- unsigned long tio__flags));
-typedef TIO_HOOK_TYPE(int) tio_fs_open_t
- OF((void *tio__cookie, tio_path_t *tio__path,
- unsigned long tio__flags));
-
-typedef TIO_HOOK_TYPE(int) tio_fs_list_t
- OF((void *tio__cookie, tio_iter_t **tio__iterp,
- tio_path_t *tio__path));
-
-# define TIO_FSFLAG_CREATE 1
-# define TIO_FSFLAG_REMOVE 2
-# define TIO_FSFLAG_MOVE 4
-# define TIO_FSFLAG_LIST 8
-# define TIO_FSFLAG_STAT 16
-# define TIO_FSFLAG_OPEN 32
-
-struct tio_fs_functions {
- tio_fs_close_t *tio_fs_functions_close;
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_copy_t
+ OF((void *tio__cookie, tio_file_cursor_t **tio__cursorp));
+
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_move_t
+ OF((void *tio__cookie, char const *tio__name));
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_create_directory_t
+ OF((void *tio__cookie, char const *tio__name));
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_remove_t
+ OF((void *tio__cookie, char const *tio__name));
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_list_t
+ OF((void *tio__cookie, tio_iter_t **tio__iterp));
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_stat_t
+ OF((void *tio__cookie, char const *tio__name,
+ tio_stat_t *tio__stat, unsigned long tio__flags));
+typedef TIO_HOOK_TYPE(int) tio_file_cursor_open_t
+ OF((void *tio__cookie, tio_stream_t **tio__streamp,
+ char const *tio__name, unsigned long tio__flags,
+ tio_off_t tio__size));
+
+# define TIO_FILECURSORFLAG_MOVE 1
+# define TIO_FILECURSORFLAG_CREATE 2
+# define TIO_FILECURSORFLAG_REMOVE 4
+# define TIO_FILECURSORFLAG_LIST 8
+# define TIO_FILECURSORFLAG_STAT 16
+# define TIO_FILECURSORFLAG_OPEN 32
+
+struct tio_file_cursor_functions {
+ tio_file_cursor_close_t *tio_file_cursor_functions_close;
+ tio_file_cursor_copy_t *tio_file_cursor_functions_copy;
+
+ /* Cursor interactions. */
+
+ tio_file_cursor_move_t *tio_file_cursor_functions_move;
/* Directory interactions. */
- tio_fs_create_t *tio_fs_functions_create;
- tio_fs_remove_t *tio_fs_functions_remove;
- tio_fs_move_t *tio_fs_functions_move;
- tio_fs_list_t *tio_fs_functions_list;
+ tio_file_cursor_create_directory_t
+ *tio_file_cursor_functions_create_directory;
+ tio_file_cursor_remove_t *tio_file_cursor_functions_remove;
+ tio_file_cursor_list_t *tio_file_cursor_functions_list;
+ tio_file_cursor_stat_t *tio_file_cursor_functions_stat;
- /* File interactions. */
+ /* Stream interactions. */
- tio_fs_stat_t *tio_fs_functions_stat;
- tio_fs_open_t *tio_fs_functions_open;
+ tio_file_cursor_open_t *tio_file_cursor_functions_open;
};
/* ---
@@ -96,58 +98,28 @@ struct tio_fs_functions {
TIO_BEGIN_DECLS
-/* Path management. */
-
-TIO_EXTERN(int) tio_get_path_child
- OF((tio_path_t *tio__path, tio_path_t **tio__newpathp,
- void *tio__name, size_t tio__name_size));
-TIO_EXTERN(int) tio_get_path_parent
- OF((tio_path_t *tio__path, tio_path_t **tio__parentp));
-
-TIO_EXTERN(int) tio_set_path_child
- OF((tio_path_t **tio__pathp, void *tio__name, size_t tio__name_size));
-TIO_EXTERN(int) tio_set_path_to_parent
- OF((tio_path_t **tio__pathp));
-
-# define tio_free_path(PATH) tio_free(PATH)
+# define tio_next_file(ITER, STATP) \
+ tio_next((ITER), (tio_stat_t **)(void **)(STATP))
-TIO_EXTERN(int) tio_get_path_name
- OF((tio_path_t *tio__path, void **tio__namep,
- size_t *tio__name_sizep));
+/* File cursor management. */
-/* Filesystem management. */
-
-TIO_EXTERN(int) tio_create
- OF((tio_fs_t *tio__fs, tio_path_t *tio__path,
- tio_filetype_t tio__ftyp, ...));
-TIO_EXTERN(int) tio_remove
- OF((tio_fs_t *tio__fs, tio_path_t *tio__path));
TIO_EXTERN(int) tio_move
- OF((tio_fs_t *tio__fs, tio_path_t const *tio__path,
- tio_path_t const *tio__destpath));
+ OF((tio_file_cursor_t *tio__cursor, char const *tio__name));
-TIO_EXTERN(int) tio_stat
- OF((tio_fs_t *tio__fs, tio_stat_t *tio__stat,
- unsigned long tio__flags));
-TIO_EXTERN(int) tio_open
- OF((tio_fs_t *tio__fs, tio_stream_t **tio__streamp,
- tio_path_t const *tio__path, unsigned long tio__flags));
+TIO_EXTERN(int) tio_create_directory
+ OF((tio_file_cursor_t *tio__cursor, char const *tio__name));
+TIO_EXTERN(int) tio_remove
+ OF((tio_file_cursor_t *tio__cursor, char const *tio__name));
TIO_EXTERN(int) tio_list
- OF((tio_fs_t *tio__fs, tio_iter_t **tio__
-
-/* Get a native filesystem. */
-
-# define tio_next_fs(ITER, FSNAMEP) \
- tio_next((ITER), (void **)(char const **)(FSNAMEP))
-TIO_EXTERN(int) tio_list_fs
- OF((tio_iter_t **tio__iterp));
+ OF((tio_file_cursor_t *tio__cursor, tio_iter_t **tio__iterp));
+TIO_EXTERN(int) tio_stat
+ OF((tio_file_cursor_t *tio__cursor, char const *tio__name,
+ tio_stat_t *tio__stat, unsigned long tio__flags));
-TIO_EXTERN(int) tio_get_fs
- OF((tio_fs_t **tio__fsp, char const *tio__name));
-TIO_EXTERN(int) tio_get_fs_folder
- OF((tio_fs_t **tio__fsp, tio_fs_t *tio__fs,
- tio_path_t *tio__foldpath));
+TIO_EXTERN(int) tio_open_file
+ OF((tio_file_cursor_t *tio__cursor, tio_stream_t **tio__streamp,
+ char const *tio__name, unsigned long tio__flags));
TIO_END_DECLS
TIO_END_NAMESPACE
diff --git a/include/libtio/io/usb.h b/include/libtio/io/usb.h
index c18e612..2098229 100644
--- a/include/libtio/io/usb.h
+++ b/include/libtio/io/usb.h
@@ -43,7 +43,7 @@ TIO_BEGIN_DECLS
* Use `tio_list_usb_devices(tio_iter_t **iterp, tio_system_t *system)` */
# define tio_next_usb_device(ITER, PTRP) \
- (tio_next((ITER), (void **)(tio_usb_device_t **)(PTRP)))
+ (tio_next((ITER), (void **)(tio_usb_device_t * const *)(PTRP)))
TIO_END_DECLS
TIO_END_NAMESPACE
diff --git a/include/libtio/system.h b/include/libtio/system.h
index ecc5a5b..63e3452 100644
--- a/include/libtio/system.h
+++ b/include/libtio/system.h
@@ -2,6 +2,7 @@
# define TIO_SYSTEM_H 20190907
# include "cdefs.h"
# include "stream.h"
+# include "time.h"
TIO_BEGIN_NAMESPACE
@@ -35,7 +36,9 @@ typedef TIO_HOOK_TYPE(int) tio_list_serial_ports_t
typedef TIO_HOOK_TYPE(int) tio_sleep_t
OF((void *tio__cookie, unsigned long tio__ms));
typedef TIO_HOOK_TYPE(int) tio_get_timer_t
- OF((void *tio__cookie, tio_timer_t **tio__timer));
+ OF((void *tio__cookie, tio_timer_t **tio__timerp));
+typedef TIO_HOOK_TYPE(int) tio_get_time_t
+ OF((void *tio__cookie, tio_time_t **tio__timep));
/* Callbacks definition. */
@@ -55,6 +58,7 @@ struct tio_system_functions {
tio_list_serial_ports_t *tio_system_functions_list_serial_ports;
tio_sleep_t *tio_system_functions_sleep;
+ tio_get_time_t *tio_system_functions_get_time;
tio_get_timer_t *tio_system_functions_get_timer;
};
@@ -91,6 +95,8 @@ TIO_EXTERN(int) tio_sleep_on_system
OF((tio_system_t *tio__system, unsigned long tio__ms));
TIO_EXTERN(int) tio_get_system_timer
OF((tio_system_t *tio__system, tio_timer_t **tio__timerp));
+TIO_EXTERN(int) tio_get_system_time
+ OF((tio_system_t *tio__system, tio_time_t **tio__timep));
# define tio_sleep(MS) tio_sleep_on_system(NULL, (MS))
# define tio_get_timer(TP) tio_get_system_timer(NULL, (TP))
diff --git a/include/libtio/time.h b/include/libtio/time.h
new file mode 100644
index 0000000..694e1c2
--- /dev/null
+++ b/include/libtio/time.h
@@ -0,0 +1,111 @@
+#ifndef LIBTIO_TIME_H
+# define LIBTIO_TIME_H 20200110
+# include "cdefs.h"
+# include "error.h"
+
+TIO_BEGIN_NAMESPACE
+
+TIO_STRUCT(tio_time, tio_time_t)
+TIO_STRUCT(tio_timedelta, tio_timedelta_t)
+
+/* Format strings here use the following format:
+ *
+ * - a character representing the type of data, amongst:
+ *
+ * * 'i': a `int` component.
+ * * 'l': a `long` component.
+ * * 'd': a `double` component.
+ * * 's': a string component, with:
+ * - on input: a 'char const *' component.
+ * - on output: a 'char *' and a 'size_t' components.
+ * * 'r': a `tio_timedelta_t const *` component.
+ *
+ * - two characters representing the data identifier, usually the first
+ * character is the category (e.g. 'g' for gregorian), and the second
+ * the data component.
+ *
+ * ---
+ *
+ * Broken-down time components.
+ *
+ * Serves to represent the year. */
+
+# define TIO_TIMEFIELD_YEAR "lgy" /* current year */
+# define TIO_TIMEFIELD_MONTH "igm" /* current month */
+# define TIO_TIMEFIELD_WEEK "igw" /* current week (ISO-8601) */
+# define TIO_TIMEFIELD_DAY "igd" /* current day of month */
+# define TIO_TIMEFIELD_YDAY "igD" /* current day of year */
+# define TIO_TIMEFIELD_WDAY "igW" /* current day of week */
+# define TIO_TIMEFIELD_HOUR "igh" /* current hour */
+# define TIO_TIMEFIELD_MIN "igM" /* current minute */
+# define TIO_TIMEFIELD_SEC "igs" /* current second */
+
+# define TIO_TIMEFIELD_TSTAMPHIGH "lgT" /* timestamp (high 32-bit) */
+# define TIO_TIMEFIELD_TSTAMPLOW "lgt" /* timestamp (low 32-bit) */
+# define TIO_TIMEFIELD_NSEC "lgn" /* current nanosecond */
+
+# define TIO_TIMEFIELD_ZONEHOUR "igz" /* time zone hour */
+# define TIO_TIMEFIELD_ZONEMIN "igZ" /* time zone minute */
+# define TIO_TIMEFIELD_RES "rgr" /* time resolution */
+
+/* Time units for deltas and calculations. */
+
+# define TIO_TIMEUNIT_YEAR "lgy" /* years */
+# define TIO_TIMEUNIT_MONTH "igm" /* months */
+# define TIO_TIMEUNIT_DAY "igd" /* days */
+# define TIO_TIMEUNIT_HOUR "igh" /* hours */
+# define TIO_TIMEUNIT_MIN "igM" /* minutes */
+# define TIO_TIMEUNIT_SEC "igs" /* seconds */
+# define TIO_TIMEUNIT_NSEC "lgn" /* nanoseconds */
+
+/* ---
+ * Public interface.
+ * --- */
+
+TIO_BEGIN_DECLS
+
+/* Time delta management. */
+
+# if defined(__STDC__) && __STDC__
+TIO_EXTERN(int) tio_make_timedelta(tio_timedelta_t **tio__deltap,
+ char const *tio__units, ...);
+# else
+TIO_EXTERN(int) tio_make_timedelta();
+# endif
+
+/* Time management. */
+
+# if defined(__STDC__) && __STDC__
+TIO_EXTERN(int) tio_make_time(tio_time_t **tio__timep,
+ char const *tio__fields, ...);
+
+TIO_EXTERN(int) tio_add_time(tio_time_t *tio__timep,
+ char const *tio__units, ...);
+TIO_EXTERN(int) tio_remove_time(tio_time_t *tio__timep,
+ char const *tio__units, ...);
+# else
+TIO_EXTERN(int) tio_make_time();
+
+TIO_EXTERN(int) tio_add_time();
+TIO_EXTERN(int) tio_remove_time();
+# endif
+
+TIO_EXTERN(int) tio_copy_time
+ OF((tio_time_t **tio__timep, tio_time_t const *tio__time));
+
+TIO_EXTERN(int) tio_add_timedelta
+ OF((tio_time_t *tio__time, tio_timedelta_t const *tio__delta));
+TIO_EXTERN(int) tio_remove_timedelta
+ OF((tio_time_t *tio__time, tio_timedelta_t const *tio__delta));
+
+# if defined(__STDC__) && __STDC__
+TIO_EXTERN(int) tio_get_time(tio_time_t *tio__time,
+ char const *tio__fields, ...);
+# else
+TIO_EXTERN(int) tio_get_time_fields();
+# endif
+
+TIO_END_DECLS
+TIO_END_NAMESPACE
+
+#endif /* LIBTIO_CHRONO_H */
diff --git a/lib/error/names.c b/lib/error/names.c
index 9603210..56dca1b 100644
--- a/lib/error/names.c
+++ b/lib/error/names.c
@@ -14,9 +14,10 @@ TIO_LOCAL_DATA(char const *) errors[] = {
"tio_error_notfound", "an element wasn't found",
"tio_error_access", "not enough privileges to operate",
"tio_error_lock", "a locking or unlocking operation has failed",
+ "tio_error_nedata", "not enough data to represent the data type",
+ "tio_error_tmdata", "contradictory data to represent the data type",
- UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,
- UNKNOWN, UNKNOWN, UNKNOWN,
+ UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN,
"tio_error_stream", "invalid/erroneous stream",
"tio_error_read", "a read operation has failed",
diff --git a/lib/internals.h b/lib/internals.h
index 2486435..6726143 100644
--- a/lib/internals.h
+++ b/lib/internals.h
@@ -158,39 +158,4 @@ TIO_EXTERN(void) tio_log_mem();
# endif
# endif
-/* ---
- * Function with candidates.
- * --- */
-
-/* This set of macros are used when declaring a function with
- * candidates. */
-
-# define CANDIDATE_FUNC_BODY(FUNC_TYPE, CANDIDATES, ARGS) \
- { \
- static FUNC_TYPE *last_successful_candidate = (FUNC_TYPE *)0; \
- \
- if (last_successful_candidate) { \
- int err = (*last_successful_candidate) ARGS; \
- if (err != tio_error_op) \
- return (err); \
- } \
- \
- { \
- FUNC_TYPE * const *func; \
- \
- for (func = (CANDIDATES); func; func++) { \
- int err; \
- \
- err = (**func) ARGS; \
- if (err == tio_error_op) \
- continue ; \
- \
- last_successful_candidate = *func; \
- return (err); \
- } \
- } \
- \
- return (tio_error_op); \
- }
-
#endif /* LOCAL_INTERNALS_H */
diff --git a/lib/system/unix/fs.c b/lib/system/unix/fs.c
new file mode 100644
index 0000000..c277ef1
--- /dev/null
+++ b/lib/system/unix/fs.c
@@ -0,0 +1,177 @@
+#include "internals.h"
+#ifndef LIBTIO_DISABLED_UNIX
+
+/* ---
+ * Directory listing iterator.
+ * --- */
+
+TIO_STRUCT(dir_cookie, dir_cookie_t)
+struct dir_cookie {
+ char *path;
+ DIR *dp;
+
+ char *start;
+ tio_stat_t filest;
+};
+
+TIO_HOOK(int) next_dir_entry(dir_cookie_t *cookie, tio_stat_t **statp)
+{
+ struct dirent *dr;
+ tio_file_type_t ftyp;
+
+ *statp = NULL;
+
+ while (1) {
+ /* Get the next entry. */
+
+ if (!(dr = readdir(cookie->dp)))
+ return (tio_error_iter);
+
+ /* Read the entry stat. */
+
+ {
+ struct statx statxbuf;
+
+ strcpy(cookie->start, dr->d_name);
+ if (statx(AT_FDCWD, cookie->path,
+ AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW, STATX_ALL,
+ &statxbuf))
+ continue ;
+
+ /* Read the file type. */
+
+ switch (statxbuf.stx_mode & S_IFMT) {
+ case S_IFDIR:
+ ftyp = TIO_FILETYPE_DIR;
+ break;
+ case S_IFREG:
+ ftyp = TIO_FILETYPE_REG;
+ break;
+ default:
+ ftyp = TIO_FILETYPE_OTH;
+ }
+
+ /* Fill in the structure and we're good. */
+
+ cookie->filest.tio_stat_type = ftyp;
+ cookie->filest.tio_stat_size = statxbuf.stx_size;
+
+# define STXTIME(FNL, SRC) \
+ tio_make_time(&FNL, TIO_TIMEFIELD_TSTAMPHIGH TIO_TIMEFIELD_TSTAMPLOW \
+ TIO_TIMEFIELD_NSEC, \
+ (unsigned long)(((SRC).tv_sec & 0x7fffffff00000000) >> 32), \
+ (unsigned long)((SRC).tv_sec & 0x00000000ffffffff), \
+ (unsigned long)((SRC).tv_nsec))
+
+ STXTIME(cookie->filest.tio_stat_btime,
+ statxbuf.stx_btime)
+ STXTIME(cookie->filest.tio_stat_atime,
+ statxbuf.stx_atime)
+ STXTIME(cookie->filest.tio_stat_mtime,
+ statxbuf.stx_mtime)
+
+# undef STXTIME
+
+ break;
+ }
+ }
+
+ *statp = &cookie->filest;
+ return (tio_ok);
+}
+
+/* ---
+ * File cursor definition.
+ * --- */
+
+TIO_STRUCT(cookie, cookie_t)
+struct cookie {
+ char *path;
+ char **pathp;
+};
+
+TIO_LOCAL(void) close_cursor(cookie_t *cookie)
+{
+ /* TODO */
+}
+
+TIO_LOCAL(int) copy_cursor(cookie_t *cookie, tio_file_cursor_t **cursorp)
+{
+ /* TODO */
+}
+
+TIO_LOCAL(int) move_cursor(cookie_t *cookie, char const *name)
+{
+ /* TODO */
+}
+
+TIO_LOCAL(int) create_directory(cookie_t *cookie, char const *name)
+{
+ /* TODO */
+}
+
+TIO_LOCAL(int) remove_file(cookie_t *cookie, char const *name)
+{
+ /* TODO */
+}
+
+TIO_LOCAL(int) stat_file(cookie_t *cookie, char const *name,
+ tio_stat_t *st, unsigned long flags)
+{
+ /* TODO */
+}
+
+TIO_LOCAL(int) open_file(cookie_t *cookie, tio_stream_t **streamp,
+ char const *name, unsigned long flags, tio_off_t size)
+{
+ /* TODO */
+}
+
+/* ---
+ * File cursor opening function.
+ * --- */
+
+TIO_LOCAL_DATA(tio_file_cursor_functions_t const) unix_file_functions = {
+ (tio_file_cursor_close_t *)&close_cursor,
+ (tio_file_cursor_copy_t *)&copy_cursor,
+
+ (tio_file_cursor_move_t *)&move_cursor,
+ (tio_file_cursor_create_directory_t *)&create_directory,
+ (tio_file_cursor_list_t *)&remove_file,
+ (tio_file_cursor_stat_t *)&stat_file,
+ (tio_file_cursor_open_t *)&open_file
+};
+
+/* `tio_unix_open_file_cursor()`: open a file cursor on a given path. */
+
+TIO_EXTERN(int) tio_unix_open_file_cursor(tio_file_cursor_t **cursorp,
+ char const *path)
+{
+ size_t maxpathlen;
+ char *rpresult;
+ cookie_t *cookie;
+
+ maxpathlen = tio_unix_path_max();
+ cookie = tio_alloc(1, sizeof(cookie_t) + maxpathlen);
+ if (!cookie)
+ return (tio_error_alloc);
+
+
+ pathlen = strlen(path);
+
+ if (pathlen > maxpathlen) {
+ msg((ll_error, "given path length was above maximum path length"));
+ msg((ll_error, "given: %zu, maximum: %zu", pathlen, maxpathlen));
+ return (tio_error_arg);
+ }
+
+ /* TODO:
+ * - get the canonical representation of the path (absolute if
+ * relative, remove the double slashes, etc).
+ * - check if it is a directory? although it could evolve...
+ * - allocate the cookie and create the corresponding object. */
+
+ return (tio_error_op);
+}
+
+#endif
diff --git a/lib/system/unix/internals.h b/lib/system/unix/internals.h
index b9189bb..51f4f1f 100644
--- a/lib/system/unix/internals.h
+++ b/lib/system/unix/internals.h
@@ -17,17 +17,25 @@
TIO_EXTERN(size_t) tio_unix_path_max
OF((void));
+TIO_EXTERN(int) tio_unix_canonicalize_path
+ OF((char *tio__outp, char const *tio__src, size_t tio__size));
TIO_EXTERN(int) tio_unix_list_serial_ports
OF((void *cookie, tio_iter_t **tio__iterp));
-TIO_EXTERN(int) tio_unix_open_file
- OF((tio_stream_t **tio__streamp, int tio__fd, int tio__cl));
-TIO_EXTERN(int) tio_unix_open_serial_file
- OF((tio_stream_t **tio__streamp, int tio__fd, int tio__cl));
-TIO_EXTERN(int) tio_unix_open_serial_port
- OF((void *cookie, tio_stream_t **tio__streamp, char const *tio__path,
- tio_serial_attrs_t const *tio__attrs));
+/* Open a stream.
+ *
+ * `CLOSE`: should close the stream afterwards.
+ * `WRITE`: should be a writable stream.
+ * `SERIAL`: should be a serial stream instead of a generic one. */
+
+# define TIO_UNIX_OPEN_FLAG_CLOSE 1
+# define TIO_UNIX_OPEN_FLAG_WRITE 2
+# define TIO_UNIX_OPEN_FLAG_SERIAL 4
+
+TIO_EXTERN(int) tio_unix_open
+ OF((tio_stream_t **tio__streamp, int tio__fd,
+ unsigned long tio__flags));
# endif
#endif
diff --git a/lib/system/unix/open.c b/lib/system/unix/open.c
index c6c2e5d..452e155 100644
--- a/lib/system/unix/open.c
+++ b/lib/system/unix/open.c
@@ -13,13 +13,65 @@ TIO_HOOK(int) tio_unix_sleep(void *cookie, unsigned long ms)
return (tio_ok);
}
+TIO_HOOK(int) open_serial_stream(void *cookie, tio_stream_t **streamp,
+ char const *path, tio_serial_attrs_t const *attrs)
+{
+ int fd, err;
+
+ (void)cookie;
+
+ fd = open(path, O_NOCTTY | O_RDWR);
+ if (fd < 0) switch (errno) {
+ case ENODEV: case ENOENT: case ENXIO:
+ case EPIPE: case ESPIPE:
+ msg((ll_error, "couldn't open serial device: %s",
+ strerror(errno)));
+ return (tio_error_notfound);
+
+ case EACCES:
+ msg((ll_error, "permission denied"));
+ return (tio_error_notfound);
+
+ default:
+ msg((ll_error, "unknown error: %s (0x%02X)",
+ strerror(errno), errno));
+ return (tio_error_unknown);
+ }
+
+ err = tio_unix_open(streamp, fd,
+ TIO_UNIX_OPEN_FLAG_CLOSE | TIO_UNIX_OPEN_FLAG_WRITE
+ | TIO_UNIX_OPEN_FLAG_SERIAL);
+ if (err)
+ return (err);
+
+ /* Okay, we'll just set the attributes up directly using the stream
+ * interface, #yolo. */
+
+ err = tio_set_serial_attrs(*streamp, attrs, TIO_SERIALFLAG_ALL);
+ if (err) {
+ tio_close(*streamp);
+ *streamp = NULL;
+
+ msg((ll_error, "set serial attrs returned error %s, "
+ "giving up stream creating operation.", tio_error_name(err)));
+
+ return (tio_error_unknown);
+ }
+
+ return (tio_ok);
+}
+
+/* ---
+ * Main opening function.
+ * --- */
+
TIO_LOCAL_DATA(tio_system_functions_t) unix_system_functions = {
(tio_close_t *)NULL,
(tio_open_usb_stream_t *)NULL,
(tio_list_usb_devices_t *)NULL,
- (tio_open_serial_stream_t *)&tio_unix_open_serial_port,
+ (tio_open_serial_stream_t *)&open_serial_stream,
(tio_list_serial_ports_t *)&tio_unix_list_serial_ports,
(tio_sleep_t *)&tio_unix_sleep,
diff --git a/lib/system/unix/stream.c b/lib/system/unix/stream.c
index f271afc..ab64714 100644
--- a/lib/system/unix/stream.c
+++ b/lib/system/unix/stream.c
@@ -418,23 +418,29 @@ TIO_LOCAL_DATA(tio_serial_functions_t const) unix_serial_functions = {
(tio_write_t *)unix_write,
(tio_set_serial_attrs_t *)unix_set_attrs,
- NULL,
- NULL,
- NULL
+ NULL, NULL, NULL
};
/* ---
* Open the stream.
* --- */
-/* `tio_unix_open_file()`: open the UNIX stream from a file descriptor. */
+/* `tio_unix_open()`: open the UNIX stream from a file descriptor.
+ * Opens according to the following flags, using the `TIO_UNIX_OPEN_FLAG_*`
+ * prefix:
+ *
+ * `CLOSE`: close the stream at the end.
+ * `WRITE`: allow writes to the stream.
+ * `SERIAL`: open a serial stream instead of a generic one (disables
+ * seeking). */
-TIO_EXTERN(int) tio_unix_open_file(tio_stream_t **streamp, int fd, int cl)
+TIO_EXTERN(int) tio_unix_open(tio_stream_t **streamp, int fd,
+ unsigned long flags)
{
cookie_t *cookie = NULL;
- unsigned int mode = TIO_OPENFLAG_READ | TIO_OPENFLAG_WRITE
- | TIO_OPENFLAG_SEEK;
- int err;
+ int err = tio_error_unknown;
+ int read_allowed = 0, write_allowed = 0, seek_allowed = 0;
+ tio_off_t offset;
/* Check if the device is valid. */
@@ -443,70 +449,23 @@ TIO_EXTERN(int) tio_unix_open_file(tio_stream_t **streamp, int fd, int cl)
goto fail;
}
- if (read(fd, NULL, 0) < 0)
- mode &= ~TIO_OPENFLAG_READ;
- if (write(fd, NULL, 0) < 0)
- mode &= ~TIO_OPENFLAG_WRITE;
- if (!(mode & (TIO_OPENFLAG_READ | TIO_OPENFLAG_WRITE))) {
- err = tio_error_arg;
- goto fail;
- }
+ read_allowed = read(fd, NULL, 0) >= 0;
- /* Allocate cookie. */
+ if (flags | TIO_UNIX_OPEN_FLAG_WRITE) {
+ write_allowed = write(fd, NULL, 0) < 0;
- if (!(cookie = tio_alloc(1, sizeof(cookie_t)))) {
- err = tio_error_alloc;
- goto fail;
+ if (!write_allowed) {
+ err = tio_error_write;
+ goto fail;
+ }
}
- cookie->fd = fd;
- cookie->cl = cl;
- cookie->to = -1;
- cookie->str = 0;
- cookie->end = -1;
-
- /* Initialize for real. */
-
- msg((ll_info, "Initializing UNIX stream with fd %d", fd));
+ seek_allowed = ~flags & TIO_UNIX_OPEN_FLAG_SERIAL;
- /* Final call. */
-
- return (tio_open(streamp, NULL, cookie, mode, &unix_functions, 0));
-fail:
- if (fd >= 0 && cl)
- close(fd);
- return (err);
-}
-
-/* `tio_unix_open_serial_file()`: open a serial stream using a file
- * descriptor representing a serial port. */
-
-TIO_EXTERN(int) tio_unix_open_serial_file(tio_stream_t **streamp,
- int fd, int cl)
-{
- cookie_t *cookie = NULL;
- tio_serial_attrs_t attrs;
- int err;
-
- /* Check if the device is valid. */
-
- if (fd < 0) {
+ if (!read_allowed && !write_allowed) {
err = tio_error_arg;
goto fail;
}
- if (read(fd, NULL, 0) < 0) {
- err = tio_error_read;
- goto fail;
- }
- if (write(fd, NULL, 0) < 0) {
- err = tio_error_write;
- goto fail;
- }
-
- /* Get the initial serial attributes. */
-
- if ((err = unix_get_attrs(&attrs, fd)))
- goto fail;
/* Allocate cookie. */
@@ -516,74 +475,64 @@ TIO_EXTERN(int) tio_unix_open_serial_file(tio_stream_t **streamp,
}
cookie->fd = fd;
- cookie->cl = cl;
+ cookie->cl = flags & TIO_UNIX_OPEN_FLAG_CLOSE;
cookie->to = -1;
cookie->str = 0;
cookie->end = -1;
- /* Initialize for real. */
-
- msg((ll_info, "Initializing UNIX stream with fd %d", fd));
-
- /* Final call. */
-
- return (tio_open_serial(streamp, NULL, cookie, TIO_OPENFLAG_SERIAL_READ
- | TIO_OPENFLAG_SERIAL_WRITE | TIO_OPENFLAG_SERIAL_SETATTRS,
- &unix_serial_functions, &attrs, TIO_SERIALFLAG_ALL));
-fail:
- if (fd >= 0 && cl)
- close(fd);
- return (err);
-}
-
-/* `tio_open_unix_serial_port()`: open a serial port using
- * a serial port path. */
+ /* If seeking on the stream is allowed, we want to get the current
+ * position on the stream. */
-TIO_EXTERN(int) tio_open_unix_serial_port(void *cookie,
- tio_stream_t **streamp, char const *path,
- tio_serial_attrs_t const *attrs)
-{
- int fd, err;
-
- (void)cookie;
-
- fd = open(path, O_NOCTTY | O_RDWR);
- if (fd < 0) switch (errno) {
- case ENODEV: case ENOENT: case ENXIO:
- case EPIPE: case ESPIPE:
- msg((ll_error, "couldn't open serial device: %s",
- strerror(errno)));
- return (tio_error_notfound);
+ offset = 0;
+ if (seek_allowed) {
+ off_t pos = lseek(fd, 0, SEEK_CUR);
+ if (pos = (off_t)-1) switch (errno) {
+ case ESPIPE:
+ seek_allowed = 0;
+ break;
- case EACCES:
- msg((ll_error, "permission denied"));
- return (tio_error_notfound);
+ default:
+ msg((ll_error, "Error %d encountered on lseek while "
+ "gathering file position: %s", errno,
+ strerror(errno)));
+ goto fail;
+ }
- default:
- msg((ll_error, "unknown error: %s (0x%02X)",
- strerror(errno), errno));
- return (tio_error_unknown);
+ offset = (tio_off_t)pos;
}
- err = tio_unix_open_serial_file(streamp, fd, 1);
- if (err)
- return (err);
+ /* Initialize for real.
+ * If we want to open a serial stream, let's let's.
+ * Otherwise, open a generic stream. */
- /* Okay, we'll just set the attributes up directly using the stream
- * interface, #yolo. */
+ msg((ll_info, "Creating UNIX stream with fd %d", fd));
- err = tio_set_serial_attrs(*streamp, attrs, TIO_SERIALFLAG_ALL);
- if (err) {
- tio_close(*streamp);
- *streamp = NULL;
+ if (flags & TIO_UNIX_OPEN_FLAG_SERIAL) {
+ tio_serial_attrs_t attrs;
- msg((ll_error, "set serial attrs returned error %s, "
- "giving up stream creating operation.", tio_error_name(err)));
+ /* Get the current attributes for creating the serial stream. */
- return (tio_error_unknown);
+ if ((err = unix_get_attrs(&attrs, fd)))
+ goto fail;
+
+ return (tio_open_serial(streamp, NULL, cookie,
+ (read_allowed ? TIO_OPENFLAG_SERIAL_READ : 0) |
+ (write_allowed ? TIO_OPENFLAG_SERIAL_WRITE : 0) |
+ TIO_OPENFLAG_SERIAL_SETATTRS,
+ &unix_serial_functions, &attrs, TIO_SERIALFLAG_ALL));
}
- return (tio_ok);
+ return (tio_open(streamp, NULL, cookie,
+ (read_allowed ? TIO_OPENFLAG_READ : 0) |
+ (write_allowed ? TIO_OPENFLAG_WRITE : 0) |
+ (seek_allowed ? TIO_OPENFLAG_SEEK : 0),
+ &unix_functions, offset));
+fail:
+ if (cookie)
+ tio_free(cookie);
+ if (fd >= 0 && flags & TIO_UNIX_OPEN_FLAG_CLOSE)
+ close(fd);
+ return (err);
}
#endif
diff --git a/lib/system/unix/time.c.draft b/lib/system/unix/time.c.draft
new file mode 100644
index 0000000..b5778b6
--- /dev/null
+++ b/lib/system/unix/time.c.draft
@@ -0,0 +1,29 @@
+#include "internals.h"
+#ifndef LIBTIO_DISABLED_UNIX
+
+TIO_EXTERN(int) tio_unix_get_time(void *cookie, tio_time_t **timep)
+{
+ struct timespec res;
+ struct timespec tp;
+ time_t t;
+ time_t utcoff;
+
+ /* TODO: not really failsafe, ain't it? */
+
+ /* Get the UTC offset. */
+
+ t = time(NULL);
+ utcoff = timegm(localtime(&t)) - t;
+
+ /* Get the timespec. */
+
+ clock_getres(CLOCK_REALTIME, &res);
+ clock_gettime(CLOCK_REALTIME, &tp);
+
+ /* Create the time out of all of this. */
+
+ return (tio_make_time(timep, "rzsn",
+ tio_make_timedelta(
+}
+
+#endif
diff --git a/lib/system/unix/utils.c b/lib/system/unix/utils.c
index cf4869d..a2563c7 100644
--- a/lib/system/unix/utils.c
+++ b/lib/system/unix/utils.c
@@ -19,4 +19,95 @@ TIO_EXTERN(size_t) tio_unix_path_max(void)
return ((size_t)path_max);
}
+/* `tio_unix_canonicalize_path()`: get the canonical path out of the
+ * given path from the current directory. */
+
+TIO_EXTERN(int) tio_unix_canonicalize_path(char *outp, char const *src,
+ size_t size)
+{
+ int delta = 0;
+ char *wd = NULL;
+ char *out = outp;
+
+ /* To do this operation, we're actually going to do it backwards,
+ * start from the last character of the path and go back.
+ *
+ * Let's go through the path first. */
+
+#define GO_THROUGH_PATH(PATH) \
+ { \
+ size_t len = strlen(src), partlen; \
+ char const *partstart; \
+ \
+ len = strlen(src); \
+ \
+ while (1) { \
+ while (len && src[len] == '/') \
+ len--; \
+ if (!len) \
+ break; \
+ \
+ partstart = (char const *)memrchr(src, '/', len); \
+ partstart = partstart ? partstart + 1 : p; \
+ \
+ partlen = (&src[len] - partstart + 1); \
+ if (partlen == 1 && partstart[0] == '.') \
+ else if (partlen == 2 && partstart[0] == '.' \
+ && partstart[1] == '.') \
+ delta = delta - 1; \
+ else if (delta < 0) \
+ delta++; \
+ else { \
+ if (size < partlen + 1) \
+ goto fail; \
+ for (++partlen; partlen; partlen--) \
+ *outp++ = partstart[partlen - 1]; \
+ *outp++ = '/'; \
+ } \
+ } \
+ }
+
+ GO_THROUGH_PATH(src)
+
+ /* If the path wasn't absolute, we actually want to add the
+ * working directory at the beginning of the path. */
+
+ if (*src != '/') {
+ char *wd;
+ size_t wdsize = tio_unix_path_max() + 1;
+
+ wd = tio_alloc(1, wdsize);
+ if (!wd)
+ return (tio_error_alloc);
+
+ if (!getcwd(wd, wdsize - 1)) {
+ msg((ll_error, "getcwd failed: %s", strerror(errno)));
+ tio_free(wd);
+ return (tio_error_unknown);
+ }
+
+ GO_THROUGH_PATH(wd)
+ tio_free(wd);
+ }
+
+ /* Now that we're done, add the last NUL and reverse the path. */
+
+ if (!size)
+ return (tio_error_arg);
+ *(outp + 1) = '\0';
+
+ while (out < outp) {
+ char tmp = *out;
+
+ *out = *outp;
+ *outp = tmp;
+ }
+
+ return (tio_ok);
+
+fail:
+ tio_free(wd);
+ return (tio_error_arg);
+}
+
#endif
diff --git a/lib/system/windows/stream.c b/lib/system/windows/stream.c
index ce69458..717a8e2 100644
--- a/lib/system/windows/stream.c
+++ b/lib/system/windows/stream.c
@@ -493,8 +493,8 @@ fail:
return (err);
}
-/* `tio_open_windows_serial_handle()`: open a generic Windows stream out of a
- * handle. */
+/* `tio_open_windows_serial_handle()`: open a generic Windows stream out
+ * of a handle. */
TIO_EXTERN(int) tio_win_open_serial_handle(tio_stream_t **streamp,
HANDLE handle, int cl)
diff --git a/lib/time/internals.h b/lib/time/internals.h
new file mode 100644
index 0000000..8fffa24
--- /dev/null
+++ b/lib/time/internals.h
@@ -0,0 +1,62 @@
+#ifndef LOCAL_TIME_INTERNALS_H
+# define LOCAL_TIME_INTERNALS_H 20200123
+# include "../internals.h"
+
+/* Time types:
+ *
+ * - `TIO_TIMETYPE_GREGORIAN_NOTZ`: gregorian calendar date and time,
+ * without any timezone added.
+ *
+ * Supported fields: g_year, g_month, g_day, g_hour, g_min, g_sec,
+ * g_nsec, res.
+ *
+ * - `TIO_TIMETYPE_GREGORIAN_TZ`: gregorian calendar date and time,
+ * with an added timezone.
+ *
+ * Supported fields: g_year, g_month, g_day, g_hour, g_min, g_sec,
+ * g_nsec, res, g_tzhour, g_tzmin. */
+
+# define TIO_TIMETYPE_GREGORIAN_NOTZ 1
+# define TIO_TIMETYPE_GREGORIAN_TZ 2
+
+struct tio_time {
+ int tio_time_type;
+
+ /* Gregorian calendar components. */
+
+ long tio_time_g_year;
+ int tio_time_g_month;
+ int tio_time_g_day;
+ int tio_time_g_hour;
+ int tio_time_g_min;
+ int tio_time_g_sec;
+ long tio_time_g_nsec;
+
+ int tio_time_g_tzhour;
+ int tio_time_g_tzmin;
+
+ /* General components. */
+
+ tio_timedelta_t tio_time_res;
+};
+
+/* Time delta types:
+ *
+ * - `TIO_TIMEDELTATYPE_GREGORIAN`: gregorian calendar difference. */
+
+# define TIO_TIMEDELTATYPE_GREGORIAN 1
+
+struct tio_timedelta {
+ int tio_timedelta_type;
+
+ /* Gregorian calendar components. */
+
+ long tio_time_g_years;
+ int tio_time_g_months;
+ int tio_time_g_days;
+ int tio_time_g_minutes;
+ int tio_time_g_sec;
+ long tio_time_g_nsec;
+};
+
+#endif