? Napada me neco jako system ("/bin/ls"), a odchytit output. Ale urcite bude elgantnejsi verze vyuzivajici nake knihovni funkce..
Printable View
? Napada me neco jako system ("/bin/ls"), a odchytit output. Ale urcite bude elgantnejsi verze vyuzivajici nake knihovni funkce..
Kód:/*
* Meno: Ondrej Zary
* Kruzok: info7
* Datum: 10.5.2003
* Zadanie: zadanie04c
* Kompilacia: cc pripravne_04c.c -o pripravne_04c
*
* Text zadania:
* Napiste program, ktory v zadanom adresari, uvedenom ako argument, a jeho
* podadresaroch, najde vsetky symbolicke linky.
* Ak bude program spusteny s prepinacom -e, vyhlada len linky s existujucim
* cielovym suborom; s prepinacom -n len linky s neexistujucim cielovym suborom.
* Program NESMIE pouzivat na prehladavanie adresara externy program (musi
* prehladavat adresar rekurzivne).
*
* Syntax:
* zadanie2 [-h] [-e] [-n] adresar
*
* Format vystupu (na standardny vystup):
* Output: 'najdena linka'
*
* Bonusove body mozete ziskat ak vas program bude vediet prehladavat lubovolny
* pocet zadanych adresarov (ak nebude adresar uvedeny, prehladava sa aktualny
* pracovny adresar).
*
* Rieste aj cast zadania oznacenu ako bonusovu.
*
* Program musi osetrovat pocet a spravnost argumentov. Program musi mat help,
* ktory sa vypise pri zadani argumentu -h a ma tvar:
* Meno programu (C) meno autora
*
* Usage: <meno_programu> <arg1> <arg2> ...
* <arg1>: xxxxxx
* <arg2>: yyyyy
*
* Korektny vystup programu musi ist na standardny vystup (stdout).
* Chybovy vystup programu musi ist na chybovy vystup (stderr).
* Chybovy vystup musi mat tvar:
* Error: 'adresar, subor, program,... pri ktorom nastala chyba': popis chyby ...
* Ak program pouziva nejake pomocne vypisy, musia ist na chybovy vystup a
* musia mat tvar:
* Debug: vypis ...
*
* Program sa musi dat skompilovat !!!
* Prikaz pre kompilaciu je uvedeny vyssie, v pripade potreby ho modifikujte !!!
*
* Poznamky: (sem vlozte pripadne poznamky k vypracovanemu zadaniu)
*
* Riesenie:
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
void Help() {
printf("zadanie04 (c) Rainbow\n");
printf("usage: zadanie04 [-h] [-e] [-n] [adresar] [...]\n");
printf(" -h: tento help\n");
printf(" -e: hlada len linky s existujucim cielovym suborom\n");
printf(" -n: hlada len linky s neexistujucim cielovym suborom\n");
printf(" adresar: adresar(e), kde sa budu linky hladat\n");
}
void SearchDir(char *dirname, int type) {
DIR *dp;
struct dirent *dir;
struct stat filestat;
char path[PATH_MAX];
char buf[PATH_MAX];
int i,desc;
if ((dp=opendir(dirname)) == NULL) {
fprintf(stderr,"Error: chyba pri citani adresara %s\n",dirname);
return;
}
while ((dir=readdir(dp)) != NULL) {
if ((!strcmp(dir->d_name,".")) || (!strcmp(dir->d_name,".."))) continue;
strcpy(path,dirname);
if (dirname[strlen(dirname)-1] != '/') strcat(path,"/");
strcat(path,dir->d_name);
if (lstat(path,&filestat) == -1) {
fprintf(stderr,"Error: chyba pri lstat\n");
continue;
}
if (S_ISDIR(filestat.st_mode)) SearchDir(path,type);
if (S_ISLNK(filestat.st_mode)) {
if (type == 0) {
printf("Output: '%s'\n",path);
continue;
}
if ((i=readlink(path,buf,sizeof(buf))) == -1) {
fprintf(stderr,"Error: chyba pri readlink %s\n",path);
continue;
}
buf[i]=0;
if ((desc=open(buf,O_RDONLY)) == -1) {
if (type == 2) printf("Output: '%s'\n",path);
}
else {
if (type == 1) printf("Output: '%s'\n",path);
close(desc);
}
}
}
closedir(dp);
}
int main(int argc, char *argv[]) {
int o,type=0;
opterr = 0;
while ((o = getopt(argc, argv, "hen")) != -1)
switch (o) {
case 'h':
Help();
exit(0);
case 'e':
type=1;
break;
case 'n':
type=2;
break;
case '?':
default:
fprintf(stderr, "Error: '%c': nespravny prepinac\n", optopt);
exit(1);
}
argc -= optind;
argv += optind;
if (argc == 0) SearchDir(".",type);
else for (o=0;o<argc;o++) SearchDir(argv[o],type);
}
Dik moc, to sem potreboval..