Commit a26b276f authored by Michal Vrábel's avatar Michal Vrábel
Browse files

zmena formatovania v copymaster

parent 4684daf8
CC = gcc
CFLAGS = -std=gnu11 -Wall -Wextra
LDFLAGS =
SOURCES = copymaster.c util.c
HEADERS = util.h
SOURCES = copymaster.c misc.c
HEADERS = misc.h
OBJDIR = obj
EXECUTABLE = copymaster
......
#include <stdio.h>
#include "util.h"
#include "misc.h"
int main(int argc, char* argv[])
{
struct CopymasterOptions cpm_options = ParseCopymaterOptions(argc, argv);
//-------------------------------------------------------------------
// Kontrola hodnot prepinacov
//-------------------------------------------------------------------
// Vypis hodnot prepinacov odstrante z finalnej verzie
printf("infile: %s\n", cpm_options.infile);
printf("outfile: %s\n", cpm_options.outfile);
printf("fast: %d\n", cpm_options.fast);
printf("slow: %d\n", cpm_options.slow);
printf("create: %d\n", cpm_options.create);
printf("create_mode: %o\n", (unsigned int)cpm_options.create_mode);
printf("overwrite: %d\n", cpm_options.overwrite);
printf("append: %d\n", cpm_options.append);
printf("lseek: %d\n", cpm_options.lseek);
printf("lseek_options.x: %d\n", cpm_options.lseek_options.x);
printf("lseek_options.pos1: %ld\n", cpm_options.lseek_options.pos1);
printf("lseek_options.pos2: %ld\n", cpm_options.lseek_options.pos2);
printf("lseek_options.num: %lu\n", cpm_options.lseek_options.num);
printf("directory: %d\n", cpm_options.directory);
printf("delete_opt: %d\n", cpm_options.delete_opt);
printf("chmod: %d\n", cpm_options.chmod);
printf("chmod_mode: %o\n", (unsigned int)cpm_options.chmod_mode);
printf("inode: %d\n", cpm_options.inode);
printf("inode_number: %lu\n", cpm_options.inode_number);
printf("infile:\t%s\n", cpm_options.infile);
printf("outfile:\t%s\n", cpm_options.outfile);
printf("fast:\t%d\n", cpm_options.fast);
printf("slow:\t%d\n", cpm_options.slow);
printf("create:\t%d\n", cpm_options.create);
printf("create_mode:\t%d\n", cpm_options.create_mode);
printf("overwrite:\t%d\n", cpm_options.overwrite);
printf("append:\t%d\n", cpm_options.append);
printf("lseek:\t%d\n", cpm_options.lseek);
printf("lseek_options.x:\t%d\n", cpm_options.lseek_options.x);
printf("lseek_options.pos1:\t%ld\n", cpm_options.lseek_options.pos1);
printf("lseek_options.pos2:\t%ld\n", cpm_options.lseek_options.pos2);
printf("lseek_options.num:\t%lu\n", cpm_options.lseek_options.num);
printf("directory:\t%d\n", cpm_options.directory);
printf("delete_opt:\t%d\n", cpm_options.delete_opt);
printf("chmod:\t%d\n", cpm_options.chmod);
printf("chmod_mode:\t%d\n", cpm_options.chmod_mode);
printf("inode:\t%d\n", cpm_options.inode);
printf("inode_number:\t%lu\n", cpm_options.inode_number);
printf("umask:\t%d\n", cpm_options.umask);
printf("umask_mode:\t%o\n", (unsigned int)cpm_options.umask_mode);
printf("link:\t%d\n", cpm_options.link);
printf("truncate:\t%d\n", cpm_options.truncate);
printf("truncate_size:\t%ld\n", cpm_options.truncate_size);
printf("sparse:\t%d\n", cpm_options.sparse);
for(unsigned int i=0; i<kUMASK_OPTIONS_MAX_SZ; ++i) {
if (cpm_options.umask_options[i][0] != 0) {
printf("umask_options[%u]: %s\n", i, cpm_options.umask_options[i]);
}
else {
break;
}
}
printf("link: %d\n", cpm_options.link);
printf("truncate: %d\n", cpm_options.truncate);
printf("truncate_size: %ld\n", cpm_options.truncate_size);
printf("sparse: %d\n", cpm_options.sparse);
//-------------------------------------------------------------------
......
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include "util.h"
struct CopymasterOptions ParseCopymaterOptions(int argc, char *argv[]) {
extern int optind;
extern char* optarg;
struct CopymasterOptions cpm_options = {0};
// premenne pre argumenty prepinacov
char c, x;
unsigned long long num;
long pos1, pos2;
while (1)
{
// viac informacii o spracovani jednotlivych prepinacov
// - pozri: man 3 getopt
int option_index = 0;
static struct option long_options[] = {
{ "fast", no_argument, 0, 'f' },
{ "slow", no_argument, 0, 's' },
{ "create", required_argument, 0, 'c' },
{ "overwrite", no_argument, 0, 'o' },
{ "append", no_argument, 0, 'a' },
{ "lseek", required_argument, 0, 'l' },
{ "directory", required_argument, 0, 'D' },
{ "delete", no_argument, 0, 'd' },
{ "chmod", required_argument, 0, 'm' },
{ "inode", required_argument, 0, 'i' },
{ "umask", required_argument, 0, 'u' },
{ "link", no_argument, 0, 'K' },
{ "truncate", required_argument, 0, 't' },
{ "sparse", no_argument, 0, 'S' },
{ 0, 0, 0, 0 },
};
c = getopt_long(argc, argv, "fsc:oal:Ddm:i:u:lt:s",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'f':
cpm_options.fast = 1;
break;
case 's':
cpm_options.slow = 1;
break;
case 'c':
cpm_options.create = 1;
sscanf(optarg, "%o", &cpm_options.create_mode);
break;
case 'o':
cpm_options.overwrite = 1;
break;
case 'a':
cpm_options.append = 1;
break;
case 'l':
sscanf(optarg, "%c,%ld,%ld,%llu", &x, &pos1, &pos2, &num);
cpm_options.lseek_options.pos1 = (off_t)pos1;
cpm_options.lseek_options.pos2 = (off_t)pos2;
cpm_options.lseek_options.num = (size_t)num;
switch (x)
{
case 'b':
cpm_options.lseek_options.x = SEEK_SET;
break;
case 'e':
cpm_options.lseek_options.x = SEEK_END;
break;
case 'c':
cpm_options.lseek_options.x = SEEK_CUR;
break;
}
break;
case 'D':
cpm_options.directory = 1;
break;
case 'd':
cpm_options.delete_opt = 1;
break;
case 'm':
cpm_options.chmod = 1;
sscanf(optarg, "%o", &cpm_options.chmod_mode);
break;
case 'i':
cpm_options.inode = 1;
sscanf(optarg, "%lu", &cpm_options.inode_number);
break;
case 'u':
// TODO
cpm_options.umask = 1;
sscanf(optarg, "%o", &cpm_options.umask_mode);
break;
case 'k':
cpm_options.link = 1;
break;
case 't':
cpm_options.truncate = 1;
sscanf(optarg, "%ld", &pos1);
cpm_options.truncate_size = (off_t)pos1;
break;
case '?':
default:
fprintf(stderr, "Usage: %s [s|f] [-t size] [-l x,pos1,pos2,num] \n", argv[0]);
exit(EXIT_FAILURE);
}
}
// Kontrola ci boli zadanie parametre infile a outfile
if (optind + 1 >= argc) {
FatalError(' ', "CHYBA PREPINACOV", 20); // TODO toto by mala byt unikatna chyba
exit(EXIT_FAILURE);
}
cpm_options.infile = argv[optind];
cpm_options.outfile = argv[optind + 1];
return cpm_options;
}
void FatalError(char c, const char* msg, int exit_status)
{
fprintf(stderr, "%c:%d\n", c, errno); // removed space
fprintf(stderr, "%c:%s\n", c, strerror(errno));
fprintf(stderr, "%c:%s\n", c, msg);
exit(exit_status);
}
#ifndef UTIL_H
#define UTIL_H
#include <sys/types.h>
struct CopymasterOptionsLseek {
int x;
off_t pos1;
off_t pos2;
size_t num;
};
struct CopymasterOptions {
const char * infile;
const char * outfile;
int fast;
int slow;
int create;
mode_t create_mode;
int overwrite;
int append;
int lseek;
struct CopymasterOptionsLseek lseek_options;
int directory;
int delete_opt;
int chmod;
mode_t chmod_mode;
int inode;
ino_t inode_number;
int umask;
mode_t umask_mode;
int link;
int truncate;
off_t truncate_size;
int sparse;
};
struct CopymasterOptions ParseCopymaterOptions(int argc, char *argv[]);
void FatalError(char c, const char* msg, int exit_status);
#endif /* UTIL_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment