Untitled
unknown
plain_text
4 years ago
4.5 kB
6
Indexable
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <math.h>
#include "chunkMethods.h"
#define ERROR_CALLOC 5
#define ERROR_WRITE 4
#define SUCCESS 0
int BUFSIZE = 1;
int main(int argc, char* argv[]) {
int count = 0;
char* buffer;
int input_fd = STDIN_FILENO;
int output_fd;
char *prefix;
char *suffix;
int defaultTrue = 1;
int return_num_bytes_read;
int return_num_bytes_write;
int hasLines = 0;
int hasWords = 0;
int hasCharacters = 0;
int hasF = 0;
int fileArg;
int defaultP = 1;
int defaultS = 1;
if ( (argc - 1) % 2 != 0) {
printNotSetError();
printCommandLineRules();
exit(0);
}
for (int i = 0; i < argc; i++) {
// checking for lines, words, and character counts in options
if (strcmp(argv[i], "-l") == 0) {
if (atoi(argv[i + 1]) == 0) {
fprintf(stderr, "Invalid command line argument. Exiting. \n");
printCommandLineRules();
exit(0);
}
count = atoi(argv[i + 1]);
defaultTrue = 0;
hasLines = 1;
}
if (strcmp(argv[i], "-w") == 0) {
if (atoi(argv[i + 1]) == 0) {
fprintf(stderr, "Invalid command line argument. Exiting. \n");
printCommandLineRules();
exit(0);
}
count = atoi(argv[i + 1]);
defaultTrue = 0;
hasWords = 1;
}
if (strcmp(argv[i], "-c") == 0) {
if (atoi(argv[i + 1]) == 0) {
fprintf(stderr, "Invalid command line argument. Exiting. \n");
printCommandLineRules();
exit(0);
}
count = atoi(argv[i + 1]);
defaultTrue = 0;
hasCharacters = 1;
}
if (strcmp(argv[i], "-f") == 0) {
fileArg = i + 1;
hasF = 1;
if (access(argv[i + 1], F_OK) != 0) {
fprintf(stderr, "File doesn't exist. \n");
exit(0);
}
input_fd = open(argv[i + 1], O_RDONLY);
}
if (strcmp(argv[i], "-p") == 0) {
prefix = argv[i + 1];
defaultP = 0;
}
if (strcmp(argv[i], "-s") == 0) {
suffix = argv[i + 1];
defaultS = 0;
}
if (defaultTrue == 1) {
count = 1000;
}
if (defaultP == 1) {
prefix = "x";
}
if (defaultS == 1) {
char suffixArray[3] = {'a', 'a', '\0'};
suffix = suffixArray;;
}
} // END FOR LOOP
// checks the values
printValues(count, prefix, suffix, hasLines, hasWords, hasCharacters);
// by this point, buffer is already made and file is opened.
// need to read files and write to files
buffer = (char *) calloc(1, sizeof(char));
if (buffer == NULL) {
perror("calloc");
return ERROR_CALLOC;
}
// declare a file name
char fileName[strlen(prefix) + strlen(suffix)];
strcpy(fileName, prefix);
strcat(fileName, suffix);
strcat(fileName, ".txt");
printf("%s\n", fileName);
int writeCount = 0;
int BUFSIZE1 = 1;
int tempSuffix = atoi(suffix);
// open original file
if (hasF == 1) {
input_fd = open(argv[fileArg], O_RDONLY);
} else {
if (!isatty(STDIN_FILENO)) {
input_fd = STDIN_FILENO;
} else {
noInputFileError();
}
}
// checking for files of the same name
if (access(fileName, F_OK) == 0) {
fileAlreadyExistsError();
} else {
printf("Continuing...\n");
}
// creating a new file for output
output_fd = open(fileName, O_WRONLY | O_CREAT, 0644);
// to read input, read into buffer
while ((return_num_bytes_read = read(input_fd, buffer, BUFSIZE1)) > 0) {
// write from buffer into new file
return_num_bytes_write = write(output_fd, buffer, (ssize_t) return_num_bytes_read);
if (hasLines == 1) {
if (*buffer == '\n') {
writeCount++;
}
}
// if they write -c
if (hasCharacters == 1) {
writeCount++;
}
// if they write -w
if (hasWords == 1) {
if (*buffer == ' ' || *buffer == '\n') {
writeCount++;
}
}
if (writeCount == count) {
if (isdigit(suffix[0]) != 0 && isdigit(suffix[1]) != 0) {
tempSuffix++;
sprintf(suffix, "%02d", tempSuffix);
} else if (suffix[0] >= 'a' || suffix[0] <= 'z') {
if (suffix[1] >= 'a' || suffix[1] <= 'z') {
suffix[1]++;
if (suffix[1] == '{') {
suffix[1] = 'a';
suffix[0]++;
}
}
}
strcpy(fileName, prefix);
strcat(fileName, suffix);
strcat(fileName, ".txt");
// making a new file with new suffix ?
output_fd = open(fileName, O_WRONLY | O_CREAT, 0644);
writeCount = 0;
}
if (return_num_bytes_read != return_num_bytes_write) {
perror("write");
return ERROR_WRITE;
}
}
printf("Done.\n");
close(input_fd);
close(output_fd);
return SUCCESS;
} // END MAIN
Editor is loading...