/* Ohjelma joka vaihtaa sek„ levyn ett„ hakemiston. Polku otetaan komentorivilt„ jos komentorivi on olemassa, muuten p„„tesy”tt”n„. Virhekoodit: 1 ei levy„ 2 ei hakemistoa 3 hakemistoa ei voida luoda 4 laiton hakemisto Optiolla /B (Build) voidaan pakottaa luomaan ko. hakemisto. Optiolla /W (Write) saadaan virheilmoitukset ruutuun. Vesa Lappalainen 14.12.1992 23.12.1992 + optiot /E ja /I */ #define VERSIO "vl 23.12.1992" #include #include #include #include #include #include "mjonot.h" static char st[500]; static int verbose; static char *errors[] = { "", "Ei levy„ %2s\n", "Ei voida vaihtaa hakemistoon: %s\n", "Ei voida luoda hakemistoa: %s\n", "Laiton hakemisto %s\n", }; void help(void) { printf("\n" "cdd [path] [optiot] [< pathfile]\n" "\n" " Vaihdetaan levy ja hakemisto.%48s\n" " Jollei polkua ole annettu, luetaan se stdinputista.\n" "\n" " Optiot:\n" " /? - t„m„ avustus\n" " /B - (Build) luodaan hakemisto, mik„li sit„ ei ole.\n" " /W - (Write) tulostetaan virheilmoitukset n„ytt””n.\n" " /I[ehto] - (Include) hakemiston nimen pit„„ t„ytt„„ ehto.\n" " Ehdon pit„„ t„ytty„ vaihdon j„lkeen. Voi olla useasti.\n" " esim. /I*\OMAT\* => \OMAT\ pit„„ olla osana.\n" " /E[ehto] - (Exclude) hakemisto ei saa t„yt„„ ehtoa\n" "\n" ,VERSIO); exit(4); } /****************************************************************************/ #define MAX_INC 20 typedef struct { char *mask; int ex; } inc_type; static inc_type inc[MAX_INC]; static int inc_count = 0; static int inc_error = -1; static char current[80]; static char *inc_text[] = { "Pit„isi olla %s mutta on %s\n" , "Ei saa olla %s mutta on %s\n" }; int check_include(void) { int i,tasmaa,last_inc=-1; inc_error = -1; if ( !inc_count ) return 0; getcwd(N_S(current)); for (i=inc_count-1; i>=0; i--) { tasmaa = !wildmat(current,inc[i].mask); if ( tasmaa && inc[i].ex ) { inc_error = i; return 1; } if ( tasmaa && !inc[i].ex ) return 0; if ( !inc[i].ex ) last_inc = i; } if ( last_inc >= 0 ) { inc_error = last_inc; return 2; } inc_error = -1; return 0; } int get_include(int ex,char *s) { if ( inc_count >= MAX_INC ) return 1; inc[inc_count].mask = strdup(strupr(s)); inc[inc_count++].ex = ex; return 0; } static char inc_er_text[100]; char *inc_error_text(void) { if ( inc_error < 0 ) return ""; sprintf(inc_er_text,inc_text[inc[inc_error].ex],inc[inc_error].mask,current); return inc_er_text; } /****************************************************************************/ int poista_optio(char *s) { char *pf,*pt; pf = s+2; pt = s; do { *pt++ = *pf; } while ( *pf++ ); return 1; } int onko_optio(char *s,char o) /* 1 = on optio, 0 = ei. Jos optio, se poistetaan. ** Samalla jono muuttuu aina isoiksi. */ { char opt[3]="/?"; opt[1]=o; if ( strncmp(strupr(s),opt,2) == 0 ) return poista_optio(s); opt[0]='-'; if ( strncmp(strupr(s),opt,2) == 0 ) return poista_optio(s); opt[0]=o; opt[1]='/'; if ( strncmp(strupr(s),opt,2) == 0 ) return poista_optio(s); return 0; } int error(int n) { if ( verbose ) { printf(errors[n],st); printf(inc_error_text()); } return n; } int main(int argc, char *argv[]) { char *p,*s; int i,luodaan=0,path=0,levy,l; for (i=1; i1 && p[l-1]=='\\' ) p[l-1]=0; if ( !chdir(st) ) goto check_inc; if ( !luodaan ) return error(2); if ( *p == '\\' ) if ( chdir("\\") ) return error(2); else p++; while ( ( s = strchr(p,'\\') ) != NULL ) { *s = 0; mkdir(p); if ( chdir(p) ) return error(3); *s = '\\'; p = s+1; } if ( !*p ) return 0; if ( mkdir(p) ) return error(3); if ( chdir(p) ) return error(2); check_inc: if ( check_include() ) return error(4); return 0; }