Question
UNIX C-Programming Need at least 2 .c files & 1 .h file You will implement the command chunk to divide a large file ( filename.txt
UNIX C-Programming Need at least 2 .c files & 1 .h file
You will implement the command chunk to divide a large file (filename.txt) into files of 1,000 lines each. At the UNIX shell prompt, your command should accept either:
chunk [options] -f filename.txt [-p prefix] [-s suffix] chunk [options] [-p prefix] < filename.txt
[options] in square brackets indicates that the command recognizes both excluding [options], or including options. The options modifies the size of output of the files, as follows.
-l line_count ---- Creates smaller files of line_count lines in length (instead of the default of 1,000 lines). -w word_count ---- Creates smaller files of world_count lines in length (instead of the default of 1,000 lines). -c character_count ---- Creates smaller files of character_count lines in length (instead of the default of 1,000 lines). .
The chunk command will give each output file it creates the name prefix with an extension or suffix tacked to the end that indicates its order. By default, the chunk command should use:
2 character alphabetical suffixes: ----- aa to the first output file, ----- ab to the second output file, proceeding through the alphabet to ----- ac ..., az, ba, bb, bc, ... bz, ca, cb, ... cz, ... ----- zz for subsequent files.
If the user prefers numeric:
-s suffix ---- Creates a 2 digit character numeric suffix, with starting number indicated by the suffix, so that if suffix -is 00, then chunk will generate:
----- 00 to the first output file, ----- 01 to the second output file, proceeding through the alphabet (numberic) to ----- 99 for subsequent files. -p prefix ---- If a prefix is not indicated, the default prefix will be x.
EXIT STATUS The chunk utility exits 0 on success, and >0 if an error occurs.
Example:
chunk -l 100 -f maria.txt -p part- -s 00
Here chunk divides file maria.txt to new files named part-00, part-01, part-02, ... part-?? that are each 100 lines long, except possibly the last file that may be less than 100 lines long.
chunk -w 100 -f maria.txt -p part- -s 00
Here chunk divides file maria.txt to new files named part-00, part-01, part-02, ... part-?? that are each 100 words, except possibly for the last file that may be less than 100 words long.
Assume that if an argument as an optional parameter that parameter is included.
Other Requirements:
The join the files back together again the cat command:
cat part-00 part-01 part-02 ... > maria2.txt
should generate file maria2.txt so that it is identical to the original maria.txt.
The permissions of the output files should be the same as the permissions of the input file.
You should use try to make your program as efficient as possible.
Your program should not overwrite existing files, e.g., if there is a file part-01 in your directory already, your program should print out an error message and terminate.
Your program should be readable.
You should comment your code. For printing out errors, you must print to stderr.
You may only use read(), write() for reading the input file, and writing to the output files. You may use other input/output functions for other things, e.g., for printing errors for example, or for debugging, you may use fprintf(), if you prefer.
pareargs.c:
#include #include #include
/* GLOBALS */ int number_random_numbers = 1; int seed = 1; int verbose = 0; int help = 0;
void getCommanLine( int argc, char **argv ) { extern char *optarg; extern int optind; int ch;
if( argc > 5 ) fprintf( stderr, "usage: generate [-n ] " );
while ( ( ch = getopt ( argc, argv, ":vn:s:h" ) ) != EOF ) { switch ( ch ) { case 's': // with optional integer argument { seed = atoi( optarg ); break; } case 'n': { printf("hello %s ", optarg); number_random_numbers = atoi( optarg ); break; } case 'v': { verbose = 1; break; } case 'h': { help = 1; break; } case '?': default: { fprintf ( stderr, "%s: unrecognized option -%c ", argv[0], ch ); exit(1); break ; } } /* switch */ } /* while */ }
int main( int argc, char *argv[] ) { int i;
getCommanLine( argc, argv ); if( help == 1 ) { printf( "%s -s seedinteger -n #rands -v -h ", argv[0] ) ; printf( "%s -s 55 -n 10 -v -h ", argv[0] ) ; }
if( verbose == 1 ) printf( "%s verbose is set to %d ", argv[0], verbose ) ;
srand(seed);
if( verbose == 1 ) { if( number_random_numbers == 1 ) printf( "generating 1 random number: " ) ; else printf( "generating %d random numbers: ", number_random_numbers ) ; } for( i = 0; i < number_random_numbers; i ++ ) printf( "%d ", rand() ) ; }
MakeFile:
CFLAGS = -g -Wall CC = gcc
# if make followed by no target then will compile 'Hello' all: parseargs
# before the "rm" there is only one white space character: a "TAB". # If you have any other white space this Makefile while not work
# before the "${CC}" there is only one white space character: a "TAB". # If you have any other white space this makefile while not work
parseargs: parseargs.o $(CC) $(CFLAGS) $@.c -o $@
# generic %.o : %.c $(CC) $(CFLAGS) -c $< -o $@
clean: rm -f core *.o a.out rm -f parseargs rm -f DIRECTORY.html rm -rf *.dSYM
webclean: rm -f DIRECTORY.html
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started