Browse Source

Update to stage0-posix 1.3.

master
Andrius Štikonas 3 months ago
parent
commit
b885cb5727
  1. 8
      .gitmodules
  2. 100
      LICENSES/GPL-1.0-or-later.txt
  3. 2
      SHA256SUMS.sources
  4. 8
      dev-utils/fletcher16-gen/.gitignore
  5. 42
      dev-utils/fletcher16-gen/compile-fletcher16-gen.sh
  6. 40
      dev-utils/fletcher16-gen/fletcher16-gen.c
  7. 17
      dev-utils/m2-functions/file_print.c
  8. 21
      dev-utils/m2-functions/in_set.c
  9. 24
      dev-utils/m2-functions/match.c
  10. 159
      dev-utils/m2-functions/numerate_number.c
  11. 20
      dev-utils/m2-functions/require.c
  12. 59
      dev-utils/m2-functions/string.c
  13. 45
      parts.rst
  14. 71
      sysa.py
  15. 132
      sysa/after.kaem
  16. 128
      sysa/after.kaem.run
  17. 13
      sysa/base.kaem.run
  18. 1
      sysa/bootstrap-seeds
  19. 4
      sysa/gzip-1.2.4/checksums
  20. 2
      sysa/gzip-1.2.4/gzip-1.2.4.kaem
  21. 26
      sysa/mes/checksums
  22. 9
      sysa/mes/mes.kaem
  23. 3
      sysa/mescc-tools-extra/checksums
  24. 130
      sysa/mescc-tools-extra/mescc-tools-extra.kaem
  25. 161
      sysa/mescc-tools-extra/src/chmod.c
  26. 323
      sysa/mescc-tools-extra/src/cp.c
  27. 103
      sysa/mescc-tools-extra/src/fletcher16.c
  28. 17
      sysa/mescc-tools-extra/src/functions/file_print.c
  29. 21
      sysa/mescc-tools-extra/src/functions/in_set.c
  30. 24
      sysa/mescc-tools-extra/src/functions/match.c
  31. 159
      sysa/mescc-tools-extra/src/functions/numerate_number.c
  32. 20
      sysa/mescc-tools-extra/src/functions/require.c
  33. 59
      sysa/mescc-tools-extra/src/functions/string.c
  34. 8
      sysa/mescc-tools-seed/checksums
  35. 1
      sysa/mescc-tools-seed/src/mescc-tools-patched
  36. 1
      sysa/mescc-tools-seed/src/mescc-tools-seed
  37. 2
      sysa/patch-2.5.9/checksums
  38. 2
      sysa/patch-2.5.9/patch-2.5.9.kaem
  39. 38
      sysa/pre-sha.sha256sums
  40. 2
      sysa/sed-4.0.9/checksums/pass1
  41. 2
      sysa/sed-4.0.9/sed-4.0.9.kaem
  42. 1
      sysa/sha-2-61555d/checksums
  43. 151
      sysa/sha-2-61555d/patches/frontend.patch
  44. 30
      sysa/sha-2-61555d/sha-2-61555d.kaem
  45. 12
      sysa/stage0-posix/checksums
  46. 1
      sysa/stage0-posix/src
  47. 2
      sysa/tar-1.12/checksums
  48. 2
      sysa/tar-1.12/tar-1.12.kaem
  49. 29
      sysa/tcc-0.9.26/checksums
  50. 25
      sysa/tcc-0.9.26/tcc-0.9.26.kaem
  51. 1
      sysa/untar/checksums
  52. 14
      sysa/untar/untar.kaem

8
.gitmodules

@ -4,11 +4,8 @@
# SPDX-License-Identifier: MIT
[submodule "sysa/mescc-tools-seed/src/mescc-tools-seed"]
path = sysa/mescc-tools-seed/src/mescc-tools-seed
path = sysa/stage0-posix/src
url = https://github.com/oriansj/mescc-tools-seed/
[submodule "bootstrap-seeds"]
path = sysa/bootstrap-seeds
url = https://github.com/oriansj/bootstrap-seeds
[submodule "sysa/mes/src/mes"]
path = sysa/mes/src/mes
url = https://gitlab.com/janneke/mes.git
@ -18,9 +15,6 @@
[submodule "sysa/tcc-0.9.26/src/tcc-0.9.26"]
path = sysa/tcc-0.9.26/src/tcc-0.9.26
url = https://gitlab.com/janneke/tinycc
[submodule "sysa/mescc-tools-patched"]
path = sysa/mescc-tools-seed/src/mescc-tools-patched
url = https://github.com/oriansj/mescc-tools
[submodule "sysa/tcc-0.9.27/src/tcc-0.9.27"]
path = sysa/tcc-0.9.27/src/tcc-0.9.27
url = git://repo.or.cz/tinycc.git

100
LICENSES/GPL-1.0-or-later.txt
File diff suppressed because it is too large
View File

2
SHA256SUMS.sources

@ -73,10 +73,8 @@ ac610bda97abe0d9f6b7c963255a11dcb196c25e337c61f94e4778d632f1d8fd patch-2.7.6.ta
a5e66f6ebf701b0567f569f57cae82abf5ce57af70a2b45ae71323b61f49134e perl-5.6.2.tar.gz
3ec396d97debb6c1a112c1c6ff58b03a55866b75cd4fe06b74295ac9c5fc5ff2 perl-5f2dc80.tar.bz2
c365874794187f8444e5d22998cd5888ffa47f36def4b77517a808dec27c0600 sed-4.0.9.tar.gz
404a8d72427a559c074e581bf8f7d5a73283faf249cd3faf6dc1c6faf97d07c7 sha-2-61555d.tar.gz
c6c37e888b136ccefab903c51149f4b7bd659d69d4aea21245f61053a57aa60a tar-1.12.tar.gz
63bebd26879c5e1eea4352f0d03c991f966aeb3ddeb3c7445c902568d5411d28 tar-1.34.tar.xz
de23af78fca90ce32dff2dd45b3432b2334740bb9bb7b05bf60fdbfc396ceb9c tcc-0.9.27.tar.bz2
988403c1542d15ad044600b909997ba3079b10e03224c61188117f3676b02caa texinfo-6.7.tar.xz
1b700d375e253e88bdf79caa244284a899b80f59fd64fdd3aaa6408b7bd715ae untar.c
166c48d2842519bc4f96333bff9e265f8cdda44d38e40594ef3f9bbb52890490 xz-5.0.5.tar.bz2

