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

Skompilovatelna verzia

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