Convert utils to C.

This commit is contained in:
Jonas 'Sortie' Termansen 2016-02-29 00:40:20 +01:00
parent 1cc29fe0ee
commit af40496ffb
59 changed files with 273 additions and 189 deletions

View File

@ -1084,7 +1084,7 @@ static size_t shebang_count_arguments(char* line)
// as the logic in these files: // as the logic in these files:
// * kernel/process.cpp // * kernel/process.cpp
// * libc/unistd/execvpe.c // * libc/unistd/execvpe.c
// * utils/which.cpp // * utils/which.c
// NOTE: See comments in execvpe() for algorithmic commentary. // NOTE: See comments in execvpe() for algorithmic commentary.
static bool sys_execve_alloc(addralloc_t* alloc, size_t size) static bool sys_execve_alloc(addralloc_t* alloc, size_t size)

View File

@ -94,7 +94,7 @@ int execvpe_attempt(const char* filename,
// as the logic in these files: // as the logic in these files:
// * kernel/process.cpp // * kernel/process.cpp
// * libc/unistd/execvpe.c // * libc/unistd/execvpe.c
// * utils/which.cpp // * utils/which.c
int execvpe(const char* filename, char* const* argv, char* const* envp) int execvpe(const char* filename, char* const* argv, char* const* envp)
{ {

View File

@ -5,10 +5,10 @@ include ../build-aux/version.mak
include ../build-aux/dirs.mak include ../build-aux/dirs.mak
OPTLEVEL?=$(DEFAULT_OPTLEVEL) OPTLEVEL?=$(DEFAULT_OPTLEVEL)
CXXFLAGS?=$(OPTLEVEL) CFLAGS?=$(OPTLEVEL)
CFLAGS:=$(CFLAGS) -Wall -Wextra
CPPFLAGS:=$(CPPFLAGS) -DVERSIONSTR=\"$(VERSION)\" CPPFLAGS:=$(CPPFLAGS) -DVERSIONSTR=\"$(VERSION)\"
CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti
BINARIES_EXCEPT_INSTALL:=\ BINARIES_EXCEPT_INSTALL:=\
basename \ basename \
@ -80,8 +80,8 @@ install: all
install $(BINARIES_EXCEPT_INSTALL) $(DESTDIR)$(BINDIR) install $(BINARIES_EXCEPT_INSTALL) $(DESTDIR)$(BINDIR)
install xinstall $(DESTDIR)$(BINDIR)/install install xinstall $(DESTDIR)$(BINDIR)/install
%: %.cpp %: %.c
$(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $< -o $@ $(CC) -std=gnu11 $(CFLAGS) $(CPPFLAGS) $< -o $@
clean: clean:
rm -f $(BINARIES) *.o rm -f $(BINARIES) *.o

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
basename.cpp basename.c
Strip directory and suffix from filenames. Strip directory and suffix from filenames.
*******************************************************************************/ *******************************************************************************/
@ -24,6 +24,7 @@
#include <error.h> #include <error.h>
#include <libgen.h> #include <libgen.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -104,7 +105,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'a': multiple = true; break; case 'a': multiple = true; break;
case 's': case 's':

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
cat.cpp cat.c
Concatenate and print files to the standard output. Concatenate and print files to the standard output.
*******************************************************************************/ *******************************************************************************/
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -134,7 +135,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'u': /* Ignored, POSIX compatibility. */ break; case 'u': /* Ignored, POSIX compatibility. */ break;
default: default:

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
chkblayout.cpp chkblayout.c
Changes the current keyboard layout. Changes the current keyboard layout.
*******************************************************************************/ *******************************************************************************/
@ -79,7 +79,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
chmod.cpp chmod.c
Change file mode bits. Change file mode bits.
*******************************************************************************/ *******************************************************************************/
@ -29,6 +29,7 @@
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -360,7 +361,8 @@ int main(int argc, char* argv[])
argv[i] = NULL; argv[i] = NULL;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': flags |= FLAG_CHANGES; break; case 'c': flags |= FLAG_CHANGES; break;
case 'H': symderef = SYMDEREF_ARGUMENTS; break; case 'H': symderef = SYMDEREF_ARGUMENTS; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
chroot.cpp chroot.c
Runs a process with another root directory. Runs a process with another root directory.
*******************************************************************************/ *******************************************************************************/
@ -29,6 +29,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <fsmarshall.h> #include <fsmarshall.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -87,7 +88,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'd': devices = true; break; case 'd': devices = true; break;
default: default:

View File

@ -15,14 +15,11 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
chvideomode.cpp chvideomode.c
Menu for changing the screen resolution. Menu for changing the screen resolution.
*******************************************************************************/ *******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_LIMIT_MACROS
#include <sys/display.h> #include <sys/display.h>
#include <sys/keycodes.h> #include <sys/keycodes.h>
#include <sys/termmode.h> #include <sys/termmode.h>
@ -32,6 +29,7 @@
#include <error.h> #include <error.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -55,10 +53,11 @@ struct dispmsg_crtc_mode* GetAvailableModes(size_t* num_modes_ptr)
msg.msgid = DISPMSG_GET_CRTC_MODES; msg.msgid = DISPMSG_GET_CRTC_MODES;
msg.device = 0; msg.device = 0;
msg.connector = 0; msg.connector = 0;
size_t guess = 0; size_t guess = 1;
while ( true ) while ( true )
{ {
struct dispmsg_crtc_mode* ret = new struct dispmsg_crtc_mode[guess]; struct dispmsg_crtc_mode* ret = (struct dispmsg_crtc_mode*)
malloc(sizeof(struct dispmsg_crtc_mode) * guess);
if ( !ret ) if ( !ret )
return NULL; return NULL;
msg.modes_length = guess; msg.modes_length = guess;
@ -68,7 +67,7 @@ struct dispmsg_crtc_mode* GetAvailableModes(size_t* num_modes_ptr)
*num_modes_ptr = guess; *num_modes_ptr = guess;
return ret; return ret;
} }
delete[] ret; free(ret);
if ( errno == ERANGE && guess < msg.modes_length ) if ( errno == ERANGE && guess < msg.modes_length )
{ {
guess = msg.modes_length; guess = msg.modes_length;
@ -141,7 +140,7 @@ void filter_modes(struct dispmsg_crtc_mode* modes, size_t* num_modes_ptr, struct
*num_modes_ptr = out_num; *num_modes_ptr = out_num;
} }
size_t parse_size_t(const char* str, size_t def = 0) size_t parse_size_t(const char* str, size_t def)
{ {
if ( !str || !*str ) if ( !str || !*str )
return def; return def;
@ -152,7 +151,7 @@ size_t parse_size_t(const char* str, size_t def = 0)
return ret; return ret;
} }
bool parse_bool(const char* str, bool def = false) bool parse_bool(const char* str, bool def)
{ {
if ( !str || !*str ) if ( !str || !*str )
return def; return def;
@ -238,11 +237,11 @@ bool minmax_parameter(const char* option,
{ {
const char* parameter; const char* parameter;
if ( string_parameter(option, arg, argc, argv, ip, argv0, &parameter) ) if ( string_parameter(option, arg, argc, argv, ip, argv0, &parameter) )
return *min_result = *max_result = parse_size_t(parameter), true; return *min_result = *max_result = parse_size_t(parameter, 0), true;
if ( string_parameter(min_option, arg, argc, argv, ip, argv0, &parameter) ) if ( string_parameter(min_option, arg, argc, argv, ip, argv0, &parameter) )
return *min_result = parse_size_t(parameter), true; return *min_result = parse_size_t(parameter, 0), true;
if ( string_parameter(max_option, arg, argc, argv, ip, argv0, &parameter) ) if ( string_parameter(max_option, arg, argc, argv, ip, argv0, &parameter) )
return *max_result = parse_size_t(parameter), true; return *max_result = parse_size_t(parameter, 0), true;
return false; return false;
} }
@ -260,7 +259,7 @@ bool bool_parameter(const char* option,
{ {
const char* parameter; const char* parameter;
if ( string_parameter(option, arg, argc, argv, ip, argv0, &parameter) ) if ( string_parameter(option, arg, argc, argv, ip, argv0, &parameter) )
return *result = parse_bool(parameter), true; return *result = parse_bool(parameter, false), true;
return false; return false;
} }
@ -300,7 +299,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
@ -355,11 +355,14 @@ int main(int argc, char* argv[])
num_modes_display_length++; num_modes_display_length++;
int mode_set_error = 0; int mode_set_error = 0;
retry_pick_mode: size_t selection;
size_t selection = 0; bool decided;
bool decided = false; bool first_render;
bool first_render = true;
struct wincurpos render_at; struct wincurpos render_at;
retry_pick_mode:
selection = 0;
decided = false;
first_render = true;
memset(&render_at, 0, sizeof(render_at)); memset(&render_at, 0, sizeof(render_at));
while ( !decided ) while ( !decided )
{ {

View File

@ -15,14 +15,14 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
clear.cpp clear.c
Clear the terminal screen. Clear the terminal screen.
*******************************************************************************/ *******************************************************************************/
#include <stdio.h> #include <stdio.h>
int main(int /*argc*/, char* /*argv*/[]) int main(void)
{ {
printf("\e[H\e[2J"); printf("\e[H\e[2J");
fflush(stdout); fflush(stdout);

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
colormake.cpp colormake.c
Colors the output of the make program. Colors the output of the make program.
*******************************************************************************/ *******************************************************************************/
@ -30,13 +30,14 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
const int MODE_NONE = 1; static const int MODE_NONE = 1;
const int MODE_MAKE = 2; static const int MODE_MAKE = 2;
const int MODE_GCC = 3; static const int MODE_GCC = 3;
const int MODE_GCC_MSG = 4; static const int MODE_GCC_MSG = 4;
int main(int /*argc*/, char* argv[]) int main(int argc, char* argv[])
{ {
(void) argc;
int pipe_fds[2]; int pipe_fds[2];
if ( pipe(pipe_fds) ) if ( pipe(pipe_fds) )
error(1, errno, "pipe"); error(1, errno, "pipe");

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
column.cpp column.c
Columnate lists. Columnate lists.
*******************************************************************************/ *******************************************************************************/
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -194,7 +195,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': case 'c':
{ {

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
command-not-found.cpp command-not-found.c
Prints a notice that the attempted command wasn't found and possibly Prints a notice that the attempted command wasn't found and possibly
suggests how to install the command or suggests the proper spelling in case suggests how to install the command or suggests the proper spelling in case
of a typo. of a typo.

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
cp.cpp cp.c
Copy files and directories. Copy files and directories.
*******************************************************************************/ *******************************************************************************/
@ -27,9 +27,10 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <timespec.h> #include <timespec.h>
@ -479,7 +480,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
#ifdef CP_PRETEND_TO_BE_INSTALL #ifdef CP_PRETEND_TO_BE_INSTALL
case 'b': /* ignored */ break; case 'b': /* ignored */ break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
date.cpp date.c
Print or set system date and time. Print or set system date and time.
*******************************************************************************/ *******************************************************************************/
@ -26,7 +26,7 @@
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
int main(/*int argc, char* argv[]*/) int main(void)
{ {
time_t current_time = time(NULL); time_t current_time = time(NULL);

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
df.cpp df.c
Report free disk space. Report free disk space.
*******************************************************************************/ *******************************************************************************/
@ -30,12 +30,13 @@
#include <fstab.h> #include <fstab.h>
#include <libgen.h> #include <libgen.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <termios.h> #include <termios.h>
#include <unistd.h>
#include <wchar.h> #include <wchar.h>
__attribute__((format(printf, 1, 2))) __attribute__((format(printf, 1, 2)))
@ -695,7 +696,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'a': all = true; break; case 'a': all = true; break;
case 'h': format.magformat = MAGFORMAT_HUMAN_READABLE; break; case 'h': format.magformat = MAGFORMAT_HUMAN_READABLE; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
dirname.cpp dirname.c
Strip last component from file name. Strip last component from file name.
*******************************************************************************/ *******************************************************************************/
@ -24,6 +24,7 @@
#include <error.h> #include <error.h>
#include <libgen.h> #include <libgen.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -89,7 +90,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'z': zero = true; break; case 'z': zero = true; break;
default: default:

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
du.cpp du.c
Estimate file space usage. Estimate file space usage.
*******************************************************************************/ *******************************************************************************/
@ -25,11 +25,12 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -144,8 +145,8 @@ bool disk_usage_file_at(int relfd,
uintmax_t block_size, uintmax_t block_size,
uintmax_t* total_bytes_ptr, uintmax_t* total_bytes_ptr,
uintmax_t* num_bytes_ptr, uintmax_t* num_bytes_ptr,
dev_t expected_dev = 0, dev_t expected_dev,
mode_t* result_mode_ptr = NULL) mode_t* result_mode_ptr)
{ {
bool flag_all = flags & FLAG_ALL; bool flag_all = flags & FLAG_ALL;
bool flag_is_operand = flags & FLAG_IS_OPERAND; bool flag_is_operand = flags & FLAG_IS_OPERAND;
@ -196,7 +197,7 @@ bool disk_usage_file_at(int relfd,
uintmax_t num_bytes = S_ISBLK(st.st_mode) ? uintmax_t num_bytes = S_ISBLK(st.st_mode) ?
0 : 0 :
flags & FLAG_APPARENT_SIZE ? flags & FLAG_APPARENT_SIZE ?
(uintmax_t) st.st_size : (intmax_t) st.st_size :
st.st_blocks * 512; st.st_blocks * 512;
if ( !S_ISDIR(st.st_mode) ) if ( !S_ISDIR(st.st_mode) )
@ -340,7 +341,7 @@ bool disk_usage_files(int argc,
{ {
if ( !disk_usage_file_at(AT_FDCWD, ".", ".", flags | FLAG_IS_OPERAND, if ( !disk_usage_file_at(AT_FDCWD, ".", ".", flags | FLAG_IS_OPERAND,
symbolic_dereference, block_size, &total_bytes, symbolic_dereference, block_size, &total_bytes,
NULL) ) NULL, 0, NULL) )
success = false; success = false;
} }
else for ( int i = 1; i < argc; i++ ) else for ( int i = 1; i < argc; i++ )
@ -348,7 +349,7 @@ bool disk_usage_files(int argc,
const char* path = argv[i]; const char* path = argv[i];
if ( !disk_usage_file_at(AT_FDCWD, path, path, flags | FLAG_IS_OPERAND, if ( !disk_usage_file_at(AT_FDCWD, path, path, flags | FLAG_IS_OPERAND,
symbolic_dereference, block_size, &total_bytes, symbolic_dereference, block_size, &total_bytes,
NULL) ) NULL, 0, NULL) )
success = false; success = false;
} }
@ -358,7 +359,7 @@ bool disk_usage_files(int argc,
return success; return success;
} }
static uintmax_t get_default_block_size() static uintmax_t get_default_block_size(void)
{ {
uintmax_t result = 0; uintmax_t result = 0;
if ( !result && getenv("DU_BLOCK_SIZE") ) if ( !result && getenv("DU_BLOCK_SIZE") )
@ -391,7 +392,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'a': flags |= FLAG_ALL; break; case 'a': flags |= FLAG_ALL; break;
case 'b': flags |= FLAG_APPARENT_SIZE, block_size = 1; break; case 'b': flags |= FLAG_APPARENT_SIZE, block_size = 1; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
echo.cpp echo.c
Write arguments to standard output. Write arguments to standard output.
*******************************************************************************/ *******************************************************************************/

View File

@ -15,19 +15,20 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
env.cpp env.c
Set the environment for command invocation. Set the environment for command invocation.
*******************************************************************************/ *******************************************************************************/
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
void clean_environment() void clean_environment(void)
{ {
while ( environ[0] ) while ( environ[0] )
{ {
@ -99,7 +100,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'i': ignore_environment = true; break; case 'i': ignore_environment = true; break;
case 'u': case 'u':

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
expr.cpp expr.c
Evaluate expressions. Evaluate expressions.
*******************************************************************************/ *******************************************************************************/
@ -25,6 +25,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <locale.h> #include <locale.h>
#include <regex.h> #include <regex.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -60,21 +61,21 @@ char* print_intmax_or_die(intmax_t value)
} }
__attribute__((noreturn)) __attribute__((noreturn))
void syntax_error() void syntax_error(void)
{ {
error(2, 0, "syntax error"); error(2, 0, "syntax error");
__builtin_unreachable(); __builtin_unreachable();
} }
__attribute__((noreturn)) __attribute__((noreturn))
void non_integer_argument() void non_integer_argument(void)
{ {
error(2, 0, "non-integer argument"); error(2, 0, "non-integer argument");
__builtin_unreachable(); __builtin_unreachable();
} }
__attribute__((noreturn)) __attribute__((noreturn))
void division_by_zero() void division_by_zero(void)
{ {
error(2, 0, "division by zero"); error(2, 0, "division by zero");
__builtin_unreachable(); __builtin_unreachable();
@ -141,8 +142,9 @@ char* bool_to_boolean_value(bool b)
char* interpret_literal(char** tokens, char* interpret_literal(char** tokens,
size_t num_tokens, size_t num_tokens,
const void* = NULL) const void* ctx)
{ {
(void) ctx;
if ( num_tokens != 1 ) if ( num_tokens != 1 )
syntax_error(); syntax_error();
return strdup_or_die(tokens[0]); return strdup_or_die(tokens[0]);
@ -150,13 +152,13 @@ char* interpret_literal(char** tokens,
char* interpret_parentheses(char** tokens, char* interpret_parentheses(char** tokens,
size_t num_tokens, size_t num_tokens,
const void* = NULL) const void* ctx)
{ {
if ( 2 <= num_tokens && if ( 2 <= num_tokens &&
strcmp(tokens[0], "(") == 0 && strcmp(tokens[0], "(") == 0 &&
strcmp(tokens[num_tokens-1], ")") == 0 ) strcmp(tokens[num_tokens-1], ")") == 0 )
return interpret(tokens + 1, num_tokens - 2); return interpret(tokens + 1, num_tokens - 2);
return interpret_literal(tokens, num_tokens); return interpret_literal(tokens, num_tokens, ctx);
} }
char* evaluate_and(const char* a, const char* b) char* evaluate_and(const char* a, const char* b)

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
false.cpp false.c
Exit with a status code indicating failure. Exit with a status code indicating failure.
*******************************************************************************/ *******************************************************************************/

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
find.cpp find.c
Locate files and directories. Locate files and directories.
*******************************************************************************/ *******************************************************************************/
@ -27,6 +27,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -15,10 +15,10 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
head.cpp head.c
Output the first part of files. Output the first part of files.
*******************************************************************************/ *******************************************************************************/
#define HEAD #define HEAD
#include "tail.cpp" #include "tail.c"

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
help.cpp help.c
Prints a friendly message and print the names of everything in PATH. Prints a friendly message and print the names of everything in PATH.
*******************************************************************************/ *******************************************************************************/
@ -31,7 +31,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
int main(int /*argc*/, char* /*argv*/[]) int main(void)
{ {
printf("Please enter the name of one of the following programs:\n"); printf("Please enter the name of one of the following programs:\n");
@ -83,7 +83,8 @@ int main(int /*argc*/, char* /*argv*/[])
DIR* dir = opendir(dirname); DIR* dir = opendir(dirname);
if ( !dir ) if ( !dir )
continue; continue;
while ( struct dirent* entry = readdir(dir) ) struct dirent* entry;
while ( (entry = readdir(dir)) )
{ {
if ( entry->d_name[0] == '.' ) if ( entry->d_name[0] == '.' )
continue; continue;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
id.cpp id.c
Return user identity. Return user identity.
*******************************************************************************/ *******************************************************************************/
@ -27,6 +27,7 @@
#include <grp.h> #include <grp.h>
#include <locale.h> #include <locale.h>
#include <pwd.h> #include <pwd.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -100,7 +101,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'G': groups_only = true; break; case 'G': groups_only = true; break;
case 'g': group_only = true; break; case 'g': group_only = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
kernelinfo.cpp kernelinfo.c
Prints a kernel information string. Prints a kernel information string.
*******************************************************************************/ *******************************************************************************/
@ -58,7 +58,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
@ -83,8 +84,9 @@ int main(int argc, char* argv[])
error(1, errno, "malloc"); error(1, errno, "malloc");
for ( int i = 1; i < argc; i++ ) for ( int i = 1; i < argc; i++ )
{ {
ssize_t ret;
retry: retry:
ssize_t ret = kernelinfo(argv[i], buf, bufsize); ret = kernelinfo(argv[i], buf, bufsize);
if ( ret < 0 ) if ( ret < 0 )
{ {
if ( errno == EINVAL ) if ( errno == EINVAL )

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
kill.cpp kill.c
Terminate or signal processes. Terminate or signal processes.
*******************************************************************************/ *******************************************************************************/
@ -23,11 +23,12 @@
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h> #include <ctype.h>
#include <locale.h>
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <inttypes.h> #include <inttypes.h>
#include <locale.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -67,7 +68,7 @@ static const char* signames[128] =
}; };
__attribute__((constructor)) __attribute__((constructor))
static void signames_init() static void signames_init(void)
{ {
static char sigrtnames[SIGRTMAX - SIGRTMIN + 1][10]; static char sigrtnames[SIGRTMAX - SIGRTMIN + 1][10];
int rtnum = SIGRTMAX - SIGRTMIN + 1; int rtnum = SIGRTMAX - SIGRTMIN + 1;
@ -192,7 +193,8 @@ int main(int argc, char* argv[])
signal = arg + 1; signal = arg + 1;
else if ( arg[1] != '-' ) else if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'l': list = true; break; case 'l': list = true; break;
case 's': case 's':

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
ln.cpp ln.c
Create a hard or symbolic link. Create a hard or symbolic link.
*******************************************************************************/ *******************************************************************************/
@ -24,6 +24,7 @@
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -73,7 +74,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'f': force = true; break; case 'f': force = true; break;
case 's': symbolic = true; break; case 's': symbolic = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
ls.cpp ls.c
Lists directory contents. Lists directory contents.
*******************************************************************************/ *******************************************************************************/
@ -34,6 +34,7 @@
#include <locale.h> #include <locale.h>
#include <pwd.h> #include <pwd.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -915,7 +916,8 @@ int main(int argc, char** argv)
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case '1': option_column = false; break; // TODO: Semantics? case '1': option_column = false; break; // TODO: Semantics?
case 'a': option_all = DOTFILE_ALL; break; case 'a': option_all = DOTFILE_ALL; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
memstat.cpp memstat.c
Prints system memory usage information. Prints system memory usage information.
*******************************************************************************/ *******************************************************************************/
@ -25,16 +25,16 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#define BYTES 0
#define KIBI 1
#define MEBI 2
#define GIBI 3
#define TEBI 4
#define PEBI 5
#define EXBI 6
void printbytes(unsigned long long bytes) void printbytes(unsigned long long bytes)
{ {
const unsigned BYTES = 0;
const unsigned KIBI = 1;
const unsigned MEBI = 2;
const unsigned GIBI = 3;
const unsigned TEBI = 4;
const unsigned PEBI = 5;
const unsigned EXBI = 6;
unsigned unit = BYTES; unsigned unit = BYTES;
if ( (bytes >> 10ULL) & 1023ULL ) { unit = KIBI; } if ( (bytes >> 10ULL) & 1023ULL ) { unit = KIBI; }
if ( (bytes >> 20ULL) & 1023ULL ) { unit = MEBI; } if ( (bytes >> 20ULL) & 1023ULL ) { unit = MEBI; }
@ -63,7 +63,7 @@ void printbytes(unsigned long long bytes)
} }
} }
int main(int /*argc*/, char* /*argv*/[]) int main(void)
{ {
size_t memused = 0; size_t memused = 0;
size_t memtotal = 0; size_t memtotal = 0;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
mkdir.cpp mkdir.c
Create a directory. Create a directory.
*******************************************************************************/ *******************************************************************************/
@ -26,9 +26,10 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
static bool is_octal_string(const char* str) static bool is_octal_string(const char* str)
@ -247,7 +248,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'm': case 'm':
if ( !*(modespec = arg + 1) ) if ( !*(modespec = arg + 1) )

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
mktemp.cpp mktemp.c
Create temporary files and directories. Create temporary files and directories.
*******************************************************************************/ *******************************************************************************/
@ -25,6 +25,7 @@
#include <limits.h> #include <limits.h>
#include <locale.h> #include <locale.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -112,7 +113,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'd': directory = true; break; case 'd': directory = true; break;
case 'p': case 'p':

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
mv.cpp mv.c
Rename files and directories. Rename files and directories.
*******************************************************************************/ *******************************************************************************/
@ -26,6 +26,7 @@
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -109,7 +110,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'f': flags &= ~FLAG_ASK_OVERWRITE; break; case 'f': flags &= ~FLAG_ASK_OVERWRITE; break;
#if 0 #if 0

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
pager.cpp pager.c
Displays files one page at a time. Displays files one page at a time.
*******************************************************************************/ *******************************************************************************/
@ -29,6 +29,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <ioleast.h> #include <ioleast.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -654,7 +655,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'r': pager.flag_raw_control_chars = true; break; case 'r': pager.flag_raw_control_chars = true; break;
case 'R': pager.flag_color_sequences = true; break; case 'R': pager.flag_color_sequences = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
passwd.cpp passwd.c
Password change. Password change.
*******************************************************************************/ *******************************************************************************/
@ -99,7 +99,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': case 'c':
if ( !*(cipher = arg + 1) ) if ( !*(cipher = arg + 1) )

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
ps.cpp ps.c
Lists processes. Lists processes.
*******************************************************************************/ *******************************************************************************/
@ -27,6 +27,7 @@
#include <locale.h> #include <locale.h>
#include <psctl.h> #include <psctl.h>
#include <pwd.h> #include <pwd.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -99,7 +100,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'a': select_all = true; break; case 'a': select_all = true; break;
case 'A': select_all = true; break; case 'A': select_all = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
pstree.cpp pstree.c
Lists processes in a nice tree. Lists processes in a nice tree.
*******************************************************************************/ *******************************************************************************/
@ -26,6 +26,7 @@
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#include <psctl.h> #include <psctl.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -166,7 +167,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);

View File

@ -15,13 +15,14 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
pwd.cpp pwd.c
Prints the current working directory. Prints the current working directory.
*******************************************************************************/ *******************************************************************************/
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -88,7 +89,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'L': physical = false; break; case 'L': physical = false; break;
case 'P': physical = true; break; case 'P': physical = true; break;
@ -116,9 +118,10 @@ int main(int argc, char* argv[])
// The get_current_dir_name function will use the PWD variable if it is // The get_current_dir_name function will use the PWD variable if it is
// accurate, so we'll need to unset it if it is inappropriate to use it. // accurate, so we'll need to unset it if it is inappropriate to use it.
if ( const char* pwd = getenv("PWD") ) const char* pwd_env;
if ( (pwd_env = getenv("PWD")) )
{ {
if ( physical || !is_path_absolute(pwd) ) if ( physical || !is_path_absolute(pwd_env) )
unsetenv("PWD"); unsetenv("PWD");
} }

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
realpath.cpp realpath.c
Canonicalize filesystem paths. Canonicalize filesystem paths.
*******************************************************************************/ *******************************************************************************/
@ -70,7 +70,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'z': eol = '\0'; case 'z': eol = '\0';
default: default:

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
rm.cpp rm.c
Remove files or directories. Remove files or directories.
*******************************************************************************/ *******************************************************************************/
@ -27,6 +27,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -87,16 +88,17 @@ bool RemoveRecursively(int dirfd, const char* full, const char* rel,
return false; return false;
} }
bool ret = true; bool ret = true;
while ( struct dirent* entry = readdir(dir) ) struct dirent* entry;
while ( (entry = readdir(dir)) )
{ {
const char* name = entry->d_name; const char* name = entry->d_name;
if ( strcmp(name, ".") == 0 || strcmp(name, "..") == 0 ) if ( strcmp(name, ".") == 0 || strcmp(name, "..") == 0 )
continue; continue;
char* newfull = new char[strlen(full) + 1 + strlen(entry->d_name) + 1]; char* newfull = (char*) malloc(strlen(full) + 1 + strlen(entry->d_name) + 1);
bool addslash = !full[0] || full[strlen(full)-1] != '/'; bool addslash = !full[0] || full[strlen(full)-1] != '/';
stpcpy(stpcpy(stpcpy(newfull, full), addslash ? "/" : ""), name); stpcpy(stpcpy(stpcpy(newfull, full), addslash ? "/" : ""), name);
bool ok = RemoveRecursively(targetfd, newfull, name, false, verbose); bool ok = RemoveRecursively(targetfd, newfull, name, false, verbose);
delete[] newfull; free(newfull);
rewinddir(dir); rewinddir(dir);
if ( !ok ) { ret = false; break; } if ( !ok ) { ret = false; break; }
} }
@ -163,7 +165,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'f': force = true; break; case 'f': force = true; break;
case 'r': recursive = true; break; case 'r': recursive = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
rmdir.cpp rmdir.c
Remove an empty directory. Remove an empty directory.
*******************************************************************************/ *******************************************************************************/
@ -24,9 +24,10 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -73,7 +74,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'p': parents = true; break; case 'p': parents = true; break;
case 'v': verbose = true; break; case 'v': verbose = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
sleep.cpp sleep.c
Suspend execution for an interval of time. Suspend execution for an interval of time.
*******************************************************************************/ *******************************************************************************/
@ -24,6 +24,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -215,7 +216,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
sort.cpp sort.c
Sort, merge, or sequence check text files. Sort, merge, or sequence check text files.
*******************************************************************************/ *******************************************************************************/
@ -23,6 +23,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -168,7 +169,8 @@ char** read_input_stream_lines(size_t* result_num_lines,
size_t lines_used = 0; size_t lines_used = 0;
size_t lines_length = 0; size_t lines_length = 0;
while ( char* line = read_input_stream_line(is, delim) ) char* line;
while ( (line = read_input_stream_line(is, delim)) )
{ {
if ( lines_used == lines_length ) if ( lines_used == lines_length )
{ {
@ -318,7 +320,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': check = true; break; case 'c': check = true; break;
case 'C': check = check_quiet = false; break; case 'C': check = check_quiet = false; break;
@ -413,7 +416,7 @@ int main(int argc, char* argv[])
struct input_stream is; struct input_stream is;
memset(&is, 0, sizeof(is)); memset(&is, 0, sizeof(is));
is.files = argv + 1; is.files = (const char* const*) (argv + 1);
is.files_current = 0; is.files_current = 0;
is.files_length = argc - 1; is.files_length = argc - 1;
is.result_status = true; is.result_status = true;
@ -422,7 +425,8 @@ int main(int argc, char* argv[])
{ {
int needed_relation = unique ? 1 : 0; int needed_relation = unique ? 1 : 0;
char* prev_line = NULL; char* prev_line = NULL;
while ( char* line = read_input_stream_line(&is, delim) ) char* line;
while ( (line = read_input_stream_line(&is, delim)) )
{ {
if ( prev_line && compare(line, prev_line) < needed_relation ) if ( prev_line && compare(line, prev_line) < needed_relation )
{ {

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
stat.cpp stat.c
Display file status. Display file status.
*******************************************************************************/ *******************************************************************************/
@ -27,6 +27,7 @@
#include <grp.h> #include <grp.h>
#include <locale.h> #include <locale.h>
#include <pwd.h> #include <pwd.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -165,7 +166,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'L': dereference = true; break; case 'L': dereference = true; break;
default: default:

View File

@ -15,19 +15,20 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
tail.cpp tail.c
Output the last part of files. Output the last part of files.
*******************************************************************************/ *******************************************************************************/
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <ctype.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HEAD #ifdef HEAD
#define TAIL false #define TAIL false
@ -59,10 +60,7 @@ bool processfp(const char* inputname, FILE* fp)
char** buffer = NULL; char** buffer = NULL;
if ( specialtrailing ) if ( specialtrailing )
{ buffer = (char**) calloc(1, sizeof(char*) * abslines);
buffer = new char*[abslines];
memset(buffer, 0, sizeof(char*) * abslines);
}
long linenum; long linenum;
for ( linenum = 0; true; linenum++ ) for ( linenum = 0; true; linenum++ )
@ -110,7 +108,7 @@ bool processfp(const char* inputname, FILE* fp)
} }
} }
delete[] buffer; free(buffer);
return true; return true;
} }
@ -158,7 +156,8 @@ int main(int argc, char* argv[])
nlinesstr = arg + 1; nlinesstr = arg + 1;
else if ( arg[1] != '-' ) else if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'n': case 'n':
if ( !*(nlinesstr = arg + 1) ) if ( !*(nlinesstr = arg + 1) )

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
time.cpp time.c
Measure process running time. Measure process running time.
*******************************************************************************/ *******************************************************************************/

View File

@ -20,6 +20,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
tr.cpp tr.c
Translate, squeeze and/or delete characters. Translate, squeeze and/or delete characters.
*******************************************************************************/ *******************************************************************************/
@ -26,6 +26,7 @@
#include <error.h> #include <error.h>
#include <limits.h> #include <limits.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -540,7 +541,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': flag_complement = true; break; case 'c': flag_complement = true; break;
case 'C': flag_complement = true; break; case 'C': flag_complement = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
true.cpp true.c
Exit with a status code indicating success. Exit with a status code indicating success.
*******************************************************************************/ *******************************************************************************/

View File

@ -15,19 +15,20 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
type.cpp type.c
Lets you move the tty cursor around and easily issue ANSI escape codes. Lets you move the tty cursor around and easily issue ANSI escape codes.
*******************************************************************************/ *******************************************************************************/
#include <sys/keycodes.h> #include <sys/keycodes.h>
#include <sys/termmode.h> #include <sys/termmode.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void help(FILE* fp, const char* argv0) static void help(FILE* fp, const char* argv0)
{ {
@ -57,7 +58,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
uname.cpp uname.c
Print system information. Print system information.
*******************************************************************************/ *******************************************************************************/
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <limits.h> #include <limits.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -90,9 +91,10 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'a': flags |= LONG_MAX; break; case 'a': flags |= INT_MAX; break;
case 's': flags |= PRINT_KERNELNAME; break; case 's': flags |= PRINT_KERNELNAME; break;
case 'n': flags |= PRINT_NODENAME; break; case 'n': flags |= PRINT_NODENAME; break;
case 'r': flags |= PRINT_KERNELREL; break; case 'r': flags |= PRINT_KERNELREL; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
uniq.cpp uniq.c
Report or filter out repeated lines in a file Report or filter out repeated lines in a file
*******************************************************************************/ *******************************************************************************/
@ -23,6 +23,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -126,7 +127,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': count = true; break; case 'c': count = true; break;
case 'd': delete_singulars = true; break; case 'd': delete_singulars = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
unmount.cpp unmount.c
Unmount a filesystem. Unmount a filesystem.
*******************************************************************************/ *******************************************************************************/
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -79,7 +80,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'f': force = true; break; case 'f': force = true; break;
case 'l': lazy = true; break; case 'l': lazy = true; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
uptime.cpp uptime.c
Tell how long the system has been running. Tell how long the system has been running.
*******************************************************************************/ *******************************************************************************/
@ -56,7 +56,7 @@ void PrintElement(size_t num, const char* single, const char* multiple)
prefix = ", "; prefix = ", ";
} }
int main(int /*argc*/, char* /*argv*/[]) int main(void)
{ {
struct timespec uptime; struct timespec uptime;
if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 ) if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 )

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
wc.cpp wc.c
Counts bytes, characters, words and lines. Counts bytes, characters, words and lines.
*******************************************************************************/ *******************************************************************************/
@ -28,6 +28,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -35,14 +36,13 @@
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
const int FLAG_PRINT_NUM_BYTES = 1 << 0; #define FLAG_PRINT_NUM_BYTES (1 << 0)
const int FLAG_PRINT_NUM_CHARACTERS = 1 << 1; #define FLAG_PRINT_NUM_CHARACTERS (1 << 1)
const int FLAG_PRINT_NUM_WORDS = 1 << 2; #define FLAG_PRINT_NUM_WORDS (1 << 2)
const int FLAG_PRINT_NUM_LINES = 1 << 3; #define FLAG_PRINT_NUM_LINES (1 << 3)
const int FLAG_PRINT_COMPACT = 1 << 4; #define FLAG_PRINT_COMPACT (1 << 4)
const int DEFAULT_FLAGS = #define DEFAULT_FLAGS (FLAG_PRINT_NUM_BYTES | FLAG_PRINT_NUM_WORDS | FLAG_PRINT_NUM_LINES)
FLAG_PRINT_NUM_BYTES | FLAG_PRINT_NUM_WORDS | FLAG_PRINT_NUM_LINES;
struct word_count struct word_count
{ {
@ -257,7 +257,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'c': flags |= FLAG_PRINT_NUM_BYTES; break; case 'c': flags |= FLAG_PRINT_NUM_BYTES; break;
case 'l': flags |= FLAG_PRINT_NUM_LINES; break; case 'l': flags |= FLAG_PRINT_NUM_LINES; break;

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
which.cpp which.c
Locate a program in the PATH. Locate a program in the PATH.
*******************************************************************************/ *******************************************************************************/
@ -26,6 +26,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -36,7 +37,7 @@
// as the logic in these files: // as the logic in these files:
// * kernel/process.cpp // * kernel/process.cpp
// * libc/unistd/execvpe.c // * libc/unistd/execvpe.c
// * utils/which.cpp // * utils/which.c
// NOTE: See comments in execvpe() for algorithmic commentary. // NOTE: See comments in execvpe() for algorithmic commentary.
bool Which(const char* filename, const char* path, bool all) bool Which(const char* filename, const char* path, bool all)
@ -160,7 +161,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
case 'a': all = true; break; case 'a': all = true; break;
default: default:

View File

@ -15,10 +15,10 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
xinstall.cpp xinstall.c
Installs files into system directories. Installs files into system directories.
*******************************************************************************/ *******************************************************************************/
#define CP_PRETEND_TO_BE_INSTALL #define CP_PRETEND_TO_BE_INSTALL
#include "cp.cpp" #include "cp.c"

View File

@ -15,11 +15,12 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
yes.cpp yes.c
Repeatedly output a line. Repeatedly output a line.
*******************************************************************************/ *******************************************************************************/
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -68,7 +69,8 @@ int main(int argc, char* argv[])
break; break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
while ( char c = *++arg ) switch ( c ) char c;
while ( (c = *++arg) ) switch ( c )
{ {
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);