8
dev-utils/fletcher16-gen/.gitignore

@ -1,8 +0,0 @@
# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: MIT
fletcher16.M1
fletcher16-footer.M1
fletcher16.hex2
fletcher16-gen

42
dev-utils/fletcher16-gen/compile-fletcher16-gen.sh

@ -1,42 +0,0 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: GPL-3.0-or-later
set -ex
M2-Planet \
--architecture x86 \
-f ../m2-functions/in_set.c \
-f ../m2-functions/file_print.c \
-f ../m2-functions/numerate_number.c \
-f ../m2-functions/string.c \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/file.c \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/exit.c \
-f ../m2-functions/require.c \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/malloc.c \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/functions/calloc.c \
-f fletcher16-gen.c \
-o fletcher16.M1 \
--debug
blood-elf -f fletcher16.M1 -o fletcher16-footer.M1
M1 \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/x86_defs.M1 \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/libc-core.M1 \
-f fletcher16.M1 \
-f fletcher16-footer.M1 \
--LittleEndian \
--architecture x86 \
-o fletcher16.hex2
hex2 \
-f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/ELF-i386-debug.hex2 \
-f fletcher16.hex2 \
--LittleEndian \
--architecture x86 \
--BaseAddress 0x8048000 \
-o fletcher16-gen \
--exec_enable

40
dev-utils/fletcher16-gen/fletcher16-gen.c

@ -1,40 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdlib.h>
#include <stdio.h>
#define BUF_SIZE 1024
// CONSTANT BUF_SIZE 1024
#define PATH_MAX 512
// CONSTANT PATH_MAX 512
int fletch16(FILE* fp)
{
long sum1 = 0;
long sum2 = 0;
int index;
int c = fgetc(fp);
while(c != EOF)
{
sum1 = (sum1 + c) % 255;
sum2 = (sum2 + sum1) % 255;
c = fgetc(fp);
}
return (sum2 << 8) | sum1;
}
int main(int argc, char** argv)
{
/* Open the file containing the checksums */
FILE* fp = fopen(argv[1], "r");
require(fp != NULL, prepend_string(
prepend_string("Error opening checksum file ", argv[1]), "\n"));
int csum = fletch16(fp);
file_print(prepend_string(prepend_string(prepend_string(
numerate_number(csum), " "), argv[1]), "\n"), stdout);
}

17
dev-utils/m2-functions/file_print.c

@ -1,17 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdio.h>
// void fputc(char s, FILE* f);
void file_print(char* s, FILE* f)
{
while(0 != s[0])
{
fputc(s[0], f);
s = s + 1;
}
}

21
dev-utils/m2-functions/in_set.c

@ -1,21 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
* SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#define FALSE 0
// CONSTANT FALSE 0
#define TRUE 1
// CONSTANT TRUE 1
int in_set(int c, char* s)
{
while(0 != s[0])
{
if(c == s[0]) return TRUE;
s = s + 1;
}
return FALSE;
}

24
dev-utils/m2-functions/match.c

@ -1,24 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#define FALSE 0
// CONSTANT FALSE 0
#define TRUE 1
// CONSTANT TRUE 1
int match(char* a, char* b)
{
int i = -1;
do
{
i = i + 1;
if(a[i] != b[i])
{
return FALSE;
}
} while((0 != a[i]) && (0 !=b[i]));
return TRUE;
}

159
dev-utils/m2-functions/numerate_number.c

@ -1,159 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdlib.h>
// void* calloc(int count, int size);
#define TRUE 1
//CONSTANT TRUE 1
#define FALSE 0
//CONSTANT FALSE 0
int in_set(int c, char* s);
//CONSTANT NULL 0
char* numerate_number(int a)
{
char* result = calloc(16, sizeof(char));
if(NULL == result) return NULL;
int i = 0;
/* Deal with Zero case */
if(0 == a)
{
result[0] = '0';
return result;
}
/* Deal with negatives */
if(0 > a)
{
result[0] = '-';
i = 1;
a = a * -1;
}
/* Using the largest 10^n number possible in 32bits */
int divisor = 0x3B9ACA00;
/* Skip leading Zeros */
while(0 == (a / divisor)) divisor = divisor / 10;
/* Now simply collect numbers until divisor is gone */
while(0 < divisor)
{
result[i] = ((a / divisor) + 48);
a = a % divisor;
divisor = divisor / 10;
i = i + 1;
}
return result;
}
int char2hex(int c)
{
if (c >= '0' && c <= '9') return (c - 48);
else if (c >= 'a' && c <= 'f') return (c - 87);
else if (c >= 'A' && c <= 'F') return (c - 55);
else return -1;
}
int hex2char(int c)
{
if((c >= 0) && (c <= 9)) return (c + 48);
else if((c >= 10) && (c <= 15)) return (c + 55);
else return -1;
}
int char2dec(int c)
{
if (c >= '0' && c <= '9') return (c - 48);
else return -1;
}
int dec2char(int c)
{
if((c >= 0) && (c <= 9)) return (c + 48);
else return -1;
}
int index_number(char* s, char c)
{
int i = 0;
while(s[i] != c)
{
i = i + 1;
if(0 == s[i]) return -1;
}
return i;
}
int toupper(int c)
{
if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF);
return c;
}
int set_reader(char* set, int mult, char* input)
{
int n = 0;
int i = 0;
int hold;
int negative_p = FALSE;
if(input[0] == '-')
{
negative_p = TRUE;
i = i + 1;
}
while(in_set(input[i], set))
{
n = n * mult;
hold = index_number(set, toupper(input[i]));
/* Input managed to change between in_set and index_number */
if(-1 == hold) return 0;
n = n + hold;
i = i + 1;
}
/* loop exited before NULL and thus invalid input */
if(0 != input[i]) return 0;
if(negative_p)
{
n = 0 - n;
}
return n;
}
int numerate_string(char *a)
{
/* If NULL string */
if(0 == a[0]) return 0;
/* Deal with binary*/
else if ('0' == a[0] && 'b' == a[1])
{
return set_reader("01", 2, a+2);
}
/* Deal with hex */
else if ('0' == a[0] && 'x' == a[1])
{
return set_reader("0123456789ABCDEFabcdef", 16, a+2);
}
/* Deal with octal */
else if('0' == a[0])
{
return set_reader("01234567", 8, a+1);
}
/* Deal with decimal */
else
{
return set_reader("0123456789", 10, a);
}
}

