Commit 4684daf8 authored by Michal Vrabel's avatar Michal Vrabel
Browse files

Skompilovatelna verzia

parent ed2d002e
CC = gcc
CFLAGS = -std=gnu11 -Wall -Wextra
LDFLAGS =
SOURCES = copymaster.c
HEADERS =
OBJDIR = build
SOURCES = copymaster.c util.c
HEADERS = util.h
OBJDIR = obj
EXECUTABLE = copymaster
_OBJ = $(SOURCES:.c=.o)
......@@ -13,7 +13,7 @@ _DEPS = $(HEADERS)
DEPS = $(_DEPS)
OBJ = $(patsubst %,$(OBJDIR)/%,$(_OBJ))
$(OBJDIR)/%.o: %.c %.h
$(OBJDIR)/%.o: %.c $(DEPS) directories
$(CC) -c -o $@ $< $(CFLAGS)
$(EXECUTABLE): $(OBJ)
......
// sem vlozte potrebne hlavickove subory --------------------------------
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
// ----------------------------------------------------------------------
int main(int argc, char* argv[])
{
extern char* optarg;
extern int optind, opterr, optopt;
// lokalne premenne
int c;
int digit_optind = 0;
// premenne pre argumenty prepinacov
long size;
char x;
long pos1, pos2, num;
#include "util.h"
// spracovanie prepinacov
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
// viac informacii o spracovani jednotlivych moznosti - pozri - // man 3
// getopt
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, "sft:l:", // doplnte dalsie volby (options)
// podla zadania - aj ci su s argumentom
long_options, &option_index);
if (c == -1)
break; // ziadne dalsie volby
switch (c)
{
case 's': // spracovanie - moznost 's'
printf("prepinac - s\n");
// pokracovanie spracovania
break;
case 'f': // spracovanie - moznosť 'f'
printf("prepinac - f\n");
// pokracovanie spracovania
break;
case 't': // spracovanie - moznost 't'
printf("prepinac - t\n");
size = atoi(optarg); // spracovanie argumentu danej volby (option)
printf("size=%ld\n", size);
// pokracovanie spracovania
break;
case 'l': // spracovanie - option 'l'
printf("prepinac - l\n");
sscanf(optarg, "%c,%ld,%ld,%ld", &x, &pos1, &pos2,
&num); // spracovanie argumentu danej voľby (option)
printf("x=%c\n", x);
printf("pos1=%ld\n", pos1);
printf("pos2=%ld\n", pos2);
printf("num=%ld\n", num);
// ... pokracovanie spracovania
break;
int main(int argc, char* argv[])
{
struct CopymasterOptions cpm_options = ParseCopymaterOptions(argc, argv);
// analogickym sposobom osetrite dalsie volby
// ....
// vsetky volby osetrene
// Kontrola hodnot prepinacov
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);
// osetrenie chyby spracovania prepinacov
case '?':
// break;
default:
fprintf(stderr, "Usage: %s [s|f] [-t size] [-l x,pos1,pos2,num] \n", argv[0]);
exit(EXIT_FAILURE);
}
//-------------------------------------------------------------------
// Osetrenie prepinacov pred kopirovanim
//-------------------------------------------------------------------
if (cpm_options.fast && cpm_options.slow) {
FatalError(' ', "CHYBA PREPINACOV", 20); // TODO znak pre prefix
}
// koniec spracovanie prepinacov
// *****************************************************************************************************************
//*******************************************
// Sem pridajte kod pre osetrenie prepinacov pred kopirovanim
//******************************************
//*******************************************
// Sem pridajte kod pre kopirovanie suborov
//******************************************
//*******************************************
// Sem pridajte kod pre vypis adresara
//******************************************
//*******************************************
// Sem pridajte kod pre osetrenie prepinacov po kopirovani
//******************************************
// TODO Nezabudnut dalsie kontroly kombinacii prepinacov ...
//-------------------------------------------------------------------
// Kopirovanie suborov
//-------------------------------------------------------------------
// TODO Implementovat kopirovanie suborov
// cpm_options.infile
// cpm_options.outfile
//-------------------------------------------------------------------
// Vypis adresara
//-------------------------------------------------------------------
if (cpm_options.directory) {
// TODO Implementovat vypis adresara
}
//-------------------------------------------------------------------
// Osetrenie prepinacov po kopirovani
//-------------------------------------------------------------------
// TODO Implementovat osetrenie prepinacov po kopirovani
return 0;
}
#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;
extern int optind, opterr, optopt;
struct CopymasterOptions cpm_options;
struct CopymasterOptions cpm_options = {0};
// premenne pre argumenty prepinacov
long size; //
char x;
long pos1, pos2, num;
char c, x;
unsigned long long num;
long pos1, pos2;
// spracovanie prepinacov
while (1)
{
int this_option_optind = optind ? optind : 1;
// viac informacii o spracovani jednotlivych prepinacov
// - pozri: man 3 getopt
int option_index = 0;
// viac informacii o spracovani jednotlivych moznosti - pozri - // man 3
// getopt
static struct option long_options[] = {
{ "fast", no_argument, 0, 'f' },
......@@ -38,49 +47,97 @@ struct CopymasterOptions ParseCopymaterOptions(int argc, char *argv[]) {
{ "sparse", no_argument, 0, 'S' },
{ 0, 0, 0, 0 },
};
c = getopt_long(argc, argv, "sft:l:", // doplnte dalsie volby (options)
// podla zadania - aj ci su s argumentom
long_options, &option_index);
c = getopt_long(argc, argv, "fsc:oal:Ddm:i:u:lt:s",
long_options, &option_index);
if (c == -1)
break; // ziadne dalsie volby
break;
switch (c)
{
case 's': // spracovanie - moznost 's'
printf("prepinac - s\n");
// pokracovanie spracovania
case 'f':
cpm_options.fast = 1;
break;
case 'f': // spracovanie - moznosť 'f'
printf("prepinac - f\n");
// pokracovanie spracovania
case 's':
cpm_options.slow = 1;
break;
case 't': // spracovanie - moznost 't'
printf("prepinac - t\n");
size = atoi(optarg); // spracovanie argumentu danej volby (option)
printf("size=%ld\n", size);
// pokracovanie spracovania
case 'c':
cpm_options.create = 1;
sscanf(optarg, "%o", &cpm_options.create_mode);
break;
case 'l': // spracovanie - option 'l'
printf("prepinac - l\n");
sscanf(optarg, "%c,%ld,%ld,%ld", &x, &pos1, &pos2,
&num); // spracovanie argumentu danej voľby (option)
printf("x=%c\n", x);
printf("pos1=%ld\n", pos1);
printf("pos2=%ld\n", pos2);
printf("num=%ld\n", num);
// ... pokracovanie spracovania
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;
// analogickym sposobom osetrite dalsie volby
// ....
// vsetky volby osetrene
// osetrenie chyby spracovania prepinacov
case '?':
// break;
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);
}
......@@ -16,19 +16,27 @@ struct CopymasterOptions {
int fast;
int slow;
int create;
int create_mode;
mode_t create_mode;
int overwrite;
int append;
int lseek;
struct CopymasterOptionsLseek 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