1
0

remove previous work on mm(1)

This commit is contained in:
dtb 2022-06-15 21:19:43 -04:00
parent b31e066bd5
commit bdd95bdf8e

View File

@ -1,95 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libmm.h"
#define STDIN_NAME "<stdin>"
#define STDOUT_NAME "<stdout>"
enum error{ /* possible errors */
FILE_ACCESS_ERROR,
REALLOC_ERROR
};
void
error(char *argv0, char *file_name, enum error errcode){
switch(errcode){
case FILE_ACCESS_ERROR:
fprintf(stderr, "%s: %s: cannot open file\n", argv0, file_name);
break;
case REALLOC_ERROR:
fprintf(stderr, "%s: error reallocating array\n", argv0);
break;
}
exit(1);
}
int
main(int argc, char *argv[]){
char *file_name;
FILE **input = (FILE **)malloc(sizeof(FILE*));
FILE **output = (FILE **)malloc(sizeof(FILE*));
int i;
size_t inputc = 1;
size_t outputc = 1;
*input = NULL;
*output = NULL;
/* messy argparsing, fix me to use POSIX getopt */
for(i = 1; i < argc; ++i){
if(!strcmp(argv[i++], "-i")){
if(i == argc){
fprintf(stderr, "%s: Missing option argument for \"-i\"\n", argv[0]);
return 1;
}
file_name = argv[i];
input[inputc-1] = fopen(file_name, "r");
if(input[inputc-1] == NULL){
free(input);
free(output);
error(argv[0], file_name, FILE_ACCESS_ERROR);
}
/* in one go, increment inputc, reallocate the input array, and set the new space in the array to NULL */
if((input = realloc(input, sizeof(FILE*) * (++inputc))) == NULL){
free(output);
error(argv[0], file_name, REALLOC_ERROR);
}
input[inputc-1] = NULL;
}else if(!strcmp(argv[i++], "-o")){
if(i == argc){
fprintf(stderr, "%s: Missing option argument for \"-o\"\n", argv[0]);
return 1;
}
file_name = argv[i];
output[outputc-1] = fopen(file_name, "r");
if(input[outputc-1] == NULL){
free(input);
free(output);
error(argv[0], file_name, FILE_ACCESS_ERROR);
}
if((output = realloc(output, sizeof(FILE*) * (++outputc))) == NULL){
free(input);
error(argv[0], file_name, REALLOC_ERROR);
}
output[outputc-1] = NULL;
}else{
fprintf(stderr, "%s: %s: Unrecognized option.\n", argv[0], argv[i]);
return 1;
}
}
/* default to stdin/stdout */
if(*input == NULL){
*input = stdout;
++inputc;
}
if(*output = NULL){
*output = stdout;
++outputc;
}
for(i = 0; i < inputc; ++i)
tee(input[inputc], output, outputc);
return 0;
}