20
dev-utils/m2-functions/require.c

@ -1,20 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
* SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdio.h>
#include<stdlib.h>
void file_print(char* s, FILE* f);
void require(int bool, char* error)
{
if(!bool)
{
file_print(error, stderr);
exit(EXIT_FAILURE);
}
}

59
dev-utils/m2-functions/string.c

@ -1,59 +0,0 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdlib.h>
#include<stdio.h>
#define MAX_STRING 4096
//CONSTANT MAX_STRING 4096
// void* calloc(int count, int size);
void file_print(char* s, FILE* f);
char* copy_string(char* target, char* source)
{
while(0 != source[0])
{
target[0] = source[0];
target = target + 1;
source = source + 1;
}
return target;
}
char* postpend_char(char* s, char a)
{
char* ret = calloc(MAX_STRING, sizeof(char));
if(NULL == ret) return NULL;
char* hold = copy_string(ret, s);
hold[0] = a;
return ret;
}
char* prepend_char(char a, char* s)
{
char* ret = calloc(MAX_STRING, sizeof(char));
if(NULL == ret) return NULL;
ret[0] = a;
copy_string((ret+1), s);
return ret;
}
char* prepend_string(char* add, char* base)
{
char* ret = calloc(MAX_STRING, sizeof(char));
if(NULL == ret) return NULL;
copy_string(copy_string(ret, add), base);
return ret;
}
int string_length(char* a)
{
int i = 0;
while(0 != a[i]) i = i + 1;
return i;
}

45
parts.rst

@ -6,8 +6,8 @@
.. SPDX-License-Identifier: CC-BY-SA-4.0
mescc-tools-seed
================
stage0-posix
============
This is where all the magic begins. We start with our hex0 and kaem
seeds and bootstrap our way up to M2-Planet, a subset of C, and mes-m2,
@ -28,9 +28,10 @@ taken here:
- M2-Planet (v1) compiles blood-elf (final)
- M2-Planet (v1) compiles get_machine
- M2-Planet (v1) compiles M2-Planet (final)
- M2-Planet (final) compiles mescc-tools-extra (see next step)
This seems very intimidating, but becomes clearer when reading the
source: https://github.com/oriansj/mescc-tools-seed/blob/master/x86/
source: https://github.com/oriansj/stage0-posix/blob/master/x86/
(start at mescc-tools-seed-kaem.kaem).
From here, we can move on from the lowest level stuff.
@ -38,19 +39,18 @@ From here, we can move on from the lowest level stuff.
mescc-tools-extra
=================
mescc-tools and mes-m2 are the projects bootstrapped by
mescc-tools-seed. However, we have some currently unmerged additions to
mescc-tools that we require for this project, namely filesystem
mescc-tools-extra contains some additional programs, namely filesystem
utilities ``cp`` and ``chown``. This allows us to have one unified
directory for our binaries. Futhermore, we also build ``fletcher16``, a
preliminary checksumming tool, that we use to ensure reproducibility and
authenticity of generated binaries.
directory for our binaries. Futhermore, we also build ``sha256sum``, a
checksumming tool, that we use to ensure reproducibility and authenticity
of generated binaries. We also build initial ``untar`` and ``ungz``
utilities to deal with compressed archives.
``/after``
==========
We now move into the ``/after`` directory. As mescc-tools-seed has no
concept of ``chdir()`` (not added until very late in mescc-tools-seed),
We now move into the ``/after`` directory. As stage0-posix has no
concept of ``chdir()`` (not added until very late in stage0-posix),
we have to copy a lot of files into the root of the initramfs, making it
very messy. We get into the move ordered directory ``/after`` here,
copying over all of the required binaries from ``/``.
@ -86,14 +86,6 @@ recompiled 5(!) times to add new features that are required for other
features, namely ``long long`` and ``float``. Each time, the libc is
also recompiled.
untar
=====
``tar`` is the most common archive format used by software source
code, often compressed also. To avoid continuing using submodules we
switch to software distribution using tar archives. ``untar.c`` is
a single file implementation of tar format and is part of libarchive 3.4.
Note that now we begin to delve into the realm of old GNU software,
using older versions compilable by tinycc. Prior to this point, all tools
have been adapted significantly for the bootstrap; now, we will be using
@ -125,21 +117,6 @@ patch 2.5.9
significantly more complex edits, including just changes to lines.
Luckily, we are able to patch ``patch`` using ``sed`` only.
sha-2
=====
``sha-2`` is a standalone external ``sha256sum`` implementation,
originally as a library, but patched to have a command line interface.
It is mostly output-compatible with ``sha256sum`` from coreutils. We use
this in replacement of ``fletcher16``.
Redo checksums using ``sha256sum``
==================================
We have now just built ``sha256sum``, which has a significantly (many orders
of magnitude) lower collision rate than ``fletcher16``, so we recheck all of
the existing binaries using ``sha256sum``.
make 3.80
=========

71
sysa.py

