├── LICENSE
├── Makefile
├── README.md
├── README
├── EN
│ └── README
├── FR
│ └── README
└── TR
│ └── README
├── bin
├── BASE32.EXE
├── BASE64.EXE
├── ECHO.EXE
├── FALSE.COM
├── RMDIR.EXE
└── TRUE.COM
├── config.h
├── gl
├── fadvise.c
├── fadvise.h
├── xdec2int.c
├── xdec2int.h
└── xdec2umx.c
├── lib
├── assert.h
├── assure.h
├── base32.c
├── base32.h
├── base64.c
├── base64.h
├── basenaml.c
├── bin-io.c
├── bin-io.h
├── const.h
├── die.h
├── dirname.h
├── dirnamel.c
├── dosname.h
├── errno.h
├── error.c
├── error.h
├── exitfail.c
├── exitfail.h
├── fcntl.c
├── fcntl.h
├── getopt.c
├── getopt.h
├── getopt_i.h
├── getprogn.c
├── getprogn.h
├── ignorval.h
├── intprops.h
├── kitten
│ ├── kitten.c
│ └── kitten.h
├── lang
│ ├── lang.c
│ └── lang.h
├── long-opt.c
├── long-opt.h
├── quote.h
├── rmdir.c
├── stpslash.c
├── verify.h
├── xbin-io.c
├── xbin-io.h
├── xdec2umx.c
├── xstr2umx.c
├── xstrtol.c
└── xstrtol.h
├── nls
├── base32.en
├── base64.en
├── false.en
├── lib
│ └── xbin-io.en
├── system-h.en
└── true.en
└── src
├── base32.c
├── base64.c
├── echo.c
├── false.c
├── pfprintf.c
├── pfprintf.h
├── rmdir.c
├── system.h
└── true.c
/Makefile:
--------------------------------------------------------------------------------
1 | # Makefile - DOS Coreutils Makefile
2 | # Written by Ercan Ersoy and LiquidFox1776.
3 | # TODO Implement autodepends
4 |
5 | CC = *wcc
6 | CFLAGS = -oneatx -ohirbk -ol -ol+ -oi -ei -zp4 -0 -s -ri -ms /bt=dos
7 | LD = *wlink
8 | LDFLAGS = option eliminate option vfremoval
9 |
10 | all: src\base32.exe src\base64.exe src\echo.exe src\false.com src\rmdir.exe src\true.com
11 |
12 | src\base64.exe: src\base64.obj lib\base64.obj lib\basenaml.obj lib\bin-io.obj lib\dirnamel.obj lib\error.obj lib\exitfail.obj lib\fcntl.obj lib\getopt.obj lib\getprogn.obj lib\xbin-io.obj lib\xdec2umx.obj lib\xstrtol.obj lib\xstr2umx.obj gl\fadvise.obj gl\xdec2int.obj lib\kitten\kitten.obj lib\lang\lang.obj
13 | $(LD) $(LDFLAGS) option stack=8192 file {$?} #the stack size must be increased from the default size or a stack overflow will occur
14 |
15 | src\base32.exe: src\base32.obj lib\base32.obj lib\basenaml.obj lib\bin-io.obj lib\dirnamel.obj lib\error.obj lib\exitfail.obj lib\fcntl.obj lib\getopt.obj lib\getprogn.obj lib\xbin-io.obj lib\xdec2umx.obj lib\xstrtol.obj lib\xstr2umx.obj gl\fadvise.obj gl\xdec2int.obj lib\kitten\kitten.obj lib\lang\lang.obj
16 | $(LD) $(LDFLAGS) option stack=8192 file {$?} #the stack size must be increased from the default size or a stack overflow will occur
17 |
18 | src\echo.exe: src\echo.obj lib\basenaml.obj lib\getprogn.obj lib\kitten\kitten.obj lib\lang\lang.obj
19 | $(LD) $(LDFLAGS) file {$?}
20 |
21 | src\false.com: src\false.obj lib\basenaml.obj lib\dirnamel.obj lib\error.obj lib\exitfail.obj lib\fcntl.obj lib\getopt.obj lib\getprogn.obj lib\kitten\kitten.obj lib\lang\lang.obj
22 | $(LD) $(LDFLAGS) system com file {$?}
23 |
24 | src\rmdir.exe: src\rmdir.obj lib\basenaml.obj lib\dirnamel.obj lib\error.obj lib\exitfail.obj lib\fcntl.obj lib\getopt.obj lib\getprogn.obj lib\stpslash.obj gl\fadvise.obj lib\kitten\kitten.obj lib\lang\lang.obj src\pfprintf.obj
25 | $(LD) $(LDFLAGS) file {$?}
26 |
27 | src\true.com: src\true.obj lib\basenaml.obj lib\dirnamel.obj lib\error.obj lib\exitfail.obj lib\fcntl.obj lib\getopt.obj lib\getprogn.obj lib\kitten\kitten.obj lib\lang\lang.obj
28 | $(LD) $(LDFLAGS) system com file {$?}
29 |
30 | #commands\dirname.com: commands\dirname.obj lib\getopt.obj lib\dirnamel.obj lib\basenaml.obj
31 | # $(LD) $(LDFLAGS) system com file {$?}
32 |
33 | gl\xdec2int.obj: gl\xdec2int.c
34 | $(CC) $(CFLAGS) $? -fo=$@
35 |
36 | gl\fadvise.obj: gl\fadvise.c
37 | $(CC) $(CFLAGS) $? -fo=$@
38 |
39 | gl\xdec2umx.obj: gl\xdec2umx.c
40 | $(CC) $(CFLAGS) $? -fo=$@
41 |
42 | lib\basenaml.obj: lib\basenaml.c
43 | $(CC) $(CFLAGS) $? -fo=$@
44 |
45 | lib\dirnamel.obj: lib\dirnamel.c
46 | $(CC) $(CFLAGS) $? -fo=$@
47 |
48 | lib\getopt.obj: lib\getopt.c
49 | $(CC) $(CFLAGS) $? -fo=$@
50 |
51 | lib\fcntl.obj: lib\fcntl.c
52 | $(CC) $(CFLAGS) $? -fo=$@
53 |
54 | lib\xstrtol.obj: lib\xstrtol.c
55 | $(CC) $(CFLAGS) $? -fo=$@
56 |
57 | lib\base32.obj: lib\base32.c
58 | $(CC) $(CFLAGS) $? -fo=$@
59 |
60 | lib\error.obj: lib\error.c
61 | $(CC) $(CFLAGS) $? -fo=$@
62 |
63 | lib\bin-io.obj: lib\bin-io.c
64 | $(CC) $(CFLAGS) $? -fo=$@
65 |
66 | lib\getprogn.obj: lib\getprogn.c
67 | $(CC) $(CFLAGS) $? -fo=$@
68 |
69 | lib\exitfail.obj: lib\exitfail.c
70 | $(CC) $(CFLAGS) $? -fo=$@
71 |
72 | lib\stpslash.obj: lib\stpslash.c
73 | $(CC) $(CFLAGS) $? -fo=$@
74 |
75 | lib\xbin-io.obj: lib\xbin-io.c
76 | $(CC) $(CFLAGS) $? -fo=$@
77 |
78 | lib\base64.obj: lib\base64.c
79 | $(CC) $(CFLAGS) $? -fo=$@
80 |
81 | lib\xstr2umx.obj: lib\xstr2umx.c
82 | $(CC) $(CFLAGS) $? -fo=$@
83 |
84 | lib\xdec2umx.obj: lib\xdec2umx.c
85 | $(CC) $(CFLAGS) $? -fo=$@
86 |
87 | lib\kitten\kitten.obj: lib\kitten\kitten.c
88 | $(CC) $(CFLAGS) $? -fo=$@
89 |
90 | lib\lang\lang.obj: lib\lang\lang.c
91 | $(CC) $(CFLAGS) $? -fo=$@
92 |
93 | src\base32.obj: src\base32.c
94 | $(CC) $(CFLAGS) $? -fo=$@
95 |
96 | src\base64.obj: src\base64.c
97 | $(CC) $(CFLAGS) $? -fo=$@
98 |
99 | src\echo.obj: src\echo.c
100 | $(CC) $(CFLAGS) $? -fo=$@
101 |
102 | src\false.obj: src\false.c
103 | $(CC) $(CFLAGS) $? -fo=$@
104 |
105 | src\pfprintf.obj: src\pfprintf.c
106 | $(CC) $(CFLAGS) $? -fo=$@
107 |
108 | src\rmdir.obj: src\rmdir.c
109 | $(CC) $(CFLAGS) $? -fo=$@
110 |
111 | src\true.obj: src\true.c
112 | $(CC) $(CFLAGS) $? -fo=$@
113 |
114 | #commands\dirname.obj: commands\dirname.c
115 | # $(CC) $(CFLAGS) $? -fo=$@
116 |
117 | install: .SYMBOLIC
118 | #@if exist install\install.bat install\install.bat #execute the installer
119 |
120 | clean-obj: .SYMBOLIC
121 | @if exist src\*.obj del src\*.obj
122 | @if exist lib\*.obj del lib\*.obj
123 | @if exist gl\*.obj del gl\*.obj
124 | @if exist lib\kitten\*.obj del lib\kitten\*.obj
125 | @if exist lib\lang\*.obj del lib\lang\*.obj
126 |
127 | clean-all: .SYMBOLIC
128 | @if exist src\*.obj del src\*.obj
129 | @if exist lib\*.obj del lib\*.obj
130 | @if exist gl\*.obj del gl\*.obj
131 | @if exist lib\kitten\*.obj del lib\kitten\*.obj
132 | @if exist lib\lang\*.obj del lib\lang\*.obj
133 | @if exist src\*.exe del src\*.exe
134 | @if exist src\*.com del src\*.com
135 |
136 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FreeDOS Coreutils
2 |
3 | 16 bit DOS port of GNU Coreutils for FreeDOS
4 |
5 | Copyright (C) 2018 Ercan Ersoy
6 | This software is licensed under GNU General Public License version 3.
7 |
8 | Thanks to Atnode for his contributions.
9 |
10 | ## Contribute
11 |
12 | If you want to contribute to this project, you can make a pull request to the FreeDOS Coreutils repository.
--------------------------------------------------------------------------------
/README/EN/README:
--------------------------------------------------------------------------------
1 | FreeDOS Coreutils
2 |
3 | 16 bit DOS port of GNU Coreutils for FreeDOS
4 |
5 | Copyright (C) 2018 Ercan Ersoy
6 | This software is licensed under GNU General Public License version 3.
7 |
8 | Thanks to Atnode for his contributions.
9 |
10 | Contribute:
11 |
12 | If you want to contribute to this project, you can make a pull request
13 | to the FreeDOS Coreutils repository.
14 |
--------------------------------------------------------------------------------
/README/FR/README:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/README/FR/README
--------------------------------------------------------------------------------
/README/TR/README:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/README/TR/README
--------------------------------------------------------------------------------
/bin/BASE32.EXE:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/bin/BASE32.EXE
--------------------------------------------------------------------------------
/bin/BASE64.EXE:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/bin/BASE64.EXE
--------------------------------------------------------------------------------
/bin/ECHO.EXE:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/bin/ECHO.EXE
--------------------------------------------------------------------------------
/bin/FALSE.COM:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/bin/FALSE.COM
--------------------------------------------------------------------------------
/bin/RMDIR.EXE:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/bin/RMDIR.EXE
--------------------------------------------------------------------------------
/bin/TRUE.COM:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cobalt-OS/DOSUTIL/9f8f1a93d9b69702dac58423a74793d31be5c8ec/bin/TRUE.COM
--------------------------------------------------------------------------------
/config.h:
--------------------------------------------------------------------------------
1 | #ifndef __CONFIG_H__
2 | #define __CONFIG_H__
3 |
4 | #define MAX_LANG_CODE_LENGTH 3
5 | #define MAX_PATH 260
6 |
7 | #endif /* __CONFIG_H__ */
8 |
9 |
--------------------------------------------------------------------------------
/gl/fadvise.c:
--------------------------------------------------------------------------------
1 | /* Declare an access pattern hint for files.
2 | Copyright (C) 2010-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see . */
16 |
17 | /* Without this pragma, gcc suggests that (given !HAVE_POSIX_FADVISE)
18 | the fdadvise function might be a candidate for attribute 'const'. */
19 |
20 | #include "fadvise.h"
21 |
22 | #include
23 | #include "..\lib\fcntl.h"
24 | #include "..\lib\ignorval.h"
25 |
26 | void
27 | fdadvise (int fd, off_t offset, off_t len, fadvice_t advice)
28 | {
29 | #if HAVE_POSIX_FADVISE
30 | ignore_value (posix_fadvise (fd, offset, len, advice));
31 | #endif
32 | }
33 |
34 | void
35 | fadvise (FILE *fp, fadvice_t advice)
36 | {
37 | if (fp)
38 | fdadvise (fileno (fp), 0, 0, advice);
39 | }
40 |
--------------------------------------------------------------------------------
/gl/fadvise.h:
--------------------------------------------------------------------------------
1 | /* Declare an access pattern hint for files.
2 | Copyright (C) 2010-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see . */
16 |
17 | #include
18 | #include "..\lib\fcntl.h"
19 | #include
20 |
21 | /* There are a few hints one can provide, which have the
22 | following characteristics on Linux 2.6.31 at least.
23 |
24 | POSIX_FADV_SEQUENTIAL
25 | Doubles the size of read ahead done for file
26 | POSIX_FADV_WILLNEED
27 | _synchronously_ prepopulate the buffer cache with the file
28 | POSIX_FADV_NOREUSE
29 | Could lower priority of data in buffer caches,
30 | but currently does nothing.
31 | POSIX_FADV_DONTNEED
32 | Drop the file from cache.
33 | Note this is automatically done when files are unlinked.
34 |
35 | We use this enum "type" both to make it explicit that
36 | these options are mutually exclusive, and to discourage
37 | the passing of the possibly undefined POSIX_FADV_... values.
38 | Note we could #undef the POSIX_FADV_ values, but that would
39 | preclude using the posix_fadvise() function with its standard
40 | constants. Using posix_fadvise() might be required if the return
41 | value is needed, but it must be guarded by appropriate #ifdefs. */
42 |
43 | #if HAVE_POSIX_FADVISE
44 | typedef enum {
45 | FADVISE_NORMAL = POSIX_FADV_NORMAL,
46 | FADVISE_SEQUENTIAL = POSIX_FADV_SEQUENTIAL,
47 | FADVISE_NOREUSE = POSIX_FADV_NOREUSE,
48 | FADVISE_DONTNEED = POSIX_FADV_DONTNEED,
49 | FADVISE_WILLNEED = POSIX_FADV_WILLNEED,
50 | FADVISE_RANDOM = POSIX_FADV_RANDOM
51 | } fadvice_t;
52 | #else
53 | typedef enum {
54 | FADVISE_NORMAL,
55 | FADVISE_SEQUENTIAL,
56 | FADVISE_NOREUSE,
57 | FADVISE_DONTNEED,
58 | FADVISE_WILLNEED,
59 | FADVISE_RANDOM
60 | } fadvice_t;
61 | #endif
62 |
63 | /* We ignore any errors as these hints are only advisory.
64 | There is the chance one can pass invalid ADVICE, which will
65 | not be indicated, but given the simplicity of the interface
66 | this is unlikely. Also not returning errors allows the
67 | unconditional passing of descriptors to non standard files,
68 | which will just be ignored if unsupported. */
69 |
70 | void fdadvise (int fd, off_t offset, off_t len, fadvice_t advice);
71 | void fadvise (FILE *fp, fadvice_t advice);
72 |
--------------------------------------------------------------------------------
/gl/xdec2int.c:
--------------------------------------------------------------------------------
1 | /* Convert decimal strings with bounds checking and exit on error.
2 |
3 | Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 |
19 | #include "xdec2int.h"
20 | #include "xdec2umx.c"
21 | #include "..\lib\errno.h"
22 | #include
23 | #include
24 |
25 | #include "..\lib\error.h"
26 | //#include "..\lib\quote.h"
27 | #include "..\lib\xstrtol.h"
28 |
29 |
30 | #define __xdectoint_t uintmax_t
31 | #define __xstrtol xstrtoumax
32 |
33 | /* Parse numeric string N_STR of base BASE, and return the value.
34 | Exit on parse error or if MIN or MAX are exceeded.
35 | Strings can have multiplicative SUFFIXES if specified.
36 | ERR is printed along with N_STR on error. */
37 |
38 | __xdectoint_t
39 | __xnumtoint (const char *n_str, int base, __xdectoint_t min, __xdectoint_t max,
40 | const char *suffixes, const char *err, int err_exit)
41 | {
42 | strtol_error s_err;
43 |
44 | __xdectoint_t tnum;
45 | s_err = __xstrtol (n_str, NULL, base, &tnum, suffixes);
46 |
47 | if (s_err == LONGINT_OK)
48 | {
49 | if (tnum < min || max < tnum)
50 | {
51 | s_err = LONGINT_OVERFLOW;
52 | /* Use have the INT range as a heuristic to distinguish
53 | type overflow rather than other min/max limits. */
54 | if (tnum > INT_MAX/2)
55 | errno = EOVERFLOW;
56 | #if __xdectoint_signed
57 | else if (tnum < INT_MIN/2)
58 | errno = EOVERFLOW;
59 | #endif
60 | else
61 | errno = ERANGE;
62 | }
63 | }
64 | else if (s_err == LONGINT_OVERFLOW)
65 | errno = EOVERFLOW;
66 | else if (s_err == LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW)
67 | errno = 0; /* Don't show ERANGE errors for invalid numbers. */
68 |
69 | if (s_err != LONGINT_OK)
70 | {
71 | /* EINVAL error message is redundant in this context. */
72 | error (err_exit ? err_exit : EXIT_FAILURE, errno == EINVAL ? 0 : errno,
73 | "%s: %s", err, n_str); //quote (n_str));
74 | }
75 |
76 | return tnum;
77 | }
78 |
79 | /* Parse decimal string N_STR, and return the value.
80 | Exit on parse error or if MIN or MAX are exceeded.
81 | Strings can have multiplicative SUFFIXES if specified.
82 | ERR is printed along with N_STR on error. */
83 |
84 | __xdectoint_t
85 | __xdectoint (const char *n_str, __xdectoint_t min, __xdectoint_t max,
86 | const char *suffixes, const char *err, int err_exit)
87 | {
88 | return __xnumtoint (n_str, 10, min, max, suffixes, err, err_exit);
89 | }
90 |
--------------------------------------------------------------------------------
/gl/xdec2int.h:
--------------------------------------------------------------------------------
1 | /* Convert decimal strings with bounds checking and exit on error.
2 |
3 | Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | #ifndef XDECTOINT_H_
19 | # define XDECTOINT_H_ 1
20 |
21 | # include
22 | #include
23 | #include "..\lib\errno.h"
24 |
25 | # define _DECLARE_XDECTOINT(name, type) \
26 | type name (const char *n_str, type min, type max, \
27 | const char *suffixes, const char *err, int err_exit);
28 | # define _DECLARE_XNUMTOINT(name, type) \
29 | type name (const char *n_str, int base, type min, type max, \
30 | const char *suffixes, const char *err, int err_exit);
31 |
32 | //_DECLARE_XDECTOINT (xdectoimax, intmax_t)
33 | _DECLARE_XDECTOINT (xdectoumax, uintmax_t)
34 |
35 | _DECLARE_XNUMTOINT (xnumtoimax, intmax_t)
36 | _DECLARE_XNUMTOINT (xnumtoumax, uintmax_t)
37 |
38 | #endif /* not XDECTOINT_H_ */
39 |
--------------------------------------------------------------------------------
/gl/xdec2umx.c:
--------------------------------------------------------------------------------
1 | #define __xdectoint xdectoumax
2 | #define __xnumtoint xnumtoumax
3 | #define __xdectoint_t uintmax_t
4 | #define __xstrtol xstrtoumax
5 | #define __xdectoint_signed 0
6 | //#include "xdectoint.c"
7 |
8 |
--------------------------------------------------------------------------------
/lib/assert.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2011-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software; you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3, or (at your option)
7 | any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, see . */
16 | /* Written by LiquidFox1776 */
17 |
18 | #include
19 |
20 |
21 |
--------------------------------------------------------------------------------
/lib/assure.h:
--------------------------------------------------------------------------------
1 | /* Run-time assert-like macros.
2 |
3 | Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by Paul Eggert. */
19 | /* Ported to FreeDOS by LiquidFox1776 */
20 |
21 | #ifndef _GL_ASSURE_H
22 | #define _GL_ASSURE_H
23 |
24 | #include "assert.h"
25 |
26 | /* Check E's value at runtime, and report an error and abort if not.
27 | However, do nothng if NDEBUG is defined.
28 |
29 | Unlike standard 'assert', this macro always compiles E even when NDEBUG
30 | is defined, so as to catch typos and avoid some GCC warnings. */
31 |
32 | #ifdef NDEBUG
33 | # define assure(E) ((void) (0 && (E)))
34 | #else
35 | # define assure(E) assert (E)
36 | #endif
37 |
38 | #endif
39 |
--------------------------------------------------------------------------------
/lib/base32.c:
--------------------------------------------------------------------------------
1 | /* base32.c -- Encode binary data using printable characters.
2 | Copyright (C) 1999-2001, 2004-2006, 2009-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software; you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 2, or (at your option)
7 | any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, see . */
16 |
17 | /* Adapted from Simon Josefsson's base64 code by Gijs van Tulder.
18 | * Ported to FreeDOS by LiquidFox1776
19 | *
20 | * See also RFC 4648 .
21 | *
22 | * Be careful with error checking. Here is how you would typically
23 | * use these functions:
24 | *
25 | * bool ok = base32_decode_alloc (in, inlen, &out, &outlen);
26 | * if (!ok)
27 | * FAIL: input was not valid base32
28 | * if (out == NULL)
29 | * FAIL: memory allocation error
30 | * OK: data in OUT/OUTLEN
31 | *
32 | * size_t outlen = base32_encode_alloc (in, inlen, &out);
33 | * if (out == NULL && outlen == 0 && inlen != 0)
34 | * FAIL: input too long
35 | * if (out == NULL)
36 | * FAIL: memory allocation error
37 | * OK: data in OUT/OUTLEN.
38 | *
39 | */
40 |
41 |
42 | /* Get prototype. */
43 | #include "base32.h"
44 |
45 | /* Get malloc. */
46 | #include
47 |
48 | /* Get UCHAR_MAX. */
49 | #include
50 |
51 | #include
52 |
53 | /* C89 compliant way to cast 'char' to 'unsigned char'. */
54 | static unsigned char
55 | to_uchar (char ch)
56 | {
57 | return ch;
58 | }
59 |
60 | /* Base32 encode IN array of size INLEN into OUT array of size OUTLEN.
61 | If OUTLEN is less than BASE32_LENGTH(INLEN), write as many bytes as
62 | possible. If OUTLEN is larger than BASE32_LENGTH(INLEN), also zero
63 | terminate the output buffer. */
64 | void
65 | base32_encode (const char *in, size_t inlen,
66 | char *out, size_t outlen)
67 | {
68 | static const char b32str[32] =
69 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
70 |
71 | while (inlen && outlen)
72 | {
73 | *out++ = b32str[(to_uchar (in[0]) >> 3) & 0x1f];
74 | if (!--outlen)
75 | break;
76 | *out++ = b32str[((to_uchar (in[0]) << 2)
77 | + (--inlen ? to_uchar (in[1]) >> 6 : 0))
78 | & 0x1f];
79 | if (!--outlen)
80 | break;
81 | *out++ =
82 | (inlen
83 | ? b32str[(to_uchar (in[1]) >> 1) & 0x1f]
84 | : '=');
85 | if (!--outlen)
86 | break;
87 | *out++ =
88 | (inlen
89 | ? b32str[((to_uchar (in[1]) << 4)
90 | + (--inlen ? to_uchar (in[2]) >> 4 : 0))
91 | & 0x1f]
92 | : '=');
93 | if (!--outlen)
94 | break;
95 | *out++ =
96 | (inlen
97 | ? b32str[((to_uchar (in[2]) << 1)
98 | + (--inlen ? to_uchar (in[3]) >> 7 : 0))
99 | & 0x1f]
100 | : '=');
101 | if (!--outlen)
102 | break;
103 | *out++ =
104 | (inlen
105 | ? b32str[(to_uchar (in[3]) >> 2) & 0x1f]
106 | : '=');
107 | if (!--outlen)
108 | break;
109 | *out++ =
110 | (inlen
111 | ? b32str[((to_uchar (in[3]) << 3)
112 | + (--inlen ? to_uchar (in[4]) >> 5 : 0))
113 | & 0x1f]
114 | : '=');
115 | if (!--outlen)
116 | break;
117 | *out++ = inlen ? b32str[to_uchar (in[4]) & 0x1f] : '=';
118 | if (!--outlen)
119 | break;
120 | if (inlen)
121 | inlen--;
122 | if (inlen)
123 | in += 5;
124 | }
125 |
126 | if (outlen)
127 | *out = '\0';
128 | }
129 |
130 | /* Allocate a buffer and store zero terminated base32 encoded data
131 | from array IN of size INLEN, returning BASE32_LENGTH(INLEN), i.e.,
132 | the length of the encoded data, excluding the terminating zero. On
133 | return, the OUT variable will hold a pointer to newly allocated
134 | memory that must be deallocated by the caller. If output string
135 | length would overflow, 0 is returned and OUT is set to NULL. If
136 | memory allocation failed, OUT is set to NULL, and the return value
137 | indicates length of the requested memory block, i.e.,
138 | BASE32_LENGTH(inlen) + 1. */
139 | size_t
140 | base32_encode_alloc (const char *in, size_t inlen, char **out)
141 | {
142 | size_t outlen = 1 + BASE32_LENGTH (inlen);
143 |
144 | /* Check for overflow in outlen computation.
145 | *
146 | * If there is no overflow, outlen >= inlen.
147 | *
148 | * TODO Is this a sufficient check? (See the notes in base64.c.)
149 | */
150 | if (inlen > outlen)
151 | {
152 | *out = NULL;
153 | return 0;
154 | }
155 |
156 | *out = malloc (outlen);
157 | if (!*out)
158 | return outlen;
159 |
160 | base32_encode (in, inlen, *out, outlen);
161 |
162 | return outlen - 1;
163 | }
164 |
165 | /* With this approach this file works independent of the charset used
166 | (think EBCDIC). However, it does assume that the characters in the
167 | Base32 alphabet (A-Z2-7) are encoded in 0..255. POSIX
168 | 1003.1-2001 require that char and unsigned char are 8-bit
169 | quantities, though, taking care of that problem. But this may be a
170 | potential problem on non-POSIX C99 platforms.
171 |
172 | IBM C V6 for AIX mishandles "#define B32(x) ...'x'...", so use "_"
173 | as the formal parameter rather than "x". */
174 | #define B32(_) \
175 | ((_) == 'A' ? 0 \
176 | : (_) == 'B' ? 1 \
177 | : (_) == 'C' ? 2 \
178 | : (_) == 'D' ? 3 \
179 | : (_) == 'E' ? 4 \
180 | : (_) == 'F' ? 5 \
181 | : (_) == 'G' ? 6 \
182 | : (_) == 'H' ? 7 \
183 | : (_) == 'I' ? 8 \
184 | : (_) == 'J' ? 9 \
185 | : (_) == 'K' ? 10 \
186 | : (_) == 'L' ? 11 \
187 | : (_) == 'M' ? 12 \
188 | : (_) == 'N' ? 13 \
189 | : (_) == 'O' ? 14 \
190 | : (_) == 'P' ? 15 \
191 | : (_) == 'Q' ? 16 \
192 | : (_) == 'R' ? 17 \
193 | : (_) == 'S' ? 18 \
194 | : (_) == 'T' ? 19 \
195 | : (_) == 'U' ? 20 \
196 | : (_) == 'V' ? 21 \
197 | : (_) == 'W' ? 22 \
198 | : (_) == 'X' ? 23 \
199 | : (_) == 'Y' ? 24 \
200 | : (_) == 'Z' ? 25 \
201 | : (_) == '2' ? 26 \
202 | : (_) == '3' ? 27 \
203 | : (_) == '4' ? 28 \
204 | : (_) == '5' ? 29 \
205 | : (_) == '6' ? 30 \
206 | : (_) == '7' ? 31 \
207 | : -1)
208 |
209 | static const signed char b32[0x100] = {
210 | B32 (0), B32 (1), B32 (2), B32 (3),
211 | B32 (4), B32 (5), B32 (6), B32 (7),
212 | B32 (8), B32 (9), B32 (10), B32 (11),
213 | B32 (12), B32 (13), B32 (14), B32 (15),
214 | B32 (16), B32 (17), B32 (18), B32 (19),
215 | B32 (20), B32 (21), B32 (22), B32 (23),
216 | B32 (24), B32 (25), B32 (26), B32 (27),
217 | B32 (28), B32 (29), B32 (30), B32 (31),
218 | B32 (32), B32 (33), B32 (34), B32 (35),
219 | B32 (36), B32 (37), B32 (38), B32 (39),
220 | B32 (40), B32 (41), B32 (42), B32 (43),
221 | B32 (44), B32 (45), B32 (46), B32 (47),
222 | B32 (48), B32 (49), B32 (50), B32 (51),
223 | B32 (52), B32 (53), B32 (54), B32 (55),
224 | B32 (56), B32 (57), B32 (58), B32 (59),
225 | B32 (60), B32 (61), B32 (62), B32 (63),
226 | B32 (32), B32 (65), B32 (66), B32 (67),
227 | B32 (68), B32 (69), B32 (70), B32 (71),
228 | B32 (72), B32 (73), B32 (74), B32 (75),
229 | B32 (76), B32 (77), B32 (78), B32 (79),
230 | B32 (80), B32 (81), B32 (82), B32 (83),
231 | B32 (84), B32 (85), B32 (86), B32 (87),
232 | B32 (88), B32 (89), B32 (90), B32 (91),
233 | B32 (92), B32 (93), B32 (94), B32 (95),
234 | B32 (96), B32 (97), B32 (98), B32 (99),
235 | B32 (100), B32 (101), B32 (102), B32 (103),
236 | B32 (104), B32 (105), B32 (106), B32 (107),
237 | B32 (108), B32 (109), B32 (110), B32 (111),
238 | B32 (112), B32 (113), B32 (114), B32 (115),
239 | B32 (116), B32 (117), B32 (118), B32 (119),
240 | B32 (120), B32 (121), B32 (122), B32 (123),
241 | B32 (124), B32 (125), B32 (126), B32 (127),
242 | B32 (128), B32 (129), B32 (130), B32 (131),
243 | B32 (132), B32 (133), B32 (134), B32 (135),
244 | B32 (136), B32 (137), B32 (138), B32 (139),
245 | B32 (140), B32 (141), B32 (142), B32 (143),
246 | B32 (144), B32 (145), B32 (146), B32 (147),
247 | B32 (148), B32 (149), B32 (150), B32 (151),
248 | B32 (152), B32 (153), B32 (154), B32 (155),
249 | B32 (156), B32 (157), B32 (158), B32 (159),
250 | B32 (160), B32 (161), B32 (162), B32 (163),
251 | B32 (132), B32 (165), B32 (166), B32 (167),
252 | B32 (168), B32 (169), B32 (170), B32 (171),
253 | B32 (172), B32 (173), B32 (174), B32 (175),
254 | B32 (176), B32 (177), B32 (178), B32 (179),
255 | B32 (180), B32 (181), B32 (182), B32 (183),
256 | B32 (184), B32 (185), B32 (186), B32 (187),
257 | B32 (188), B32 (189), B32 (190), B32 (191),
258 | B32 (192), B32 (193), B32 (194), B32 (195),
259 | B32 (196), B32 (197), B32 (198), B32 (199),
260 | B32 (200), B32 (201), B32 (202), B32 (203),
261 | B32 (204), B32 (205), B32 (206), B32 (207),
262 | B32 (208), B32 (209), B32 (210), B32 (211),
263 | B32 (212), B32 (213), B32 (214), B32 (215),
264 | B32 (216), B32 (217), B32 (218), B32 (219),
265 | B32 (220), B32 (221), B32 (222), B32 (223),
266 | B32 (224), B32 (225), B32 (226), B32 (227),
267 | B32 (228), B32 (229), B32 (230), B32 (231),
268 | B32 (232), B32 (233), B32 (234), B32 (235),
269 | B32 (236), B32 (237), B32 (238), B32 (239),
270 | B32 (240), B32 (241), B32 (242), B32 (243),
271 | B32 (244), B32 (245), B32 (246), B32 (247),
272 | B32 (248), B32 (249), B32 (250), B32 (251),
273 | B32 (252), B32 (253), B32 (254), B32 (255)
274 | };
275 |
276 | #if UCHAR_MAX == 255
277 | # define uchar_in_range(c) true
278 | #else
279 | # define uchar_in_range(c) ((c) <= 255)
280 | #endif
281 |
282 | /* Return true if CH is a character from the Base32 alphabet, and
283 | false otherwise. Note that '=' is padding and not considered to be
284 | part of the alphabet. */
285 | bool
286 | isbase32 (char ch)
287 | {
288 | return uchar_in_range (to_uchar (ch)) && 0 <= b32[to_uchar (ch)];
289 | }
290 |
291 | /* Initialize decode-context buffer, CTX. */
292 | void
293 | base32_decode_ctx_init (struct base32_decode_context *ctx)
294 | {
295 | ctx->i = 0;
296 | }
297 |
298 | /* If CTX->i is 0 or 8, there are eight or more bytes in [*IN..IN_END), and
299 | none of those eight is a newline, then return *IN. Otherwise, copy up to
300 | 4 - CTX->i non-newline bytes from that range into CTX->buf, starting at
301 | index CTX->i and setting CTX->i to reflect the number of bytes copied,
302 | and return CTX->buf. In either case, advance *IN to point to the byte
303 | after the last one processed, and set *N_NON_NEWLINE to the number of
304 | verified non-newline bytes accessible through the returned pointer. */
305 | static char *
306 | get_8 (struct base32_decode_context *ctx,
307 | char const *__restrict *in, char const *__restrict in_end,
308 | size_t *n_non_newline)
309 | {
310 | if (ctx->i == 8)
311 | ctx->i = 0;
312 |
313 | if (ctx->i == 0)
314 | {
315 | char const *t = *in;
316 | if (8 <= in_end - *in && memchr (t, '\n', 8) == NULL)
317 | {
318 | /* This is the common case: no newline. */
319 | *in += 8;
320 | *n_non_newline = 8;
321 | return (char *) t;
322 | }
323 | }
324 |
325 | {
326 | /* Copy non-newline bytes into BUF. */
327 | char const *p = *in;
328 | while (p < in_end)
329 | {
330 | char c = *p++;
331 | if ((c != '\n') && (c != '\r'))
332 | {
333 | ctx->buf[ctx->i++] = c;
334 | if (ctx->i == 8)
335 | break;
336 | }
337 | }
338 |
339 | *in = p;
340 | *n_non_newline = ctx->i;
341 | return ctx->buf;
342 | }
343 | }
344 |
345 | #define return_false \
346 | do \
347 | { \
348 | *outp = out; \
349 | return false; \
350 | } \
351 | while (false)
352 |
353 | /* Decode eight bytes of base32-encoded data, IN, of length INLEN
354 | into the output buffer, *OUT, of size *OUTLEN bytes. Return true if
355 | decoding is successful, false otherwise. If *OUTLEN is too small,
356 | as many bytes as possible are written to *OUT. On return, advance
357 | *OUT to point to the byte after the last one written, and decrement
358 | *OUTLEN to reflect the number of bytes remaining in *OUT. */
359 | static bool
360 | decode_8 (char const *__restrict in, size_t inlen,
361 | char *__restrict *outp, size_t *outleft)
362 | {
363 | char *out = *outp;
364 | if (inlen < 8)
365 | return false;
366 |
367 | if (!isbase32 (in[0]) || !isbase32 (in[1]) )
368 | return false;
369 |
370 | if (*outleft)
371 | {
372 | *out++ = ((b32[to_uchar (in[0])] << 3)
373 | | (b32[to_uchar (in[1])] >> 2));
374 | --*outleft;
375 | }
376 |
377 | if (in[2] == '=')
378 | {
379 | if (in[3] != '=' || in[4] != '=' || in[5] != '='
380 | || in[6] != '=' || in[7] != '=')
381 | return_false;
382 | }
383 | else
384 | {
385 | if (!isbase32 (in[2]) || !isbase32 (in[3]))
386 | return_false;
387 |
388 | if (*outleft)
389 | {
390 | *out++ = ((b32[to_uchar (in[1])] << 6)
391 | | (b32[to_uchar (in[2])] << 1)
392 | | (b32[to_uchar (in[3])] >> 4));
393 | --*outleft;
394 | }
395 |
396 | if (in[4] == '=')
397 | {
398 | if (in[5] != '=' || in[6] != '=' || in[7] != '=')
399 | return_false;
400 | }
401 | else
402 | {
403 | if (!isbase32 (in[4]))
404 | return_false;
405 |
406 | if (*outleft)
407 | {
408 | *out++ = ((b32[to_uchar (in[3])] << 4)
409 | | (b32[to_uchar (in[4])] >> 1));
410 | --*outleft;
411 | }
412 |
413 | if (in[5] == '=')
414 | {
415 | if (in[6] != '=' || in[7] != '=')
416 | return_false;
417 | }
418 | else
419 | {
420 | if (!isbase32 (in[5]) || !isbase32 (in[6]))
421 | return_false;
422 |
423 | if (*outleft)
424 | {
425 | *out++ = ((b32[to_uchar (in[4])] << 7)
426 | | (b32[to_uchar (in[5])] << 2)
427 | | (b32[to_uchar (in[6])] >> 3));
428 | --*outleft;
429 | }
430 |
431 | if (in[7] != '=')
432 | {
433 | if (!isbase32 (in[7]))
434 | return_false;
435 |
436 | if (*outleft)
437 | {
438 | *out++ = ((b32[to_uchar (in[6])] << 5)
439 | | (b32[to_uchar (in[7])]));
440 | --*outleft;
441 | }
442 | }
443 | }
444 | }
445 | }
446 |
447 | *outp = out;
448 | return true;
449 | }
450 |
451 | /* Decode base32-encoded input array IN of length INLEN to output array
452 | OUT that can hold *OUTLEN bytes. The input data may be interspersed
453 | with newlines. Return true if decoding was successful, i.e. if the
454 | input was valid base32 data, false otherwise. If *OUTLEN is too
455 | small, as many bytes as possible will be written to OUT. On return,
456 | *OUTLEN holds the length of decoded bytes in OUT. Note that as soon
457 | as any non-alphabet, non-newline character is encountered, decoding
458 | is stopped and false is returned. If INLEN is zero, then process
459 | only whatever data is stored in CTX.
460 |
461 | Initially, CTX must have been initialized via base32_decode_ctx_init.
462 | Subsequent calls to this function must reuse whatever state is recorded
463 | in that buffer. It is necessary for when a octuple of base32 input
464 | bytes spans two input buffers.
465 |
466 | If CTX is NULL then newlines are treated as garbage and the input
467 | buffer is processed as a unit. */
468 |
469 | bool
470 | base32_decode_ctx (struct base32_decode_context *ctx,
471 | const char *__restrict in, size_t inlen,
472 | char *__restrict out, size_t *outlen)
473 | {
474 | size_t outleft = *outlen;
475 | bool ignore_newlines = ctx != NULL;
476 | bool flush_ctx = false;
477 | unsigned int ctx_i = 0;
478 |
479 | if (ignore_newlines)
480 | {
481 | ctx_i = ctx->i;
482 | flush_ctx = inlen == 0;
483 | }
484 |
485 |
486 | while (true)
487 | {
488 | size_t outleft_save = outleft;
489 | if (ctx_i == 0 && !flush_ctx)
490 | {
491 | while (true)
492 | {
493 | /* Save a copy of outleft, in case we need to re-parse this
494 | block of four bytes. */
495 | outleft_save = outleft;
496 | if (!decode_8 (in, inlen, &out, &outleft))
497 | break;
498 |
499 | in += 8;
500 | inlen -= 8;
501 | }
502 | }
503 |
504 | if (inlen == 0 && !flush_ctx)
505 | break;
506 |
507 | /* Handle the common case of 72-byte wrapped lines.
508 | This also handles any other multiple-of-8-byte wrapping. */
509 | if (inlen && (*in == '\n' || *in == '\r') && ignore_newlines)
510 | {
511 | ++in;
512 | --inlen;
513 | continue;
514 | }
515 |
516 | /* Restore OUT and OUTLEFT. */
517 | out -= outleft_save - outleft;
518 | outleft = outleft_save;
519 |
520 | {
521 | char const *in_end = in + inlen;
522 | char const *non_nl;
523 |
524 | if (ignore_newlines)
525 | non_nl = get_8 (ctx, &in, in_end, &inlen);
526 | else
527 | non_nl = in; /* Might have nl in this case. */
528 |
529 | /* If the input is empty or consists solely of newlines (0 non-newlines),
530 | then we're done. Likewise if there are fewer than 8 bytes when not
531 | flushing context and not treating newlines as garbage. */
532 | if (inlen == 0 || (inlen < 8 && !flush_ctx && ignore_newlines))
533 | {
534 | inlen = 0;
535 | break;
536 | }
537 | if (!decode_8 (non_nl, inlen, &out, &outleft))
538 | break;
539 |
540 | inlen = in_end - in;
541 | }
542 | }
543 |
544 | *outlen -= outleft;
545 |
546 | return inlen == 0;
547 | }
548 |
549 | /* Allocate an output buffer in *OUT, and decode the base32 encoded
550 | data stored in IN of size INLEN to the *OUT buffer. On return, the
551 | size of the decoded data is stored in *OUTLEN. OUTLEN may be NULL,
552 | if the caller is not interested in the decoded length. *OUT may be
553 | NULL to indicate an out of memory error, in which case *OUTLEN
554 | contains the size of the memory block needed. The function returns
555 | true on successful decoding and memory allocation errors. (Use the
556 | *OUT and *OUTLEN parameters to differentiate between successful
557 | decoding and memory error.) The function returns false if the
558 | input was invalid, in which case *OUT is NULL and *OUTLEN is
559 | undefined. */
560 | bool
561 | base32_decode_alloc_ctx (struct base32_decode_context *ctx,
562 | const char *in, size_t inlen, char **out,
563 | size_t *outlen)
564 | {
565 | /* This may allocate a few bytes too many, depending on input,
566 | but it's not worth the extra CPU time to compute the exact size.
567 | The exact size is 5 * inlen / 8, minus one or more bytes if the
568 | input is padded with one or more "=".
569 | Dividing before multiplying avoids the possibility of overflow. */
570 | size_t needlen = 5 * (inlen / 8) + 5;
571 |
572 | *out = malloc (needlen);
573 | if (!*out)
574 | return true;
575 |
576 | if (!base32_decode_ctx (ctx, in, inlen, *out, &needlen))
577 | {
578 | free (*out);
579 | *out = NULL;
580 | return false;
581 | }
582 |
583 | if (outlen)
584 | *outlen = needlen;
585 |
586 | return true;
587 | }
588 |
--------------------------------------------------------------------------------
/lib/base32.h:
--------------------------------------------------------------------------------
1 | /* base32.h -- Encode binary data using printable characters.
2 | Copyright (C) 2004-2006, 2009-2018 Free Software Foundation, Inc.
3 | Adapted from Simon Josefsson's base64 code by Gijs van Tulder.
4 |
5 | This program is free software; you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 2, or (at your option)
8 | any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program; if not, see . */
17 | /* Ported to FreeDOS by LiquidFox1776 */
18 |
19 | #ifndef BASE32_H
20 | # define BASE32_H
21 | #include "const.h"
22 |
23 | /* Get size_t. */
24 | # include
25 |
26 | /* Get bool. */
27 | # include
28 |
29 | /* This uses that the expression (n+(k-1))/k means the smallest
30 | integer >= n/k, i.e., the ceiling of n/k. */
31 | # define BASE32_LENGTH(inlen) ((((inlen) + 4) / 5) * 8)
32 |
33 | struct base32_decode_context
34 | {
35 | unsigned int i;
36 | char buf[8];
37 | };
38 |
39 | extern bool isbase32 (char ch);
40 |
41 | extern void base32_encode (const char *__restrict in, size_t inlen,
42 | char *__restrict out, size_t outlen);
43 |
44 | extern size_t base32_encode_alloc (const char *in, size_t inlen, char **out);
45 |
46 | extern void base32_decode_ctx_init (struct base32_decode_context *ctx);
47 |
48 | extern bool base32_decode_ctx (struct base32_decode_context *ctx,
49 | const char *__restrict in, size_t inlen,
50 | char *__restrict out, size_t *outlen);
51 |
52 | extern bool base32_decode_alloc_ctx (struct base32_decode_context *ctx,
53 | const char *in, size_t inlen,
54 | char **out, size_t *outlen);
55 |
56 | #define base32_decode(in, inlen, out, outlen) \
57 | base32_decode_ctx (NULL, in, inlen, out, outlen)
58 |
59 | #define base32_decode_alloc(in, inlen, out, outlen) \
60 | base32_decode_alloc_ctx (NULL, in, inlen, out, outlen)
61 |
62 | #endif /* BASE32_H */
63 |
--------------------------------------------------------------------------------
/lib/base64.h:
--------------------------------------------------------------------------------
1 | /* base64.h -- Encode binary data using printable characters.
2 | Copyright (C) 2004-2006, 2009-2018 Free Software Foundation, Inc.
3 | Written by Simon Josefsson.
4 |
5 | This program is free software; you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 2, or (at your option)
8 | any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program; if not, see . */
17 | /* Ported to FreeDOS by LiquidFox1776 */
18 |
19 | #ifndef BASE64_H
20 | # define BASE64_H
21 | #include "const.h"
22 |
23 | /* Get size_t. */
24 | # include
25 |
26 | /* Get bool. */
27 | # include
28 |
29 | # ifdef __cplusplus
30 | extern "C" {
31 | # endif
32 |
33 | /* This uses that the expression (n+(k-1))/k means the smallest
34 | integer >= n/k, i.e., the ceiling of n/k. */
35 | # define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)
36 |
37 | struct base64_decode_context
38 | {
39 | unsigned int i;
40 | char buf[4];
41 | };
42 |
43 | extern bool isbase64 (char ch) _GL_ATTRIBUTE_CONST;
44 |
45 | extern void base64_encode (const char *__restrict in, size_t inlen,
46 | char *__restrict out, size_t outlen);
47 |
48 | extern size_t base64_encode_alloc (const char *in, size_t inlen, char **out);
49 |
50 | extern void base64_decode_ctx_init (struct base64_decode_context *ctx);
51 |
52 | extern bool base64_decode_ctx (struct base64_decode_context *ctx,
53 | const char *__restrict in, size_t inlen,
54 | char *__restrict out, size_t *outlen);
55 |
56 | extern bool base64_decode_alloc_ctx (struct base64_decode_context *ctx,
57 | const char *in, size_t inlen,
58 | char **out, size_t *outlen);
59 |
60 | #define base64_decode(in, inlen, out, outlen) \
61 | base64_decode_ctx (NULL, in, inlen, out, outlen)
62 |
63 | #define base64_decode_alloc(in, inlen, out, outlen) \
64 | base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)
65 |
66 | # ifdef __cplusplus
67 | }
68 | # endif
69 |
70 | #endif /* BASE64_H */
71 |
--------------------------------------------------------------------------------
/lib/basenaml.c:
--------------------------------------------------------------------------------
1 | /* basename-lgpl.c -- return the last element in a file name
2 |
3 | Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2018 Free Software
4 | Foundation, Inc.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | Ported to FreeDos by LiquidFox1776
20 | Some changes applied by Ercan Ersoy. */
21 |
22 | #include
23 | #include "dirname.h"
24 |
25 | /* Return the address of the last file name component of NAME. If
26 | NAME has no relative file name components because it is a file
27 | system root, return the empty string. */
28 |
29 | char *last_component(char const *name)
30 | {
31 | char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
32 | char const *p;
33 | bool saw_slash = false;
34 |
35 |
36 | while (ISSLASH (*base))
37 | base++;
38 |
39 | for(p = base; *p; p++)
40 | {
41 | if(ISSLASH(*p))
42 | {
43 | saw_slash = true;
44 | }
45 | else if(saw_slash)
46 | {
47 | base = p;
48 | saw_slash = false;
49 | }
50 | }
51 |
52 | return (char *) base;
53 | }
54 |
55 | /* Return the length of the basename NAME. Typically NAME is the
56 | value returned by base_name or last_component. Act like strlen
57 | (NAME), except omit all trailing slashes. */
58 |
59 | size_t base_len (char const *name)
60 | {
61 | size_t len;
62 | size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
63 |
64 | for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)
65 | {
66 | continue;
67 | }
68 |
69 | if(DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1 && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
70 | {
71 | return 2;
72 | }
73 |
74 | if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len && len == prefix_len && ISSLASH (name[prefix_len]))
75 | {
76 | return prefix_len + 1;
77 | }
78 |
79 | return len;
80 | }
81 |
--------------------------------------------------------------------------------
/lib/bin-io.c:
--------------------------------------------------------------------------------
1 | /* Binary mode I/O.
2 | Copyright 2017-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #define BINARY_IO_INLINE _GL_EXTERN_INLINE
19 | #include "bin-io.h"
20 | # include "errno.h"
21 | # include
22 |
23 |
24 | int
25 | __gl_setmode_check (int fd)
26 | {
27 | return 0;
28 | /*if (isatty(fd))
29 | {
30 | errno = EINVAL;
31 | return -1;
32 | }
33 | else
34 | return 0;*/
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/lib/bin-io.h:
--------------------------------------------------------------------------------
1 | /* Binary mode I/O.
2 | Copyright (C) 2001, 2003, 2005, 2008-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #ifndef _BINARY_H
19 | #define _BINARY_H
20 | #include "const.h"
21 |
22 | /* For systems that distinguish between text and binary I/O.
23 | O_BINARY is guaranteed by the gnulib . */
24 | #include "fcntl.h" // includes
25 |
26 | /* The MSVC7 doesn't like to be included after '#define fileno ...',
27 | so we include it here first. */
28 | #include
29 |
30 | #ifndef BINARY_IO_INLINE
31 | #define BINARY_IO_INLINE _GL_INLINE
32 | #endif
33 |
34 | #define __gl_setmode setmode
35 |
36 | extern int __gl_setmode_check (int);
37 |
38 | /* Set FD's mode to MODE, which should be either O_TEXT or O_BINARY.
39 | Return the old mode if successful, -1 (setting errno) on failure.
40 | Ordinarily this function would be called 'setmode', since that is
41 | its name on MS-Windows, but it is called 'set_binary_mode' here
42 | to avoid colliding with a BSD function of another name. */
43 |
44 | BINARY_IO_INLINE int
45 | set_binary_mode (int fd, int mode)
46 | {
47 | int r = __gl_setmode_check (fd);
48 | return r != 0 ? r : __gl_setmode (fd, mode);
49 | }
50 |
51 | /* This macro is obsolescent. */
52 | #define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
53 |
54 |
55 | #endif /* _BINARY_H */
56 |
--------------------------------------------------------------------------------
/lib/const.h:
--------------------------------------------------------------------------------
1 | /*Copyright (C) 2001, 2003, 2005, 2008-2018 Free Software Foundation, Inc.
2 |
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU General Public License as published by
5 | the Free Software Foundation; either version 3 of the License, or
6 | (at your option) any later version.
7 |
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 |
13 | You should have received a copy of the GNU General Public License
14 | along with this program. If not, see . */
15 | /* written by LiquidFox1776 */
16 |
17 | #define _GL_ATTRIBUTE_CONST
18 | #define _GL_UNUSED
19 | #define _GL_INLINE inline
20 | #define _GL_EXTERN_INLINE extern inline
21 |
22 | #ifndef _
23 | #define _(String) String
24 | #endif
25 |
26 |
--------------------------------------------------------------------------------
/lib/die.h:
--------------------------------------------------------------------------------
1 | /* Report an error and exit.
2 | Copyright 2016-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software; you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3, or (at your option)
7 | any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 | 02110-1301, USA. */
18 | /* Ported to FreeDOS by LiquidFox1776 */
19 |
20 | #ifndef DIE_H
21 | # define DIE_H
22 |
23 | # include "error.h"
24 | # include
25 | # include "verify.h"
26 | # include
27 |
28 |
29 | /* Like 'error (STATUS, ...)', except STATUS must be a nonzero constant.
30 | This may pacify the compiler or help it generate better code. */
31 | # define die(status, ...) \
32 | verify_expr (status, (error (status, __VA_ARGS__), assume (false))); close_nls_file(true);
33 |
34 | #endif /* DIE_H */
35 |
--------------------------------------------------------------------------------
/lib/dirname.h:
--------------------------------------------------------------------------------
1 | /* Take file names apart into directory and base names.
2 |
3 | Copyright (C) 1998, 2001, 2003-2006, 2009-2018 Free Software Foundation,
4 | Inc.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | Ported to FreeDos by LiquidFox1776
20 | Some changes applied by Ercan Ersoy. */
21 |
22 | #ifndef DIRNAME_H_
23 | #define DIRNAME_H_ 1
24 | #include
25 | #include
26 |
27 | #include "dosname.h"
28 |
29 | #ifndef DIRECTORY_SEPARATOR
30 | #define DIRECTORY_SEPARATOR '\\'
31 | #endif
32 |
33 | #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
34 | #define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
35 | #endif
36 |
37 | #ifdef __cplusplus
38 | extern "C" {
39 | #endif
40 |
41 | #if GNULIB_DIRNAME
42 | char *base_name(char const *file) _GL_ATTRIBUTE_MALLOC;
43 | char *dir_name(char const *file);
44 | #endif
45 |
46 | char *mdir_name(char const *file);
47 | size_t base_len(char const *file);
48 | size_t dir_len(char const *file);
49 | char *last_component(char const *file);
50 |
51 | bool strip_trailing_slashes (char *file);
52 |
53 | #ifdef __cplusplus
54 | } /* extern "C" */
55 | #endif
56 |
57 | #endif /* not DIRNAME_H_ */
58 |
--------------------------------------------------------------------------------
/lib/dirnamel.c:
--------------------------------------------------------------------------------
1 | /* dirname-lgpl.c -- return all but the last element in a file name
2 |
3 | Copyright (C) 1990, 1998, 2000-2001, 2003-2006, 2009-2018 Free Software
4 | Foundation, Inc.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | Ported to FreeDos by LiquidFox1776
20 | Some changes applied by Ercan Ersoy. */
21 |
22 | #include
23 | #include
24 | #include "dirname.h"
25 |
26 | /* Return the length of the prefix of FILE that will be used by
27 | dir_name. If FILE is in the working directory, this returns zero
28 | even though 'dir_name (FILE)' will return ".". Works properly even
29 | if there are trailing slashes (by effectively ignoring them). */
30 |
31 | size_t dir_len(char const *file)
32 | {
33 | size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
34 | size_t length;
35 |
36 | /* Advance prefix_length beyond important leading slashes. */
37 | prefix_length += (prefix_length != 0 ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && ISSLASH (file[prefix_length])) : (ISSLASH (file[0]) ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT && ISSLASH (file[1]) && ! ISSLASH (file[2]) ? 2 : 1)) : 0));
38 |
39 | /* Strip the basename and any redundant slashes before it. */
40 | for (length = last_component (file) - file; prefix_length < length; length--)
41 | {
42 | if (! ISSLASH (file[length - 1]))
43 | {
44 | break;
45 | }
46 | }
47 |
48 | return length;
49 | }
50 |
51 |
52 | /* In general, we can't use the builtin 'dirname' function if available,
53 | since it has different meanings in different environments.
54 | In some environments the builtin 'dirname' modifies its argument.
55 |
56 | Return the leading directories part of FILE, allocated with malloc.
57 | Works properly even if there are trailing slashes (by effectively
58 | ignoring them). Return NULL on failure.
59 |
60 | If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
61 | lstat (base_name (FILE)); } will access the same file. Likewise,
62 | if the sequence { chdir (dir_name (FILE));
63 | rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
64 | to "foo" in the same directory FILE was in. */
65 |
66 | char *mdir_name(char const *file)
67 | {
68 | size_t length = dir_len (file);
69 | bool append_dot = (length == 0 || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && length == FILE_SYSTEM_PREFIX_LEN (file) && file[2] != '\0' && ! ISSLASH (file[2])));
70 | char *dir = malloc (length + append_dot + 1);
71 |
72 | if (!dir)
73 | {
74 | return NULL;
75 | }
76 |
77 | memcpy (dir, file, length);
78 |
79 | if (append_dot)
80 | {
81 | dir[length++] = '.';
82 | }
83 | dir[length] = '\0';
84 |
85 | return dir;
86 | }
87 |
--------------------------------------------------------------------------------
/lib/dosname.h:
--------------------------------------------------------------------------------
1 | /* File names on FREEDOS/Windows systems.
2 |
3 | Copyright (C) 2000-2001, 2004-2006, 2009-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 |
18 | From Paul Eggert and Jim Meyering.
19 | Ported to FreeDos by LiquidFox1776
20 | Some changes applied by Ercan Ersoy.
21 |
22 | TODO This file still needs cleaned up quite a bit */
23 |
24 | #ifndef _DOSNAME_H
25 | #define _DOSNAME_H 1
26 |
27 | #if (defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DOS__ || defined __DJGPP__)
28 | /* This internal macro assumes ASCII, but all hosts that support drive
29 | letters use ASCII. */
30 | #define _IS_DRIVE_LETTER(C) (((unsigned int) (C) | ('a' - 'A')) - 'a' <= 'z' - 'a')
31 | #define FILE_SYSTEM_PREFIX_LEN(Filename) (_IS_DRIVE_LETTER ((Filename)[0]) && (Filename)[1] == ':' ? 2 : 0)
32 |
33 | #ifndef __CYGWIN__
34 | #define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 1
35 | #endif
36 | #define ISSLASH(C) ((C) == '\\')
37 | #else
38 |
39 | #define FILE_SYSTEM_PREFIX_LEN(Filename) 0
40 | #define ISSLASH(C) ((C) == '\\')
41 | #endif
42 |
43 | #ifndef FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
44 | #define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0
45 | #endif
46 |
47 | #if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
48 | #define IS_ABSOLUTE_FILE_NAME(F) ISSLASH ((F)[FILE_SYSTEM_PREFIX_LEN (F)])
49 | #else
50 | #define IS_ABSOLUTE_FILE_NAME(F) (ISSLASH ((F)[0]) || FILE_SYSTEM_PREFIX_LEN (F) != 0)
51 | #endif
52 |
53 | #define IS_RELATIVE_FILE_NAME(F) (! IS_ABSOLUTE_FILE_NAME (F))
54 |
55 | #endif /* DOSNAME_H_ */
56 |
--------------------------------------------------------------------------------
/lib/errno.h:
--------------------------------------------------------------------------------
1 | /* written by LiquidFox1776 */
2 | #ifndef __ERRNO__INCLUDED__
3 | #define __ERRNO__INCLUDED__ 1
4 |
5 | #include
6 | #define EOVERFLOW 75 /* Value to large for defined data type */
7 |
8 | #endif //End __ERRNO__INCLUDED__
9 |
--------------------------------------------------------------------------------
/lib/error.c:
--------------------------------------------------------------------------------
1 | /* Error handler for noninteractive utilities
2 | Copyright (C) 1990-1998, 2000-2007, 2009-2018 Free Software Foundation, Inc.
3 | This file is part of the GNU C Library.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by David MacKenzie . */
19 | /* Ported to FreeDOS by LiquidFox1776 */
20 |
21 |
22 | #include "error.h"
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include "fcntl.h"
28 | #include
29 |
30 | #include "getprogn.h"
31 |
32 | #ifndef _
33 | # define _(String) String
34 | #endif
35 |
36 | /* If NULL, error will flush stdout, then print on stderr the program
37 | name, a colon and a space. Otherwise, error will call this
38 | function without parameters instead. */
39 | void (*error_print_progname) (void);
40 |
41 | /* This variable is incremented each time 'error' is called. */
42 | unsigned int error_message_count;
43 |
44 | #ifdef _LIBC
45 | #else /* not _LIBC */
46 |
47 | /* The gnulib override of fcntl is not needed in this file. */
48 | //# undef fcntl
49 |
50 | //# if !(GNULIB_STRERROR_R_POSIX || HAVE_DECL_STRERROR_R)
51 | ///*# ifndef HAVE_DECL_STRERROR_R
52 | //"this configure-time declaration test was not run"
53 | //# endif*/
54 | //# if STRERROR_R_CHAR_P
55 | //char *strerror_r (int errnum, char *buf, size_t buflen);
56 | //# else
57 | //int strerror_r (int errnum, char *buf, size_t buflen);
58 | //# endif
59 | //# endif
60 |
61 | //#define program_name getprogname ()
62 |
63 | # if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r
64 | # define __strerror_r strerror_r
65 | # endif /* GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r */
66 | #endif /* not _LIBC */
67 |
68 | /* Return non-zero if FD is open. */
69 | static int
70 | is_open (int fd)
71 | {
72 | return 0 <= fcntl (fd, F_GETFL);
73 | }
74 |
75 | static void
76 | flush_stdout (void)
77 | {
78 | #if !_LIBC
79 | int stdout_fd;
80 |
81 | # if GNULIB_FREOPEN_SAFER
82 | /* Use of gnulib's freopen-safer module normally ensures that
83 | fileno (stdout) == 1
84 | whenever stdout is open. */
85 | stdout_fd = STDOUT_FILENO;
86 | # else
87 | /* POSIX states that fileno (stdout) after fclose is unspecified. But in
88 | practice it is not a problem, because stdout is statically allocated and
89 | the fd of a FILE stream is stored as a field in its allocated memory. */
90 | stdout_fd = fileno (stdout);
91 | # endif
92 | /* POSIX states that fflush (stdout) after fclose is unspecified; it
93 | is safe in glibc, but not on all other platforms. fflush (NULL)
94 | is always defined, but too draconian. */
95 | if (0 <= stdout_fd && is_open (stdout_fd))
96 | #endif
97 | fflush (stdout);
98 | }
99 |
100 | static void
101 | print_errno_message (int errnum)
102 | {
103 | char const *s;
104 |
105 | #if _LIBC || GNULIB_STRERROR_R_POSIX || defined HAVE_STRERROR_R
106 | char errbuf[1024];
107 | # if _LIBC || (!GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P)
108 | s = __strerror_r (errnum, errbuf, sizeof errbuf);
109 | # else
110 | if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
111 | s = errbuf;
112 | else
113 | s = 0;
114 | # endif
115 | #else
116 | s = strerror (errnum);
117 | #endif
118 |
119 | #if !_LIBC
120 | if (! s)
121 | s = _("Unknown system error");
122 | #endif
123 |
124 | #if _LIBC
125 | __fxprintf (NULL, ": %s", s);
126 | #else
127 | fprintf (stderr, ": %s", s);
128 | #endif
129 | }
130 |
131 | static void
132 | error_tail (int status, int errnum, const char *message, va_list args)
133 | {
134 | #if _LIBC
135 | if (_IO_fwide (stderr, 0) > 0)
136 | {
137 | size_t len = strlen (message) + 1;
138 | wchar_t *wmessage = NULL;
139 | mbstate_t st;
140 | size_t res;
141 | const char *tmp;
142 | bool use_malloc = false;
143 |
144 | while (1)
145 | {
146 | if (__libc_use_alloca (len * sizeof (wchar_t)))
147 | wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
148 | else
149 | {
150 | if (!use_malloc)
151 | wmessage = NULL;
152 |
153 | wchar_t *p = (wchar_t *) realloc (wmessage,
154 | len * sizeof (wchar_t));
155 | if (p == NULL)
156 | {
157 | free (wmessage);
158 | fputws_unlocked (L"out of memory\n", stderr);
159 | return;
160 | }
161 | wmessage = p;
162 | use_malloc = true;
163 | }
164 |
165 | memset (&st, '\0', sizeof (st));
166 | tmp = message;
167 |
168 | res = mbsrtowcs (wmessage, &tmp, len, &st);
169 | if (res != len)
170 | break;
171 |
172 | if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
173 | {
174 | /* This really should not happen if everything is fine. */
175 | res = (size_t) -1;
176 | break;
177 | }
178 |
179 | len *= 2;
180 | }
181 |
182 | if (res == (size_t) -1)
183 | {
184 | /* The string cannot be converted. */
185 | if (use_malloc)
186 | {
187 | free (wmessage);
188 | use_malloc = false;
189 | }
190 | wmessage = (wchar_t *) L"???";
191 | }
192 |
193 | __vfwprintf (stderr, wmessage, args);
194 |
195 | if (use_malloc)
196 | free (wmessage);
197 | }
198 | else
199 | #endif
200 | vfprintf (stderr, message, args);
201 |
202 | ++error_message_count;
203 | if (errnum)
204 | print_errno_message (errnum);
205 | #if _LIBC
206 | __fxprintf (NULL, "\n");
207 | #else
208 | putc ('\n', stderr);
209 | #endif
210 | fflush (stderr);
211 | if (status)
212 | exit (status);
213 | }
214 |
215 |
216 | /* Print the program name and error message MESSAGE, which is a printf-style
217 | format string with optional args.
218 | If ERRNUM is nonzero, print its corresponding system error message.
219 | Exit with status STATUS if it is nonzero. */
220 | void
221 | error (int status, int errnum, const char *message, ...)
222 | {
223 | va_list args;
224 |
225 | #if defined _LIBC && defined __libc_ptf_call
226 | /* We do not want this call to be cut short by a thread
227 | cancellation. Therefore disable cancellation for now. */
228 | int state = PTHREAD_CANCEL_ENABLE;
229 | __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
230 | 0);
231 | #endif
232 |
233 | flush_stdout ();
234 | #ifdef _LIBC
235 | _IO_flockfile (stderr);
236 | #endif
237 | if (error_print_progname)
238 | (*error_print_progname) ();
239 | else
240 | {
241 | #if _LIBC
242 | __fxprintf (NULL, "%s: ", program_name);
243 | #else
244 | fprintf (stderr, "%s: ", program_name);
245 | #endif
246 | }
247 |
248 | va_start (args, message);
249 | error_tail (status, errnum, message, args);
250 | va_end (args);
251 | }
252 |
253 | /* Sometimes we want to have at most one error per line. This
254 | variable controls whether this mode is selected or not. */
255 | int error_one_per_line;
256 |
257 | void
258 | error_at_line (int status, int errnum, const char *file_name,
259 | unsigned int line_number, const char *message, ...)
260 | {
261 | va_list args;
262 |
263 | if (error_one_per_line)
264 | {
265 | static const char *old_file_name;
266 | static unsigned int old_line_number;
267 |
268 | if (old_line_number == line_number
269 | && (file_name == old_file_name
270 | || (old_file_name != NULL
271 | && file_name != NULL
272 | && strcmp (old_file_name, file_name) == 0)))
273 |
274 | /* Simply return and print nothing. */
275 | return;
276 |
277 | old_file_name = file_name;
278 | old_line_number = line_number;
279 | }
280 |
281 | #if defined _LIBC && defined __libc_ptf_call
282 | /* We do not want this call to be cut short by a thread
283 | cancellation. Therefore disable cancellation for now. */
284 | int state = PTHREAD_CANCEL_ENABLE;
285 | __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
286 | 0);
287 | #endif
288 |
289 | flush_stdout ();
290 | #ifdef _LIBC
291 | _IO_flockfile (stderr);
292 | #endif
293 | if (error_print_progname)
294 | (*error_print_progname) ();
295 | else
296 | {
297 | #if _LIBC
298 | __fxprintf (NULL, "%s:", program_name);
299 | #else
300 | fprintf (stderr, "%s:", program_name);
301 | #endif
302 | }
303 |
304 | #if _LIBC
305 | __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
306 | file_name, line_number);
307 | #else
308 | fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
309 | file_name, line_number);
310 | #endif
311 |
312 | va_start (args, message);
313 | error_tail (status, errnum, message, args);
314 | va_end (args);
315 |
316 | #ifdef _LIBC
317 | _IO_funlockfile (stderr);
318 | # ifdef __libc_ptf_call
319 | __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
320 | # endif
321 | #endif
322 | }
323 |
324 | #ifdef _LIBC
325 | /* Make the weak alias. */
326 | # undef error
327 | # undef error_at_line
328 | weak_alias (__error, error)
329 | weak_alias (__error_at_line, error_at_line)
330 | #endif
331 |
--------------------------------------------------------------------------------
/lib/error.h:
--------------------------------------------------------------------------------
1 | /* Declaration for error-reporting function
2 | Copyright (C) 1995-1997, 2003, 2006, 2008-2018 Free Software Foundation,
3 | Inc.
4 | This file is part of the GNU C Library.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see . */
18 | /* Ported to FreeDOS by LiquidFox1776 */
19 |
20 | #ifndef _ERROR_H
21 | #define _ERROR_H 1
22 | #include "getprogn.h"
23 | #define program_name getprogname ()
24 |
25 | /* Print a message with 'fprintf (stderr, FORMAT, ...)';
26 | if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
27 | If STATUS is nonzero, terminate the program with 'exit (STATUS)'. */
28 |
29 | extern void error (int __status, int __errnum, const char *__format, ...);
30 |
31 |
32 | extern void error_at_line (int __status, int __errnum, const char *__fname,
33 | unsigned int __lineno, const char *__format, ...);
34 |
35 |
36 | /* If NULL, error will flush stdout, then print on stderr the program
37 | name, a colon and a space. Otherwise, error will call this
38 | function without parameters instead. */
39 | extern void (*error_print_progname) (void);
40 |
41 | /* This variable is incremented each time 'error' is called. */
42 | extern unsigned int error_message_count;
43 |
44 | /* Sometimes we want to have at most one error per line. This
45 | variable controls whether this mode is selected or not. */
46 | extern int error_one_per_line;
47 |
48 |
49 | #endif /* error.h */
50 |
--------------------------------------------------------------------------------
/lib/exitfail.c:
--------------------------------------------------------------------------------
1 | /* Failure exit status
2 |
3 | Copyright (C) 2002-2003, 2005-2007, 2009-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 | /* Ported to FreeDOS by LiquidFox1776 */
18 |
19 | #include "exitfail.h"
20 |
21 | #include
22 |
23 | int volatile exit_failure = EXIT_FAILURE;
24 |
--------------------------------------------------------------------------------
/lib/exitfail.h:
--------------------------------------------------------------------------------
1 | /* Failure exit status
2 |
3 | Copyright (C) 2002, 2009-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 | /* Ported to FreeDOS by LiquidFox1776 */
18 |
19 | extern int volatile exit_failure;
20 |
--------------------------------------------------------------------------------
/lib/fcntl.c:
--------------------------------------------------------------------------------
1 | /* Partial implementation of some file descriptor control.
2 |
3 | Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by LiquidFox1776*/
19 |
20 | #include "fcntl.h"
21 | #include
22 | #include "errno.h"
23 |
24 | int dupfd(int original_handle, int starting_handle); // duplicates a file handle
25 | int getfl(int handle); // Get file access mode
26 |
27 | int
28 | fcntl(int fildes, int cmd, ...){
29 | int ret;
30 |
31 | va_list argument_pointer;
32 | va_start(argument_pointer, cmd); //Initialize the argument list
33 |
34 | switch(cmd){
35 | case F_DUPFD :
36 | ret = dupfd(fildes, va_arg (argument_pointer, int)); // a_arg (argument_pinter, int) should point to a int containing what could be a new handle
37 | break;
38 | case F_GETFL :
39 | ret = getfl(fildes);
40 | break;
41 | default: //Invalid Command(or not yet implemented)
42 | errno = EINVAL;
43 | ret = FCNTL_ERROR;
44 | }
45 | va_end(argument_pointer); // Cleanup
46 | return ret;
47 | }
48 |
49 | int
50 | getfl(int handle){
51 |
52 | struct stat status_buffer;
53 | int file_access_mode;
54 | int tmp_ret;
55 |
56 | tmp_ret = fstat(handle, &status_buffer);
57 | if( tmp_ret == FSTAT_ERROR ){//An error has occurred
58 | return FCNTL_ERROR;
59 | }
60 |
61 | //get the file access mode
62 | switch(status_buffer.st_mode){
63 | case S_IRWXU : //Read and Write Access
64 | file_access_mode |= O_RDWR;
65 | break;
66 | case S_IRUSR : //Read Access
67 | file_access_mode |= O_RDONLY;
68 | break;
69 | case S_IWUSR : // Write Access
70 | file_access_mode |= O_WRONLY;
71 | break;
72 | default :
73 | break;
74 | }
75 |
76 | return file_access_mode;
77 | }
78 |
79 | int
80 | dupfd(int original_handle, int starting_handle){
81 | int tmp_ret;// A temporary return value
82 | struct stat status_buffer;//Stores the stat structure which contains information about a file handle
83 | unsigned int d_handle;//Stores the next available handle that can(or cant) be duplicated
84 |
85 | if(starting_handle < MINIMUM_FILE_HANDLE){
86 | errno = EINVAL; //File handles cant be negative
87 | return FCNTL_ERROR;
88 | }
89 |
90 | for(d_handle = starting_handle; d_handle <= MAXIMUM_FILE_HANDLE; ++d_handle){
91 | tmp_ret = fstat( d_handle, &status_buffer);
92 | if( tmp_ret == FSTAT_ERROR ){//An error has occurred which means that the handle does not exist(free to use) or it is out of range(should not occur)
93 | if( dup2(original_handle, d_handle) == 0){//No errors occurred
94 | errno = EZERO;
95 | return d_handle;
96 | }
97 | else {//An error has occurred
98 | errno = EINVAL;
99 | return FCNTL_ERROR;
100 | }
101 | }
102 | }
103 | //If we get to here we are our at the MAXIMUM_FILE_HANDLE limit and cannot go beyond that point
104 | errno = EMFILE;
105 | return FCNTL_ERROR;
106 | }
107 |
--------------------------------------------------------------------------------
/lib/fcntl.h:
--------------------------------------------------------------------------------
1 | /* Partial Implementation of
2 |
3 | Copyright (C) 2006-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by LiquidFox1776 */
19 |
20 | //TODO Not all constants or structs are currently in use but are included in the hopes that they will one day be implemented
21 |
22 |
23 | #ifndef __FCNTL_H
24 | #define __FCNTL_H 1
25 |
26 | #include
27 | #include
28 |
29 | //*******************************
30 | //We just need some constants from fcntl and not the functions it provides
31 | //
32 | #define open DO_NOT_USE_open
33 | #define sopen DO_NOT_USE_sopen
34 | #define _wopen DO_NOT_USE_wopen
35 | #define _wsopen DO_NOT_USE_wsopen
36 | #define _wcreat DO_NOT_USE_wcreat
37 |
38 | #include
39 |
40 | #undef open
41 | #undef sopen
42 | #undef _wopen
43 | #undef _wsopen
44 | #undef _wcreat
45 | //************************************************************************
46 |
47 | #include
48 |
49 | #define AT_FDCWD -30000
50 | #define F_DUPFD 0 // Return a new file descriptor which shall be the lowest numbered available
51 | #define F_GETFD 1 //Get the file descriptor flags defined in that are associated with the file descriptor fildes.
52 | #define F_SETFD 2 //Set the file descriptor flags defined in , that are associated with fildes, to the third argument, arg, taken as type int
53 | #define F_GETFL 3 //Get the file status flags and file access modes, defined in , for the file description associated with fildes
54 | #define F_SETFL 4 //Set the file status flags, defined in , for the file description associated with fildes from the corresponding bits in the third argument, arg, taken as type int
55 | #define FCNTL_ERROR -1 //The value FCNTL should return if an error occurs
56 | #define FSTAT_ERROR -1 //Fstat Error Value
57 | #define O_ACCMODE 0x000F //Mask used to get the file access mode
58 | #define MINIMUM_FILE_HANDLE 0
59 | #define MAXIMUM_FILE_HANDLE 65535 //Maximum file handle for FreeDOS
60 |
61 | struct flock{
62 | short l_type; //Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.
63 | short l_whence; //Flag for starting offset.
64 | off_t l_start; // Relative offset in bytes.
65 | off_t l_len; //Size; if 0 then until EOF.
66 | pid_t l_pid; //Process ID of the process holding the lock; returned with F_GETLK.
67 | };
68 |
69 | //extern int creat(const char *, mode_t); defined in
70 | //extern int open(const char *, int, ...); defined in
71 |
72 | extern int fcntl(int fildes, int cmd, ...);
73 |
74 |
75 | //TODO Optional Functions
76 | //int posix_fadvise(int, off_t, off_t, int);
77 | //int posix_fallocate(int, off_t, off_t);
78 |
79 | #endif // __FCNTL_H
80 |
81 |
82 |
--------------------------------------------------------------------------------
/lib/getopt.h:
--------------------------------------------------------------------------------
1 | /* Declarations for getopt and getopt_long
2 | Copyright (C) 1989-2018 Free Software Foundation, Inc.
3 | This file is part of the GNU C Library.
4 | Unlike the bulk of the getopt implementation, this file is NOT part
5 | of gnulib; gnulib also has a getopt.h but it is different.
6 | The GNU C Library is free software; you can redistribute it and/or
7 | modify it under the terms of the GNU Lesser General Public
8 | License as published by the Free Software Foundation; either
9 | version 2.1 of the License, or (at your option) any later version.
10 | The GNU C Library is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 | Lesser General Public License for more details.
14 | You should have received a copy of the GNU Lesser General Public
15 | License along with the GNU C Library; if not, see
16 | . */
17 |
18 | #define __END_DECLS
19 | #ifndef _GETOPT_H
20 | #define _GETOPT_H 1
21 |
22 | /* The type of the 'argv' argument to getopt_long and getopt_long_only
23 | is properly 'char **', since both functions may write to the array
24 | (in order to move all the options to the beginning). However, for
25 | compatibility with old versions of LSB, glibc has to use 'char *const *'
26 | instead. */
27 | #ifndef __getopt_argv_const
28 | #define __getopt_argv_const const
29 | #endif
30 |
31 | /* For communication from 'getopt' to the caller.
32 | When 'getopt' finds an option that takes an argument,
33 | the argument value is returned here.
34 | Also, when 'ordering' is RETURN_IN_ORDER,
35 | each non-option ARGV-element is returned here. */
36 |
37 | extern char *optarg;
38 |
39 | /* Index in ARGV of the next element to be scanned.
40 | This is used for communication to and from the caller
41 | and for communication between successive calls to 'getopt'.
42 | On entry to 'getopt', zero means this is the first call; initialize.
43 | When 'getopt' returns -1, this is the index of the first of the
44 | non-option elements that the caller should itself scan.
45 | Otherwise, 'optind' communicates from one call to the next
46 | how much of ARGV has been scanned so far. */
47 |
48 | extern int optind;
49 |
50 | /* Callers store zero here to inhibit the error message 'getopt' prints
51 | for unrecognized options. */
52 |
53 | extern int opterr;
54 |
55 | /* Set to an option character which was unrecognized. */
56 |
57 | extern int optopt;
58 |
59 | /* Get definitions and prototypes for functions to process the
60 | arguments in ARGV (ARGC of them, minus the program name) for
61 | options given in OPTS.
62 | Return the option character from OPTS just read. Return -1 when
63 | there are no more options. For unrecognized options, or options
64 | missing arguments, 'optopt' is set to the option letter, and '?' is
65 | returned.
66 | The OPTS string is a list of characters which are recognized option
67 | letters, optionally followed by colons, specifying that that letter
68 | takes an argument, to be placed in 'optarg'.
69 | If a letter in OPTS is followed by two colons, its argument is
70 | optional. This behavior is specific to the GNU 'getopt'.
71 | The argument '--' causes premature termination of argument
72 | scanning, explicitly telling 'getopt' that there are no more
73 | options.
74 | If OPTS begins with '-', then non-option arguments are treated as
75 | arguments to the option '\1'. This behavior is specific to the GNU
76 | 'getopt'. If OPTS begins with '+', or POSIXLY_CORRECT is set in
77 | the environment, then do not permute arguments.
78 | For standards compliance, the 'argv' argument has the type
79 | char *const *, but this is inaccurate; if argument permutation is
80 | enabled, the argv array (not the strings it points to) must be
81 | writable. */
82 |
83 | extern int getopt (int ___argc, char *const *___argv, const char *__shortopts);
84 |
85 | /* Describe the long-named options requested by the application.
86 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
87 | of 'struct option' terminated by an element containing a name which is
88 | zero.
89 | The field 'has_arg' is:
90 | no_argument (or 0) if the option does not take an argument,
91 | required_argument (or 1) if the option requires an argument,
92 | optional_argument (or 2) if the option takes an optional argument.
93 | If the field 'flag' is not NULL, it points to a variable that is set
94 | to the value given in the field 'val' when the option is found, but
95 | left unchanged if the option is not found.
96 | To have a long-named option do something other than set an 'int' to
97 | a compiled-in constant, such as set a value from 'optarg', set the
98 | option's 'flag' field to zero and its 'val' field to a nonzero
99 | value (the equivalent single-letter option character, if there is
100 | one). For long options that have a zero 'flag' field, 'getopt'
101 | returns the contents of the 'val' field. */
102 |
103 | struct option
104 | {
105 | const char *name;
106 | /* has_arg can't be an enum because some compilers complain about
107 | type mismatches in all the code that assumes it is an int. */
108 | int has_arg;
109 | int *flag;
110 | int val;
111 | };
112 |
113 | /* Names for the values of the 'has_arg' field of 'struct option'. */
114 |
115 | #define no_argument 0
116 | #define required_argument 1
117 | #define optional_argument 2
118 |
119 | extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
120 |
121 | extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind);
122 |
123 | #endif /* getopt.h */
124 |
--------------------------------------------------------------------------------
/lib/getopt_i.h:
--------------------------------------------------------------------------------
1 | /* Internal declarations for getopt.
2 | Copyright (C) 1989-2018 Free Software Foundation, Inc.
3 | This file is part of the GNU C Library and is also part of gnulib.
4 | Patches to this file should be submitted to both projects.
5 | The GNU C Library is free software; you can redistribute it and/or
6 | modify it under the terms of the GNU Lesser General Public
7 | License as published by the Free Software Foundation; either
8 | version 2.1 of the License, or (at your option) any later version.
9 | The GNU C Library is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | Lesser General Public License for more details.
13 | You should have received a copy of the GNU Lesser General Public
14 | License along with the GNU C Library; if not, see
15 | . */
16 |
17 | #ifndef _GETOPT_INT_H
18 | #define _GETOPT_INT_H 1
19 |
20 | #include ".\getopt.h"
21 |
22 | extern int _getopt_internal(int ___argc, char **___argv, const char *__shortopts, const struct option *__longopts, int *__longind, int __long_only, int __posixly_correct);
23 |
24 | /* Reentrant versions which can handle parsing multiple argument
25 | vectors at the same time. */
26 |
27 | /* Describe how to deal with options that follow non-option ARGV-elements.
28 | REQUIRE_ORDER means don't recognize them as options; stop option
29 | processing when the first non-option is seen. This is what POSIX
30 | specifies should happen.
31 | PERMUTE means permute the contents of ARGV as we scan, so that
32 | eventually all the non-options are at the end. This allows options
33 | to be given in any order, even with programs that were not written
34 | to expect this.
35 | RETURN_IN_ORDER is an option available to programs that were
36 | written to expect options and other ARGV-elements in any order
37 | and that care about the ordering of the two. We describe each
38 | non-option ARGV-element as if it were the argument of an option
39 | with character code 1.
40 | The special argument '--' forces an end of option-scanning regardless
41 | of the value of 'ordering'. In the case of RETURN_IN_ORDER, only
42 | '--' can cause 'getopt' to return -1 with 'optind' != ARGC. */
43 |
44 | enum __ord
45 | {
46 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
47 | };
48 |
49 | /* Data type for reentrant functions. */
50 | struct _getopt_data
51 | {
52 | /* These have exactly the same meaning as the corresponding global
53 | variables, except that they are used for the reentrant
54 | versions of getopt. */
55 | int optind;
56 | int opterr;
57 | int optopt;
58 | char *optarg;
59 |
60 | /* Internal members. */
61 |
62 | /* True if the internal members have been initialized. */
63 | int __initialized;
64 |
65 | /* The next char to be scanned in the option-element
66 | in which the last option character we returned was found.
67 | This allows us to pick up the scan where we left off.
68 | If this is zero, or a null string, it means resume the scan
69 | by advancing to the next ARGV-element. */
70 | char *__nextchar;
71 |
72 | /* See __ord above. */
73 | enum __ord __ordering;
74 |
75 | /* Handle permutation of arguments. */
76 |
77 | /* Describe the part of ARGV that contains non-options that have
78 | been skipped. 'first_nonopt' is the index in ARGV of the first
79 | of them; 'last_nonopt' is the index after the last of them. */
80 |
81 | int __first_nonopt;
82 | int __last_nonopt;
83 | };
84 |
85 | /* The initializer is necessary to set OPTIND and OPTERR to their
86 | default values and to clear the initialization flag. */
87 | #define _GETOPT_DATA_INITIALIZER { 1, 1 }
88 |
89 | extern int _getopt_internal_r(int ___argc, char **___argv, const char *__shortopts, const struct option *__longopts, int *__longind, int __long_only, struct _getopt_data *__data, int __posixly_correct);
90 |
91 | extern int _getopt_long_r(int ___argc, char **___argv, const char *__shortopts, const struct option *__longopts, int *__longind, struct _getopt_data *__data);
92 |
93 | extern int _getopt_long_only_r(int ___argc, char **___argv, const char *__shortopts, const struct option *__longopts, int *__longind, struct _getopt_data *__data);
94 |
95 | #endif /* getopt_int.h */
96 |
--------------------------------------------------------------------------------
/lib/getprogn.c:
--------------------------------------------------------------------------------
1 | /* Program name management.
2 | Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation; either version 2.1 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with this program. If not, see . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | /* Specification. */
19 | #include "getprogn.h"
20 | #include "errno.h" /* get program_invocation_name declaration */
21 | #include /* get __argv declaration */
22 | #include /* _cmdname function */
23 | #include
24 | #include
25 |
26 | char progname_buffer[PATH_MAX] = {0};
27 |
28 | #ifndef HAVE_GETPROGNAME
29 | #define HAVE_GETPROGNAME 1
30 | const char *
31 | getprogname (void)
32 | {
33 | return last_component(_cmdname(progname_buffer));
34 | }
35 |
36 | #endif
37 |
38 | /*
39 | * Hey Emacs!
40 | * Local Variables:
41 | * coding: utf-8
42 | * End:
43 | */
44 |
--------------------------------------------------------------------------------
/lib/getprogn.h:
--------------------------------------------------------------------------------
1 | /* Program name management.
2 | Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation; either version 2.1 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with this program. If not, see . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #ifndef _GL_GETPROGNAME_H
19 | #define _GL_GETPROGNAME_H 1
20 |
21 | #include
22 |
23 | /* Return the base name of the executing program. */
24 |
25 | #ifndef HAVE_GETPROGNAME
26 | extern const char *getprogname (void);
27 | #endif
28 | #endif
29 |
--------------------------------------------------------------------------------
/lib/ignorval.h:
--------------------------------------------------------------------------------
1 | /* ignore a function return without a compiler warning. -*- coding: utf-8 -*-
2 |
3 | Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by Jim Meyering, Eric Blake and Pádraig Brady. */
19 | /* Ported to FreeDOS by LiquidFox1776 */
20 |
21 | /* Use "ignore_value" to avoid a warning when using a function declared with
22 | gcc's warn_unused_result attribute, but for which you really do want to
23 | ignore the result. Traditionally, people have used a "(void)" cast to
24 | indicate that a function's return value is deliberately unused. However,
25 | if the function is declared with __attribute__((warn_unused_result)),
26 | gcc issues a warning even with the cast.
27 |
28 | Caution: most of the time, you really should heed gcc's warning, and
29 | check the return value. However, in those exceptional cases in which
30 | you're sure you know what you're doing, use this function.
31 |
32 | For the record, here's one of the ignorable warnings:
33 | "copy.c:233: warning: ignoring return value of 'fchown',
34 | declared with attribute warn_unused_result". */
35 |
36 | #ifndef _GL_IGNORE_VALUE_H
37 | #define _GL_IGNORE_VALUE_H
38 |
39 | /* Normally casting an expression to void discards its value, but GCC
40 | versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
41 | which may cause unwanted diagnostics in that case. Use __typeof__
42 | and __extension__ to work around the problem, if the workaround is
43 | known to be needed. */
44 | #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
45 | # define ignore_value(x) \
46 | (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
47 | #else
48 | # define ignore_value(x) ((void) (x))
49 | #endif
50 |
51 | #endif
52 |
--------------------------------------------------------------------------------
/lib/intprops.h:
--------------------------------------------------------------------------------
1 | /* intprops.h -- properties of integer types
2 |
3 | Copyright (C) 2001-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify it
6 | under the terms of the GNU Lesser General Public License as published
7 | by the Free Software Foundation; either version 2.1 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU Lesser General Public License for more details.
14 |
15 | You should have received a copy of the GNU Lesser General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by Paul Eggert. */
19 | /* Ported to FreeDOS by LiquidFox1776 */
20 |
21 | #ifndef _GL_INTPROPS_H
22 | #define _GL_INTPROPS_H
23 |
24 | #include
25 |
26 | /* Return a value with the common real type of E and V and the value of V. */
27 | #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
28 |
29 | /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
30 | . */
31 | #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
32 |
33 | /* The extra casts in the following macros work around compiler bugs,
34 | e.g., in Cray C 5.0.3.0. */
35 |
36 | /* True if the arithmetic type T is an integer type. bool counts as
37 | an integer. */
38 | #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
39 |
40 | /* True if the real type T is signed. */
41 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
42 |
43 | /* Return 1 if the real expression E, after promotion, has a
44 | signed or floating type. */
45 | #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
46 |
47 |
48 | /* Minimum and maximum values for integer types and expressions. */
49 |
50 | /* The width in bits of the integer type or expression T.
51 | Padding bits are not supported; this is checked at compile-time below. */
52 | #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
53 |
54 | /* The maximum and minimum values for the integer type T. */
55 | #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
56 | #define TYPE_MAXIMUM(t) \
57 | ((t) (! TYPE_SIGNED (t) \
58 | ? (t) -1 \
59 | : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
60 |
61 | /* The maximum and minimum values for the type of the expression E,
62 | after integer promotion. E should not have side effects. */
63 | #define _GL_INT_MINIMUM(e) \
64 | (EXPR_SIGNED (e) \
65 | ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
66 | : _GL_INT_CONVERT (e, 0))
67 | #define _GL_INT_MAXIMUM(e) \
68 | (EXPR_SIGNED (e) \
69 | ? _GL_SIGNED_INT_MAXIMUM (e) \
70 | : _GL_INT_NEGATE_CONVERT (e, 1))
71 | #define _GL_SIGNED_INT_MAXIMUM(e) \
72 | (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
73 |
74 | /* Work around OpenVMS incompatibility with C99. */
75 | #if !defined LLONG_MAX && defined __INT64_MAX
76 | # define LLONG_MAX __INT64_MAX
77 | # define LLONG_MIN __INT64_MIN
78 | #endif
79 |
80 | /* This include file assumes that signed types are two's complement without
81 | padding bits; the above macros have undefined behavior otherwise.
82 | If this is a problem for you, please let us know how to fix it for your host.
83 | This assumption is tested by the intprops-tests module. */
84 |
85 | /* Does the __typeof__ keyword work? This could be done by
86 | 'configure', but for now it's easier to do it by hand. */
87 | #if (2 <= __GNUC__ \
88 | || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
89 | || (0x5110 <= __SUNPRO_C && !__STDC__))
90 | # define _GL_HAVE___TYPEOF__ 1
91 | #else
92 | # define _GL_HAVE___TYPEOF__ 0
93 | #endif
94 |
95 | /* Return 1 if the integer type or expression T might be signed. Return 0
96 | if it is definitely unsigned. This macro does not evaluate its argument,
97 | and expands to an integer constant expression. */
98 | #if _GL_HAVE___TYPEOF__
99 | # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
100 | #else
101 | # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
102 | #endif
103 |
104 | /* Bound on length of the string representing an unsigned integer
105 | value representable in B bits. log10 (2.0) < 146/485. The
106 | smallest value of B where this bound is not tight is 2621. */
107 | #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
108 |
109 | /* Bound on length of the string representing an integer type or expression T.
110 | Subtract 1 for the sign bit if T is signed, and then add 1 more for
111 | a minus sign if needed.
112 |
113 | Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
114 | signed, this macro may overestimate the true bound by one byte when
115 | applied to unsigned types of size 2, 4, 16, ... bytes. */
116 | #define INT_STRLEN_BOUND(t) \
117 | (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
118 | + _GL_SIGNED_TYPE_OR_EXPR (t))
119 |
120 | /* Bound on buffer size needed to represent an integer type or expression T,
121 | including the terminating null. */
122 | #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
123 |
124 |
125 | /* Range overflow checks.
126 |
127 | The INT__RANGE_OVERFLOW macros return 1 if the corresponding C
128 | operators might not yield numerically correct answers due to
129 | arithmetic overflow. They do not rely on undefined or
130 | implementation-defined behavior. Their implementations are simple
131 | and straightforward, but they are a bit harder to use than the
132 | INT__OVERFLOW macros described below.
133 |
134 | Example usage:
135 |
136 | long int i = ...;
137 | long int j = ...;
138 | if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
139 | printf ("multiply would overflow");
140 | else
141 | printf ("product is %ld", i * j);
142 |
143 | Restrictions on *_RANGE_OVERFLOW macros:
144 |
145 | These macros do not check for all possible numerical problems or
146 | undefined or unspecified behavior: they do not check for division
147 | by zero, for bad shift counts, or for shifting negative numbers.
148 |
149 | These macros may evaluate their arguments zero or multiple times,
150 | so the arguments should not have side effects. The arithmetic
151 | arguments (including the MIN and MAX arguments) must be of the same
152 | integer type after the usual arithmetic conversions, and the type
153 | must have minimum value MIN and maximum MAX. Unsigned types should
154 | use a zero MIN of the proper type.
155 |
156 | These macros are tuned for constant MIN and MAX. For commutative
157 | operations such as A + B, they are also tuned for constant B. */
158 |
159 | /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
160 | See above for restrictions. */
161 | #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
162 | ((b) < 0 \
163 | ? (a) < (min) - (b) \
164 | : (max) - (b) < (a))
165 |
166 | /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
167 | See above for restrictions. */
168 | #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
169 | ((b) < 0 \
170 | ? (max) + (b) < (a) \
171 | : (a) < (min) + (b))
172 |
173 | /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
174 | See above for restrictions. */
175 | #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
176 | ((min) < 0 \
177 | ? (a) < - (max) \
178 | : 0 < (a))
179 |
180 | /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
181 | See above for restrictions. Avoid && and || as they tickle
182 | bugs in Sun C 5.11 2010/08/13 and other compilers; see
183 | . */
184 | #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
185 | ((b) < 0 \
186 | ? ((a) < 0 \
187 | ? (a) < (max) / (b) \
188 | : (b) == -1 \
189 | ? 0 \
190 | : (min) / (b) < (a)) \
191 | : (b) == 0 \
192 | ? 0 \
193 | : ((a) < 0 \
194 | ? (a) < (min) / (b) \
195 | : (max) / (b) < (a)))
196 |
197 | /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
198 | See above for restrictions. Do not check for division by zero. */
199 | #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
200 | ((min) < 0 && (b) == -1 && (a) < - (max))
201 |
202 | /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
203 | See above for restrictions. Do not check for division by zero.
204 | Mathematically, % should never overflow, but on x86-like hosts
205 | INT_MIN % -1 traps, and the C standard permits this, so treat this
206 | as an overflow too. */
207 | #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
208 | INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
209 |
210 | /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
211 | See above for restrictions. Here, MIN and MAX are for A only, and B need
212 | not be of the same type as the other arguments. The C standard says that
213 | behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
214 | A is negative then A << B has undefined behavior and A >> B has
215 | implementation-defined behavior, but do not check these other
216 | restrictions. */
217 | #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
218 | ((a) < 0 \
219 | ? (a) < (min) >> (b) \
220 | : (max) >> (b) < (a))
221 |
222 | /* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
223 | #if 5 <= __GNUC__ && !defined __ICC
224 | # define _GL_HAS_BUILTIN_OVERFLOW 1
225 | #else
226 | # define _GL_HAS_BUILTIN_OVERFLOW 0
227 | #endif
228 |
229 | /* True if __builtin_add_overflow_p (A, B, C) works. */
230 | #define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
231 |
232 | /* The _GL*_OVERFLOW macros have the same restrictions as the
233 | *_RANGE_OVERFLOW macros, except that they do not assume that operands
234 | (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
235 | that the result (e.g., A + B) has that type. */
236 | #if _GL_HAS_BUILTIN_OVERFLOW_P
237 | # define _GL_ADD_OVERFLOW(a, b, min, max) \
238 | __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
239 | # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
240 | __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
241 | # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
242 | __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
243 | #else
244 | # define _GL_ADD_OVERFLOW(a, b, min, max) \
245 | ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
246 | : (a) < 0 ? (b) <= (a) + (b) \
247 | : (b) < 0 ? (a) <= (a) + (b) \
248 | : (a) + (b) < (b))
249 | # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
250 | ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
251 | : (a) < 0 ? 1 \
252 | : (b) < 0 ? (a) - (b) <= (a) \
253 | : (a) < (b))
254 | # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
255 | (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
256 | || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
257 | #endif
258 | #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
259 | ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
260 | : (a) < 0 ? (b) <= (a) + (b) - 1 \
261 | : (b) < 0 && (a) + (b) <= (a))
262 | #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
263 | ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
264 | : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
265 | : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
266 |
267 | /* Return a nonzero value if A is a mathematical multiple of B, where
268 | A is unsigned, B is negative, and MAX is the maximum value of A's
269 | type. A's type must be the same as (A % B)'s type. Normally (A %
270 | -B == 0) suffices, but things get tricky if -B would overflow. */
271 | #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
272 | (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
273 | ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
274 | ? (a) \
275 | : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
276 | : (a) % - (b)) \
277 | == 0)
278 |
279 | /* Check for integer overflow, and report low order bits of answer.
280 |
281 | The INT__OVERFLOW macros return 1 if the corresponding C operators
282 | might not yield numerically correct answers due to arithmetic overflow.
283 | The INT__WRAPV macros also store the low-order bits of the answer.
284 | These macros work correctly on all known practical hosts, and do not rely
285 | on undefined behavior due to signed arithmetic overflow.
286 |
287 | Example usage, assuming A and B are long int:
288 |
289 | if (INT_MULTIPLY_OVERFLOW (a, b))
290 | printf ("result would overflow\n");
291 | else
292 | printf ("result is %ld (no overflow)\n", a * b);
293 |
294 | Example usage with WRAPV flavor:
295 |
296 | long int result;
297 | bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
298 | printf ("result is %ld (%s)\n", result,
299 | overflow ? "after overflow" : "no overflow");
300 |
301 | Restrictions on these macros:
302 |
303 | These macros do not check for all possible numerical problems or
304 | undefined or unspecified behavior: they do not check for division
305 | by zero, for bad shift counts, or for shifting negative numbers.
306 |
307 | These macros may evaluate their arguments zero or multiple times, so the
308 | arguments should not have side effects.
309 |
310 | The WRAPV macros are not constant expressions. They support only
311 | +, binary -, and *. The result type must be signed.
312 |
313 | These macros are tuned for their last argument being a constant.
314 |
315 | Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
316 | A % B, and A << B would overflow, respectively. */
317 |
318 | #define INT_ADD_OVERFLOW(a, b) \
319 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
320 | #define INT_SUBTRACT_OVERFLOW(a, b) \
321 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
322 | #if _GL_HAS_BUILTIN_OVERFLOW_P
323 | # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
324 | #else
325 | # define INT_NEGATE_OVERFLOW(a) \
326 | INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
327 | #endif
328 | #define INT_MULTIPLY_OVERFLOW(a, b) \
329 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
330 | #define INT_DIVIDE_OVERFLOW(a, b) \
331 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
332 | #define INT_REMAINDER_OVERFLOW(a, b) \
333 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
334 | #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
335 | INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
336 | _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
337 |
338 | /* Return 1 if the expression A B would overflow,
339 | where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
340 | assuming MIN and MAX are the minimum and maximum for the result type.
341 | Arguments should be free of side effects. */
342 | #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
343 | op_result_overflow (a, b, \
344 | _GL_INT_MINIMUM (0 * (b) + (a)), \
345 | _GL_INT_MAXIMUM (0 * (b) + (a)))
346 |
347 | /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
348 | Return 1 if the result overflows. See above for restrictions. */
349 | #define INT_ADD_WRAPV(a, b, r) \
350 | _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
351 | #define INT_SUBTRACT_WRAPV(a, b, r) \
352 | _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
353 | #define INT_MULTIPLY_WRAPV(a, b, r) \
354 | _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
355 |
356 | /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
357 | https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
358 | https://llvm.org/bugs/show_bug.cgi?id=25390
359 | For now, assume all versions of GCC-like compilers generate bogus
360 | warnings for _Generic. This matters only for older compilers that
361 | lack __builtin_add_overflow. */
362 | #if __GNUC__
363 | # define _GL__GENERIC_BOGUS 1
364 | #else
365 | # define _GL__GENERIC_BOGUS 0
366 | #endif
367 |
368 | /* Store the low-order bits of A B into *R, where OP specifies
369 | the operation. BUILTIN is the builtin operation, and OVERFLOW the
370 | overflow predicate. Return 1 if the result overflows. See above
371 | for restrictions. */
372 | #if _GL_HAS_BUILTIN_OVERFLOW
373 | # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
374 | #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
375 | # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
376 | (_Generic \
377 | (*(r), \
378 | signed char: \
379 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
380 | signed char, SCHAR_MIN, SCHAR_MAX), \
381 | short int: \
382 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
383 | short int, SHRT_MIN, SHRT_MAX), \
384 | int: \
385 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
386 | int, INT_MIN, INT_MAX), \
387 | long int: \
388 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
389 | long int, LONG_MIN, LONG_MAX), \
390 | long long int: \
391 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
392 | long long int, LLONG_MIN, LLONG_MAX)))
393 | #else
394 | # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
395 | (sizeof *(r) == sizeof (signed char) \
396 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
397 | signed char, SCHAR_MIN, SCHAR_MAX) \
398 | : sizeof *(r) == sizeof (short int) \
399 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
400 | short int, SHRT_MIN, SHRT_MAX) \
401 | : sizeof *(r) == sizeof (int) \
402 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
403 | int, INT_MIN, INT_MAX) \
404 | : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
405 | # ifdef LLONG_MAX
406 | # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
407 | (sizeof *(r) == sizeof (long int) \
408 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
409 | long int, LONG_MIN, LONG_MAX) \
410 | : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
411 | long long int, LLONG_MIN, LLONG_MAX))
412 | # else
413 | # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
414 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
415 | long int, LONG_MIN, LONG_MAX)
416 | # endif
417 | #endif
418 |
419 | /* Store the low-order bits of A B into *R, where the operation
420 | is given by OP. Use the unsigned type UT for calculation to avoid
421 | overflow problems. *R's type is T, with extrema TMIN and TMAX.
422 | T must be a signed integer type. Return 1 if the result overflows. */
423 | #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
424 | (sizeof ((a) op (b)) < sizeof (t) \
425 | ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
426 | : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
427 | #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
428 | ((overflow (a, b) \
429 | || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
430 | || (tmax) < ((a) op (b))) \
431 | ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
432 | : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
433 |
434 | /* Return the low-order bits of A B, where the operation is given
435 | by OP. Use the unsigned type UT for calculation to avoid undefined
436 | behavior on signed integer overflow, and convert the result to type T.
437 | UT is at least as wide as T and is no narrower than unsigned int,
438 | T is two's complement, and there is no padding or trap representations.
439 | Assume that converting UT to T yields the low-order bits, as is
440 | done in all known two's-complement C compilers. E.g., see:
441 | https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
442 |
443 | According to the C standard, converting UT to T yields an
444 | implementation-defined result or signal for values outside T's
445 | range. However, code that works around this theoretical problem
446 | runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
447 | https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
448 | As the compiler bug is real, don't try to work around the
449 | theoretical problem. */
450 |
451 | #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
452 | ((t) ((ut) (a) op (ut) (b)))
453 |
454 | #endif /* _GL_INTPROPS_H */
455 |
--------------------------------------------------------------------------------
/lib/kitten/kitten.c:
--------------------------------------------------------------------------------
1 | /* Functions that emulate UNIX catgets */
2 |
3 | /* Copyright (C) 1999,2000,2001 Jim Hall */
4 | /* Kitten version 2003 by Tom Ehlert, heavily modified by Eric Auer 2003 */
5 | /* One Line Modification by LiquidFox1776 2018 */
6 |
7 | /*
8 | This library is free software; you can redistribute it and/or
9 | modify it under the terms of the GNU Lesser General Public
10 | License as published by the Free Software Foundation; either
11 | version 3 of the License, or (at your option) any later version.
12 |
13 | This library is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 | Lesser General Public License for more details.
17 |
18 | You should have received a copy of the GNU Lesser General Public
19 | License along with this library; if not, write to the Free Software
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 | */
22 |
23 | #ifndef NO_KITTEN
24 |
25 | #include /* sprintf */
26 | #ifndef _MICROC_
27 | #include /* getenv */
28 | #include /* strchr */
29 | #include
30 | #ifndef __PACIFIC__
31 | #include
32 | #else
33 | #define O_RDONLY 0
34 | #define O_TEXT 0
35 | #endif
36 | #else
37 | #include
38 | #include
39 | #define O_RDONLY READONLY
40 | #define O_TEXT 0
41 | #endif
42 | /* assert we are running in small model */
43 | /* else pointer below has to be done correctly */
44 | /* char verify_small_pointers[sizeof(void*) == 2 ? 1 : -1]; */
45 |
46 | #include "kitten.h"
47 |
48 | char catcontents[8192];
49 |
50 | struct catstring
51 | {
52 | char key1;
53 | char key2;
54 | char *text;
55 | };
56 |
57 | /* Micro-C does not support typedef */
58 | #define catstring_t struct catstring
59 |
60 | catstring_t catpoints[128];
61 |
62 |
63 | /* Local prototypes */
64 |
65 | int catread (char *catfile); /* Reads a catfile into the hash */
66 | char *processEscChars (char *line); /* Converts c escape sequences to chars */
67 |
68 | int get_char (int file); /* not meant for external use */
69 | /* external use would cause consistency problems if */
70 | /* value or related file of the file handle changes */
71 |
72 | int mystrtoul (char *src, int base, int size);
73 |
74 |
75 | /* Globals */
76 |
77 | nl_catd _kitten_catalog = 0; /* _kitten_catalog descriptor or 0 */
78 | char catfile[128]; /* full path to _kitten_catalog */
79 |
80 | char getlbuf[8192]; /* read buffer for better speed */
81 | char *getlp; /* current point in buffer */
82 | int getlrem = -1; /* remaining bytes in buffer */
83 | char lastcr = 0; /* for 2byte CR LF sequences */
84 |
85 |
86 | #ifndef _MICROC_
87 | #ifndef __DJGPP__
88 |
89 | /* DOS handle based file usage */
90 |
91 | int
92 | dos_open (char *filename, int mode)
93 | {
94 | union REGS r;
95 | struct SREGS s;
96 | #ifndef __WATCOMC__
97 | if (mode); /* mode ignored - readonly supported */
98 | #endif
99 | r.h.ah = 0x3d;
100 | r.h.al = 0; /* read mode only supoported now !! */
101 | r.x.dx = FP_OFF (filename);
102 | s.ds = FP_SEG (filename);
103 | intdosx (&r, &r, &s);
104 | return ((r.x.cflag) ? -1 : (int) r.x.ax);
105 | }
106 |
107 |
108 | int
109 | dos_read (int file, void *ptr, unsigned count)
110 | {
111 | union REGS r;
112 | struct SREGS s;
113 | r.h.ah = 0x3f;
114 | r.x.bx = file;
115 | r.x.cx = count;
116 | r.x.dx = FP_OFF (ptr);
117 | s.ds = FP_SEG (ptr);
118 | intdosx (&r, &r, &s);
119 | return ((r.x.cflag) ? 0 : r.x.ax);
120 | }
121 |
122 |
123 | int
124 | dos_write (int file, void *ptr, unsigned count)
125 | {
126 | union REGS r;
127 | struct SREGS s;
128 | r.h.ah = 0x40;
129 | r.x.bx = file;
130 | r.x.cx = count;
131 | r.x.dx = FP_OFF (ptr);
132 | s.ds = FP_SEG (ptr);
133 | intdosx (&r, &r, &s);
134 | return ((r.x.cflag) ? 0 : r.x.ax);
135 | }
136 |
137 |
138 | void
139 | dos_close (int file)
140 | {
141 | union REGS r;
142 | r.h.ah = 0x3e;
143 | r.x.bx = file;
144 | intdos (&r, &r);
145 | }
146 |
147 | #endif /*DJGPP*/
148 | #endif /*Micro-C */
149 | /* Functions */
150 | /**
151 | * On success, catgets() returns a pointer to an internal
152 | * buffer area containing the null-terminated message string.
153 | * On failure, catgets() returns the value 'message'.
154 | */
155 | char *
156 | kittengets (int setnum, int msgnum, char *message)
157 | {
158 | /* In Micro-C, variables must be defined at the start of the
159 | * function and may not be immediately assigned a value
160 | */
161 | #ifdef _MICROC_
162 | int i;
163 | i = 0;
164 | #else
165 | int i = 0;
166 | #endif
167 |
168 | while ((catpoints[i].key1 != setnum) || (catpoints[i].key2 != msgnum))
169 | {
170 | if ((catpoints[i].text == NULL) || (i > 127)) /* at EOF */
171 | return message;
172 | i++;
173 | }
174 |
175 | if (catpoints[i].text == NULL)
176 | return message;
177 | else
178 | return (catpoints[i].text);
179 | }
180 |
181 |
182 | /**
183 | * Initialize kitten for program (name).
184 | */
185 |
186 | nl_catd
187 | kittenopen (char *name)
188 | {
189 | /* catopen() returns a message _kitten_catalog descriptor *
190 | * of type nl_catd on success. On failure, it returns -1. */
191 |
192 | char catlang[3]; /* from LANG environment var. */
193 | char *nlsptr; /* ptr to NLSPATH */
194 | char *lang; /* ptr to LANG */
195 | int i;
196 | #ifdef _MICROC_
197 | char *tok;
198 | int toklen;
199 | #endif
200 |
201 | /* Open the _kitten_catalog file */
202 | /* The value of `_kitten_catalog' will be set based on catread */
203 |
204 | if (_kitten_catalog)
205 | { /* Already one open */
206 | write (1, "cat already open\r\n", strlen ("cat already open\r\n"));
207 | return (-1);
208 | }
209 |
210 | for (i = 0; i < 128; i++)
211 | catpoints[i].text = NULL;
212 |
213 | if (strchr (name, '\\'))
214 | {
215 | /* unusual case: 'name' is a filename */
216 | // write (1, "found \\\r\n", 9);
217 | _kitten_catalog = catread (name);
218 | if (_kitten_catalog)
219 | return (_kitten_catalog);
220 | }
221 |
222 | /* If the message _kitten_catalog file name does not contain a directory *
223 | * separator, then we need to try to locate the message _kitten_catalog. */
224 |
225 | /* We will need the value of LANG, and may need a 2-letter abbrev of
226 | LANG later on, so get it now. */
227 |
228 | lang = getenv ("LANG");
229 |
230 | if (lang == NULL)
231 | {
232 | /* printf("no lang= found\n"); *//* not fatal, though */
233 | /* Return failure - we won't be able to locate the cat file */
234 | return (-1);
235 | }
236 |
237 | if ((strlen (lang) < 2) || ((strlen (lang) > 2) && (lang[2] != '-')))
238 | {
239 | /* Return failure - we won't be able to locate the cat file */
240 | return (-1);
241 | }
242 |
243 | memcpy (catlang, lang, 2);
244 | /* we copy the full LANG value or the part before "-" if "-" found */
245 | catlang[2] = '\0';
246 |
247 | /* step through NLSPATH */
248 |
249 | nlsptr = getenv ("NLSPATH");
250 |
251 | if (nlsptr == NULL)
252 | {
253 | /* printf("no NLSPATH= found\n"); *//* not fatal either */
254 | /* Return failure - we won't be able to locate the cat file */
255 | return (-1);
256 | }
257 |
258 | catfile[0] = '\0';
259 |
260 | while (nlsptr && nlsptr[0])
261 | {
262 | #ifdef _MICROC_
263 | tok = strchr (nlsptr, ';');
264 | #else
265 | char *tok = strchr (nlsptr, ';');
266 | int toklen;
267 | #endif
268 |
269 | if (tok == NULL)
270 | toklen = strlen (nlsptr); /* last segment */
271 | else
272 | toklen = tok - nlsptr; /* segment terminated by ';' */
273 |
274 | /* catfile = malloc(toklen+1+strlen(name)+1+strlen(lang)+1); */
275 | /* Try to find the _kitten_catalog file in each path from NLSPATH */
276 |
277 | if ((toklen + 6 + strlen (name)) > sizeof (catfile))
278 | {
279 | write (1, "NLSPATH overflow\r\n", strlen ("NLSPATH overflow\r\n"));
280 | return 0; /* overflow in NLSPATH, should never happen */
281 | }
282 |
283 | /* Rule #1: %NLSPATH%\%LANG%\cat */
284 |
285 | memcpy (catfile, nlsptr, toklen);
286 | strcpy (catfile + toklen, "\\");
287 | strcat (catfile, catlang);
288 | strcat (catfile, "\\");
289 | strcat (catfile, name);
290 | _kitten_catalog = catread (catfile);
291 | if (_kitten_catalog)
292 | return (_kitten_catalog);
293 |
294 | /* Rule #2: %NLSPATH%\cat.%LANG% */
295 |
296 | /* memcpy(catfile, nlsptr, toklen); */
297 | strcpy (catfile + toklen, "\\");
298 | strcat (catfile, name);
299 | strcat (catfile, ".");
300 | strcat (catfile, catlang);
301 | _kitten_catalog = catread (catfile);
302 | if (_kitten_catalog)
303 | return (_kitten_catalog);
304 |
305 | /* Grab next tok for the next while iteration */
306 |
307 | nlsptr = tok;
308 | if (nlsptr)
309 | nlsptr++;
310 |
311 | } /* while tok */
312 |
313 | /* We could not find it. Return failure. */
314 |
315 | return (-1);
316 | }
317 |
318 |
319 | /**
320 | * Load a message catalog into memory.
321 | */
322 |
323 | int
324 | catread (char *catfile)
325 | {
326 | int file; /* pointer to the catfile */
327 | int i;
328 | char *where;
329 | char *tok;
330 | #ifdef _MICROC_
331 | char *msg;
332 | char *key;
333 | int key1;
334 | int key2;
335 | #endif
336 |
337 | /* Get the whole catfile into a buffer and parse it */
338 |
339 | file = open (catfile, O_RDONLY | O_TEXT);
340 | if (file < 0)
341 | /* Cannot open the file. Return failure */
342 | return 0;
343 |
344 | for (i = 0; i < 128; i++)
345 | catpoints[i].text = NULL;
346 |
347 | for (i = 0; (unsigned int) i < sizeof (catcontents); i++)
348 | catcontents[i] = '\0';
349 |
350 | /* Read the file into memory */
351 | i = read (file, catcontents, sizeof (catcontents) - 1);
352 |
353 | if ((i == sizeof (catcontents) - 1) || (i < 1))
354 | return 0; /* file was too big or too small */
355 |
356 | where = catcontents;
357 | i = 0; /* catpoints entry */
358 |
359 | do
360 | {
361 | #ifndef _MICROC_
362 | char *msg;
363 | char *key;
364 | int key1 = 0;
365 | int key2 = 0;
366 | #else
367 | key1 = 0;
368 | key2 = 0;
369 | #endif
370 |
371 | tok = strchr (where, '\n');
372 |
373 | if (tok == NULL)
374 | { /* done? */
375 | close (file);
376 | return 1; /* success */
377 | }
378 |
379 | tok[0] = '\0'; /* terminate here */
380 | tok--; /* guess: \r before \n */
381 | if (tok[0] != '\r')
382 | tok++; /* if not, go back */
383 | else
384 | {
385 | tok[0] = '\0'; /* terminate here already */
386 | tok++;
387 | }
388 | tok++; /* this is where the next line starts */
389 |
390 | if ((where[0] >= '0') && (where[0] <= '9') &&
391 | ((msg = strchr (where, ':')) != NULL))
392 | {
393 | /* Skip everything which starts with # or with no digit */
394 | /* Entries look like "1.2:This is a message" */
395 |
396 | msg[0] = '\0'; /* remove : */
397 | msg++; /* go past the : */
398 |
399 | if ((key = strchr (where, '.')) != NULL)
400 | {
401 | key[0] = '\0'; /* turn . into terminator */
402 | key++; /* go past the . */
403 | key1 = mystrtoul (where, 10, strlen (where));
404 | key2 = mystrtoul (key, 10, strlen (key));
405 |
406 | if ((key1 >= 0) && (key2 >= 0))
407 | {
408 | catpoints[i].key1 = key1;
409 | catpoints[i].key2 = key2;
410 | catpoints[i].text = processEscChars (msg);
411 | if (catpoints[i].text == NULL) /* ESC parse error */
412 | catpoints[i].text = msg;
413 | i++; /* next entry! */
414 | } /* valid keys */
415 |
416 | } /* . found */
417 |
418 | } /* : and digit found */
419 |
420 | where = tok; /* go to next line */
421 |
422 | }
423 | while (1);
424 | #ifdef __PACIFIC__
425 | return 0;
426 | #endif
427 | }
428 |
429 |
430 | void
431 | kittenclose (void)
432 | {
433 | /* close a message _kitten_catalog */
434 | _kitten_catalog = 0;
435 | }
436 |
437 |
438 | /**
439 | * Parse a string that represents an unsigned integer.
440 | * Returns -1 if an error is found. The first size
441 | * chars of the string are parsed.
442 | */
443 |
444 | int
445 | mystrtoul (char *src, int base, int size)
446 | {
447 | #ifdef _MICROC_
448 | int ret;
449 | int digit;
450 | int ch;
451 | ret = 0;
452 | #else
453 | int ret = 0;
454 | #endif
455 |
456 | for (; size > 0; size--)
457 | {
458 | #ifdef _MICROC_
459 | ch = *src;
460 | #else
461 | int digit;
462 | int ch = *src;
463 | #endif
464 | src++;
465 |
466 | if (ch >= '0' && ch <= '9')
467 | digit = ch - '0';
468 | else if (ch >= 'A' && ch <= 'Z')
469 | digit = ch - 'A' + 10;
470 | else if (ch >= 'a' && ch <= 'z')
471 | digit = ch - 'a' + 10;
472 | else
473 | return -1;
474 |
475 | if (digit >= base)
476 | return -1;
477 |
478 | ret = ret * base + digit;
479 | } /* for */
480 |
481 | return ret;
482 | }
483 |
484 |
485 | /**
486 | * Process strings, converting \n, \t, \v, \b, \r, \f, \\,
487 | * \ddd, \xdd and \x0dd to actual chars.
488 | * (Note: \x is an extension to support hexadecimal)
489 | * This is used to allow the messages to use c escape sequences.
490 | * Modifies the line in-place (always same size or shorter).
491 | * Returns a pointer to input string.
492 | */
493 |
494 | char *
495 | processEscChars (char *line)
496 | {
497 | /* used when converting \xdd and \ddd (hex or octal) characters */
498 | char ch;
499 | #ifdef _MICROC_
500 | char *src;
501 | char *dst;
502 | int chx;
503 | src = line;
504 | dst = line;
505 | #else
506 | char *src = line;
507 | char *dst = line; /* possible as dst is shorter than src */
508 | #endif
509 |
510 | if (line == NULL)
511 | return line;
512 |
513 | /* cycle through copying characters, except when a \ is encountered. */
514 | while (*src != '\0')
515 | {
516 | ch = *src;
517 | src++;
518 |
519 | if (ch == '\\')
520 | {
521 | ch = *src; /* what follows slash? */
522 | src++;
523 |
524 | switch (ch)
525 | {
526 | case '\\': /* a single slash */
527 | *dst = '\\';
528 | dst++;
529 | break;
530 | case 'n': /* a newline (linefeed) */
531 | *dst = '\n';
532 | dst++;
533 | break;
534 | case 'r': /* a carriage return */
535 | *dst = '\r';
536 | dst++;
537 | break;
538 | case 't': /* a horizontal tab */
539 | *dst = '\t';
540 | dst++;
541 | break;
542 | case 'v': /* a vertical tab */
543 | *dst = '\v';
544 | dst++;
545 | break;
546 | case 'b': /* a backspace */
547 | *dst = '\b';
548 | dst++;
549 | break;
550 | case 'a': /* alert */
551 | *dst = '\a';
552 | dst++;
553 | break;
554 | case 'f': /* formfeed */
555 | *dst = '\f';
556 | dst++;
557 | break;
558 | case 'x': /* extension supporting hex numbers \xdd or \x0dd */
559 | {
560 | #ifdef _MICROC_
561 | chx = mystrtoul (src, 16, 2); /* get value */
562 | #else
563 | int chx = mystrtoul (src, 16, 2); /* get value */
564 | #endif
565 | if (chx >= 0)
566 | { /* store character */
567 | *dst = chx;
568 | dst++;
569 | src += 2;
570 | }
571 | else /* error so just store x (loose slash) */
572 | {
573 | *dst = *src;
574 | dst++;
575 | }
576 | }
577 | break;
578 | default: /* just store letter (loose slash) or handle octal */
579 | {
580 | #ifdef _MICROC_
581 | chx = mystrtoul (src, 8, 3); /* get value */
582 | #else
583 | int chx = mystrtoul (src, 8, 3); /* get value */
584 | #endif
585 | if (chx >= 0)
586 | { /* store character */
587 | *dst = chx;
588 | dst++;
589 | src += 3;
590 | }
591 | else
592 | {
593 | *dst = *src;
594 | dst++;
595 | }
596 | }
597 | break;
598 | } /* switch */
599 | } /* if backslash */
600 | else
601 | {
602 | *dst = ch;
603 | dst++;
604 | }
605 | } /* while */
606 |
607 | /* ensure '\0' terminated */
608 | *dst = '\0';
609 |
610 | return line;
611 | }
612 |
613 |
614 | int
615 | get_char (int file)
616 | {
617 | #ifdef _MICROC_
618 | int rval;
619 | rval = -1;
620 | #else
621 | int rval = -1;
622 | #endif
623 |
624 | if (getlrem <= 0)
625 | { /* (re)init buffer */
626 | getlrem = read (file, getlbuf, sizeof (getlbuf));
627 | if (getlrem <= 0)
628 | return -1; /* fail: read error / EOF */
629 | getlp = getlbuf; /* init pointer */
630 | }
631 |
632 | if (getlrem > 0)
633 | { /* consume byte from buffer */
634 | rval = getlp[0];
635 | getlp++;
636 | getlrem--;
637 | }
638 |
639 | return rval;
640 | }
641 |
642 |
643 | /**
644 | * Read a line of text from file. You must call this with
645 | * a null buffer or null size to flush buffers when you are
646 | * done with a file before using it on the next file. Cannot
647 | * be used for 2 files at the same time.
648 | */
649 |
650 | int
651 | get_line (int file, char *str, int size)
652 | {
653 | int ch;
654 | #ifdef _MICROC_
655 | int success;
656 | success = 0;
657 | #else
658 | int success = 0;
659 | #endif
660 |
661 | if ((size == 0) || (str == NULL))
662 | { /* re-init get_line buffers */
663 | getlp = getlbuf;
664 | getlrem = -1;
665 | lastcr = 0;
666 | return 0;
667 | }
668 |
669 | str[0] = '\0';
670 |
671 | while ((size > 0) && (success == 0))
672 | {
673 | ch = get_char (file);
674 | if (ch < 0)
675 | break; /* (can cause fail if no \n found yet) */
676 |
677 | if (ch == '\r')
678 | ch = get_char (file); /* ignore \r */
679 |
680 | str[0] = ch;
681 |
682 | if ((ch == '\n') || (ch == '\r'))
683 | { /* done? */
684 | str[0] = '\0';
685 | return 1; /* success */
686 | }
687 |
688 | str++;
689 | size--;
690 |
691 | } /* while */
692 |
693 | str[0] = '\0'; /* terminate buffer */
694 |
695 | return success;
696 |
697 | }
698 |
699 | #endif /*NO_KITTEN */
700 |
--------------------------------------------------------------------------------
/lib/kitten/kitten.h:
--------------------------------------------------------------------------------
1 | /* Functions that emulate UNIX catgets, some small DOS file functions */
2 |
3 | /* Copyright (C) 1999,2000 Jim Hall */
4 | /* Kitten version by Tom Ehlert, heavily modified by Eric Auer 2003 */
5 |
6 | /*
7 | This library is free software; you can redistribute it and/or
8 | modify it under the terms of the GNU Lesser General Public
9 | License as published by the Free Software Foundation; either
10 | version 2.1 of the License, or (at your option) any later version.
11 |
12 | This library is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | Lesser General Public License for more details.
16 |
17 | You should have received a copy of the GNU Lesser General Public
18 | License along with this library; if not, write to the Free Software
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 | */
21 |
22 |
23 | #ifndef _CATGETS_H
24 | #define _CATGETS_H
25 |
26 | #ifdef __cplusplus
27 | extern "C"
28 | {
29 | #endif
30 |
31 | #ifdef NO_KITTEN
32 |
33 | #define kittengets(x,y,z) (z)
34 | #define kittenclose()
35 | #define kittenopen(a)
36 |
37 | #else
38 |
39 | /* Data types */
40 |
41 | #define nl_catd int
42 |
43 | /* Functions */
44 |
45 | #define catgets(catalog, set,message_number,message) kittengets(set,message_number,message)
46 | #define catopen(name,flag) kittenopen(name)
47 | #define catclose(catalog) kittenclose()
48 |
49 |
50 | char *kittengets (int set_number, int message_number, char *message);
51 | nl_catd kittenopen (char *name);
52 | void kittenclose (void);
53 |
54 | int get_line (int file, char *buffer, int size);
55 |
56 | #ifndef _MICROC_
57 | #ifndef __DJGPP__
58 |
59 | int dos_open (char *filename, int mode);
60 | #define open(filename,mode) dos_open(filename,mode)
61 |
62 | int dos_read (int file, void *ptr, unsigned count);
63 | #define read(file, ptr, count) dos_read(file,ptr,count)
64 |
65 | int dos_write (int file, void *ptr, unsigned count);
66 | #define write(file, ptr, count) dos_write(file,ptr,count)
67 |
68 | void dos_close (int file);
69 | #define close(file) dos_close(file)
70 |
71 | #endif /*DJGPP*/
72 | #endif /*Micro-C */
73 | #endif /*NO_KITTEN */
74 | #ifdef __cplusplus
75 | }
76 | #endif
77 |
78 | #endif /* _CATGETS_H */
79 |
--------------------------------------------------------------------------------
/lib/lang/lang.c:
--------------------------------------------------------------------------------
1 | /* Wrapper for kitten library
2 | Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 | This file is part of Base64.
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 | You should have received a copy of the GNU General Public License
13 | along with this program. If not, see . */
14 |
15 | /* Written by LiquidFox1776 2018*/
16 |
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include "..\..\config.h"
22 | #include "..\kitten\kitten.h"
23 | #include "..\error.h"
24 | #include "lang.h"
25 |
26 | typedef struct NLS_CALL_STACK{
27 | char *sub_directory[MAX_PATH];
28 | char *file_name[MAX_PATH];
29 | struct NLS_CALL_STACK *next;
30 | };
31 |
32 | struct NLS_CALL_STACK *nls_call_stack;
33 |
34 | /*Pushes sub_directoy and file_name onto the stack structure*/
35 | int
36 | push_nls_call_stack(char *sub_directory, char *file_name){
37 | struct NLS_CALL_STACK *tmp_struct;
38 | nls_call_stack = (struct NLS_CALL_STACK *) malloc(sizeof(struct NLS_CALL_STACK));
39 |
40 | tmp_struct = (struct NLS_CALL_STACK*) malloc(sizeof(struct NLS_CALL_STACK));
41 |
42 | if(tmp_struct != NULL){
43 |
44 | strncpy(tmp_struct->sub_directory, sub_directory, MAX_PATH);
45 | strncpy(tmp_struct->file_name, file_name, MAX_PATH);
46 | tmp_struct->next = nls_call_stack;
47 | nls_call_stack = tmp_struct;
48 | return 0;
49 | }
50 | return -1;
51 | }
52 |
53 | /*Pops sub_directory and file_name off of the stack structure*/
54 | void
55 | pop_nls_call_stack(){
56 | struct node *tmp_struct;
57 | if (nls_call_stack != NULL){
58 |
59 | tmp_struct = nls_call_stack;
60 | nls_call_stack = nls_call_stack->next;
61 | free(tmp_struct);
62 | }
63 | }
64 | void
65 | destroy_nls_call_stack(){
66 | struct NLS_CALL_STACK *tmp_struct;
67 | tmp_struct = nls_call_stack;
68 |
69 | while(tmp_struct != NULL){
70 | pop_nls_call_stack;
71 | tmp_struct = tmp_struct->next;
72 | }
73 | }
74 |
75 | /*
76 | open_nls_file attempts to open up a kitten language file
77 | based on a root path defined by the environment variable COREUTIL_NLS_PATH
78 | sub_directory and file_name variables
79 | It uses the LANG environment variable as a file extension
80 | */
81 | int
82 | open_nls_file(const char *sub_directory, const char *file_name){
83 | char lang[MAX_LANG_CODE_LENGTH];
84 | char coreutil_nls_path[PATH_MAX];
85 | size_t len;
86 | char nls_file_path[PATH_MAX];
87 | int rc;
88 |
89 | if( getenv_s( &len, coreutil_nls_path, sizeof(coreutil_nls_path), "COREUTIL_NLS_PATH") != 0 )
90 | return -1;
91 |
92 | if( getenv_s( &len, lang, sizeof(lang), "LANG") == 0 )
93 | _snprintf(nls_file_path, PATH_MAX, "%s%s%s%s%s%s%s", coreutil_nls_path, "\\", sub_directory, "\\", file_name, ".", lang);
94 | else
95 | return -1;
96 |
97 | if(nls_call_stack != NULL) /* if catalog is open than close */
98 | kittenclose();
99 | if(kittenopen (nls_file_path) ==-1)
100 | return -1; //Failed to open
101 |
102 | rc = push_nls_call_stack(sub_directory, file_name);
103 | if(rc == -1){
104 | kittenclose();
105 | return -1;
106 | }
107 |
108 | return rc;
109 | }
110 |
111 | /* closes the kitten file and pops the nls call stack
112 | */
113 | void
114 | close_nls_file(bool from_main_program){
115 |
116 | if(from_main_program)
117 | destroy_nls_call_stack();
118 | else{
119 | pop_nls_call_stack();
120 | open_nls_file(nls_call_stack->sub_directory, nls_call_stack->file_name); /* Reopen kitten as if the prev caller did */
121 | }
122 | }
123 |
124 | /* calls kittengets*/
125 | char *get_nls_string(int set_number, int message_number, char *message){
126 |
127 | return kittengets(set_number,message_number, message);
128 | }
129 |
130 |
131 |
132 |
--------------------------------------------------------------------------------
/lib/lang/lang.h:
--------------------------------------------------------------------------------
1 | /* Header file for lang.c
2 | Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 | This file is part of Base64.
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 | You should have received a copy of the GNU General Public License
13 | along with this program. If not, see . */
14 |
15 | /* Written by LiquidFox1776 2018*/
16 |
17 | #ifndef __LANG_H_INCLUDED
18 | #define __LANG_H_INCLUDED 1
19 | #include
20 |
21 | extern int open_nls_file(const char *sub_directory, const char *file_name);
22 | extern void close_nls_file(bool from_main_program);
23 | extern char *get_nls_string(int set_number, int message_number, char *message);
24 | #endif /* __LANG_H_INCLUDED */
25 |
26 |
--------------------------------------------------------------------------------
/lib/long-opt.c:
--------------------------------------------------------------------------------
1 | /* Utility to accept --help and --version options as unobtrusively as possible.
2 |
3 | Copyright (C) 1993-1994, 1998-2000, 2002-2006, 2009-2018 Free Software
4 | Foundation, Inc.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see . */
18 |
19 | /* Written by Jim Meyering. */
20 | /* Ported to FreeDOS by LiquidFox1776 */
21 |
22 | /* Specification. */
23 | #include "long-opt.h"
24 |
25 | #include
26 | #include
27 | #include
28 | #include "getopt.h"
29 |
30 | //#include "version-etc.h"
31 |
32 | static struct option const long_options[] =
33 | {
34 | {"help", no_argument, NULL, 'h'},
35 | {"version", no_argument, NULL, 'v'},
36 | {NULL, 0, NULL, 0}
37 | };
38 |
39 | /* Process long options --help and --version, but only if argc == 2.
40 | Be careful not to gobble up "--". */
41 |
42 | void
43 | parse_long_options (int argc,
44 | char **argv,
45 | const char *command_name,
46 | const char *package,
47 | const char *version,
48 | void (*usage_func) (int),
49 | /* const char *author1, ...*/ ...)
50 | {
51 | int c;
52 | int saved_opterr;
53 |
54 | saved_opterr = opterr;
55 |
56 | /* Don't print an error message for unrecognized options. */
57 | opterr = 0;
58 |
59 | if (argc == 2
60 | && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1)
61 | {
62 | switch (c)
63 | {
64 | case 'h':
65 | (*usage_func) (EXIT_SUCCESS);
66 | break;
67 |
68 | case 'v':
69 | {
70 | va_list authors;
71 | va_start (authors, usage_func);
72 | version_etc_va (stdout, command_name, package, version, authors);
73 | exit (0);
74 | }
75 |
76 | default:
77 | /* Don't process any other long-named options. */
78 | break;
79 | }
80 | }
81 |
82 | /* Restore previous value. */
83 | opterr = saved_opterr;
84 |
85 | /* Reset this to zero so that getopt internals get initialized from
86 | the probably-new parameters when/if getopt is called later. */
87 | optind = 0;
88 | }
89 |
--------------------------------------------------------------------------------
/lib/long-opt.h:
--------------------------------------------------------------------------------
1 | /* long-options.h -- declaration for --help- and --version-handling function.
2 | Copyright (C) 1993-1994, 1998-1999, 2003, 2009-2018 Free Software
3 | Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by Jim Meyering. */
19 |
20 | void parse_long_options (int _argc,
21 | char **_argv,
22 | const char *_command_name,
23 | const char *_package,
24 | const char *_version,
25 | void (*_usage) (int),
26 | /* const char *author1, ...*/ ...);
27 |
--------------------------------------------------------------------------------
/lib/quote.h:
--------------------------------------------------------------------------------
1 | /* quote.h - prototypes for quote.c
2 |
3 | Copyright (C) 1998-2001, 2003, 2009-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 | /* Ported to FreeDOS by LiquidFox1776 */
18 | //TODO QUOTE system needs ported
19 |
20 | #ifndef QUOTE_H_
21 | # define QUOTE_H_ 1
22 |
23 | # include
24 |
25 | /* The quoting options used by quote_n and quote. Its type is incomplete,
26 | so it's useful only in expressions like '"e_quoting_options'. */
27 | extern struct quoting_options quote_quoting_options;
28 |
29 | /* Return an unambiguous printable representation of ARG (of size
30 | ARGSIZE), allocated in slot N, suitable for diagnostics. If
31 | ARGSIZE is SIZE_MAX, use the string length of the argument for
32 | ARGSIZE. */
33 | char const *quote_n_mem (int n, char const *arg, size_t argsize);
34 |
35 | /* Return an unambiguous printable representation of ARG (of size
36 | ARGSIZE), suitable for diagnostics. If ARGSIZE is SIZE_MAX, use
37 | the string length of the argument for ARGSIZE. */
38 | //char const *quote_mem (char const *arg, size_t argsize);
39 |
40 | /* Return an unambiguous printable representation of ARG, allocated in
41 | slot N, suitable for diagnostics. */
42 | //char const *quote_n (int n, char const *arg);
43 |
44 | /* Return an unambiguous printable representation of ARG, suitable for
45 | diagnostics. */
46 | //char const *quote (char const *arg);
47 |
48 | #endif /* !QUOTE_H_ */
49 |
--------------------------------------------------------------------------------
/lib/rmdir.c:
--------------------------------------------------------------------------------
1 | /* Work around rmdir bugs.
2 | Copyright (C) 1988, 1990, 1999, 2003-2006, 2009-2018 Free Software
3 | Foundation, Inc.
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3 of the License, or
7 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 | You should have received a copy of the GNU General Public License
13 | along with this program. If not, see . */
14 |
15 | #include "..\config.h"
16 | #include
17 | #include "errno.h"
18 | #include
19 | #include "dosname.h"
20 |
21 | #undef rmdir
22 |
23 | /* Remove directory DIR.
24 | Return 0 if successful, -1 if not. */
25 |
26 | int
27 | rpl_rmdir (char const *dir)
28 | {
29 | /* Work around cygwin 1.5.x bug where rmdir("dir/./") succeeds. */
30 | size_t len = strlen (dir);
31 | int result;
32 | while (len && ISSLASH (dir[len - 1]))
33 | len--;
34 | if (len && dir[len - 1] == '.' && (1 == len || ISSLASH (dir[len - 2])))
35 | {
36 | errno = EINVAL;
37 | return -1;
38 | }
39 | result = rmdir (dir);
40 | /* Work around mingw bug, where rmdir("file/") fails with EINVAL
41 | instead of ENOTDIR. We've already filtered out trailing ., the
42 | only reason allowed by POSIX for EINVAL. */
43 | if (result == -1 && errno == EINVAL)
44 | errno = ENOTDIR;
45 | return result;
46 | }
--------------------------------------------------------------------------------
/lib/stpslash.c:
--------------------------------------------------------------------------------
1 | /* stripslash.c -- remove redundant trailing slashes from a file name
2 | Copyright (C) 1990, 2001, 2003-2006, 2009-2018 Free Software Foundation,
3 | Inc.
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3 of the License, or
7 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 | You should have received a copy of the GNU General Public License
13 | along with this program. If not, see . */
14 |
15 | #include "..\config.h"
16 |
17 | #include "dirname.h"
18 |
19 | /* Remove trailing slashes from FILE. Return true if a trailing slash
20 | was removed. This is useful when using file name completion from a
21 | shell that adds a "/" after directory names (such as tcsh and
22 | bash), because on symlinks to directories, several system calls
23 | have different semantics according to whether a trailing slash is
24 | present. */
25 |
26 | bool
27 | strip_trailing_slashes (char *file)
28 | {
29 | char *base = last_component (file);
30 | char *base_lim;
31 | bool had_slash;
32 |
33 | /* last_component returns "" for file system roots, but we need to turn
34 | "///" into "/". */
35 | if (! *base)
36 | base = file;
37 | base_lim = base + base_len (base);
38 | had_slash = (*base_lim != '\0');
39 | *base_lim = '\0';
40 | return had_slash;
41 | }
--------------------------------------------------------------------------------
/lib/verify.h:
--------------------------------------------------------------------------------
1 | /* Compile-time assert-like macros.
2 |
3 | Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see . */
17 |
18 | /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
19 | /* Ported to FreeDOS by LiquidFox1776 */
20 |
21 | #ifndef _GL_VERIFY_H
22 | #define _GL_VERIFY_H
23 |
24 |
25 | /* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per C11.
26 | This is supported by GCC 4.6.0 and later, in C mode, and its use
27 | here generates easier-to-read diagnostics when verify (R) fails.
28 |
29 | Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per C++11.
30 | This will likely be supported by future GCC versions, in C++ mode.
31 |
32 | Use this only with GCC. If we were willing to slow 'configure'
33 | down we could also use it with other compilers, but since this
34 | affects only the quality of diagnostics, why bother? */
35 | #if (4 < __GNUC__ + (6 <= __GNUC_MINOR__) \
36 | && (201112L <= __STDC_VERSION__ || !defined __STRICT_ANSI__) \
37 | && !defined __cplusplus)
38 | # define _GL_HAVE__STATIC_ASSERT 1
39 | #endif
40 | /* The condition (99 < __GNUC__) is temporary, until we know about the
41 | first G++ release that supports static_assert. */
42 | #if (99 < __GNUC__) && defined __cplusplus
43 | # define _GL_HAVE_STATIC_ASSERT 1
44 | #endif
45 |
46 | /* FreeBSD 9.1 , included by and lots of other
47 | system headers, defines a conflicting _Static_assert that is no
48 | better than ours; override it. */
49 | #ifndef _GL_HAVE_STATIC_ASSERT
50 | # include
51 | # undef _Static_assert
52 | #endif
53 |
54 | /* Each of these macros verifies that its argument R is nonzero. To
55 | be portable, R should be an integer constant expression. Unlike
56 | assert (R), there is no run-time overhead.
57 |
58 | If _Static_assert works, verify (R) uses it directly. Similarly,
59 | _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
60 | that is an operand of sizeof.
61 |
62 | The code below uses several ideas for C++ compilers, and for C
63 | compilers that do not support _Static_assert:
64 |
65 | * The first step is ((R) ? 1 : -1). Given an expression R, of
66 | integral or boolean or floating-point type, this yields an
67 | expression of integral type, whose value is later verified to be
68 | constant and nonnegative.
69 |
70 | * Next this expression W is wrapped in a type
71 | struct _gl_verify_type {
72 | unsigned int _gl_verify_error_if_negative: W;
73 | }.
74 | If W is negative, this yields a compile-time error. No compiler can
75 | deal with a bit-field of negative size.
76 |
77 | One might think that an array size check would have the same
78 | effect, that is, that the type struct { unsigned int dummy[W]; }
79 | would work as well. However, inside a function, some compilers
80 | (such as C++ compilers and GNU C) allow local parameters and
81 | variables inside array size expressions. With these compilers,
82 | an array size check would not properly diagnose this misuse of
83 | the verify macro:
84 |
85 | void function (int n) { verify (n < 0); }
86 |
87 | * For the verify macro, the struct _gl_verify_type will need to
88 | somehow be embedded into a declaration. To be portable, this
89 | declaration must declare an object, a constant, a function, or a
90 | typedef name. If the declared entity uses the type directly,
91 | such as in
92 |
93 | struct dummy {...};
94 | typedef struct {...} dummy;
95 | extern struct {...} *dummy;
96 | extern void dummy (struct {...} *);
97 | extern struct {...} *dummy (void);
98 |
99 | two uses of the verify macro would yield colliding declarations
100 | if the entity names are not disambiguated. A workaround is to
101 | attach the current line number to the entity name:
102 |
103 | #define _GL_CONCAT0(x, y) x##y
104 | #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
105 | extern struct {...} * _GL_CONCAT (dummy, __LINE__);
106 |
107 | But this has the problem that two invocations of verify from
108 | within the same macro would collide, since the __LINE__ value
109 | would be the same for both invocations. (The GCC __COUNTER__
110 | macro solves this problem, but is not portable.)
111 |
112 | A solution is to use the sizeof operator. It yields a number,
113 | getting rid of the identity of the type. Declarations like
114 |
115 | extern int dummy [sizeof (struct {...})];
116 | extern void dummy (int [sizeof (struct {...})]);
117 | extern int (*dummy (void)) [sizeof (struct {...})];
118 |
119 | can be repeated.
120 |
121 | * Should the implementation use a named struct or an unnamed struct?
122 | Which of the following alternatives can be used?
123 |
124 | extern int dummy [sizeof (struct {...})];
125 | extern int dummy [sizeof (struct _gl_verify_type {...})];
126 | extern void dummy (int [sizeof (struct {...})]);
127 | extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
128 | extern int (*dummy (void)) [sizeof (struct {...})];
129 | extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
130 |
131 | In the second and sixth case, the struct type is exported to the
132 | outer scope; two such declarations therefore collide. GCC warns
133 | about the first, third, and fourth cases. So the only remaining
134 | possibility is the fifth case:
135 |
136 | extern int (*dummy (void)) [sizeof (struct {...})];
137 |
138 | * GCC warns about duplicate declarations of the dummy function if
139 | -Wredundant-decls is used. GCC 4.3 and later have a builtin
140 | __COUNTER__ macro that can let us generate unique identifiers for
141 | each dummy function, to suppress this warning.
142 |
143 | * This implementation exploits the fact that older versions of GCC,
144 | which do not support _Static_assert, also do not warn about the
145 | last declaration mentioned above.
146 |
147 | * GCC warns if -Wnested-externs is enabled and verify() is used
148 | within a function body; but inside a function, you can always
149 | arrange to use verify_expr() instead.
150 |
151 | * In C++, any struct definition inside sizeof is invalid.
152 | Use a template type to work around the problem. */
153 |
154 | /* Concatenate two preprocessor tokens. */
155 | #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
156 | #define _GL_CONCAT0(x, y) x##y
157 |
158 | /* _GL_COUNTER is an integer, preferably one that changes each time we
159 | use it. Use __COUNTER__ if it works, falling back on __LINE__
160 | otherwise. __LINE__ isn't perfect, but it's better than a
161 | constant. */
162 | #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
163 | # define _GL_COUNTER __COUNTER__
164 | #else
165 | # define _GL_COUNTER __LINE__
166 | #endif
167 |
168 | /* Generate a symbol with the given prefix, making it unique if
169 | possible. */
170 | #define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
171 |
172 | /* Verify requirement R at compile-time, as an integer constant expression
173 | that returns 1. If R is false, fail at compile-time, preferably
174 | with a diagnostic that includes the string-literal DIAGNOSTIC. */
175 |
176 | #define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
177 | (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
178 |
179 | #ifdef __cplusplus
180 | # if !GNULIB_defined_struct__gl_verify_type
181 | template
182 | struct _gl_verify_type {
183 | unsigned int _gl_verify_error_if_negative: w;
184 | };
185 | # define GNULIB_defined_struct__gl_verify_type 1
186 | # endif
187 | # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
188 | _gl_verify_type<(R) ? 1 : -1>
189 | #elif defined _GL_HAVE__STATIC_ASSERT
190 | # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
191 | struct { \
192 | _Static_assert (R, DIAGNOSTIC); \
193 | int _gl_dummy; \
194 | }
195 | #else
196 | # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
197 | struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
198 | #endif
199 |
200 | /* Verify requirement R at compile-time, as a declaration without a
201 | trailing ';'. If R is false, fail at compile-time, preferably
202 | with a diagnostic that includes the string-literal DIAGNOSTIC.
203 |
204 | Unfortunately, unlike C11, this implementation must appear as an
205 | ordinary declaration, and cannot appear inside struct { ... }. */
206 |
207 | #ifdef _GL_HAVE__STATIC_ASSERT
208 | # define _GL_VERIFY _Static_assert
209 | #else
210 | # define _GL_VERIFY(R, DIAGNOSTIC) \
211 | extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
212 | [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
213 | #endif
214 |
215 | /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
216 | #ifdef _GL_STATIC_ASSERT_H
217 | # if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
218 | # define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
219 | # endif
220 | # if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
221 | # define static_assert _Static_assert /* C11 requires this #define. */
222 | # endif
223 | #endif
224 |
225 | /* @assert.h omit start@ */
226 |
227 | /* Each of these macros verifies that its argument R is nonzero. To
228 | be portable, R should be an integer constant expression. Unlike
229 | assert (R), there is no run-time overhead.
230 |
231 | There are two macros, since no single macro can be used in all
232 | contexts in C. verify_true (R) is for scalar contexts, including
233 | integer constant expression contexts. verify (R) is for declaration
234 | contexts, e.g., the top level. */
235 |
236 | /* Verify requirement R at compile-time, as an integer constant expression.
237 | Return 1. This is equivalent to verify_expr (R, 1).
238 |
239 | verify_true is obsolescent; please use verify_expr instead. */
240 |
241 | #define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
242 |
243 | /* Verify requirement R at compile-time. Return the value of the
244 | expression E. */
245 |
246 | #define verify_expr(R, E) \
247 | (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
248 |
249 | /* Verify requirement R at compile-time, as a declaration without a
250 | trailing ';'. */
251 |
252 | #ifdef __GNUC__
253 | # define verify(R) _GL_VERIFY (R, "verify (" #R ")")
254 | #else
255 | /* PGI barfs if R is long. Play it safe. */
256 | # define verify(R) _GL_VERIFY (R, "verify (...)")
257 | #endif
258 |
259 | #ifndef __has_builtin
260 | # define __has_builtin(x) 0
261 | #endif
262 |
263 | /* Assume that R always holds. This lets the compiler optimize
264 | accordingly. R should not have side-effects; it may or may not be
265 | evaluated. Behavior is undefined if R is false. */
266 |
267 | #if (__has_builtin (__builtin_unreachable) \
268 | || 4 < __GNUC__ + (5 <= __GNUC_MINOR__))
269 | # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
270 | #elif 1200 <= _MSC_VER
271 | # define assume(R) __assume (R)
272 | #elif ((defined GCC_LINT || defined lint) \
273 | && (__has_builtin (__builtin_trap) \
274 | || 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))))
275 | /* Doing it this way helps various packages when configured with
276 | --enable-gcc-warnings, which compiles with -Dlint. It's nicer
277 | when 'assume' silences warnings even with older GCCs. */
278 | # define assume(R) ((R) ? (void) 0 : __builtin_trap ())
279 | #else
280 | # define assume(R) ((void) (0 && (R)))
281 | #endif
282 |
283 | /* @assert.h omit end@ */
284 |
285 | #endif
286 |
--------------------------------------------------------------------------------
/lib/xbin-io.c:
--------------------------------------------------------------------------------
1 | /* Binary mode I/O with checking
2 | Copyright 2017-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #define XBINARY_IO_INLINE _GL_EXTERN_INLINE
19 | #include "lang\lang.h"
20 | #include "errno.h"
21 | #include "xbin-io.h"
22 | #include "error.h"
23 | #include
24 | #include "exitfail.h"
25 | #include "verify.h"
26 |
27 | //#include "gettext.h"
28 | //#define _(msgid) gettext (msgid)
29 |
30 | //#if O_BINARY
31 |
32 | void
33 | xset_binary_mode_error (void)
34 | {
35 | open_nls_file("lib", "xbin-io");
36 | error (exit_failure, errno,_(get_nls_string(0,0,"failed to set file descriptor text/binary mode")));
37 | close_nls_file(false);
38 | assume (false);
39 | }
40 |
41 | //#endif
42 |
--------------------------------------------------------------------------------
/lib/xbin-io.h:
--------------------------------------------------------------------------------
1 | /* Binary mode I/O with checking
2 | Copyright 2017-2018 Free Software Foundation, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation; either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #ifndef _XBINARY_IO_H
19 | #define _XBINARY_IO_H
20 |
21 | #include "bin-io.h"
22 | #include "errno.h"
23 |
24 | #ifndef XBINARY_IO_INLINE
25 | # define XBINARY_IO_INLINE _GL_INLINE
26 | #endif
27 |
28 | #if O_BINARY
29 | extern void xset_binary_mode_error (void);
30 | #else
31 | XBINARY_IO_INLINE void xset_binary_mode_error (void) {}
32 | #endif
33 |
34 | /* Set the mode of FD to MODE, which should be either O_TEXT or O_BINARY.
35 | Report an error and exit if this fails. */
36 |
37 | XBINARY_IO_INLINE void
38 | xset_binary_mode (int fd, int mode)
39 | {
40 | set_binary_mode (fd, mode); // calls setmode
41 | if(errno != EZERO){
42 | xset_binary_mode_error ();
43 | }
44 | }
45 |
46 | #endif /* _XBINARY_IO_H */
47 |
--------------------------------------------------------------------------------
/lib/xdec2umx.c:
--------------------------------------------------------------------------------
1 | /* Ported to FreeDOS by LiquidFox1776 */
2 |
3 | #define __strtol strtoumax
4 | #define __strtol_t uintmax_t
5 | #define __xstrtol xstrtoumax
6 | #define STRTOL_T_MINIMUM 0
7 | #define STRTOL_T_MAXIMUM UINTMAX_MAX
8 | #include "xstrtol.c"
9 |
10 |
--------------------------------------------------------------------------------
/lib/xstr2umx.c:
--------------------------------------------------------------------------------
1 | /* Ported to FreeDOS by LiquidFox1776 */
2 |
3 | #define __strtol strtoumax
4 | #define __strtol_t uintmax_t
5 | #define __xstrtol xstrtoumax
6 | #define STRTOL_T_MINIMUM 0
7 |
8 |
--------------------------------------------------------------------------------
/lib/xstrtol.c:
--------------------------------------------------------------------------------
1 | /* A more useful interface to strtol.
2 |
3 | Copyright (C) 1995-1996, 1998-2001, 2003-2007, 2009-2018 Free Software
4 | Foundation, Inc.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see . */
18 |
19 | /* Written by Jim Meyering. */
20 | /* Ported to FreeDOS by LiquidFox1776 */
21 |
22 | #ifndef __strtol
23 | # define __strtol strtol
24 | # define __strtol_t long int
25 | # define __xstrtol xstrtol
26 | # define STRTOL_T_MINIMUM LONG_MIN
27 | # define STRTOL_T_MAXIMUM LONG_MAX
28 | #endif
29 |
30 | #include "xstrtol.h"
31 |
32 | /* Some pre-ANSI implementations (e.g. SunOS 4)
33 | need stderr defined if assertion checking is enabled. */
34 | #include
35 |
36 | #include
37 | #include "errno.h"
38 | #include
39 | #include
40 | #include
41 |
42 | #include "assure.h"
43 | #include "intprops.h"
44 |
45 | /* xstrtoll.c and xstrtoull.c, which include this file, require that
46 | ULLONG_MAX, LLONG_MAX, LLONG_MIN are defined, but does not
47 | define them on all platforms. */
48 | #ifndef ULLONG_MAX
49 | # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
50 | #endif
51 | #ifndef LLONG_MAX
52 | # define LLONG_MAX TYPE_MAXIMUM (long long int)
53 | #endif
54 | #ifndef LLONG_MIN
55 | # define LLONG_MIN TYPE_MINIMUM (long long int)
56 | #endif
57 |
58 | static strtol_error
59 | bkm_scale (__strtol_t *x, int scale_factor)
60 | {
61 | if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
62 | {
63 | *x = STRTOL_T_MINIMUM;
64 | return LONGINT_OVERFLOW;
65 | }
66 | if (STRTOL_T_MAXIMUM / scale_factor < *x)
67 | {
68 | *x = STRTOL_T_MAXIMUM;
69 | return LONGINT_OVERFLOW;
70 | }
71 | *x *= scale_factor;
72 | return LONGINT_OK;
73 | }
74 |
75 | static strtol_error
76 | bkm_scale_by_power (__strtol_t *x, int base, int power)
77 | {
78 | strtol_error err = LONGINT_OK;
79 | while (power--)
80 | err |= bkm_scale (x, base);
81 | return err;
82 | }
83 |
84 | /* FIXME: comment. */
85 |
86 | strtol_error
87 | __xstrtol (const char *s, char **ptr, int strtol_base,
88 | __strtol_t *val, const char *valid_suffixes)
89 | {
90 | char *t_ptr;
91 | char **p;
92 | __strtol_t tmp;
93 | strtol_error err = LONGINT_OK;
94 |
95 | assure (0 <= strtol_base && strtol_base <= 36);
96 |
97 | p = (ptr ? ptr : &t_ptr);
98 |
99 | errno = 0;
100 |
101 | if (! TYPE_SIGNED (__strtol_t))
102 | {
103 | const char *q = s;
104 | unsigned char ch = *q;
105 | while (isspace (ch))
106 | ch = *++q;
107 | if (ch == '-')
108 | return LONGINT_INVALID;
109 | }
110 |
111 | tmp = __strtol (s, p, strtol_base);
112 |
113 | if (*p == s)
114 | {
115 | /* If there is no number but there is a valid suffix, assume the
116 | number is 1. The string is invalid otherwise. */
117 | if (valid_suffixes && **p && strchr (valid_suffixes, **p))
118 | tmp = 1;
119 | else
120 | return LONGINT_INVALID;
121 | }
122 | else if (errno != 0)
123 | {
124 | if (errno != ERANGE)
125 | return LONGINT_INVALID;
126 | err = LONGINT_OVERFLOW;
127 | }
128 |
129 | /* Let valid_suffixes == NULL mean "allow any suffix". */
130 | /* FIXME: update all callers except the ones that allow suffixes
131 | after the number, changing last parameter NULL to "". */
132 | if (!valid_suffixes)
133 | {
134 | *val = tmp;
135 | return err;
136 | }
137 |
138 | if (**p != '\0')
139 | {
140 | int base = 1024;
141 | int suffixes = 1;
142 | strtol_error overflow;
143 |
144 | if (!strchr (valid_suffixes, **p))
145 | {
146 | *val = tmp;
147 | return err | LONGINT_INVALID_SUFFIX_CHAR;
148 | }
149 |
150 | switch (**p)
151 | {
152 | case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 'm':
153 | case 'P': case 'T': case 't': case 'Y': case 'Z':
154 |
155 | /* The "valid suffix" '0' is a special flag meaning that
156 | an optional second suffix is allowed, which can change
157 | the base. A suffix "B" (e.g. "100MB") stands for a power
158 | of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
159 | a power of 1024. If no suffix (e.g. "100M"), assume
160 | power-of-1024. */
161 |
162 | if (strchr (valid_suffixes, '0'))
163 | switch (p[0][1])
164 | {
165 | case 'i':
166 | if (p[0][2] == 'B')
167 | suffixes += 2;
168 | break;
169 |
170 | case 'B':
171 | case 'D': /* 'D' is obsolescent */
172 | base = 1000;
173 | suffixes++;
174 | break;
175 | }
176 | }
177 |
178 | switch (**p)
179 | {
180 | case 'b':
181 | overflow = bkm_scale (&tmp, 512);
182 | break;
183 |
184 | case 'B':
185 | /* This obsolescent first suffix is distinct from the 'B'
186 | second suffix above. E.g., 'tar -L 1000B' means change
187 | the tape after writing 1000 KiB of data. */
188 | overflow = bkm_scale (&tmp, 1024);
189 | break;
190 |
191 | case 'c':
192 | overflow = LONGINT_OK;
193 | break;
194 |
195 | case 'E': /* exa or exbi */
196 | overflow = bkm_scale_by_power (&tmp, base, 6);
197 | break;
198 |
199 | case 'G': /* giga or gibi */
200 | case 'g': /* 'g' is undocumented; for compatibility only */
201 | overflow = bkm_scale_by_power (&tmp, base, 3);
202 | break;
203 |
204 | case 'k': /* kilo */
205 | case 'K': /* kibi */
206 | overflow = bkm_scale_by_power (&tmp, base, 1);
207 | break;
208 |
209 | case 'M': /* mega or mebi */
210 | case 'm': /* 'm' is undocumented; for compatibility only */
211 | overflow = bkm_scale_by_power (&tmp, base, 2);
212 | break;
213 |
214 | case 'P': /* peta or pebi */
215 | overflow = bkm_scale_by_power (&tmp, base, 5);
216 | break;
217 |
218 | case 'T': /* tera or tebi */
219 | case 't': /* 't' is undocumented; for compatibility only */
220 | overflow = bkm_scale_by_power (&tmp, base, 4);
221 | break;
222 |
223 | case 'w':
224 | overflow = bkm_scale (&tmp, 2);
225 | break;
226 |
227 | case 'Y': /* yotta or 2**80 */
228 | overflow = bkm_scale_by_power (&tmp, base, 8);
229 | break;
230 |
231 | case 'Z': /* zetta or 2**70 */
232 | overflow = bkm_scale_by_power (&tmp, base, 7);
233 | break;
234 |
235 | default:
236 | *val = tmp;
237 | return err | LONGINT_INVALID_SUFFIX_CHAR;
238 | }
239 |
240 | err |= overflow;
241 | *p += suffixes;
242 | if (**p)
243 | err |= LONGINT_INVALID_SUFFIX_CHAR;
244 | }
245 |
246 | *val = tmp;
247 | return err;
248 | }
249 |
--------------------------------------------------------------------------------
/lib/xstrtol.h:
--------------------------------------------------------------------------------
1 | /* A more useful interface to strtol.
2 |
3 | Copyright (C) 1995-1996, 1998-1999, 2001-2004, 2006-2018 Free Software
4 | Foundation, Inc.
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation; either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see . */
18 |
19 | #ifndef XSTRTOL_H_
20 | # define XSTRTOL_H_ 1
21 |
22 | # include "getopt.h"
23 | # include
24 |
25 | # ifndef _STRTOL_ERROR
26 | enum strtol_error
27 | {
28 | LONGINT_OK = 0,
29 |
30 | /* These two values can be ORed together, to indicate that both
31 | errors occurred. */
32 | LONGINT_OVERFLOW = 1,
33 | LONGINT_INVALID_SUFFIX_CHAR = 2,
34 |
35 | LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW = (LONGINT_INVALID_SUFFIX_CHAR
36 | | LONGINT_OVERFLOW),
37 | LONGINT_INVALID = 4
38 | };
39 | typedef enum strtol_error strtol_error;
40 | # endif
41 |
42 | # define _DECLARE_XSTRTOL(name, type) \
43 | strtol_error name (const char *, char **, int, type *, const char *);
44 | _DECLARE_XSTRTOL (xstrtol, long int)
45 | _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
46 | _DECLARE_XSTRTOL (xstrtoimax, intmax_t)
47 | _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
48 |
49 | #if HAVE_LONG_LONG_INT
50 | _DECLARE_XSTRTOL (xstrtoll, long long int)
51 | _DECLARE_XSTRTOL (xstrtoull, unsigned long long int)
52 | #endif
53 |
54 | /* Report an error for an invalid integer in an option argument.
55 |
56 | ERR is the error code returned by one of the xstrto* functions.
57 |
58 | Use OPT_IDX to decide whether to print the short option string "C"
59 | or "-C" or a long option string derived from LONG_OPTION. OPT_IDX
60 | is -2 if the short option "C" was used, without any leading "-"; it
61 | is -1 if the short option "-C" was used; otherwise it is an index
62 | into LONG_OPTIONS, which should have a name preceded by two '-'
63 | characters.
64 |
65 | ARG is the option-argument containing the integer.
66 |
67 | After reporting an error, exit with a failure status. */
68 |
69 | void xstrtol_fatal (enum strtol_error,
70 | int, char, struct option const *,
71 | char const *);
72 |
73 | #endif /* not XSTRTOL_H_ */
74 |
--------------------------------------------------------------------------------
/nls/base32.en:
--------------------------------------------------------------------------------
1 | 0.0:Language file Created by LiquidFox1776 for base32
2 | 0.1: -d, --decode decode data\r\n -i, --ignore-garbage when decoding, ignore non-alphabet characters\r\n -w, --wrap=COLS wrap encoded lines after COLS character (default 76).\r\n Use 0 to disable line wrapping\r\n\r\n
3 | 0.2:closing standard input
4 | 0.3:\r\nThe data are encoded as described for the %s alphabet in RFC 4648.\r\nWhen decoding, the input may contain newlines in addition to the bytes of\r\nthe formal %s alphabet. Use --ignore-garbage to attempt to recover\r\nfrom any other non-alphabet bytes in the encoded stream.\r\n
5 | 0.4"extra operand %s
6 | 0.5:invalid input
7 | 0.6:invalid wrap size
8 | 0.7:read error
9 | 0.8:Usage: %s [OPTION]... [FILE]\r\nBase%d encode or decode FILE, or standard input, to standard output.\r\n
10 | 0.9:write error
11 | 0.10: --help: Display this help and exit.
12 | 0.11: --version: Display version information and exit.
13 |
14 |
--------------------------------------------------------------------------------
/nls/base64.en:
--------------------------------------------------------------------------------
1 | 0.0:Language file Created by LiquidFox1776 for base64
2 | 0.1: -d, --decode decode data\r\n -i, --ignore-garbage when decoding, ignore non-alphabet characters\r\n -w, --wrap=COLS wrap encoded lines after COLS character (default 76).\r\n Use 0 to disable line wrapping\r\n\r\n
3 | 0.2:closing standard input
4 | 0.3:\r\nThe data are encoded as described for the %s alphabet in RFC 4648.\r\nWhen decoding, the input may contain newlines in addition to the bytes of\r\nthe formal %s alphabet. Use --ignore-garbage to attempt to recover\r\nfrom any other non-alphabet bytes in the encoded stream.\r\n
5 | 0.4:extra operand %s
6 | 0.5:invalid input
7 | 0.6:invalid wrap size
8 | 0.7:read error
9 | 0.8:Usage: %s [OPTION]... [FILE]\r\nBase%d encode or decode FILE, or standard input, to standard output.\r\n
10 | 0.9:write error
11 | 0.10: --help: Display this help and exit.
12 | 0.11: --version: Display version information and exit.
13 |
14 |
--------------------------------------------------------------------------------
/nls/false.en:
--------------------------------------------------------------------------------
1 | 0.0:Language File for false.c Written by LiquidFox1776
2 | 0.1:Usage: %s [ignored command line arguments]\r\n or: %s OPTION\r\n
3 | 0.2:Exit with a status code indicating failure.
--------------------------------------------------------------------------------
/nls/lib/xbin-io.en:
--------------------------------------------------------------------------------
1 | 0.0:"failed to set file descriptor text/binary mode"
--------------------------------------------------------------------------------
/nls/system-h.en:
--------------------------------------------------------------------------------
1 | 0.0:Language file for src\system.h written by LiquidFox1776
2 | 0.1:\r\nWith no FILE, or when FILE is -, read standard input.\r\n
3 | 0.2:Try line '%s --help' for more information.\r\n
4 |
--------------------------------------------------------------------------------
/nls/true.en:
--------------------------------------------------------------------------------
1 | 0.0:Language File for True.c Written by LiquidFox1776
2 | 0.1:Usage: %s [ignored command line arguments]\r\n or: %s OPTION\r\n
3 | 0.2:Exit with a status code indicating success.
--------------------------------------------------------------------------------
/src/base32.c:
--------------------------------------------------------------------------------
1 | /* Base64 encode/decode strings or files.
2 | Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 | This file is part of Base64.
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 | You should have received a copy of the GNU General Public License
13 | along with this program. If not, see . */
14 |
15 | /* Written by Simon Josefsson . */
16 | /*Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #define BASE_TYPE 32
19 | #include "base64.c"
20 |
--------------------------------------------------------------------------------
/src/base64.c:
--------------------------------------------------------------------------------
1 | /* Base64 encode/decode strings or files.
2 | Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 | This file is part of Base64.
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 | You should have received a copy of the GNU General Public License
13 | along with this program. If not, see . */
14 |
15 | /* Written by Simon Josefsson . */
16 | /* Ported to FreeDOS by LiquidFox1776 */
17 |
18 | #if BASE_TYPE == 32
19 | # define PROGRAM_NAME "base32"
20 | #else
21 | # define BASE_TYPE 64
22 | # define PROGRAM_NAME "base64"
23 | #endif
24 |
25 | #include "system.h"
26 | #include
27 | #include "..\lib\die.h"
28 | #include "..\lib\error.h"
29 | #include "..\gl\fadvise.h"
30 | //#include "..\lib\quote.h" TODO Implement quote
31 | #include "..\lib\xstrtol.h"
32 | #include "..\gl\xdec2int.h"
33 | #include "..\lib\xbin-io.h"
34 | #include "..\lib\getopt.h"
35 |
36 | #if BASE_TYPE == 32
37 | # include "..\lib\base32.h"
38 | #else
39 | # include "..\lib\base64.h"
40 | #endif
41 |
42 | //#define AUTHORS proper_name ("Simon Josefsson")
43 | #define AUTHORS "Simon Josefsson"
44 |
45 | static struct option const long_options[] =
46 | {
47 | {"decode", no_argument, 0, 'd'},
48 | {"wrap", required_argument, 0, 'w'},
49 | {"ignore-garbage", no_argument, 0, 'i'},
50 |
51 | {GETOPT_HELP_OPTION_DECL},
52 | {GETOPT_VERSION_OPTION_DECL},
53 | {NULL, 0, NULL, 0}
54 | };
55 |
56 | char *nls_string; /* pointer for the nls strings */
57 |
58 | void
59 | usage (int status)
60 | {
61 | if (status != EXIT_SUCCESS)
62 | emit_try_help ();
63 | else
64 | {
65 | nls_string = get_nls_string(0,8,"Usage: %s [OPTION]... [FILE]\r\nBase%d encode or decode FILE, or standard input, to standard output.\r\n");
66 | printf (nls_string, PROGRAM_NAME, BASE_TYPE);
67 |
68 | emit_stdin_note ();
69 | emit_mandatory_arg_note ();
70 |
71 | nls_string = get_nls_string(0, 1," -d, --decode decode data\r\n -i, --ignore-garbage when decoding, ignore non-alphabet characters\r\n -w, --wrap=COLS wrap encoded lines after COLS character (default 76).\r\n Use 0 to disable line wrapping\r\n\r\n");
72 | fputs ( nls_string, stdout);
73 | fputs (get_nls_string(0,10," --help: Display this help and exit."), stdout);
74 | fputs ("\n", stdout);
75 | fputs (get_nls_string(0,11," --version: Display version information and exit."), stdout);
76 | fputs ("\n", stdout);
77 | nls_string = get_nls_string(0,3,"\r\nThe data are encoded as described for the %s alphabet in RFC 4648.\r\nWhen decoding, the input may contain newlines in addition to the bytes of\r\nthe formal %s alphabet. Use --ignore-garbage to attempt to recover\r\nfrom any other non-alphabet bytes in the encoded stream.\r\n");
78 | printf (nls_string,PROGRAM_NAME, PROGRAM_NAME);
79 | //emit_ancillary_info (PROGRAM_NAME);
80 | }
81 | close_nls_file(true);
82 | exit (status);
83 | }
84 |
85 | #define ENC_BLOCKSIZE (1024*3*10) // 30720
86 |
87 | #if BASE_TYPE == 32
88 | # define BASE_LENGTH BASE32_LENGTH
89 | /* Note that increasing this may decrease performance if --ignore-garbage
90 | is used, because of the memmove operation below. */
91 | # define DEC_BLOCKSIZE (1024*5) //5120
92 |
93 | /* Ensure that BLOCKSIZE is a multiple of 5 and 8. */
94 | verify (ENC_BLOCKSIZE % 40 == 0); /* So padding chars only on last block. */
95 | verify (DEC_BLOCKSIZE % 40 == 0); /* So complete encoded blocks are used. */
96 |
97 | # define base_encode base32_encode
98 | # define base_decode_context base32_decode_context
99 | # define base_decode_ctx_init base32_decode_ctx_init
100 | # define base_decode_ctx base32_decode_ctx
101 | # define isbase isbase32
102 | #else
103 | # define BASE_LENGTH BASE64_LENGTH
104 | /* Note that increasing this may decrease performance if --ignore-garbage
105 | is used, because of the memmove operation below. */
106 | # define DEC_BLOCKSIZE (1024*3) //1536 3072
107 |
108 | /* Ensure that BLOCKSIZE is a multiple of 3 and 4. */
109 | verify (ENC_BLOCKSIZE % 12 == 0); /* So padding chars only on last block. */
110 | verify (DEC_BLOCKSIZE % 12 == 0); /* So complete encoded blocks are used. */
111 |
112 | # define base_encode base64_encode
113 | # define base_decode_context base64_decode_context
114 | # define base_decode_ctx_init base64_decode_ctx_init
115 | # define base_decode_ctx base64_decode_ctx
116 | # define isbase isbase64
117 | #endif
118 |
119 | static void
120 | wrap_write (const char *buffer, size_t len,
121 | uintmax_t wrap_column, size_t *current_column, FILE *out)
122 | {
123 | size_t written;
124 |
125 | if (wrap_column == 0)
126 | {
127 | /* Simple write. */
128 | if (fwrite (buffer, 1, len, stdout) < len)
129 | die (EXIT_FAILURE, errno, (get_nls_string(0,9,"write error")));
130 | }
131 | else
132 | for (written = 0; written < len;)
133 | {
134 | uintmax_t cols_remaining = wrap_column - *current_column;
135 | size_t to_write = MIN (cols_remaining, SIZE_MAX);
136 | to_write = MIN (to_write, len - written);
137 |
138 | if (to_write == 0)
139 | {
140 | if (fputc ('\n', out) == EOF)
141 | die (EXIT_FAILURE, errno, (get_nls_string(0,9,"write error")));
142 | *current_column = 0;
143 | }
144 | else
145 | {
146 | if (fwrite (buffer + written, 1, to_write, stdout) < to_write)
147 | die (EXIT_FAILURE, errno, (get_nls_string(0,9,"write error")));
148 | *current_column += to_write;
149 | written += to_write;
150 | }
151 | }
152 | }
153 |
154 | static void
155 | do_encode (FILE *in, FILE *out, uintmax_t wrap_column)
156 | {
157 | size_t current_column = 0;
158 | char inbuf[ENC_BLOCKSIZE];
159 | #if BASE_TYPE == 32
160 | char outbuf[49152];
161 | #else
162 | char outbuf[40960]; // char outbuf[BASE_LENGTH (ENC_BLOCKSIZE)];
163 | #endif
164 |
165 | size_t sum;
166 |
167 | do
168 | {
169 | size_t n;
170 |
171 | sum = 0;
172 | do
173 | {
174 | n = fread (inbuf + sum, 1, ENC_BLOCKSIZE - sum, in);
175 | sum += n;
176 | }
177 | while (!feof (in) && !ferror (in) && sum < ENC_BLOCKSIZE);
178 |
179 | if (sum > 0)
180 | {
181 | /* Process input one block at a time. Note that ENC_BLOCKSIZE
182 | is sized so that no pad chars will appear in output. */
183 | base_encode (inbuf, sum, outbuf, BASE_LENGTH(sum));
184 |
185 | wrap_write (outbuf, BASE_LENGTH (sum), wrap_column,
186 | ¤t_column, out);
187 | }
188 | }
189 | while (!feof (in) && !ferror (in) && sum == ENC_BLOCKSIZE);
190 |
191 | /* When wrapping, terminate last line. */
192 | if (wrap_column && current_column > 0 && fputc ('\n', out) == EOF)
193 | die (EXIT_FAILURE, errno, (get_nls_string(0,9,"write error")));
194 |
195 | if (ferror (in))
196 | die (EXIT_FAILURE, errno, (get_nls_string(0,7,"read error")));
197 | }
198 |
199 | static void
200 | do_decode (FILE *in, FILE *out, bool ignore_garbage)
201 | {
202 | char inbuf[BASE_LENGTH (DEC_BLOCKSIZE)];
203 | char outbuf[DEC_BLOCKSIZE];
204 | size_t sum, i;
205 | struct base_decode_context ctx;
206 |
207 | base_decode_ctx_init (&ctx);
208 |
209 | do
210 | {
211 | bool ok;
212 | size_t n;
213 | unsigned int k;
214 |
215 | sum = 0;
216 | do
217 | {
218 | n = fread (inbuf + sum, 1, BASE_LENGTH (DEC_BLOCKSIZE) - sum, in);
219 |
220 | if (ignore_garbage)
221 | {
222 | for (i = 0; n > 0 && i < n;)
223 | {
224 | if (isbase (inbuf[sum + i]) || inbuf[sum + i] == '=')
225 | i++;
226 | else
227 | memmove (inbuf + sum + i, inbuf + sum + i + 1, --n - i);
228 | }
229 | }
230 |
231 | sum += n;
232 |
233 | if (ferror (in))
234 | die (EXIT_FAILURE, errno, (get_nls_string(0,7,"read error")));
235 | }
236 | while (sum < BASE_LENGTH (DEC_BLOCKSIZE) && !feof (in));
237 |
238 | /* The following "loop" is usually iterated just once.
239 | However, when it processes the final input buffer, we want
240 | to iterate it one additional time, but with an indicator
241 | telling it to flush what is in CTX. */
242 | for (k = 0; k < 1 + !!feof (in); k++)
243 | {
244 | if (k == 1 && ctx.i == 0)
245 | break;
246 | n = DEC_BLOCKSIZE;
247 | ok = base_decode_ctx (&ctx, inbuf, (k == 0 ? sum : 0), outbuf, &n);
248 |
249 | if (fwrite (outbuf, 1, n, out) < n)
250 | die (EXIT_FAILURE, errno, (get_nls_string(0,9,"write error")));
251 |
252 | if (!ok)
253 | die (EXIT_FAILURE, 0, (get_nls_string(0,5,"invalid input")));
254 | }
255 | }
256 | while (!feof (in));
257 | }
258 |
259 | int
260 | main (int argc, char **argv)
261 | {
262 | int opt;
263 | FILE *input_fh;
264 | const char *infile;
265 |
266 | /* True if --decode has been given and we should decode data. */
267 | bool decode = false;
268 | /* True if we should ignore non-base-alphabetic characters. */
269 | bool ignore_garbage = false;
270 | /* Wrap encoded data around the 76:th column, by default. */
271 | uintmax_t wrap_column = 76;
272 |
273 | open_nls_file("", PROGRAM_NAME);
274 |
275 | while ((opt = getopt_long (argc, argv, "diw:", long_options, NULL)) != -1)
276 | switch (opt)
277 | {
278 | case 'd':
279 | decode = true;
280 | break;
281 |
282 | case 'w':
283 | wrap_column = xdectoumax (optarg, 0, UINTMAX_MAX, "",
284 | (get_nls_string(0,6,"invalid wrap size")), 0);
285 | break;
286 |
287 | case 'i':
288 | ignore_garbage = true;
289 | break;
290 |
291 | case_GETOPT_HELP_CHAR;
292 | case_GETOPT_VERSION_CHAR(PROGRAM_NAME, AUTHORS);
293 |
294 | default:
295 | usage (EXIT_FAILURE);
296 | break;
297 | }
298 |
299 | if (argc - optind > 1)
300 | {
301 | error (0, 0, (get_nls_string(0,4,"extra operand %s")), argv[optind]);//quote (argv[optind]));
302 | usage (EXIT_FAILURE);
303 | }
304 |
305 | if (optind < argc)
306 | infile = argv[optind];
307 | else
308 | infile = "-";
309 |
310 | if (STREQ (infile, "-"))
311 | {
312 | xset_binary_mode (STDIN_FILENO, O_BINARY); //calls setmode
313 | input_fh = stdin;
314 | }
315 | else
316 | {
317 | input_fh = fopen (infile, "rb");
318 | if (input_fh == NULL)
319 | die (EXIT_FAILURE, errno, "%s", infile); //quotef (infile));
320 | }
321 |
322 | fadvise (input_fh, FADVISE_SEQUENTIAL);
323 |
324 | if (decode)
325 | do_decode (input_fh, stdout, ignore_garbage);
326 | else
327 | do_encode (input_fh, stdout, wrap_column);
328 |
329 | if (fclose (input_fh) == EOF)
330 | {
331 | if (STREQ (infile, "-")){
332 | die (EXIT_FAILURE, errno, (get_nls_string(0,2,"closing standard input")));
333 | }
334 | else{
335 | die (EXIT_FAILURE, errno, "%s", infile);// quotef (infile) );
336 | }
337 | }
338 |
339 | close_nls_file(true);
340 | return EXIT_SUCCESS;
341 | }
342 |
343 |
--------------------------------------------------------------------------------
/src/echo.c:
--------------------------------------------------------------------------------
1 | /* echo.c, derived from code echo.c in Bash.
2 | Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU General Public License as published by
5 | the Free Software Foundation, either version 3 of the License, or
6 | (at your option) any later version.
7 | This program is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see . */
13 | /* Ported to FreeDOS by LiquidFox1776 2018 */
14 |
15 | #include "..\config.h"
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include "system.h"
22 | #include "..\lib\error.h"
23 |
24 | /* The official name of this program (e.g., no 'g' prefix). */
25 | #define PROGRAM_NAME "echo"
26 |
27 | #define AUTHORS "Brian Fox, Chet Ramey"
28 |
29 | /* If true, interpret backslash escapes by default. */
30 | #ifndef DEFAULT_ECHO_TO_XPG
31 | enum { DEFAULT_ECHO_TO_XPG = false };
32 | #endif
33 |
34 | void
35 | usage (int status)
36 | {
37 | if (status != EXIT_SUCCESS)
38 | emit_try_help ();
39 | else
40 | {
41 | printf (_("\
42 | Usage: %s [SHORT-OPTION]... [STRING]...\n\
43 | or: %s LONG-OPTION\n\
44 | "), program_name, program_name);
45 | fputs (_("\
46 | Echo the STRING(s) to standard output.\n\
47 | \n\
48 | -n do not output the trailing newline\n\
49 | "), stdout);
50 | fputs (_(DEFAULT_ECHO_TO_XPG
51 | ? _("\
52 | -e enable interpretation of backslash escapes (default)\n\
53 | -E disable interpretation of backslash escapes\n")
54 | : _("\
55 | -e enable interpretation of backslash escapes\n\
56 | -E disable interpretation of backslash escapes (default)\n")),
57 | stdout);
58 | fputs (HELP_OPTION_DESCRIPTION, stdout);
59 | fputs (VERSION_OPTION_DESCRIPTION, stdout);
60 | fputs (_("\
61 | \n\
62 | If -e is in effect, the following sequences are recognized:\n\
63 | \n\
64 | "), stdout);
65 | fputs (_("\
66 | \\\\ backslash\n\
67 | \\a alert (BEL)\n\
68 | \\b backspace\n\
69 | \\c produce no further output\n\
70 | \\e escape\n\
71 | \\f form feed\n\
72 | \\n new line\n\
73 | \\r carriage return\n\
74 | \\t horizontal tab\n\
75 | \\v vertical tab\n\
76 | "), stdout);
77 | fputs (_("\
78 | \\0NNN byte with octal value NNN (1 to 3 digits)\n\
79 | \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
80 | "), stdout);
81 | printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
82 | //emit_ancillary_info (PROGRAM_NAME);
83 | }
84 | exit (status);
85 | }
86 |
87 | /* Convert C from hexadecimal character to integer. */
88 | static int
89 | hextobin (unsigned char c)
90 | {
91 | switch (c)
92 | {
93 | default: return c - '0';
94 | case 'a': case 'A': return 10;
95 | case 'b': case 'B': return 11;
96 | case 'c': case 'C': return 12;
97 | case 'd': case 'D': return 13;
98 | case 'e': case 'E': return 14;
99 | case 'f': case 'F': return 15;
100 | }
101 | }
102 |
103 | /* Print the words in LIST to standard output. If the first word is
104 | '-n', then don't print a trailing newline. We also support the
105 | echo syntax from Version 9 unix systems. */
106 |
107 | int
108 | main (int argc, char **argv)
109 | {
110 | bool display_return = true;
111 | bool allow_options =
112 | (! getenv ("POSIXLY_CORRECT")
113 | || (! DEFAULT_ECHO_TO_XPG && 1 < argc && STREQ (argv[1], "-n")));
114 |
115 | /* System V machines already have a /bin/sh with a v9 behavior.
116 | Use the identical behavior for these machines so that the
117 | existing system shell scripts won't barf. */
118 | bool do_v9 = DEFAULT_ECHO_TO_XPG;
119 |
120 | /* We directly parse options, rather than use parse_long_options, in
121 | order to avoid accepting abbreviations. */
122 | if (allow_options && argc == 2)
123 | {
124 | if (STREQ (argv[1], "--help"))
125 | usage (EXIT_SUCCESS);
126 |
127 | if (STREQ (argv[1], "--version"))
128 | {
129 | version(PROGRAM_NAME, AUTHORS);
130 | return EXIT_SUCCESS;
131 | }
132 | }
133 |
134 | --argc;
135 | ++argv;
136 |
137 | if (allow_options)
138 | while (argc > 0 && *argv[0] == '-')
139 | {
140 | char const *temp = argv[0] + 1;
141 | size_t i;
142 |
143 | /* If it appears that we are handling options, then make sure that
144 | all of the options specified are actually valid. Otherwise, the
145 | string should just be echoed. */
146 |
147 | for (i = 0; temp[i]; i++)
148 | switch (temp[i])
149 | {
150 | case 'e': case 'E': case 'n':
151 | break;
152 | default:
153 | goto just_echo;
154 | }
155 |
156 | if (i == 0)
157 | goto just_echo;
158 |
159 | /* All of the options in TEMP are valid options to ECHO.
160 | Handle them. */
161 | while (*temp)
162 | switch (*temp++)
163 | {
164 | case 'e':
165 | do_v9 = true;
166 | break;
167 |
168 | case 'E':
169 | do_v9 = false;
170 | break;
171 |
172 | case 'n':
173 | display_return = false;
174 | break;
175 | }
176 |
177 | argc--;
178 | argv++;
179 | }
180 |
181 | just_echo:
182 |
183 | if (do_v9)
184 | {
185 | while (argc > 0)
186 | {
187 | char const *s = argv[0];
188 | unsigned char c;
189 |
190 | while ((c = *s++))
191 | {
192 | if (c == '\\' && *s)
193 | {
194 | switch (c = *s++)
195 | {
196 | case 'a': c = '\a'; break;
197 | case 'b': c = '\b'; break;
198 | case 'c': return EXIT_SUCCESS;
199 | case 'e': c = '\x1B'; break;
200 | case 'f': c = '\f'; break;
201 | case 'n': c = '\n'; break;
202 | case 'r': c = '\r'; break;
203 | case 't': c = '\t'; break;
204 | case 'v': c = '\v'; break;
205 | case 'x':
206 | {
207 | unsigned char ch = *s;
208 | if (! isxdigit (ch))
209 | goto not_an_escape;
210 | s++;
211 | c = hextobin (ch);
212 | ch = *s;
213 | if (isxdigit (ch))
214 | {
215 | s++;
216 | c = c * 16 + hextobin (ch);
217 | }
218 | }
219 | break;
220 | case '0':
221 | c = 0;
222 | if (! ('0' <= *s && *s <= '7'))
223 | break;
224 | c = *s++;
225 | FALLTHROUGH;
226 | case '1': case '2': case '3':
227 | case '4': case '5': case '6': case '7':
228 | c -= '0';
229 | if ('0' <= *s && *s <= '7')
230 | c = c * 8 + (*s++ - '0');
231 | if ('0' <= *s && *s <= '7')
232 | c = c * 8 + (*s++ - '0');
233 | break;
234 | case '\\': break;
235 |
236 | not_an_escape:
237 | default: putchar ('\\'); break;
238 | }
239 | }
240 | putchar (c);
241 | }
242 | argc--;
243 | argv++;
244 | if (argc > 0)
245 | putchar (' ');
246 | }
247 | }
248 | else
249 | {
250 | while (argc > 0)
251 | {
252 | fputs (argv[0], stdout);
253 | argc--;
254 | argv++;
255 | if (argc > 0)
256 | putchar (' ');
257 | }
258 | }
259 |
260 | if (display_return)
261 | putchar ('\n');
262 | return EXIT_SUCCESS;
263 | }
264 |
265 |
--------------------------------------------------------------------------------
/src/false.c:
--------------------------------------------------------------------------------
1 | #define EXIT_STATUS EXIT_FAILURE
2 | #include "true.c"
3 |
4 |
--------------------------------------------------------------------------------
/src/pfprintf.c:
--------------------------------------------------------------------------------
1 | /* prog-fprintf.c - common formating output functions and definitions
2 | Copyright (C) 2008-2018 Free Software Foundation, Inc.
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU General Public License as published by
5 | the Free Software Foundation, either version 3 of the License, or
6 | (at your option) any later version.
7 | This program is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see . */
13 |
14 | #include "..\config.h"
15 | #include
16 | #include
17 |
18 | #include "system.h"
19 | #include "..\lib\error.h"
20 | #include "pfprintf.h"
21 |
22 | /* Display program name followed by variable list.
23 | Used for e.g. verbose output */
24 | void
25 | prog_fprintf (FILE *fp, char const *fmt, ...)
26 | {
27 | va_list ap;
28 | fputs (program_name, fp);
29 | fputs (": ", fp);
30 | va_start (ap, fmt);
31 | vfprintf (fp, fmt, ap);
32 | va_end (ap);
33 | fputc ('\n', fp);
34 | }
--------------------------------------------------------------------------------
/src/pfprintf.h:
--------------------------------------------------------------------------------
1 | /* prog-fprintf.h - common formating output functions and definitions
2 | Copyright (C) 2008-2018 Free Software Foundation, Inc.
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU General Public License as published by
5 | the Free Software Foundation, either version 3 of the License, or
6 | (at your option) any later version.
7 | This program is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see . */
13 |
14 | #ifndef PROG_FPRINTF_H
15 | # define PROG_FPRINTF_H
16 |
17 | # include
18 |
19 | extern void prog_fprintf (FILE *fp, char const *fmt, ...);
20 | //__attribute__ ((__format__ (__printf__, 2, 3)));
21 |
22 | #endif
23 |
24 |
--------------------------------------------------------------------------------
/src/rmdir.c:
--------------------------------------------------------------------------------
1 | /* rmdir -- remove directories
2 | Copyright (C) 1990-2018 Free Software Foundation, Inc.
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU General Public License as published by
5 | the Free Software Foundation, either version 3 of the License, or
6 | (at your option) any later version.
7 | This program is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see . */
13 |
14 | /* Options:
15 | -p, --parent Remove any parent dirs that are explicitly mentioned
16 | in an argument, if they become empty after the
17 | argument file is removed.
18 | David MacKenzie */
19 | /* Ported to FreeDOS by LiquidFox1776 */
20 |
21 | #include "..\config.h"
22 | #include
23 | #include /* rmdir */
24 | #include
25 | #include /* _findfile functions */
26 | #include
27 | #include "system.h"
28 | #include "..\lib\error.h"
29 | #include "..\lib\errno.h"
30 | #include "..\lib\getopt.h"
31 | #include "..\lib\dosname.h" /*IS_RELATIVE_FILE_NAME macro */
32 | #include "pfprintf.h"
33 |
34 |
35 | /* The official name of this program (e.g., no 'g' prefix). */
36 | #define PROGRAM_NAME "rmdir"
37 |
38 | #define AUTHORS "David MacKenzie"
39 |
40 | /* If true, remove empty parent directories. */
41 | static bool remove_empty_parents;
42 |
43 | /* If true, don't treat failure to remove a nonempty directory
44 | as an error. */
45 | static bool ignore_fail_on_non_empty;
46 |
47 | /* If true, output a diagnostic for every directory processed. */
48 | static bool verbose;
49 |
50 | /* For long options that have no equivalent short option, use a
51 | non-character as a pseudo short option, starting with CHAR_MAX + 1. */
52 | enum
53 | {
54 | IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
55 | };
56 |
57 | static struct option const longopts[] =
58 | {
59 | /* Don't name this '--force' because it's not close enough in meaning
60 | to e.g. rm's -f option. */
61 | {"ignore-fail-on-non-empty", no_argument, NULL,
62 | IGNORE_FAIL_ON_NON_EMPTY_OPTION},
63 |
64 | {"path", no_argument, NULL, 'p'}, /* Deprecated. */
65 | {"parents", no_argument, NULL, 'p'},
66 | {"verbose", no_argument, NULL, 'v'},
67 | {GETOPT_HELP_OPTION_DECL},
68 | {GETOPT_VERSION_OPTION_DECL},
69 | {NULL, 0, NULL, 0}
70 | };
71 |
72 | /* This function determines if a directory is empty
73 | is_empty_dir is normally found in system.h
74 | as of this writing 7-7-2018 openat is not implemented.
75 | Portitions of this function may eventually be moved to sytem.h
76 | */
77 | bool
78 | is_empty_dir(const char *dir){
79 |
80 | struct _finddata_t fileinfo;
81 | long handle;
82 | long rc;
83 | bool dir_is_empty = true;
84 | char cwd[PATH_MAX +1];
85 | char final_path[PATH_MAX +1];
86 |
87 | if( IS_RELATIVE_FILE_NAME(dir) ){ // processing AT_FDCWD
88 |
89 | getcwd(cwd, PATH_MAX);
90 | if(errno == 0){
91 | strncpy(final_path, cwd, PATH_MAX);
92 | strncat(final_path, "\\", 1);
93 | strncat(final_path, dir, PATH_MAX);
94 | }
95 | else { // getcwd failed
96 | strncat(final_path, dir, PATH_MAX);
97 | strncat(final_path, "\\", 1);
98 | }
99 | }
100 | else { // Working with an absolute path
101 | strncat(final_path, dir, PATH_MAX);
102 | strncat(final_path, "\\", 1);
103 | }
104 |
105 | handle = _findfirst(final_path, &fileinfo);
106 | rc = handle;
107 |
108 | while( rc != -1) /* loop through the files in final_path */
109 | {
110 | rc = _findnext(handle, &fileinfo );
111 | if( !((strcmp(fileinfo.name, ".") == 0) || strcmp(fileinfo.name,"..") == 0)){ /* ignore . and .. */
112 | dir_is_empty = false;
113 | break;
114 | }
115 | }
116 |
117 | _findclose(handle);
118 | return dir_is_empty;
119 | }
120 |
121 | /* Return true if ERROR_NUMBER is one of the values associated
122 | with a failed rmdir due to non-empty target directory. */
123 | static bool
124 | errno_rmdir_non_empty (int error_number)
125 | {
126 | return error_number == ENOTEMPTY || error_number == EEXIST;
127 | }
128 |
129 | /* Return true if when rmdir fails with errno == ERROR_NUMBER
130 | the directory may be empty. */
131 | static bool
132 | errno_may_be_empty (int error_number)
133 | {
134 | switch (error_number)
135 | {
136 | case EACCES:
137 | case EPERM:
138 | case EROFS:
139 | case EEXIST:
140 | case EBUSY:
141 | return true;
142 | default:
143 | return false;
144 | }
145 | }
146 |
147 | /* Return true if an rmdir failure with errno == error_number
148 | for DIR is ignorable. */
149 | static bool
150 | ignorable_failure (int error_number, char const *dir)
151 | {
152 | return (ignore_fail_on_non_empty
153 | && (errno_rmdir_non_empty (error_number)
154 | || (errno_may_be_empty (error_number)
155 | && is_empty_dir (dir)))); //old code && is_empty_dir (AT_FDCWD, dir))));
156 | }
157 |
158 | /* Remove any empty parent directories of DIR.
159 | If DIR contains slash characters, at least one of them
160 | (beginning with the rightmost) is replaced with a NUL byte.
161 | Return true if successful. */
162 |
163 | static bool
164 | remove_parents (char *dir)
165 | {
166 | char *slash;
167 | bool ok = true;
168 |
169 | strip_trailing_slashes (dir);
170 | while (1)
171 | {
172 | slash = strrchr (dir, '\\');
173 | if (slash == NULL)
174 | break;
175 | /* Remove any characters after the slash, skipping any extra
176 | slashes in a row. */
177 | while (slash > dir && *slash == '\\')
178 | --slash;
179 | slash[1] = 0;
180 |
181 | /* Give a diagnostic for each attempted removal if --verbose. */
182 | if (verbose)
183 | prog_fprintf (stdout, _("removing directory, %s"), dir);//quoteaf (dir));
184 |
185 | ok = (rmdir (dir) == 0);
186 |
187 | if (!ok)
188 | {
189 | /* Stop quietly if --ignore-fail-on-non-empty. */
190 | if (ignorable_failure (errno, dir))
191 | {
192 | ok = true;
193 | }
194 | else
195 | {
196 | /* Barring race conditions, DIR is expected to be a directory. */
197 | error (0, errno, _("failed to remove directory %s"),dir);//quoteaf (dir));
198 | }
199 | break;
200 | }
201 | }
202 | return ok;
203 | }
204 |
205 | void
206 | usage (int status)
207 | {
208 | if (status != EXIT_SUCCESS)
209 | emit_try_help ();
210 | else
211 | {
212 | printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
213 | fputs (_("\
214 | Remove the DIRECTORY(ies), if they are empty.\n\
215 | \n\
216 | --ignore-fail-on-non-empty ignore each failure that is solely because\r\n\
217 | a directory is non-empty\r\n"), stdout);
218 | fputs (_("\
219 | -p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a\\b\\c' is\
220 | \n\
221 | similar to 'rmdir a\\b\\c a\\b a'\n\
222 | -v, --verbose output a diagnostic for every directory processed\n\
223 | "), stdout);
224 | fputs (HELP_OPTION_DESCRIPTION, stdout);
225 | fputs("\n", stdout);
226 | fputs (VERSION_OPTION_DESCRIPTION, stdout);
227 | //emit_ancillary_info (PROGRAM_NAME);
228 | }
229 | exit (status);
230 | }
231 |
232 | int
233 | main (int argc, char **argv)
234 | {
235 | bool ok = true;
236 | int optc;
237 |
238 | remove_empty_parents = false;
239 |
240 | while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
241 | {
242 | switch (optc)
243 | {
244 | case 'p':
245 | remove_empty_parents = true;
246 | break;
247 | case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
248 | ignore_fail_on_non_empty = true;
249 | break;
250 | case 'v':
251 | verbose = true;
252 | break;
253 | case_GETOPT_HELP_CHAR;
254 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
255 | default:
256 | usage (EXIT_FAILURE);
257 | }
258 | }
259 |
260 | if (optind == argc)
261 | {
262 | error (0, 0, _("missing operand"));
263 | usage (EXIT_FAILURE);
264 | }
265 |
266 | for (; optind < argc; ++optind)
267 | {
268 | char *dir = argv[optind];
269 |
270 | /* Give a diagnostic for each attempted removal if --verbose. */
271 | if (verbose)
272 | prog_fprintf (stdout, _("removing directory, %s"), dir);//quoteaf (dir));
273 |
274 | if (rmdir (dir) != 0)
275 | {
276 | if (ignorable_failure (errno, dir))
277 | continue;
278 |
279 | /* Here, the diagnostic is less precise, since we have no idea
280 | whether DIR is a directory. */
281 | error (0, errno, _("failed to remove %s"), dir);//quoteaf (dir));
282 | ok = false;
283 | }
284 | else if (remove_empty_parents)
285 | {
286 | ok &= remove_parents (dir);
287 | }
288 | }
289 |
290 | return ok ? EXIT_SUCCESS : EXIT_FAILURE;
291 | }
--------------------------------------------------------------------------------
/src/system.h:
--------------------------------------------------------------------------------
1 | /* system.h - FreeDOS Coreutils main header file
2 | Some of the code is given below from GNU Coreutils.
3 | Written by LiquidFox1776. */
4 |
5 | #include "..\config.h"
6 | #include "..\lib\lang\lang.h" /* Kitten Wrapper */
7 | #include
8 | #include
9 | #include
10 |
11 | #define __DOS__ 1
12 | #define _(String) String
13 | extern void version(const char *prog_name, const char *authors);
14 |
15 | void
16 | version(const char *prog_name, const char *authors)
17 | {
18 | printf("\n%s - %s\n", prog_name, authors);
19 | }
20 |
21 |
22 | enum
23 | {
24 | GETOPT_HELP_CHAR = (CHAR_MIN - 2),
25 | GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
26 | };
27 |
28 | #define GETOPT_HELP_OPTION_DECL \
29 | "help", no_argument, NULL, GETOPT_HELP_CHAR
30 | #define GETOPT_VERSION_OPTION_DECL \
31 | "version", no_argument, NULL, GETOPT_VERSION_CHAR
32 |
33 | #define case_GETOPT_HELP_CHAR \
34 | case GETOPT_HELP_CHAR: \
35 | usage (EXIT_SUCCESS); \
36 | break;
37 |
38 | #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
39 | case GETOPT_VERSION_CHAR: \
40 | version(Program_name, Authors); \
41 | exit (EXIT_SUCCESS); \
42 | break;
43 |
44 | #define HELP_OPTION_DESCRIPTION \
45 | (" --help: Display this help and exit.")
46 | #define VERSION_OPTION_DESCRIPTION \
47 | (" --version: Display version information and exit.")
48 |
49 | #define emit_try_help()\
50 | do\
51 | {\
52 | open_nls_file("","system-h");\
53 | fprintf(stderr, get_nls_string(0,2,"Try line '%s --help' for more information.\r\n"),\
54 | program_name);\
55 | close_nls_file(false);\
56 | }\
57 | while(0)
58 |
59 | static inline void
60 | emit_stdin_note (void)
61 | {
62 | open_nls_file("","system-h");
63 | fputs (get_nls_string(0,1,"\r\nWith no FILE, or when FILE is -, read standard input.\r\n"), stdout);
64 | close_nls_file(false);
65 | }
66 | static inline void
67 | emit_mandatory_arg_note (void)
68 | {
69 | fputs (_("\r\nMandatory arguments to long options are mandatory for short options too.\r\n"), stdout);
70 | }
71 |
72 | #define STREQ(a, b) (strcmp (a, b) == 0)
73 |
74 | #ifndef MAX
75 | # define MAX(a, b) ((a) > (b)) ? (a) : (b))
76 | #endif
77 |
78 | #ifndef MIN
79 | # define MIN(a, b) (((a) < (b)) ? (a) : (b))
80 | #endif
81 |
82 | #define USAGE_BUILTIN_WARNING \
83 | _("\n" \
84 | "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
85 | "the version described here. Please refer to your shell's documentation\n" \
86 | "for details about the options it supports.\n")
87 |
88 | #ifndef FALLTHROUGH
89 | #define FALLTHROUGH
90 | #endif
91 |
92 |
--------------------------------------------------------------------------------
/src/true.c:
--------------------------------------------------------------------------------
1 | /* Exit with a status code indicating success.
2 | Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU General Public License as published by
5 | the Free Software Foundation, either version 3 of the License, or
6 | (at your option) any later version.
7 | This program is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see . */
13 | /* Ported to FreeDOS by LiquidFox1776 */
14 |
15 | #include "..\config.h"
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include "..\lib\error.h"
21 |
22 | #include "system.h"
23 |
24 | /* Act like "true" by default; false.c overrides this. */
25 | #ifndef EXIT_STATUS
26 | # define EXIT_STATUS EXIT_SUCCESS
27 | #endif
28 |
29 | #if EXIT_STATUS == EXIT_SUCCESS
30 | # define PROGRAM_NAME "true"
31 | #else
32 | # define PROGRAM_NAME "false"
33 | #endif
34 |
35 | #define AUTHORS "Jim Meyering"
36 |
37 | void
38 | usage (int status)
39 | {
40 | char *nls_string = get_nls_string(0,1,"Usage: %s [ignored command line arguments]\r\n or: %s OPTION\r\n");
41 |
42 | printf (nls_string, program_name, program_name);
43 | if(EXIT_STATUS == EXIT_SUCCESS)
44 | nls_string = get_nls_string(0,2,"Exit with a status code indicating success.");
45 | else
46 | nls_string = get_nls_string(0,2,"Exit with a status code indicating failure.");
47 | printf ("%s\r\n\r\n", nls_string);
48 | fputs (HELP_OPTION_DESCRIPTION, stdout);
49 | fputs("\n", stdout);
50 | fputs (VERSION_OPTION_DESCRIPTION, stdout);
51 | //emit_ancillary_info (PROGRAM_NAME);
52 | close_nls_file(true);
53 | exit (status);
54 | }
55 |
56 | int
57 | main (int argc, char **argv)
58 | {
59 | /* Recognize --help or --version only if it's the only command-line
60 | argument. */
61 | open_nls_file("", PROGRAM_NAME);
62 | if (argc == 2)
63 | {
64 | if (STREQ (argv[1], "--help"))
65 | usage (EXIT_STATUS);
66 |
67 | if (STREQ (argv[1], "--version"))
68 | version(PROGRAM_NAME, AUTHORS);
69 | }
70 |
71 | close_nls_file(true);
72 | return EXIT_STATUS;
73 | }
74 |
75 |
--------------------------------------------------------------------------------