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

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
// by the OpenBeOS license.
@ -47,10 +47,9 @@
#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);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -63,73 +62,66 @@ static int KBytesPerChunk = 1400; // determines size of output files
void
usage ()
{
puts ("usage: chop [-n kbyte_per_chunk] file");
puts ("Splits file into smaller files named file00, file01...");
puts ("Default split size is 1400k");
}
usage()
{
printf("Usage: chop [-n kbyte_per_chunk] file\n");
printf("Splits file into smaller files named file00, file01...\n");
printf("Default split size is 1400k\n");
}
int
main (int argc, char *argv[])
{
main(int argc, char *argv[])
{
char *arg = NULL;
char *first;
if ((argc < 2) || (argc > 4))
{
usage ();
if ((argc < 2) || (argc > 4)) {
usage();
return 0;
}
}
first = *++argv;
if (strcmp (first, "--help") == 0)
{
usage ();
if (strcmp(first, "--help") == 0) {
usage();
return 0;
}
}
if (strcmp (first, "-n") == 0)
{
if (--argc > 1)
{
if (strcmp(first, "-n") == 0) {
if (--argc > 1) {
char *num = *++argv;
if (!isdigit (*num))
printf ("-n option needs a numeric argument\n");
else
{
int b = atoi (num);
if (b < 1) b = 1;
KBytesPerChunk = b;
if (!isdigit(*num))
printf("-n option needs a numeric argument\n");
else {
int b = atoi(num);
KBytesPerChunk = (b < 1 ? 1 : b);
if (--argc > 1)
arg = *++argv;
else
printf ("no file specified\n");
}
printf("no file specified\n");
}
else
printf ("-n option needs a numeric argument\n");
}
else
printf("-n option needs a numeric argument\n");
}
else
arg = first;
if (arg)
do_chop (arg);
do_chop(arg);
putchar ('\n');
return 0;
}
}
void
do_chop (char *fname)
{
do_chop(char *fname)
{
// do some checks for validity
// then call chop_file() to do the actual read/writes
@ -138,55 +130,51 @@ do_chop (char *fname)
int fd;
// input file must exist
if (stat (fname, &e) == -1)
{
fprintf (stderr, "'%s': no such file or directory\n", fname);
if (stat(fname, &e) == -1) {
fprintf(stderr, "'%s': no such file or directory\n", fname);
return;
}
}
// and it must be not be a directory
if (S_ISDIR (e.st_mode))
{
fprintf (stderr, "'%s' is a directory\n", fname);
if (S_ISDIR(e.st_mode)) {
fprintf(stderr, "'%s' is a directory\n", fname);
return;
}
}
// needs to be big enough such that splitting it actually does something
fsize = e.st_size;
if (fsize < (KBytesPerChunk * 1024))
{
fprintf (stderr, "'%s': file is already small enough\n", fname);
if (fsize < (KBytesPerChunk * 1024)) {
fprintf(stderr, "'%s': file is already small enough\n", fname);
return;
}
}
// also, don't chop up if chunk files are already present
{
char buf[256];
strcpy (buf, fname);
strcat (buf, "00");
if (stat (buf, &e) >= 0)
{
fprintf (stderr, "'%s' already exists - aborting\n", buf);
return;
char buf[256];
strcpy(buf, fname);
strcat(buf, "00");
if (stat(buf, &e) >= 0) {
fprintf(stderr, "'%s' already exists - aborting\n", buf);
return;
}
}
// finally! chop up the file
fd = open (fname, O_RDONLY);
fd = open(fname, O_RDONLY);
if (fd < 0)
fprintf (stderr, "can't open '%s': %s\n", fname, strerror (errno));
else
{
chop_file (fd, fname, fsize);
close (fd);
}
fprintf(stderr, "can't open '%s': %s\n", fname, strerror(errno));
else {
chop_file(fd, fname, fsize);
close(fd);
}
}
void
chop_file (int fdin, char *fname, off_t fsize)
{
chop_file(int fdin, char *fname, off_t fsize)
{
const off_t chunk_size = KBytesPerChunk * 1024; // max bytes written to any output file
bool open_next_file = true; // when to open a new output file
@ -204,57 +192,52 @@ chop_file (int fdin, char *fname, off_t fsize)
char *beg = Block; // pointer to the beginning of the block data to be written out
char *end = Block; // end of the current block (init to beginning to force first block read)
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)
{
if (beg >= end)
{
while (total_written < fsize) {
if (beg >= end) {
// read in another block
got = read (fdin, Block, BLOCKSIZE);
got = read(fdin, Block, BLOCKSIZE);
if (got <= 0)
break;
beg = Block;
end = Block + got - 1;
}
}
if (open_next_file)
{
if (open_next_file) {
// start a new output file
sprintf (fnameN, "%s%02d", fname, index++);
fdout = open (fnameN, O_WRONLY|O_CREAT);
if (fdout < 0)
{
fprintf (stderr, "unable to create chunk file '%s': %s\n", fnameN, strerror (errno));
sprintf(fnameN, "%s%02d", fname, index++);
fdout = open(fnameN, O_WRONLY|O_CREAT);
if (fdout < 0) {
fprintf(stderr, "unable to create chunk file '%s': %s\n", fnameN, strerror(errno));
return;
}
}
curr_written = 0;
open_next_file = false;
}
}
needed = chunk_size - curr_written;
avail = end - beg + 1;
if (needed > avail)
needed = avail;
if (needed > 0)
{
put = write (fdout, beg, needed);
if (needed > 0) {
put = write(fdout, beg, needed);
beg += put;
}
curr_written += put;
total_written += put;
if (curr_written >= chunk_size)
{
// the current output file is full
close (fdout);
open_next_file = true;
}
curr_written += put;
total_written += put;
}
if (curr_written >= chunk_size) {
// the current output file is full
close(fdout);
open_next_file = true;
}
}
// close up the last output file
close (fdout);
}
close(fdout);
}

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
// by the OpenBeOS license.
@ -8,7 +8,7 @@
//
// File: error.c
// Author: Daniel Reinhold (danielre@users.sf.net)
// 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[])
{
if (argc != 2)
printf("usage: error number\n"
"Displays the message text for OS error codes. "
printf("Usage: error number\n"
"Prints the message text for OS error codes. "
"The error number can be in decimal, hex or octal.\n");
else
print_error(argv[1]);

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
// by the OpenBeOS license.
@ -19,188 +19,173 @@
#include <sys/stat.h>
void do_hd (char *);
void dump_file (FILE *);
void display (uint32, uint8 *);
void display (uint32, uint8 *);
void do_hd (char *);
void dump_file (FILE *);
char *hexbytes (uint8 *);
char *printable (uint8 *);
void usage ();
void usage (void);
int BytesBetweenSpaces = 1;
static int BytesBetweenSpace = 1;
void
usage ()
{
printf ("usage:\thd [-n N] [file]\n");
puts ("\t-n expects a number between 1 and 16 and specifies");
puts ("\tthe number of bytes between spaces.");
puts ("");
puts ("\tIf no file is specified, input is read from stdin");
}
{
printf ("Usage:\thd [-n N] [file]\n");
printf("\t-n expects a number between 1 and 16 and specifies\n");
printf("\tthe number of bytes between spaces.\n");
printf("\n\tIf no file is specified, input is read from stdin\n");
}
int
main (int argc, char *argv[])
{
main(int argc, char *argv[])
{
char *arg = NULL;
if (argc == 1)
{
dump_file (stdin);
}
else
{
dump_file(stdin);
else {
char *first = *++argv;
if (strcmp (first, "--help") == 0)
{
usage ();
if (strcmp(first, "--help") == 0) {
usage();
return 0;
}
}
if (strcmp (first, "-n") == 0)
{
if (--argc > 1)
{
if (strcmp(first, "-n") == 0) {
if (--argc > 1) {
char *num = *++argv;
if (!isdigit (*num))
printf ("-n option needs a numeric argument\n");
else
{
int b = atoi (num);
if (!isdigit(*num))
printf("-n option needs a numeric argument\n");
else {
int b = atoi(num);
if (b < 1) b = 1;
if (b > 16) b = 16;
BytesBetweenSpaces = b;
BytesBetweenSpace = b;
if (--argc > 1)
arg = *++argv;
else
printf ("no file specified\n");
}
printf("no file specified\n");
}
else
printf ("-n option needs a numeric argument\n");
}
else
printf("-n option needs a numeric argument\n");
}
else
arg = first;
if (arg)
do_hd (arg);
}
putchar ('\n');
return 0;
do_hd(arg);
}
putchar('\n');
return 0;
}
void
do_hd (char *fname)
{
do_hd(char *fname)
{
struct stat e;
if (stat (fname, &e) == -1)
{
fprintf (stderr, "'%s': no such file or directory\n", fname);
if (stat(fname, &e) == -1) {
fprintf(stderr, "'%s': no such file or directory\n", fname);
return;
}
if (S_ISDIR (e.st_mode))
fprintf (stderr, "'%s' is a directory\n", fname);
else
{
FILE *fp = fopen (fname, "rb");
if (fp)
{
dump_file (fp);
fclose (fp);
}
else
fprintf (stderr, "'%s': %s\n", fname, strerror (errno));
}
}
if (S_ISDIR(e.st_mode))
fprintf(stderr, "'%s' is a directory\n", fname);
else {
FILE *fp = fopen(fname, "rb");
if (fp) {
dump_file(fp);
fclose(fp);
}
else
fprintf(stderr, "'%s': %s\n", fname, strerror(errno));
}
}
void
dump_file (FILE *fp)
{
dump_file(FILE *fp)
{
size_t got;
uint32 offset = 0;
uint8 data[16];
while ((got = fread (data, 1, 16, fp)) == 16)
{
display (offset, data);
while ((got = fread(data, 1, 16, fp)) == 16) {
display(offset, data);
offset += 16;
}
if (got > 0)
{
memset (data+got, ' ', 16-got);
display (offset, data);
}
}
if (got > 0) {
memset(data+got, ' ', 16-got);
display(offset, data);
}
}
void
display (uint32 offset, uint8 *data)
{
printf ("%08x ", offset);
printf (" %s ", hexbytes (data));
printf ("%16s ", printable (data));
display(uint32 offset, uint8 *data)
{
printf("%08x ", offset);
printf(" %s ", hexbytes(data));
printf("%16s ", printable(data));
putchar ('\n');
}
putchar('\n');
}
char *
hexbytes (uint8 *s)
{
hexbytes(uint8 *s)
{
static char buf[64];
char *p = buf;
uint8 c;
int i;
int n = 0;
for (i = 0; i < 16; ++i)
{
for (i = 0; i < 16; ++i) {
c = *s++;
*p++ = "0123456789abcdef"[c/16];
*p++ = "0123456789abcdef"[c%16];
if (++n == BytesBetweenSpaces)
{
if (++n == BytesBetweenSpace) {
*p++ = ' ';
n = 0;
}
if ((i == 7) && (BytesBetweenSpaces == 1))
*p++ = ' ';
}
if ((i == 7) && (BytesBetweenSpace == 1))
*p++ = ' ';
}
*p++ = ' ';
*p = 0;
return buf;
}
}
char *
printable (uint8 *s)
{
{
static char buf[16];
int n = 16;
char *p = buf;
uint8 c;
int i = 16;
while (n--)
{
while (i--) {
c = *s++;
*p++ = (isgraph (c) ? c : '.');
}
*p++ = (isgraph(c) ? c : '.');
}
*p = 0;
return buf;
}
}

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
// by the OpenBeOS license.
@ -23,80 +23,71 @@ void show_memory_totals (void);
int
main (int argc, char **argv)
{
//
show_memory_totals ();
main(int argc, char **argv)
{
show_memory_totals();
if (argc == 1)
{
// list for all teams
if (argc == 1) {
int32 cookie = 0;
team_info info;
while (get_next_team_info (&cookie, &info) >= B_OK)
{
list_area_info (info.team);
}
}
// list for all teams
while (get_next_team_info(&cookie, &info) >= B_OK)
list_area_info(info.team);
}
else
// list for each team_id on the command line
while (--argc)
{
list_area_info (atoi (*++argv));
}
list_area_info(atoi(*++argv));
return 0;
}
}
void
show_memory_totals ()
{
show_memory_totals()
{
int32 max = 0, used = 0, left;
system_info sys;
if (get_system_info (&sys) == B_OK)
{
if (get_system_info(&sys) == B_OK) {
// pages are 4KB
max = sys.max_pages * 4;
used = sys.used_pages * 4;
}
}
left = max - used;
printf ("memory: total: %4dKB, used: %4dKB, left: %4dKB\n", max, used, left);
}
printf("memory: total: %4dKB, used: %4dKB, left: %4dKB\n", max, used, left);
}
void
list_area_info (team_id id)
{
int32 cookie = 0;
team_info this_team;
area_info this_area;
list_area_info(team_id id)
{
int32 cookie = 0;
team_info this_team;
area_info this_area;
if (get_team_info (id, &this_team) == B_BAD_TEAM_ID)
{
printf ("\nteam %d unknown\n", id);
if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
printf("\nteam %d unknown\n", id);
return;
}
printf ("\nTEAM %4d (%s):\n", id, this_team.args);
printf (" ID name address size alloc. #-cow #-in #-out\n");
printf ("--------------------------------------------------------------------------------\n");
while (get_next_area_info (id, &cookie, &this_area) == B_OK)
{
printf ("%5d %29s %.8x %8x %8x %5d %5d %5d\n",
this_area.area,
this_area.name,
this_area.address,
this_area.size,
this_area.ram_size,
this_area.copy_count,
this_area.in_count,
this_area.out_count);
}
}
printf("\nTEAM %4d (%s):\n", id, this_team.args);
printf(" ID name address size alloc. #-cow #-in #-out\n");
printf("--------------------------------------------------------------------------------\n");
while (get_next_area_info(id, &cookie, &this_area) == B_OK) {
printf("%5d %29s %.8x %8x %8x %5d %5d %5d\n",
this_area.area,
this_area.name,
this_area.address,
this_area.size,
this_area.ram_size,
this_area.copy_count,
this_area.in_count,
this_area.out_count);
}
}

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
// by the OpenBeOS license.
@ -18,62 +18,54 @@
#include <OS.h>
void list_image_info (team_id id);
void list_image_info (team_id id);
int
main (int argc, char **argv)
{
//
if (argc == 1)
{
// list for all teams
main(int argc, char **argv)
{
if (argc == 1) {
int32 cookie = 0;
team_info info;
while (get_next_team_info (&cookie, &info) >= B_OK)
{
list_image_info (info.team);
}
}
// list for all teams
while (get_next_team_info(&cookie, &info) >= B_OK)
list_image_info(info.team);
}
else
// list for each team_id on the command line
while (--argc)
{
list_image_info (atoi (*++argv));
}
list_image_info(atoi(*++argv));
return 0;
}
}
void
list_image_info (team_id id)
{
int32 cookie = 0;
team_info this_team;
image_info this_image;
list_image_info(team_id id)
{
int32 cookie = 0;
team_info this_team;
image_info this_image;
if (get_team_info (id, &this_team) == B_BAD_TEAM_ID)
{
printf ("\nteam %d unknown\n", id);
if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
printf("\nteam %d unknown\n", id);
return;
}
printf ("\nTEAM %4d (%s):\n", id, this_team.args);
printf (" ID name text data seq# init#\n");
printf ("--------------------------------------------------------------------------------------------------------\n");
while (get_next_image_info (id, &cookie, &this_image) == B_OK)
{
printf ("%5d %64s %.8x %.8x %4d %10u\n",
this_image.id,
this_image.name,
this_image.text,
this_image.data,
this_image.sequence,
this_image.init_order);
}
}
printf("\nTEAM %4d (%s):\n", id, this_team.args);
printf(" ID name text data seq# init#\n");
printf("--------------------------------------------------------------------------------------------------------\n");
while (get_next_image_info(id, &cookie, &this_image) == B_OK) {
printf("%5d %64s %.8x %.8x %4d %10u\n",
this_image.id,
this_image.name,
this_image.text,
this_image.data,
this_image.sequence,
this_image.init_order);
}
}

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
// by the OpenBeOS license.
@ -23,75 +23,66 @@ void show_port_totals (void);
int
main (int argc, char **argv)
{
//
show_port_totals ();
main(int argc, char **argv)
{
show_port_totals();
if (argc == 1)
{
// list for all teams
if (argc == 1) {
int32 cookie = 0;
team_info info;
while (get_next_team_info (&cookie, &info) >= B_OK)
{
list_team_ports (info.team);
}
}
// list for all teams
while (get_next_team_info(&cookie, &info) >= B_OK)
list_team_ports(info.team);
}
else
// list for each team_id on the command line
while (--argc)
{
list_team_ports (atoi (*++argv));
}
list_team_ports(atoi(*++argv));
return 0;
}
}
void
show_port_totals ()
{
show_port_totals()
{
int32 max = 0, used = 0, left;
system_info sys;
if (get_system_info (&sys) == B_OK)
{
if (get_system_info(&sys) == B_OK) {
max = sys.max_ports;
used = sys.used_ports;
}
}
left = max - used;
printf ("port: total: %5d, used: %5d, left: %5d\n", max, used, left);
}
printf("port: total: %5d, used: %5d, left: %5d\n", max, used, left);
}
void
list_team_ports (team_id id)
{
list_team_ports(team_id id)
{
int32 cookie = 0;
port_info this_port;
team_info this_team;
if (get_team_info (id, &this_team) == B_BAD_TEAM_ID)
{
printf ("\nteam %d unknown\n", id);
if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
printf("\nteam %d unknown\n", id);
return;
}
printf ("\nTEAM %4d (%s):\n", id, this_team.args);
printf (" ID name capacity queued\n");
printf ("----------------------------------------------------\n");
while (get_next_port_info (id, &cookie, &this_port) == B_OK)
{
printf ("%5d %28s %8d %6d\n",
this_port.port,
this_port.name,
this_port.capacity,
this_port.queue_count);
}
}
printf("\nTEAM %4d (%s):\n", id, this_team.args);
printf(" ID name capacity queued\n");
printf("----------------------------------------------------\n");
while (get_next_port_info(id, &cookie, &this_port) == B_OK) {
printf("%5d %28s %8d %6d\n",
this_port.port,
this_port.name,
this_port.capacity,
this_port.queue_count);
}
}

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
// by the OpenBeOS license.
@ -29,7 +29,7 @@ main(int argc, char *argv[])
char *arg = (argc == 2 ? argv[1] : NULL);
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");
return 1;
}
@ -45,26 +45,25 @@ print_env(char *arg)
if (arg == NULL) {
// print all environment 'key=value' pairs (one per line)
while (*env)
while (*env)
printf("%s\n", *env++);
return 0;
}
else {
} else {
// print only the value of the specified variable
char *s;
int len = strlen(arg);
bool found = false;
while ((s = *env++) != NULL)
if (!strncmp(s, arg, len)) {
char *p = strchr(s, '=');
if (p) {
while ((s = *env++) != NULL)
if (!strncmp(s, arg, len)) {
char *p = strchr(s, '=');
if (p) {
printf("%s\n", p+1);
found = true;
}
}
return found ? 0 : 1;
return (found ? 0 : 1);
}
}

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
// by the OpenBeOS license.
@ -80,7 +80,7 @@ int
main(int argc, char *argv[])
{
if (argc < 2)
printf("usage: printf format [arguments]\n");
printf("Usage: printf format [arguments]\n");
else
do_printf(argc, argv);
@ -108,8 +108,7 @@ do_printf(int argc, char *argv[])
++Format, putchar('%');
else
print_next_arg();
}
else {
} else {
e = escaped(c, &Format);
putchar(e);
}
@ -194,7 +193,7 @@ get_next_specifier(char *spec_buffer, int buflen)
if (++len > buflen)
break;
if (isalpha (c) || c == '%' || c == '\\') {
if (isalpha(c) || c == '%' || c == '\\') {
fc = c;
break;
}
@ -272,13 +271,13 @@ print_escaped_string(char *fmt, char *s)
char c;
char e;
while (lpad-- > 0) putchar (' '); // pad on the left
while (lpad-- > 0) putchar(' '); // pad on the left
while (len-- > 0) {
c = *s++;
e = escaped(c, &s);
putchar(e);
}
while (rpad-- > 0) putchar (' '); // pad on the right
while (rpad-- > 0) putchar(' '); // pad on the right
}
if (halt)
@ -383,7 +382,7 @@ escaped(char c, char **src)
int
get_decimal (char **src)
get_decimal(char **src)
{
// grabs a decimal integer value from the source string
// and returns the full computed value.
@ -394,7 +393,7 @@ get_decimal (char **src)
char c;
int n = 0;
while (isdigit (c = *s)) {
while (isdigit(c = *s)) {
++s;
n = n * 10 + (c - '0');
}
@ -407,7 +406,7 @@ get_decimal (char **src)
#define isodigit(c) (isdigit(c) && !(c == '8' || c == '9'))
bool
parse_octal (char **src, int min_digits, int max_digits, char *octval)
parse_octal(char **src, int min_digits, int max_digits, char *octval)
{
// searches for an octal value in the source string.
// returns true if valid octal digits were found
@ -427,7 +426,7 @@ parse_octal (char **src, int min_digits, int max_digits, char *octval)
for (i = 0; i < max_digits; ++i) {
c = *s;
if (isodigit (c)) {
if (isodigit(c)) {
++s;
n = n * 8 + (c - '0');
}
@ -446,7 +445,7 @@ parse_octal (char **src, int min_digits, int max_digits, char *octval)
bool
parse_hex (char **src, int min_digits, int max_digits, char *hexval)
parse_hex(char **src, int min_digits, int max_digits, char *hexval)
{
// searches for a hex value in the source string.
// returns true if valid hex digits were found
@ -466,7 +465,7 @@ parse_hex (char **src, int min_digits, int max_digits, char *hexval)
for (i = 0; i < max_digits; ++i) {
c = tolower(*s);
if (isxdigit (c)) {
if (isxdigit(c)) {
++s;
n *= 16;
if (c > '9')

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
// by the OpenBeOS license.
@ -20,13 +20,12 @@
#include <sys/stat.h>
void usage (void);
void append_file (int, int);
void do_unchop (char *, char *);
void concatenate (int, int);
bool valid_file (char *);
void replace (char *, char *);
char *temp_file ();
void usage (void);
bool valid_file (char *);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -40,150 +39,141 @@ static int Errors = 0;
void
usage ()
{
puts ("usage: unchop file");
puts ("Concatenates files named file00, file01... into file");
}
usage()
{
printf("Usage: unchop file\n");
printf("Concatenates files named file00, file01... into file\n");
}
int
main (int argc, char *argv[])
{
if (argc != 2)
{
usage ();
main(int argc, char *argv[])
{
if (argc != 2) {
usage();
return 0;
}
else
{
}
else {
char *origfile = argv[1];
char *tmpfile = origfile;
bool needs_replace = false;
if (valid_file (origfile))
{
if (valid_file(origfile)) {
// output file already exists -- write to temp file
tmpfile = temp_file ();
tmpfile = temp_file();
needs_replace = true;
}
do_unchop (tmpfile, origfile);
if (needs_replace)
{
if (Errors == 0)
replace (origfile, tmpfile);
else
remove (tmpfile);
}
}
putchar ('\n');
return Errors;
do_unchop(tmpfile, origfile);
if (needs_replace) {
if (Errors == 0)
replace(origfile, tmpfile);
else
remove(tmpfile);
}
}
return Errors;
}
void
do_unchop (char *outfile, char *basename)
{
int fdout = open (outfile, O_WRONLY|O_CREAT|O_APPEND);
do_unchop(char *outfile, char *basename)
{
int fdout = open(outfile, O_WRONLY|O_CREAT|O_APPEND);
if (fdout < 0)
fprintf (stderr, "can't open '%s': %s\n", outfile, strerror (errno));
else
{
fprintf(stderr, "can't open '%s': %s\n", outfile, strerror(errno));
else {
int i;
char fnameN[256];
for (i = 0; i < 999999; ++i)
{
sprintf (fnameN, "%s%02d", basename, i);
for (i = 0; i < 999999; ++i) {
sprintf(fnameN, "%s%02d", basename, i);
if (valid_file (fnameN))
{
int fdin = open (fnameN, O_RDONLY);
if (fdin < 0)
{
fprintf (stderr, "can't open '%s': %s\n", fnameN, strerror (errno));
if (valid_file(fnameN)) {
int fdin = open(fnameN, O_RDONLY);
if (fdin < 0) {
fprintf(stderr, "can't open '%s': %s\n", fnameN, strerror(errno));
++Errors;
}
else
{
concatenate (fdin, fdout);
close (fdin);
}
} else {
append_file(fdin, fdout);
close(fdin);
}
else
{
} else {
if (i == 0)
printf ("No chunk files present (%s)", fnameN);
printf("No chunk files present (%s)", fnameN);
break;
}
}
close (fdout);
}
close(fdout);
}
}
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;
for (;;)
{
got = read (fdin, Block, BLOCKSIZE);
for (;;) {
got = read(fdin, Block, BLOCKSIZE);
if (got <= 0)
break;
write (fdout, Block, got);
}
write(fdout, Block, got);
}
}
bool
valid_file (char *fname)
{
valid_file(char *fname)
{
// for this program, a valid file is one that:
// a) exists (that always helps)
// b) is a regular file (not a directory, link, etc.)
struct stat e;
if (stat (fname, &e) == -1)
{
if (stat(fname, &e) == -1) {
// no such file
return false;
}
return (S_ISREG (e.st_mode));
}
return (S_ISREG(e.st_mode));
}
void
replace (char *origfile, char *newfile)
{
replace(char *origfile, char *newfile)
{
// replace the contents of the original file
// with the contents of the new file
char buf[1000];
// delete the original file
remove (origfile);
remove(origfile);
// rename the new file to the original file name
sprintf (buf, "mv \"%s\" \"%s\"", newfile, origfile);
system (buf);
}
sprintf(buf, "mv \"%s\" \"%s\"", newfile, origfile);
system(buf);
}
char *
temp_file ()
{
char *tmp = tmpnam (NULL);
temp_file()
{
// creates a new, temporary file and returns its name
FILE *fp = fopen (tmp, "w");
fclose (fp);
char *tmp = tmpnam(NULL);
FILE *fp = fopen(tmp, "w");
fclose(fp);
return tmp;
}
}

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
// by the OpenBeOS license.
@ -12,6 +12,7 @@
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include <OS.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
@ -25,107 +26,91 @@ void display (int, int, int, char *);
// a few globals to brighten your day
int TotalLines = 0;
int TotalWords = 0;
int TotalBytes = 0;
int ShowLines = 1;
int ShowWords = 1;
int ShowBytes = 1;
static int TotalLines = 0;
static int TotalWords = 0;
static int TotalBytes = 0;
static bool ShowLines = true;
static bool ShowWords = true;
static bool ShowBytes = true;
int
main (int argc, char *argv[])
{
main(int argc, char *argv[])
{
if (argc == 1)
{
wc_file (stdin, "");
}
else
{
int i;
int file_count = 0;
int reset_opts = 0;
wc_file(stdin, "");
else {
int i;
int file_count = 0;
bool reset_opts = false;
char *arg;
char c;
char c;
// pass 1: collect and set the options
for (i = 1; i < argc; ++i)
{
for (i = 1; i < argc; ++i) {
arg = argv[i];
if (arg[0] == '-')
{
if (!reset_opts)
{
ShowLines = ShowWords = ShowBytes = 0;
reset_opts = 1;
}
while (c = *++arg)
{
if (c == 'l') ShowLines = 1;
else if (c == 'w') ShowWords = 1;
else if (c == 'c') ShowBytes = 1;
}
if (arg[0] == '-') {
if (!reset_opts) {
ShowLines = ShowWords = ShowBytes = false;
reset_opts = true;
}
while ((c = *++arg) != '\0') {
if (c == 'l') ShowLines = true;
else if (c == 'w') ShowWords = true;
else if (c == 'c') ShowBytes = true;
}
}
}
// pass 2: process filename args
for (i = 1; i < argc; ++i)
{
for (i = 1; i < argc; ++i) {
arg = argv[i];
if (arg[0] != '-')
file_count += do_wc (arg);
}
file_count += do_wc(arg);
}
if (file_count > 1)
display (TotalLines, TotalWords, TotalBytes, "total");
}
display(TotalLines, TotalWords, TotalBytes, "total");
}
putchar ('\n');
return 0;
}
int
do_wc (char *fname)
{
do_wc(char *fname)
{
struct stat e;
if (stat (fname, &e) == -1)
{
fprintf (stderr, "'%s': no such file or directory\n", fname);
if (stat(fname, &e) == -1) {
fprintf(stderr, "'%s': no such file or directory\n", fname);
return 0;
}
}
if (S_ISDIR (e.st_mode))
{
fprintf (stderr, "'%s' is a directory\n", fname);
display (0, 0, 0, fname);
if (S_ISDIR(e.st_mode)) {
fprintf(stderr, "'%s' is a directory\n", fname);
display(0, 0, 0, fname);
return 1;
}
else
{
FILE *fp = fopen (fname, "rb");
if (fp)
{
wc_file (fp, fname);
fclose (fp);
} else {
FILE *fp = fopen(fname, "rb");
if (fp) {
wc_file(fp, fname);
fclose(fp);
return 1;
}
else
{
fprintf (stderr, "'%s': %s\n", fname, strerror (errno));
} else {
fprintf(stderr, "'%s': %s\n", fname, strerror(errno));
return 0;
}
}
}
}
void
wc_file (FILE *fp, char *fname)
{
wc_file(FILE *fp, char *fname)
{
int lc = 0; // line count
int wc = 0; // word count
int bc = 0; // byte count
@ -133,20 +118,18 @@ wc_file (FILE *fp, char *fname)
int ns = 0; // non-spaces (consecutive count)
int c;
while ((c = fgetc (fp)) != EOF)
{
while ((c = fgetc(fp)) != EOF) {
++bc;
if (c == '\n')
++lc;
if (isspace (c))
{
if (isspace(c)) {
if (ns > 0)
++wc, ns = 0;
}
}
else
++ns;
}
}
if (ns > 0)
++wc;
@ -155,18 +138,18 @@ wc_file (FILE *fp, char *fname)
TotalWords += wc;
TotalBytes += bc;
display (lc, wc, bc, fname);
}
display(lc, wc, bc, fname);
}
void
display (int lc, int wc, int bc, char *fname)
{
if (ShowLines) printf ("%7d", lc);
if (ShowWords) printf ("%8d", wc);
if (ShowBytes) printf ("%8d", bc);
if (*fname) printf (" %s", fname);
{
if (ShowLines) printf("%7d", lc);
if (ShowWords) printf("%8d", wc);
if (ShowBytes) printf("%8d", bc);
if (*fname) printf(" %s", fname);
putchar ('\n');
}
putchar('\n');
}