@ -146,36 +146,41 @@ class SysA:
self.after()
def stage0_posix(self):
"""Copy in all the stage0-posix (formerly known as mescc-tools-seed)"""
mescc_tools_seed_base_dir = os.path.join(self.sysa_dir, 'mescc-tools-seed',
'src', 'mescc-tools-seed')
mescc_tools_seed_dir = os.path.join(mescc_tools_seed_base_dir, self.arch)
copy_tree(mescc_tools_seed_dir, self.tmp_dir)
"""Copy in all of the stage0-posix"""
stage0_posix_base_dir = os.path.join(self.sysa_dir, 'stage0-posix', 'src')
stage0_posix_arch_dir = os.path.join(stage0_posix_base_dir, self.arch)
copy_tree(stage0_posix_arch_dir, self.tmp_dir)
m2_planet_dir = os.path.join(mescc_tools_seed_base_dir, 'M2-Planet')
m2_planet_dir = os.path.join(stage0_posix_base_dir, 'M2-Planet')
copytree(m2_planet_dir, self.tmp_dir)
# M2libc
m2libc_dir = os.path.join(stage0_posix_base_dir, 'M2libc')
copytree(m2libc_dir, self.tmp_dir)
# mescc-tools
mescc_tools_dir = os.path.join(stage0_posix_base_dir, 'mescc-tools')
copytree(mescc_tools_dir, self.tmp_dir)
# mescc-tools-extra
# Some additional tools such as cp, chmod, untar and ungz
mescc_tools_extra_dir = os.path.join(stage0_posix_base_dir, 'mescc-tools-extra')
copytree(mescc_tools_extra_dir, self.tmp_dir)
# At the moment not useful for bootstrap but easier to keep it
mes_m2_dir = os.path.join(mescc_tools_seed_base_dir, 'mes-m2')
mes_m2_dir = os.path.join(stage0_posix_base_dir, 'mes-m2')
copytree(mes_m2_dir, self.tmp_dir)
mescc_tools_patched_dir = os.path.join(self.sysa_dir, 'mescc-tools-seed',
'src', 'mescc-tools-patched')
shutil.copytree(mescc_tools_patched_dir,
os.path.join(self.tmp_dir, 'mescc-tools'), shutil.ignore_patterns('*.git*'))
# bootstrap seeds
bootstrap_seeds_dir = os.path.join(self.sysa_dir, 'bootstrap-seeds')
bootstrap_seeds_dir = os.path.join(self.sysa_dir, 'stage0-posix', 'src', 'bootstrap-seeds')
copytree(bootstrap_seeds_dir, self.tmp_dir)
kaem_optional_seed = os.path.join(bootstrap_seeds_dir, 'POSIX',
self.arch, 'kaem-optional-seed')
shutil.copy2(kaem_optional_seed, os.path.join(self.tmp_dir, 'init'))
# replace the init kaem with our own custom one
shutil.move(os.path.join(self.tmp_dir, 'kaem.run'),
os.path.join(self.tmp_dir, 'mescc-tools-seed.kaem.run'))
shutil.copy2(os.path.join(self.sysa_dir, 'base.kaem.run'),
os.path.join(self.tmp_dir, 'kaem.run'))
# stage0-posix hook to continue running live-bootstrap
shutil.copy2(os.path.join(self.sysa_dir, 'after.kaem'),
os.path.join(self.tmp_dir, 'after.kaem'))
# create directories needed
os.mkdir(os.path.join(self.tmp_dir, 'bin'))
@ -183,15 +188,14 @@ class SysA:
def after(self):
"""
Prepare sources in /after directory.
After mescc-tools-seed we get into our own directory because
the mescc-tools-seed one is hella messy.
After stage0-posix we get into our own directory because
the stage0-posix one is hella messy.
"""
self.create_after_dirs()
self.create_configuration_file()
self.mescc_tools_checksum()
self.stage0_posix_checksum()
self.deploy_extra_files()
self.mescc_tools_extra()
self.mes()
self.tcc_0_9_26()
self.get_packages()
@ -233,26 +237,19 @@ class SysA:
# Needed for patch to work, although can be fixed with TMPDIR
os.mkdir(os.path.join(self.tmp_dir, 'tmp'))
def mescc_tools_checksum(self):
"""Early fletcher16 checksum files"""
shutil.copy2(os.path.join(self.sysa_dir, 'mescc-tools-seed', 'checksums'),
os.path.join(self.after_dir, 'mescc-tools-seed-checksums'))
def stage0_posix_checksum(self):
"""Early checksum files"""
shutil.copy2(os.path.join(self.sysa_dir, 'stage0-posix', 'checksums'),
os.path.join(self.after_dir, 'stage0-posix-checksums'))
def deploy_extra_files(self):
"""Deploy misc files"""
extra_files = ['helpers.sh', 'run.sh', 'run2.sh', 'pre-sha.sha256sums']
extra_files = ['helpers.sh', 'run.sh', 'run2.sh']
for extra_file in extra_files:
shutil.copy2(os.path.join(self.sysa_dir, extra_file), self.after_dir)
shutil.copy2(os.path.join(self.sysa_dir, 'after.kaem'), self.tmp_dir)
shutil.copy2(os.path.join(self.sysa_dir, 'after.kaem.run'),
os.path.join(self.after_dir, 'kaem.run'))
shutil.copy2(os.path.join(self.git_dir, 'SHA256SUMS.sources'), self.after_dir)
def mescc_tools_extra(self):
"""Some additional tools such as cp and chmod (for M2-Planet)"""
copytree(os.path.join(self.sysa_dir, 'mescc-tools-extra'), self.after_dir)
def mes(self):
"""GNU Mes"""
copytree(os.path.join(self.sysa_dir, 'mes'), self.after_dir)
@ -267,8 +264,6 @@ class SysA:
# pylint: disable=line-too-long,too-many-statements
def get_packages(self):
"""Prepare remaining sources"""
# untar from libarchive 3.4
self.get_file("https://raw.githubusercontent.com/libarchive/libarchive/3.4/contrib/untar.c")
# gzip 1.2.4
self.get_file("https://mirrors.kernel.org/gnu/gzip/gzip-1.2.4.tar", mkbuild=True)
@ -282,10 +277,6 @@ class SysA:
# patch 2.5.9
self.get_file("https://ftp.gnu.org/pub/gnu/patch/patch-2.5.9.tar.gz", mkbuild=True)
# sha-2 61555d
self.get_file("https://github.com/amosnier/sha-2/archive/61555d.tar.gz", mkbuild=True,
output="sha-2-61555d.tar.gz")
# make 3.80
self.get_file("https://mirrors.kernel.org/gnu/make/make-3.80.tar.gz", mkbuild=True)

132
sysa/after.kaem

