fixed the formatting

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1480 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Daniel Reinhold
2002-10-10 19:05:58 +00:00
parent 570f7d0456
commit 00d29fe4dd
10 changed files with 444 additions and 531 deletions
+26 -43
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -47,10 +47,9 @@
#include <sys/stat.h> #include <sys/stat.h>
void usage (void);
void do_chop (char *);
void chop_file (int, char *, off_t); void chop_file (int, char *, off_t);
void do_chop (char *);
void usage (void);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -65,9 +64,9 @@ static int KBytesPerChunk = 1400; // determines size of output files
void void
usage() usage()
{ {
puts ("usage: chop [-n kbyte_per_chunk] file"); printf("Usage: chop [-n kbyte_per_chunk] file\n");
puts ("Splits file into smaller files named file00, file01..."); printf("Splits file into smaller files named file00, file01...\n");
puts ("Default split size is 1400k"); printf("Default split size is 1400k\n");
} }
@@ -78,34 +77,27 @@ main (int argc, char *argv[])
char *arg = NULL; char *arg = NULL;
char *first; char *first;
if ((argc < 2) || (argc > 4)) if ((argc < 2) || (argc > 4)) {
{
usage(); usage();
return 0; return 0;
} }
first = *++argv; first = *++argv;
if (strcmp (first, "--help") == 0) if (strcmp(first, "--help") == 0) {
{
usage(); usage();
return 0; return 0;
} }
if (strcmp (first, "-n") == 0) if (strcmp(first, "-n") == 0) {
{ if (--argc > 1) {
if (--argc > 1)
{
char *num = *++argv; char *num = *++argv;
if (!isdigit(*num)) if (!isdigit(*num))
printf("-n option needs a numeric argument\n"); printf("-n option needs a numeric argument\n");
else else {
{
int b = atoi(num); int b = atoi(num);
KBytesPerChunk = (b < 1 ? 1 : b);
if (b < 1) b = 1;
KBytesPerChunk = b;
if (--argc > 1) if (--argc > 1)
arg = *++argv; arg = *++argv;
@@ -138,23 +130,20 @@ do_chop (char *fname)
int fd; int fd;
// input file must exist // input file must exist
if (stat (fname, &e) == -1) if (stat(fname, &e) == -1) {
{
fprintf(stderr, "'%s': no such file or directory\n", fname); fprintf(stderr, "'%s': no such file or directory\n", fname);
return; return;
} }
// and it must be not be a directory // and it must be not be a directory
if (S_ISDIR (e.st_mode)) if (S_ISDIR(e.st_mode)) {
{
fprintf(stderr, "'%s' is a directory\n", fname); fprintf(stderr, "'%s' is a directory\n", fname);
return; return;
} }
// needs to be big enough such that splitting it actually does something // needs to be big enough such that splitting it actually does something
fsize = e.st_size; fsize = e.st_size;
if (fsize < (KBytesPerChunk * 1024)) if (fsize < (KBytesPerChunk * 1024)) {
{
fprintf(stderr, "'%s': file is already small enough\n", fname); fprintf(stderr, "'%s': file is already small enough\n", fname);
return; return;
} }
@@ -162,11 +151,11 @@ do_chop (char *fname)
// also, don't chop up if chunk files are already present // also, don't chop up if chunk files are already present
{ {
char buf[256]; char buf[256];
strcpy(buf, fname); strcpy(buf, fname);
strcat(buf, "00"); strcat(buf, "00");
if (stat (buf, &e) >= 0) if (stat(buf, &e) >= 0) {
{
fprintf(stderr, "'%s' already exists - aborting\n", buf); fprintf(stderr, "'%s' already exists - aborting\n", buf);
return; return;
} }
@@ -176,8 +165,7 @@ do_chop (char *fname)
fd = open(fname, O_RDONLY); fd = open(fname, O_RDONLY);
if (fd < 0) if (fd < 0)
fprintf(stderr, "can't open '%s': %s\n", fname, strerror(errno)); fprintf(stderr, "can't open '%s': %s\n", fname, strerror(errno));
else else {
{
chop_file(fd, fname, fsize); chop_file(fd, fname, fsize);
close(fd); close(fd);
} }
@@ -206,10 +194,8 @@ chop_file (int fdin, char *fname, off_t fsize)
printf("Chopping up %s into %d kbyte chunks\n", fname, KBytesPerChunk); printf("Chopping up %s into %d kbyte chunks\n", fname, KBytesPerChunk);
while (total_written < fsize) while (total_written < fsize) {
{ if (beg >= end) {
if (beg >= end)
{
// read in another block // read in another block
got = read(fdin, Block, BLOCKSIZE); got = read(fdin, Block, BLOCKSIZE);
if (got <= 0) if (got <= 0)
@@ -219,13 +205,12 @@ chop_file (int fdin, char *fname, off_t fsize)
end = Block + got - 1; end = Block + got - 1;
} }
if (open_next_file) if (open_next_file) {
{
// start a new output file // start a new output file
sprintf(fnameN, "%s%02d", fname, index++); sprintf(fnameN, "%s%02d", fname, index++);
fdout = open(fnameN, O_WRONLY|O_CREAT); fdout = open(fnameN, O_WRONLY|O_CREAT);
if (fdout < 0) if (fdout < 0) {
{
fprintf(stderr, "unable to create chunk file '%s': %s\n", fnameN, strerror(errno)); fprintf(stderr, "unable to create chunk file '%s': %s\n", fnameN, strerror(errno));
return; return;
} }
@@ -238,17 +223,15 @@ chop_file (int fdin, char *fname, off_t fsize)
if (needed > avail) if (needed > avail)
needed = avail; needed = avail;
if (needed > 0) if (needed > 0) {
{
put = write(fdout, beg, needed); put = write(fdout, beg, needed);
beg += put; beg += put;
}
curr_written += put; curr_written += put;
total_written += put; total_written += put;
}
if (curr_written >= chunk_size) if (curr_written >= chunk_size) {
{
// the current output file is full // the current output file is full
close(fdout); close(fdout);
open_next_file = true; open_next_file = true;
+4 -4
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -8,7 +8,7 @@
// //
// File: error.c // File: error.c
// Author: Daniel Reinhold ([email protected]) // Author: Daniel Reinhold ([email protected])
// Description: displays error message text for OS error codes // Description: prints error message text for OS error codes
// //
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
@@ -26,8 +26,8 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (argc != 2) if (argc != 2)
printf("usage: error number\n" printf("Usage: error number\n"
"Displays the message text for OS error codes. " "Prints the message text for OS error codes. "
"The error number can be in decimal, hex or octal.\n"); "The error number can be in decimal, hex or octal.\n");
else else
print_error(argv[1]); print_error(argv[1]);
+25 -40
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -19,25 +19,24 @@
#include <sys/stat.h> #include <sys/stat.h>
void display (uint32, uint8 *);
void do_hd (char *); void do_hd (char *);
void dump_file (FILE *); void dump_file (FILE *);
void display (uint32, uint8 *);
char *hexbytes (uint8 *); char *hexbytes (uint8 *);
char *printable (uint8 *); char *printable (uint8 *);
void usage (); void usage (void);
int BytesBetweenSpaces = 1; static int BytesBetweenSpace = 1;
void void
usage () usage ()
{ {
printf ("usage:\thd [-n N] [file]\n"); printf ("Usage:\thd [-n N] [file]\n");
puts ("\t-n expects a number between 1 and 16 and specifies"); printf("\t-n expects a number between 1 and 16 and specifies\n");
puts ("\tthe number of bytes between spaces."); printf("\tthe number of bytes between spaces.\n");
puts (""); printf("\n\tIf no file is specified, input is read from stdin\n");
puts ("\tIf no file is specified, input is read from stdin");
} }
@@ -47,34 +46,28 @@ main (int argc, char *argv[])
char *arg = NULL; char *arg = NULL;
if (argc == 1) if (argc == 1)
{
dump_file(stdin); dump_file(stdin);
}
else else {
{
char *first = *++argv; char *first = *++argv;
if (strcmp (first, "--help") == 0) if (strcmp(first, "--help") == 0) {
{
usage(); usage();
return 0; return 0;
} }
if (strcmp (first, "-n") == 0) if (strcmp(first, "-n") == 0) {
{ if (--argc > 1) {
if (--argc > 1)
{
char *num = *++argv; char *num = *++argv;
if (!isdigit(*num)) if (!isdigit(*num))
printf("-n option needs a numeric argument\n"); printf("-n option needs a numeric argument\n");
else else {
{
int b = atoi(num); int b = atoi(num);
if (b < 1) b = 1; if (b < 1) b = 1;
if (b > 16) b = 16; if (b > 16) b = 16;
BytesBetweenSpaces = b; BytesBetweenSpace = b;
if (--argc > 1) if (--argc > 1)
arg = *++argv; arg = *++argv;
@@ -102,19 +95,16 @@ do_hd (char *fname)
{ {
struct stat e; struct stat e;
if (stat (fname, &e) == -1) if (stat(fname, &e) == -1) {
{
fprintf(stderr, "'%s': no such file or directory\n", fname); fprintf(stderr, "'%s': no such file or directory\n", fname);
return; return;
} }
if (S_ISDIR(e.st_mode)) if (S_ISDIR(e.st_mode))
fprintf(stderr, "'%s' is a directory\n", fname); fprintf(stderr, "'%s' is a directory\n", fname);
else else {
{
FILE *fp = fopen(fname, "rb"); FILE *fp = fopen(fname, "rb");
if (fp) if (fp) {
{
dump_file(fp); dump_file(fp);
fclose(fp); fclose(fp);
} }
@@ -131,14 +121,12 @@ dump_file (FILE *fp)
uint32 offset = 0; uint32 offset = 0;
uint8 data[16]; uint8 data[16];
while ((got = fread (data, 1, 16, fp)) == 16) while ((got = fread(data, 1, 16, fp)) == 16) {
{
display(offset, data); display(offset, data);
offset += 16; offset += 16;
} }
if (got > 0) if (got > 0) {
{
memset(data+got, ' ', 16-got); memset(data+got, ' ', 16-got);
display(offset, data); display(offset, data);
} }
@@ -165,19 +153,17 @@ hexbytes (uint8 *s)
int i; int i;
int n = 0; int n = 0;
for (i = 0; i < 16; ++i) for (i = 0; i < 16; ++i) {
{
c = *s++; c = *s++;
*p++ = "0123456789abcdef"[c/16]; *p++ = "0123456789abcdef"[c/16];
*p++ = "0123456789abcdef"[c%16]; *p++ = "0123456789abcdef"[c%16];
if (++n == BytesBetweenSpaces) if (++n == BytesBetweenSpace) {
{
*p++ = ' '; *p++ = ' ';
n = 0; n = 0;
} }
if ((i == 7) && (BytesBetweenSpaces == 1)) if ((i == 7) && (BytesBetweenSpace == 1))
*p++ = ' '; *p++ = ' ';
} }
*p++ = ' '; *p++ = ' ';
@@ -191,12 +177,11 @@ char *
printable (uint8 *s) printable (uint8 *s)
{ {
static char buf[16]; static char buf[16];
int n = 16;
char *p = buf; char *p = buf;
uint8 c; uint8 c;
int i = 16;
while (n--) while (i--) {
{
c = *s++; c = *s++;
*p++ = (isgraph(c) ? c : '.'); *p++ = (isgraph(c) ? c : '.');
} }
+6 -15
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -25,26 +25,20 @@ void show_memory_totals (void);
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
//
show_memory_totals(); show_memory_totals();
if (argc == 1) if (argc == 1) {
{
// list for all teams
int32 cookie = 0; int32 cookie = 0;
team_info info; team_info info;
// list for all teams
while (get_next_team_info(&cookie, &info) >= B_OK) while (get_next_team_info(&cookie, &info) >= B_OK)
{
list_area_info(info.team); list_area_info(info.team);
} }
}
else else
// list for each team_id on the command line // list for each team_id on the command line
while (--argc) while (--argc)
{
list_area_info(atoi(*++argv)); list_area_info(atoi(*++argv));
}
return 0; return 0;
} }
@@ -56,8 +50,7 @@ show_memory_totals ()
int32 max = 0, used = 0, left; int32 max = 0, used = 0, left;
system_info sys; system_info sys;
if (get_system_info (&sys) == B_OK) if (get_system_info(&sys) == B_OK) {
{
// pages are 4KB // pages are 4KB
max = sys.max_pages * 4; max = sys.max_pages * 4;
used = sys.used_pages * 4; used = sys.used_pages * 4;
@@ -76,8 +69,7 @@ list_area_info (team_id id)
team_info this_team; team_info this_team;
area_info this_area; area_info this_area;
if (get_team_info (id, &this_team) == B_BAD_TEAM_ID) if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
{
printf("\nteam %d unknown\n", id); printf("\nteam %d unknown\n", id);
return; return;
} }
@@ -86,8 +78,7 @@ list_area_info (team_id id)
printf(" ID name address size alloc. #-cow #-in #-out\n"); printf(" ID name address size alloc. #-cow #-in #-out\n");
printf("--------------------------------------------------------------------------------\n"); printf("--------------------------------------------------------------------------------\n");
while (get_next_area_info (id, &cookie, &this_area) == B_OK) while (get_next_area_info(id, &cookie, &this_area) == B_OK) {
{
printf("%5d %29s %.8x %8x %8x %5d %5d %5d\n", printf("%5d %29s %.8x %8x %8x %5d %5d %5d\n",
this_area.area, this_area.area,
this_area.name, this_area.name,
+5 -13
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -25,24 +25,18 @@ void list_image_info (team_id id);
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
// if (argc == 1) {
if (argc == 1)
{
// list for all teams
int32 cookie = 0; int32 cookie = 0;
team_info info; team_info info;
// list for all teams
while (get_next_team_info(&cookie, &info) >= B_OK) while (get_next_team_info(&cookie, &info) >= B_OK)
{
list_image_info(info.team); list_image_info(info.team);
} }
}
else else
// list for each team_id on the command line // list for each team_id on the command line
while (--argc) while (--argc)
{
list_image_info(atoi(*++argv)); list_image_info(atoi(*++argv));
}
return 0; return 0;
} }
@@ -55,8 +49,7 @@ list_image_info (team_id id)
team_info this_team; team_info this_team;
image_info this_image; image_info this_image;
if (get_team_info (id, &this_team) == B_BAD_TEAM_ID) if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
{
printf("\nteam %d unknown\n", id); printf("\nteam %d unknown\n", id);
return; return;
} }
@@ -65,8 +58,7 @@ list_image_info (team_id id)
printf(" ID name text data seq# init#\n"); printf(" ID name text data seq# init#\n");
printf("--------------------------------------------------------------------------------------------------------\n"); printf("--------------------------------------------------------------------------------------------------------\n");
while (get_next_image_info (id, &cookie, &this_image) == B_OK) while (get_next_image_info(id, &cookie, &this_image) == B_OK) {
{
printf("%5d %64s %.8x %.8x %4d %10u\n", printf("%5d %64s %.8x %.8x %4d %10u\n",
this_image.id, this_image.id,
this_image.name, this_image.name,
+6 -15
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -25,26 +25,20 @@ void show_port_totals (void);
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
//
show_port_totals(); show_port_totals();
if (argc == 1) if (argc == 1) {
{
// list for all teams
int32 cookie = 0; int32 cookie = 0;
team_info info; team_info info;
// list for all teams
while (get_next_team_info(&cookie, &info) >= B_OK) while (get_next_team_info(&cookie, &info) >= B_OK)
{
list_team_ports(info.team); list_team_ports(info.team);
} }
}
else else
// list for each team_id on the command line // list for each team_id on the command line
while (--argc) while (--argc)
{
list_team_ports(atoi(*++argv)); list_team_ports(atoi(*++argv));
}
return 0; return 0;
} }
@@ -56,8 +50,7 @@ show_port_totals ()
int32 max = 0, used = 0, left; int32 max = 0, used = 0, left;
system_info sys; system_info sys;
if (get_system_info (&sys) == B_OK) if (get_system_info(&sys) == B_OK) {
{
max = sys.max_ports; max = sys.max_ports;
used = sys.used_ports; used = sys.used_ports;
} }
@@ -75,8 +68,7 @@ list_team_ports (team_id id)
port_info this_port; port_info this_port;
team_info this_team; team_info this_team;
if (get_team_info (id, &this_team) == B_BAD_TEAM_ID) if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
{
printf("\nteam %d unknown\n", id); printf("\nteam %d unknown\n", id);
return; return;
} }
@@ -85,8 +77,7 @@ list_team_ports (team_id id)
printf(" ID name capacity queued\n"); printf(" ID name capacity queued\n");
printf("----------------------------------------------------\n"); printf("----------------------------------------------------\n");
while (get_next_port_info (id, &cookie, &this_port) == B_OK) while (get_next_port_info(id, &cookie, &this_port) == B_OK) {
{
printf("%5d %28s %8d %6d\n", printf("%5d %28s %8d %6d\n",
this_port.port, this_port.port,
this_port.name, this_port.name,
+4 -5
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -29,7 +29,7 @@ main(int argc, char *argv[])
char *arg = (argc == 2 ? argv[1] : NULL); char *arg = (argc == 2 ? argv[1] : NULL);
if ((argc > 2) || (arg && !strcmp(arg, "--help"))) { if ((argc > 2) || (arg && !strcmp(arg, "--help"))) {
printf("usage: printenv [VARIABLE]\n" printf("Usage: printenv [VARIABLE]\n"
"If no environment VARIABLE is specified, print them all.\n"); "If no environment VARIABLE is specified, print them all.\n");
return 1; return 1;
} }
@@ -49,8 +49,7 @@ print_env(char *arg)
printf("%s\n", *env++); printf("%s\n", *env++);
return 0; return 0;
} } else {
else {
// print only the value of the specified variable // print only the value of the specified variable
char *s; char *s;
int len = strlen(arg); int len = strlen(arg);
@@ -65,6 +64,6 @@ print_env(char *arg)
} }
} }
return found ? 0 : 1; return (found ? 0 : 1);
} }
} }
+3 -4
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -80,7 +80,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (argc < 2) if (argc < 2)
printf("usage: printf format [arguments]\n"); printf("Usage: printf format [arguments]\n");
else else
do_printf(argc, argv); do_printf(argc, argv);
@@ -108,8 +108,7 @@ do_printf(int argc, char *argv[])
++Format, putchar('%'); ++Format, putchar('%');
else else
print_next_arg(); print_next_arg();
} } else {
else {
e = escaped(c, &Format); e = escaped(c, &Format);
putchar(e); putchar(e);
} }
+26 -36
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -20,13 +20,12 @@
#include <sys/stat.h> #include <sys/stat.h>
void usage (void); void append_file (int, int);
void do_unchop (char *, char *); void do_unchop (char *, char *);
void concatenate (int, int);
bool valid_file (char *);
void replace (char *, char *); void replace (char *, char *);
char *temp_file (); char *temp_file ();
void usage (void);
bool valid_file (char *);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -42,27 +41,24 @@ static int Errors = 0;
void void
usage() usage()
{ {
puts ("usage: unchop file"); printf("Usage: unchop file\n");
puts ("Concatenates files named file00, file01... into file"); printf("Concatenates files named file00, file01... into file\n");
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (argc != 2) if (argc != 2) {
{
usage(); usage();
return 0; return 0;
} }
else else {
{
char *origfile = argv[1]; char *origfile = argv[1];
char *tmpfile = origfile; char *tmpfile = origfile;
bool needs_replace = false; bool needs_replace = false;
if (valid_file (origfile)) if (valid_file(origfile)) {
{
// output file already exists -- write to temp file // output file already exists -- write to temp file
tmpfile = temp_file(); tmpfile = temp_file();
needs_replace = true; needs_replace = true;
@@ -70,8 +66,7 @@ main (int argc, char *argv[])
do_unchop(tmpfile, origfile); do_unchop(tmpfile, origfile);
if (needs_replace) if (needs_replace) {
{
if (Errors == 0) if (Errors == 0)
replace(origfile, tmpfile); replace(origfile, tmpfile);
else else
@@ -79,7 +74,6 @@ main (int argc, char *argv[])
} }
} }
putchar ('\n');
return Errors; return Errors;
} }
@@ -90,31 +84,24 @@ do_unchop (char *outfile, char *basename)
int fdout = open(outfile, O_WRONLY|O_CREAT|O_APPEND); int fdout = open(outfile, O_WRONLY|O_CREAT|O_APPEND);
if (fdout < 0) if (fdout < 0)
fprintf(stderr, "can't open '%s': %s\n", outfile, strerror(errno)); fprintf(stderr, "can't open '%s': %s\n", outfile, strerror(errno));
else
{ else {
int i; int i;
char fnameN[256]; char fnameN[256];
for (i = 0; i < 999999; ++i) for (i = 0; i < 999999; ++i) {
{
sprintf(fnameN, "%s%02d", basename, i); sprintf(fnameN, "%s%02d", basename, i);
if (valid_file (fnameN)) if (valid_file(fnameN)) {
{
int fdin = open(fnameN, O_RDONLY); int fdin = open(fnameN, O_RDONLY);
if (fdin < 0) if (fdin < 0) {
{
fprintf(stderr, "can't open '%s': %s\n", fnameN, strerror(errno)); fprintf(stderr, "can't open '%s': %s\n", fnameN, strerror(errno));
++Errors; ++Errors;
} } else {
else append_file(fdin, fdout);
{
concatenate (fdin, fdout);
close(fdin); close(fdin);
} }
} } else {
else
{
if (i == 0) if (i == 0)
printf("No chunk files present (%s)", fnameN); printf("No chunk files present (%s)", fnameN);
break; break;
@@ -126,12 +113,14 @@ do_unchop (char *outfile, char *basename)
void void
concatenate (int fdin, int fdout) append_file(int fdin, int fdout)
{ {
// appends the entire contents of the input file
// to the output file
ssize_t got; ssize_t got;
for (;;) for (;;) {
{
got = read(fdin, Block, BLOCKSIZE); got = read(fdin, Block, BLOCKSIZE);
if (got <= 0) if (got <= 0)
break; break;
@@ -150,8 +139,7 @@ valid_file (char *fname)
struct stat e; struct stat e;
if (stat (fname, &e) == -1) if (stat(fname, &e) == -1) {
{
// no such file // no such file
return false; return false;
} }
@@ -180,6 +168,8 @@ replace (char *origfile, char *newfile)
char * char *
temp_file() temp_file()
{ {
// creates a new, temporary file and returns its name
char *tmp = tmpnam(NULL); char *tmp = tmpnam(NULL);
FILE *fp = fopen(tmp, "w"); FILE *fp = fopen(tmp, "w");
+28 -45
View File
@@ -1,6 +1,6 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// //
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// This software is part of the OpenBeOS distribution and is covered // This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license. // by the OpenBeOS license.
@@ -12,6 +12,7 @@
// //
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include <OS.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@@ -25,14 +26,13 @@ void display (int, int, int, char *);
// a few globals to brighten your day // a few globals to brighten your day
int TotalLines = 0; static int TotalLines = 0;
int TotalWords = 0; static int TotalWords = 0;
int TotalBytes = 0; static int TotalBytes = 0;
int ShowLines = 1;
int ShowWords = 1;
int ShowBytes = 1;
static bool ShowLines = true;
static bool ShowWords = true;
static bool ShowBytes = true;
@@ -40,40 +40,33 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (argc == 1) if (argc == 1)
{
wc_file(stdin, ""); wc_file(stdin, "");
}
else else {
{
int i; int i;
int file_count = 0; int file_count = 0;
int reset_opts = 0; bool reset_opts = false;
char *arg; char *arg;
char c; char c;
// pass 1: collect and set the options // pass 1: collect and set the options
for (i = 1; i < argc; ++i) for (i = 1; i < argc; ++i) {
{
arg = argv[i]; arg = argv[i];
if (arg[0] == '-') if (arg[0] == '-') {
{ if (!reset_opts) {
if (!reset_opts) ShowLines = ShowWords = ShowBytes = false;
{ reset_opts = true;
ShowLines = ShowWords = ShowBytes = 0;
reset_opts = 1;
} }
while (c = *++arg) while ((c = *++arg) != '\0') {
{ if (c == 'l') ShowLines = true;
if (c == 'l') ShowLines = 1; else if (c == 'w') ShowWords = true;
else if (c == 'w') ShowWords = 1; else if (c == 'c') ShowBytes = true;
else if (c == 'c') ShowBytes = 1;
} }
} }
} }
// pass 2: process filename args // pass 2: process filename args
for (i = 1; i < argc; ++i) for (i = 1; i < argc; ++i) {
{
arg = argv[i]; arg = argv[i];
if (arg[0] != '-') if (arg[0] != '-')
file_count += do_wc(arg); file_count += do_wc(arg);
@@ -83,7 +76,6 @@ main (int argc, char *argv[])
display(TotalLines, TotalWords, TotalBytes, "total"); display(TotalLines, TotalWords, TotalBytes, "total");
} }
putchar ('\n');
return 0; return 0;
} }
@@ -93,29 +85,22 @@ do_wc (char *fname)
{ {
struct stat e; struct stat e;
if (stat (fname, &e) == -1) if (stat(fname, &e) == -1) {
{
fprintf(stderr, "'%s': no such file or directory\n", fname); fprintf(stderr, "'%s': no such file or directory\n", fname);
return 0; return 0;
} }
if (S_ISDIR (e.st_mode)) if (S_ISDIR(e.st_mode)) {
{
fprintf(stderr, "'%s' is a directory\n", fname); fprintf(stderr, "'%s' is a directory\n", fname);
display(0, 0, 0, fname); display(0, 0, 0, fname);
return 1; return 1;
} } else {
else
{
FILE *fp = fopen(fname, "rb"); FILE *fp = fopen(fname, "rb");
if (fp) if (fp) {
{
wc_file(fp, fname); wc_file(fp, fname);
fclose(fp); fclose(fp);
return 1; return 1;
} } else {
else
{
fprintf(stderr, "'%s': %s\n", fname, strerror(errno)); fprintf(stderr, "'%s': %s\n", fname, strerror(errno));
return 0; return 0;
} }
@@ -133,14 +118,12 @@ wc_file (FILE *fp, char *fname)
int ns = 0; // non-spaces (consecutive count) int ns = 0; // non-spaces (consecutive count)
int c; int c;
while ((c = fgetc (fp)) != EOF) while ((c = fgetc(fp)) != EOF) {
{
++bc; ++bc;
if (c == '\n') if (c == '\n')
++lc; ++lc;
if (isspace (c)) if (isspace(c)) {
{
if (ns > 0) if (ns > 0)
++wc, ns = 0; ++wc, ns = 0;
} }