@ -1,12 +1,138 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2021 Paul Dersey <pdersey@gmail.com>
# SPDX-FileCopyrightText: 2020-2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: GPL-3.0-or-later
cd after
set -ex
PATH=/after/bin:/bin
# Finally, reinvoke kaem in this directory
../bin/kaem --file kaem.run
# Set commonly used variables
prefix=/after
bindir=${prefix}/bin
libdir=${prefix}/lib
incdir=${prefix}/include
MES_PREFIX=${prefix}/mes/src/mes
GUILE_LOAD_PATH=${prefix}/mes/src/nyacc/module:${prefix}/mes/src/mes/mes/module:${prefix}/mes/src/mes/module
cd ${prefix}
# Remove remaining dependencies on / (root of /after)
cp ../bin/blood-elf bin/blood-elf
cp ../bin/catm bin/catm
cp ../bin/chmod bin/chmod
cp ../bin/get_machine bin/get_machine
cp ../bin/hex2 bin/hex2
cp ../bin/kaem bin/kaem
cp ../bin/M1 bin/M1
cp ../bin/M2-Planet bin/M2-Planet
cp ../bin/mes-m2 bin/mes-m2
cp ../bin/sha256sum bin/sha256sum
cp ../bin/ungz bin/ungz
cp ../bin/untar bin/untar
/bin/cp /bin/cp /after/bin/cp
/bin/chmod 755 bin/blood-elf
/bin/chmod 755 bin/catm
/bin/chmod 755 bin/chmod
/bin/chmod 755 bin/cp
/bin/chmod 755 bin/get_machine
/bin/chmod 755 bin/hex2
/bin/chmod 755 bin/kaem
/bin/chmod 755 bin/M1
/bin/chmod 755 bin/M2-Planet
/bin/chmod 755 bin/mes-m2
/bin/chmod 755 bin/sha256sum
/bin/chmod 755 bin/ungz
/bin/chmod 755 bin/untar
sha256sum -c stage0-posix-checksums
PATH=/after/bin
# mes
pkg="mes"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# tcc 0.9.26
pkg="tcc-0.9.26"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# gzip
pkg="gzip-1.2.4"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# tar
pkg="tar-1.12"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# sed
pkg="sed-4.0.9"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# patch
pkg="patch-2.5.9"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# sha-2
#pkg="sha-2-61555d"
#cd ${pkg}
#kaem --file ${pkg}.kaem
#cd ..
# Check all up to this part as sha256sum
#sha256sum -c pre-sha.sha256sums
# make
pkg="make-3.80"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# bzip2
pkg="bzip2-1.0.8"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# tcc 0.9.27
pkg="tcc-0.9.27"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# coreutils
pkg="coreutils-5.0"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# heirloom-devtools
pkg="heirloom-devtools-070527"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# bash
pkg="bash-2.05b"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
bash run.sh

128
sysa/after.kaem.run

@ -1,128 +0,0 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2021 Paul Dersey <pdersey@gmail.com>
# SPDX-FileCopyrightText: 2020-2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: GPL-3.0-or-later
set -ex
PATH=/after/bin:/bin
# Set commonly used variables
prefix=/after
bindir=${prefix}/bin
libdir=${prefix}/lib
incdir=${prefix}/include
MES_PREFIX=${prefix}/mes/src/mes
GUILE_LOAD_PATH=${prefix}/mes/src/nyacc/module:${prefix}/mes/src/mes/mes/module:${prefix}/mes/src/mes/module
# cp and chown (mescc-tools-extra)
pkg="mescc-tools-extra"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# Remove remaining dependencies on / (root of /after)
cp ../bin/hex2 bin/hex2
cp ../bin/M1 bin/M1
cp ../bin/M2-Planet bin/M2-Planet
cp ../bin/blood-elf bin/blood-elf
cp ../bin/get_machine bin/get_machine
cp ../bin/mes-m2 bin/mes-m2
cp ../bin/kaem bin/kaem
cp ../catm bin/catm
chmod 755 bin/hex2 bin/M1 bin/M2-Planet bin/blood-elf \
bin/get_machine bin/mes-m2 bin/kaem bin/catm
fletcher16 mescc-tools-seed-checksums
PATH=/after/bin
# mes
pkg="mes"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# tcc 0.9.26
pkg="tcc-0.9.26"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# untar
pkg="untar"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# gzip
pkg="gzip-1.2.4"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# tar
pkg="tar-1.12"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# sed
pkg="sed-4.0.9"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# patch
pkg="patch-2.5.9"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# sha-2
pkg="sha-2-61555d"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# Check all up to this part as sha256sum
sha256sum -c pre-sha.sha256sums
# make
pkg="make-3.80"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# bzip2
pkg="bzip2-1.0.8"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# tcc 0.9.27
pkg="tcc-0.9.27"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# coreutils
pkg="coreutils-5.0"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# heirloom-devtools
pkg="heirloom-devtools-070527"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
# bash
pkg="bash-2.05b"
cd ${pkg}
kaem --file ${pkg}.kaem
cd ..
bash run.sh

13
sysa/base.kaem.run

@ -1,13 +0,0 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2020-2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Part 1: mescc-tools-seed
./bootstrap-seeds/POSIX/x86/kaem-optional-seed mescc-tools-seed.kaem.run
# Part 2-infinity: get the hell out of this mess of a directory
bin/kaem --verbose --strict --file after.kaem

1
sysa/bootstrap-seeds

@ -1 +0,0 @@
Subproject commit cfa108377c15382f58b20394150840e1099b6767

4
sysa/gzip-1.2.4/checksums

@ -1,3 +1 @@
20706 /after/bin/gzip
20706 /after/bin/gunzip
20706 /after/bin/zcat
c8c923c0bea92a642e1b3e5dde162bb7c35ea14a75e0f8fbb811d7ea92edffe6 /after/bin/gzip

2
sysa/gzip-1.2.4/gzip-1.2.4.kaem

@ -43,4 +43,4 @@ chmod 755 ${bindir}/zcat
cd ../..
# Checksums
fletcher16 checksums
sha256sum -c checksums

26
sysa/mes/checksums

@ -1,13 +1,13 @@
17767 /after/bin/mes
23643 /after/bin/mescc.scm
36724 /after/lib/x86-mes/crt1.s
42390 /after/lib/x86-mes/crt1.o
39638 /after/lib/x86-mes/x86.M1
526 /after/lib/x86-mes/libmescc.s
10591 /after/lib/x86-mes/libc+tcc.s
45027 /after/lib/x86-mes/libc.s
17259 /after/lib/x86-mes/libmescc.a
20204 /after/lib/x86-mes/libc+tcc.a
34645 /after/lib/x86-mes/libc.a
8245 /after/lib/linux/x86-mes/elf32-header.hex2
15474 /after/lib/linux/x86-mes/elf32-footer-single-main.hex2
d2e38ce7eab153cb2a019de92d12f1afc81f3ca0d4e068597fdab472250c7408 /after/bin/mes-m2
b91c81e543b878ead4cc975df75e3d211f7bd2dcdd4c90e42df99d44311ce71e /after/bin/mescc.scm
90811dafd33ad56b8e4b0adcc04263f9329b9047b7cc337abe8151a75017172c /after/lib/x86-mes/crt1.s
e065af5f73d40b20029e9ba23143660f99c6abacb82d1b4c8f2067a4e395d553 /after/lib/x86-mes/crt1.o
c9944a799d584abfa76f385c14ac0caf6f46d03b34bf2712493602b12826c6b2 /after/lib/x86-mes/x86.M1
d8646707db6aa2a76fdc5dbb3521376439e357f9f1de1d67f02a1afeefd342ac /after/lib/x86-mes/libmescc.s
aaf89a9d6818cdb8ece73454631b1a1ae83503e5eb7777d38cdaf141cba0e530 /after/lib/x86-mes/libc+tcc.s
b5ce4e1288a27864156d74268090c13aea6b5a261fa81c75bfbe844d0689d03d /after/lib/x86-mes/libc.s
52f697278ccdff5e457f27e10f465a91ab9858f0c6cee0683831cadb3109bbb7 /after/lib/x86-mes/libmescc.a
3156e619dbd85c471e2a8d053ba536eaaa8f91da657003777b8e87e7bab4266d /after/lib/x86-mes/libc+tcc.a
400615ca47339a3149680748b319f089ca5cb7ff2fdb42cce252f8c408445c1b /after/lib/x86-mes/libc.a
b16ab368bc4c7b8bd896d03cba565a60e97760dea4da9f5c8a1a3d2902a79df6 /after/lib/linux/x86-mes/elf32-header.hex2
f9873d9aab12e70f24d97f8319e17a1e698ca60779ae9a6ab3ede648cd60fc61 /after/lib/linux/x86-mes/elf32-footer-single-main.hex2

9
sysa/mes/mes.kaem

@ -11,7 +11,7 @@ set -ex
MES_ARENA=20000000
MES_MAX_ARENA=20000000
MES_STACK=6000000
MES=${bindir}/mes
MES=${bindir}/mes-m2
libdir=${prefix}/mes/src/mes/lib
cp files/mescc.scm ${bindir}/
@ -23,7 +23,6 @@ cd src/mes
# Bootstrap Mes using M2-Planet
kaem --verbose --file kaem.run
cp bin/mes-m2 ${MES}
chmod 755 ${MES}
# Build Mes C library using mes-m2 bootstrapped Mes
@ -265,11 +264,7 @@ cp include/sys/ucontext.h ${incdir}/sys/ucontext.h
cp include/sys/user.h ${incdir}/sys/user.h
cp include/sys/wait.h ${incdir}/sys/wait.h
# Test mes
libdir=${prefix}/lib
${MES} -c "(display 'Hello,Mes!) (newline)"
cd ../..
# Checksums
fletcher16 checksums
sha256sum -c checksums

3
sysa/mescc-tools-extra/checksums

@ -1,3 +0,0 @@
27475 /after/bin/chmod
51523 /after/bin/cp
37928 /after/bin/fletcher16

130
sysa/mescc-tools-extra/mescc-tools-extra.kaem

@ -1,130 +0,0 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2020-2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: GPL-3.0-or-later
set -ex
cd src
# cp command
M2-Planet \
-f /M2-Planet/test/common_x86/functions/file.c \
-f /M2-Planet/test/common_x86/functions/exit.c \
-f /M2-Planet/test/common_x86/functions/malloc.c \
-f /M2-Planet/test/common_x86/functions/getcwd.c \
-f /M2-Planet/test/common_x86/functions/chdir.c \
-f functions/string.c \
-f functions/file_print.c \
-f functions/match.c \
-f functions/require.c \
-f /M2-Planet/functions/calloc.c \
-f functions/in_set.c \
-f functions/numerate_number.c \
-f cp.c \
--architecture x86 \
--debug \
-o cp.M1
blood-elf -f cp.M1 -o cp-footer.M1
M1 \
-f /M2-Planet/test/common_x86/x86_defs.M1 \
-f /M2-Planet/test/common_x86/libc-core.M1 \
-f cp.M1 \
-f cp-footer.M1 \
--LittleEndian \
--architecture x86 \
-o hold
hex2 \
-f /M2-Planet/test/common_x86/ELF-i386-debug.hex2 \
-f hold \
--LittleEndian \
--architecture x86 \
--BaseAddress 0x8048000 \
-o ${bindir}/cp \
--exec_enable
# chmod command
M2-Planet \
-f /M2-Planet/test/common_x86/functions/file.c \
-f /M2-Planet/test/common_x86/functions/exit.c \
-f /M2-Planet/test/common_x86/functions/malloc.c \
-f /M2-Planet/test/common_x86/functions/getcwd.c \
-f /M2-Planet/test/common_x86/functions/chdir.c \
-f functions/string.c \
-f functions/file_print.c \
-f functions/match.c \
-f functions/require.c \
-f functions/in_set.c \
-f functions/numerate_number.c \
-f /M2-Planet/functions/calloc.c \
-f /M2-Planet/test/common_x86/functions/stat.c \
-f chmod.c \
--architecture x86 \
--debug \
-o chmod.M1
blood-elf -f chmod.M1 -o chmod-footer.M1
M1 \
-f /M2-Planet/test/common_x86/x86_defs.M1 \
-f /M2-Planet/test/common_x86/libc-core.M1 \
-f chmod.M1 \
-f chmod-footer.M1 \
--LittleEndian \
--architecture x86 \
-o hold
hex2 \
-f /M2-Planet/test/common_x86/ELF-i386-debug.hex2 \
-f hold \
--LittleEndian \
--architecture x86 \
--BaseAddress 0x8048000 \
-o ${bindir}/chmod \
--exec_enable
# fletcher16 command
M2-Planet \
-f /M2-Planet/test/common_x86/functions/file.c \
-f /M2-Planet/test/common_x86/functions/exit.c \
-f functions/numerate_number.c \
-f functions/string.c \
-f functions/file_print.c \
-f functions/match.c \
-f functions/require.c \
-f functions/in_set.c \
-f /M2-Planet/functions/calloc.c \
-f /M2-Planet/test/common_x86/functions/malloc.c \
-f fletcher16.c \
--architecture x86 \
--debug \
-o fletcher16.M1
blood-elf -f fletcher16.M1 -o fletcher16-footer.M1
M1 \
-f /M2-Planet/test/common_x86/x86_defs.M1 \
-f /M2-Planet/test/common_x86/libc-core.M1 \
-f fletcher16.M1 \
-f fletcher16-footer.M1 \
--LittleEndian \
--architecture x86 \
-o hold
hex2 \
-f /M2-Planet/test/common_x86/ELF-i386-debug.hex2 \
-f hold \
--LittleEndian \
--architecture x86 \
--BaseAddress 0x8048000 \
-o ${bindir}/fletcher16 \
--exec_enable
cd ..
# Checksums
fletcher16 checksums

161
sysa/mescc-tools-extra/src/chmod.c

@ -1,161 +0,0 @@
/*
* SPDX-FileCopyrightText: 2020 fosslinux <fosslinux@aussies.space>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
/* Define all of the constants */
// CONSTANT FALSE 0
#define FALSE 0
// CONSTANT TRUE 1
#define TRUE 1
// CONSTANT MAX_STRING 4096
#define MAX_STRING 4096
// CONSTANT MAX_ARRAY 256
#define MAX_ARRAY 256
/* There are all of the constants we need for the bitmask */
/* constant name */ /* decimal */ /* octal */
// CONSTANT S_ISUID 2048 /* 04000 */
// CONSTANT S_ISGID 1024 /* 02000 */
// CONSTANT S_ISVTX 512 /* 01000 */
// CONSTANT S_IRUSR 256 /* 00400 */
// CONSTANT S_IWUSR 128 /* 00200 */
// CONSTANT S_IXUSR 64 /* 00100 */
// CONSTANT S_IRGRP 32 /* 00040 */
// CONSTANT S_IWGRP 16 /* 00020 */
// CONSTANT S_IXGRP 8 /* 00010 */
// CONSTANT S_IROTH 4 /* 00004 */
// CONSTANT S_IWOTH 2 /* 00002 */
// CONSTANT S_IXOTH 1 /* 00001 */
/* Prototypes for external funcs */
void file_print(char* s, FILE* f);
char* prepend_char(char a, char* s);
int numerate_string(char *a);
void require(int bool, char* error);
char* copy_string(char* target, char* source);
int match(char* a, char* b);
/* Globals */
int verbose;
/* UTILITY FUNCTIONS */
/* Function to find the length of a char**; an array of strings */
int array_length(char** array)
{
int length = 0;
while(array[length] != NULL)
{
length = length + 1;
}
return length;
}
/* PROCESSING FUNCTIONS */
int main(int argc, char** argv)
{
/* Initialize variables */
char** files = calloc(MAX_ARRAY, sizeof(char*));
require(files != NULL, "Memory initialization of files failed\n");
int files_index = 0;
char* mode = NULL;
/* Set defaults */
verbose = FALSE;
int i = 1;
/* Loop arguments */
while(i <= argc)
{
if(NULL == argv[i])
{ /* Ignore and continue */
i = i + 1;
}
else if(match(argv[i], "-h") || match(argv[i], "--help"))
{
file_print("Usage: ", stdout);
file_print(argv[0], stdout);
file_print(" [-h | --help] [-V | --version] [-v | --verbose]\n", stdout);
exit(EXIT_SUCCESS);
}
else if(match(argv[i], "-V") || match(argv[i], "--version"))
{ /* Output version */
file_print("chmod version 1.1.0\n", stdout);
exit(EXIT_SUCCESS);
}
else if(match(argv[i], "-v") || match(argv[i], "--verbose"))
{
verbose = TRUE;
i = i + 1;
}
else if(argv[i][0] != '-')
{ /* It must be the file or the mode */
if(mode == NULL)
{ /* Mode always comes first */
mode = calloc(MAX_STRING, sizeof(char));
require(mode != NULL, "Memory initialization of mode failed\n");
copy_string(mode, argv[i]);
}
else
{ /* It's a file, as the mode is already done */
files[files_index] = calloc(MAX_STRING, sizeof(char));
require(files[files_index] != NULL, "Memory initialization of files[files_index] failed\n");
copy_string(files[files_index], argv[i]);
files_index = files_index + 1;
}
i = i + 1;
}
else
{ /* Unknown argument */
file_print("UNKNOWN_ARGUMENT\n", stderr);
exit(EXIT_FAILURE);
}
}
/* Ensure the two values have values */
require(mode != NULL, "Provide a mode\n");
require(files[0] != NULL, "Provide a file\n");
/* Convert the mode str into octal */
if(mode[0] != '0')
{ /* We need to indicate it is octal */
mode = prepend_char('0', mode);
}
int omode = numerate_string(mode);
/* Loop over files to be operated on */
FILE* test;
for(i = 0; i < array_length(files); i = i + 1)
{
/* Make sure the file can be opened */
test = fopen(files[i], "r");
require(test != NULL, "A file cannot be read\n");
fclose(test);
/* Verbose message */
if(verbose)
{
file_print("mode of '", stdout);
file_print(files[i], stdout);
file_print("' changed to ", stdout);
file_print(mode, stdout);
file_print("\n", stdout);
}
/* Perform the chmod */
chmod(files[i], omode);
free(files[i]);
}
free(mode);
free(files);
}

323
sysa/mescc-tools-extra/src/cp.c

@ -1,323 +0,0 @@
/*
* SPDX-FileCopyrightText: 2020 fosslinux <fosslinux@aussies.space>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
/* Define all of the constants */
// CONSTANT FALSE 0
#define FALSE 0
// CONSTANT TRUE 1
#define TRUE 1
// CONSTANT MAX_STRING 4096
#define MAX_STRING 4096
// CONSTANT MAX_ARRAY 256
#define MAX_ARRAY 256
/* Prototypes for external funcs */
int match(char* a, char* b);
void file_print(char* s, FILE* f);
char* copy_string(char* target, char* source);
char* prepend_string(char* add, char* base);
void require(int bool, char* error);
char* postpend_char(char* s, char a);
int string_length(char* a);
void require(int bool, char* error);
/* Globals */
int verbose;
/* UTILITY FUNCTIONS */
/* Function to find a character's position in a string (last match) */
int find_last_char_pos(char* string, char a)
{
int i = string_length(string) - 1;
if(i < 0) return i;
while(i >= 0)
{
/*
* This conditional should be in the while conditional but we are
* running into the M2-Planet short-circuit bug.
*/
if(a == string[i]) break;
i = i - 1;
}
return i;
}
/* Function to find the length of a char**; an array of strings */
int array_length(char** array)
{
int length = 0;
while(array[length] != NULL)
{
length = length + 1;
}
return length;
}
/* PROCESSING FUNCTIONS */
char* directory_dest(char* dest, char* source, int require_directory)
{
/*
* First, check if it is a directory to copy to.
* We have two ways of knowing this:
* - If the destination ends in a slash, the user has explicitly said
* it is a directory.
* - Normally we would use stat() but we don't want to force support for
* that syscall onto the kernel, so we just attempt to chdir() into it
* and if it works then it must be a directory. A bit hacky, bit it
* works.
*/
int isdirectory = FALSE;
if(dest[string_length(dest) - 1] == '/')
{
isdirectory = TRUE;
}
if(!isdirectory)
{ /* Use the other testing method */
/*
* Get the current path so that we can chdir back to it if it does
* chdir successfully.
*/
char* current_path = calloc(MAX_STRING, sizeof(char));
require(current_path != NULL, "Memory initialization of current_path in directory_dest failed\n");
getcwd(current_path, MAX_STRING);
require(!match("", current_path), "getcwd() failed\n");
/*
* chdir expects an absolute path.
* If the first character is / then it is already absolute, otherwise
* it is relative and needs to be changed (by appending current_path
* to the dest path).
*/
char* chdir_dest = calloc(MAX_STRING, sizeof(char));
require(chdir_dest != NULL, "Memory initialization of chdir_dest in directory_dest failed\n");
if(dest[0] != '/')
{ /* The path is relative, append current_path */
copy_string(chdir_dest, prepend_string(prepend_string(current_path, "/"), dest));
}
else
{ /* The path is absolute */
copy_string(chdir_dest, dest);
}
if(0 <= chdir(chdir_dest))
{ /* chdir returned successfully */
/*
* But because of M2-Planet, that doesn't mean anything actually
* happened, check that before we go any further.
*/
char* new_path = calloc(MAX_STRING, sizeof(char));
require(new_path != NULL, "Memory initialization of new_path in directory_dest failed\n");
getcwd(new_path, MAX_STRING);
if(!match(current_path, new_path))
{
isdirectory = TRUE;
chdir(current_path);
}
}
free(chdir_dest);
free(current_path);
}
/*
* If it isn't a directory, and we require one, error out.
* Otherwise, just return what we were given, we're done here.
*/
if(require_directory) require(isdirectory, "Provide a directory destination for multiple source files\n");
if(!isdirectory) return dest;
/* If it is, we need to make dest a full path */
/* 1. Get the basename of source */
char* basename = calloc(MAX_STRING, sizeof(char));
require(basename != NULL, "Memory initialization of basename in directory_dest failed\n");
int last_slash_pos = find_last_char_pos(source, '/');
if(last_slash_pos >= 0)
{ /* Yes, there is a slash in it, copy over everything after that pos */
int spos; /* source pos */
int bpos = 0; /* basename pos */
int source_length = string_length(source);
/* Do the actual copy */
for(spos = last_slash_pos + 1; spos < string_length(source); spos = spos + 1)
{
basename[bpos] = source[spos];
bpos = bpos + 1;
}
}
else
{ /* No, there is no slash in it, hence the basename is just the source */
copy_string(basename, source);
}
/* 2. Ensure our dest (which is a directory) has a trailing slash */
if(dest[string_length(dest) - 1] != '/')
{
dest = postpend_char(dest, '/');
}
/* 3. Add the basename to the end of the directory */
dest = prepend_string(dest, basename);
free(basename);
/* Now we have a returnable path! */
return dest;
}
int copy_file(char* source, char* dest)
{
if(verbose)
{ /* Output message */
/* Of the form 'source' -> 'dest' */
file_print("'", stdout);
file_print(source, stdout);
file_print("' -> '", stdout);
file_print(dest, stdout);
file_print("'\n", stdout);
}
/* Open source and dest as FILE*s */
FILE* fsource = fopen(source, "r");
require(fsource != NULL, prepend_string(
prepend_string("Error opening source file ", source), "\n"));
FILE* fdest = fopen(dest, "w");
require(fdest >= 0, prepend_string(
prepend_string("Error opening destination file", dest), "\n"));
/*
* The following loop reads a character from the source and writes it to the
* dest file. This is all M2-Planet supports.
*/
char c = fgetc(fsource);
while(c != EOF)
{
fputc(c, fdest);
c = fgetc(fsource);
}
/* Cleanup */
fclose(fsource);
fclose(fdest);
}
int main(int argc, char** argv)
{
/* Initialize variables */
char** sources = calloc(MAX_ARRAY, sizeof(char*));
require(sources != NULL, "Memory initialization of sources failed\n");
int sources_index = 0;
char* dest = NULL;
/* Set defaults */
verbose = FALSE;
int i = 1;
int j;
int args_found;
/* Loop arguments */
while(i <= argc)
{
if(NULL == argv[i])
{ /* Ignore and continue */
i = i + 1;
}
else if(match(argv[i], "-h") || match(argv[i], "--help"))
{
file_print("Usage: ", stdout);
file_print(argv[0], stdout);
file_print(" [-h | --help] [-V | --version] [-v | --verbose] source1 source2 sourcen destination\n", stdout);
exit(EXIT_SUCCESS);
}
else if(match(argv[i], "-V") || match(argv[i], "--version"))
{ /* Output version */
file_print("cp version 1.1.0\n", stdout);
exit(EXIT_SUCCESS);
}
else if(match(argv[i], "-v") || match(argv[i], "--verbose"))
{
verbose = TRUE;
i = i + 1;
}
else if(argv[i][0] != '-')
{ /* It is not an option */
/*
* We can tell if this is the source file or the destination file
* through looking *ahead*. If it is the last of this type of argument then
* it must be the destination. (1 destination, many sources).
*/
j = i + 1;
args_found = 0;
while(j < array_length(argv))
{
if(argv[j][0] != '-')
{ /* It's one of these type of arguments */
args_found = args_found + 1;
}
j = j + 1;
}
if(args_found == 0)