int main(int argc, char**argv){ if(argc!=5) { print_man(); } else { if(!strcmp(argv[1],"-s")) { printf("Splitting rom...\n"); split_files(argv[2],argv[3],argv[4]); } else if(!strcmp(argv[1],"-m")) { printf("Merging rom...\n"); merge_file(argv[2],argv[3],argv[4]); } else { print_man(); } } return 0; }
void mypwd(char* arg){ if(strcmp(arg, "--help") == 0){ print_man("mypwd"); return; } printf("%s\n", full_path); }
void mycat(char* arg){ if(strcmp(arg, "--help") == 0){ print_man("mycat"); return; } int i, param_count, number, fd, count, read_count; char buf[1]; char** params = (char**) malloc(sizeof(char*)); param_count = parse_arg(arg, params); count = 0; read_count = 0; if(param_count < 1 || param_count > 2){ printf("error:not enough argument!\n"); print_man("mycat"); free(params); return; } if(param_count == 2) number = atoi(params[1]); else number = -1; fd = open(params[0], O_RDONLY); if(fd == -1){ printf("error:can not open the file!\n"); print_man("mycat"); return; } if(number == -1) while(read(fd, buf, 1) > 0) printf("%s", buf); else while((count = read(fd, buf, 1)) > 0){ printf("%s", buf); read_count += count; if(read_count >= number) break; } close(fd); for(i=0;i<param_count;i++) free(params[i]); free(params); }
void tree_man(void *arg, const struct roff_man *man) { print_meta(&man->meta); if (man->meta.hasbody == 0) puts("body = empty"); putchar('\n'); print_man(man->first->child, 0); }
void myexit(char* arg){ if(strcmp(arg, "--help") == 0){ print_man("exit"); return; } printf("exit myshell\n"); exit(1); }
void html_man(void *arg, const struct man *man) { struct mhtml mh; memset(&mh, 0, sizeof(struct mhtml)); print_man(man_meta(man), man_node(man), &mh, (struct html *)arg); putchar('\n'); }
void mycp(char* arg){ if(strcmp(arg, "--help") == 0){ print_man("mycp"); return; } int fd_src, fd_dst, cnt, i; char buf[1]; char** params = (char**) malloc(sizeof(char*)); cnt = parse_arg(arg, params); if(cnt < 2 || cnt > 3){ printf("error:not enough argument!\n"); print_man("mycp"); i = 0; while(params[i] != 0) free(params[i++]); free(params); return; } if(cnt == 3){ if(strcmp(params[0], "-f") != 0){ printf("error:unknown option!\n"); print_man("mycp"); i = 0; while(params[i] != 0) free(params[i++]); free(params); return; } fd_src = open(params[1], O_RDONLY); fd_dst = open(params[2], O_CREAT | O_WRONLY | O_TRUNC); }else{ fd_src = open(params[0], O_RDONLY); fd_dst = open(params[1], O_CREAT | O_WRONLY | O_EXCL); } if(fd_src == -1 || fd_dst == -1){ printf("error:can not open the file!\n"); print_man("mycp"); if(fd_src != -1) close(fd_src); if(fd_dst != -1) close(fd_dst); i = 0; while(params[i] != 0) free(params[i++]); free(params); return; } while(read(fd_src, buf, 1) > 0) write(fd_dst, buf, 1); close(fd_src); close(fd_dst); if(cnt == 3) chmod(params[2], 0644); else chmod(params[1], 0644); for(i=0;i<cnt;i++) free(params[i]); free(params); }
static void print_man(const struct roff_node *n, int indent) { const char *p, *t; int i; if (n == NULL) return; t = p = NULL; switch (n->type) { case ROFFT_ROOT: t = "root"; break; case ROFFT_ELEM: t = "elem"; break; case ROFFT_TEXT: t = "text"; break; case ROFFT_BLOCK: t = "block"; break; case ROFFT_HEAD: t = "head"; break; case ROFFT_BODY: t = "body"; break; case ROFFT_TBL: break; case ROFFT_EQN: t = "eqn"; break; default: abort(); } switch (n->type) { case ROFFT_TEXT: p = n->string; break; case ROFFT_ELEM: case ROFFT_BLOCK: case ROFFT_HEAD: case ROFFT_BODY: p = man_macronames[n->tok]; break; case ROFFT_ROOT: p = "root"; break; case ROFFT_TBL: break; case ROFFT_EQN: p = "EQ"; break; default: abort(); } if (n->span) { assert(NULL == p && NULL == t); print_span(n->span, indent); } else { for (i = 0; i < indent; i++) putchar(' '); printf("%s (%s) ", p, t); if (NODE_LINE & n->flags) putchar('*'); printf("%d:%d", n->line, n->pos + 1); if (NODE_EOS & n->flags) putchar('.'); putchar('\n'); } if (n->eqn) print_box(n->eqn->root->first, indent + 4); if (n->child) print_man(n->child, indent + (n->type == ROFFT_BLOCK ? 2 : 4)); if (n->next) print_man(n->next, indent); }
/* ARGSUSED */ void tree_man(void *arg, const struct man *man) { print_man(man_node(man), 0); }
static void print_man(const struct man_node *n, int indent) { const char *p, *t; int i; t = p = NULL; switch (n->type) { case (MAN_ROOT): t = "root"; break; case (MAN_ELEM): t = "elem"; break; case (MAN_TEXT): t = "text"; break; case (MAN_BLOCK): t = "block"; break; case (MAN_HEAD): t = "block-head"; break; case (MAN_BODY): t = "block-body"; break; case (MAN_TAIL): t = "block-tail"; break; case (MAN_TBL): /* FALLTHROUGH */ case (MAN_EQN): break; default: abort(); /* NOTREACHED */ } switch (n->type) { case (MAN_TEXT): p = n->string; break; case (MAN_ELEM): /* FALLTHROUGH */ case (MAN_BLOCK): /* FALLTHROUGH */ case (MAN_HEAD): /* FALLTHROUGH */ case (MAN_TAIL): /* FALLTHROUGH */ case (MAN_BODY): p = man_macronames[n->tok]; break; case (MAN_ROOT): p = "root"; break; case (MAN_TBL): /* FALLTHROUGH */ case (MAN_EQN): break; default: abort(); /* NOTREACHED */ } if (n->span) { assert(NULL == p && NULL == t); print_span(n->span, indent); } else if (n->eqn) { assert(NULL == p && NULL == t); print_box(n->eqn->root, indent); } else { for (i = 0; i < indent; i++) putchar('\t'); printf("%s (%s) %d:%d\n", p, t, n->line, n->pos); } if (n->child) print_man(n->child, indent + 1); if (n->next) print_man(n->next, indent); }
int main(int argc, char *argv[]){ int tries=0, length=0, found=0, instances, failures=0, i; /*char character;*/ char word[arbitrary_word_length], blank_word[arbitrary_word_length], whole_word[arbitrary_word_length], guess[arbitrary_word_length]; char c; char skip_ch; printf("Input a word to play:"); /*scanf("%s", word); while(word[length]!='\0'){ length++; }*/ while((c=getchar())!='\n'){ if(!isalpha(c)){ do{ skip_ch=fgetc(stdin); }while(skip_ch != '\n'); break; } word[length]=c; blank_word[length]='_'; whole_word[length]=c; length++; if(length>=arbitrary_word_length){ printf("Word is too long, watcha doing making a word over 100 characters long?"); exit(EXIT_FAILURE); } } if(length<1){ printf("Input is not a word!"); exit(EXIT_FAILURE); } word[length]='\0'; blank_word[length]='\0'; whole_word[length]='\0'; printf("You chose %s, with length %d",word, length); /*for(i=0; i<100; i++){ printf("\n"); }*/ printf("\n"); while(found<length){ tries++; printf("Input a character or whole word guess: "); /*if(scanf("%c", &character)!=1){ printf("Invalid input"); exit(EXIT_FAILURE); } do{ skip_ch=fgetc(stdin); }while(skip_ch != '\n');*/ i=0; guess[0]='\0'; while((c=getchar())!='\n'){ if(!isalpha(c)){ do{ skip_ch=fgetc(stdin); }while(skip_ch != '\n'); break; } guess[i]=c; i++; } if(guess[0]=='\0') { continue; } guess[i]='\0'; if(i>1) { if(!strcmp(guess, whole_word)) { printf("You correctly guessed the word!"); exit(EXIT_SUCCESS); } else { failures++; printf("'%s' is not the word.\n", guess); print_man(failures); } } else { /*if(in_word(blank_word, length, guess[0], &found,blank_word) { printf("You've already found that letter"); continue; }*/ instances=in_word(word, length, guess[0], &found, blank_word); if(instances){ printf("'%c' is in the word: %s\n", guess[0], blank_word); } else { failures++; printf("'%c' is not in the word.\n", guess[0]); print_man(failures); } } } printf("You got the word!"); return 0; }
int main(int argc, char** argv) { char ip_buff[16]; unsigned int port_cible; struct sockaddr_in cible; struct sockaddr_in my_infos; unsigned int sizeof_my_infos=sizeof(my_infos); int sock; short cont; char buff[BUF_LEN]; cible.sin_family=AF_INET; print("Client générique v1.0\n\n"); //choix de l'ip du serveur cible cont=0; //pas d'ip correcte while(!(cont!=0))//tant que pas d'ip correcte { //demande d'une IP d'une IP print("Ip à contacter -> "); scanf("%15s", ip_buff); flush(); //convertion en adresse cont=inet_aton(ip_buff, (struct in_addr*) &cible.sin_addr); if(cont==0) { //ip incorrecte print("Ip entrée incorrecte, recommencez :\n"); } else { print("Ip correcte\n"); } } //demande d'un port de connexion print("Port de connexion -> "); scanf("%6ud", &port_cible); flush(); cible.sin_port=htons(port_cible); //affichage print("\nConnection au serveur "); aff_info(&cible); print(" ...\n"); //création du socket sock=socket(AF_INET, SOCK_STREAM, 0); if(sock==-1) { fprintf(stderr,"Erreur -> socket : %s\n", msg_err); exit(-1); } //connection du socket if(connect(sock, (struct sockaddr*) &cible, sizeof(cible))<0) { fprintf(stderr,"Erreur -> connect : %s\n", msg_err); exit(-1); } //affichage de mes infos de connexion getsockname(sock, (struct sockaddr*) &my_infos, &sizeof_my_infos); print("connecté !\n\n##########################\n\nMes infos de connection : "); aff_info(&my_infos); print("\n\n"); print_man(); print("\n\n"); cont=1; while(cont!=0) //boucle principale { print("action -> "); fgets(buff, BUF_LEN-1, stdin); //récupération de la commande buff[strlen(buff)-1]='\0'; //flush(); switch(buff[0]) { case 'q' : //fermeture du programme cont=0; break; case 'h' : print_man(); //affichage de l'aide break; case 'r' : reading(sock); //reception de données break; case 'w' : writing(sock, buff); //envoi du message suivant 'w ' break; default : print("commande incorrecte :"); //Affichage du man si erreur print_man(); break; } } print("Arrêt du client ...\n"); return 0; }
int main(int argc, char* argv[]) { /*init of all tabs and vars*/ char* alphabet = "abcdefghijklmnopqrstuvwxyz"; char* clef = "notaverysmartkey"; char* tab; int taillalpha = 26; int tailletab; int tailleclef; int* iclef; int* itab; Boolean casetwo = FALSE; Boolean unknown_char = FALSE; /*test options */ int c; while(1) { int option_index = 0; static struct option long_options[] = {{"alphabet", required_argument, 0, 'a'}, {"help", no_argument, 0, 'h'}, {"skip", no_argument, 0, 's'}, {"key", required_argument, 0, 'k'}}; c = getopt_long(argc, argv, "a:hk:s", long_options, &option_index); if(c == -1) { break; } switch(c) { case 'a': printf("option a with value of '%s'\n", optarg); alphabet = file_to_string(optarg); break; case 'h': print_man(TRUE); return 0; break; case 's': printf("option c with value of '%s'\n", optarg); unknown_char = TRUE; break; case 'k': printf("option d with value '%s'\n", optarg); clef = file_to_string(optarg); break; default: printf("?? getopt returned character code 0%o ??\n", c); return 1; break; } } /*test args*/ switch(argc - optind) { case 0: /*in no argument read tab from keyboard*/ printf("pas d'argument entrer une chaine\n"); tab = calloc(100, sizeof(char)); fgets(tab, 99, stdin); break; case 1: /* if one argument then read file and put it in tab*/ printf("arg 1 %s\n", argv[optind]); tab = file_to_string(argv[optind]); printf("tableau lue dans %s = %s\n", argv[optind], tab); break; case 2: /*if 2 argumpent read file an put it in tab then set casetow to true*/ printf("arg2 %s\n", argv[optind - 1]); casetwo = TRUE; tab = file_to_string(argv[optind]); break; default: print_man(TRUE); return 1; break; } if(unknown_char == TRUE) { /* remove uknow char if -s */ printf("remove unknown char\n"); rm_unknown_char(tab, alphabet); } tailletab = strlen(tab); tailleclef = strlen(clef); taillalpha = strlen(alphabet); itab = malloc(tailletab * sizeof(int)); iclef = malloc(tailleclef * sizeof(int)); printf("taille clef %d\n", tailleclef); /*convert tab and key to int tab*/ string_to_int(tab, alphabet, itab); string_to_int(clef, alphabet, iclef); /*test invalid char in the key*/ testclef(iclef, tailleclef); /*decod tab with the key, minus done with a pointer to the fuction*/ codclef(iclef, itab, tailletab, tailleclef, taillalpha, &addition); /*convert the final int tab to a char tab*/ int_to_alpha(itab, tab, alphabet, tailletab); /*if a dest file have been specified write the result into the file elese print the result */ if(casetwo == FALSE) { printf("message final: %s\n", tab); } else { /*argv[optind+1] = file name*/ printf("ecriture de %s dans %s\n", tab, argv[optind + 1]); write_file(argv[optind + 1], tab); } /*free all alocations*/ free(itab); free(iclef); free(tab); return 0; }
static void print_man(const struct man_node *n, int indent) { const char *p, *t; int i; t = p = NULL; switch (n->type) { case MAN_ROOT: t = "root"; break; case MAN_ELEM: t = "elem"; break; case MAN_TEXT: t = "text"; break; case MAN_BLOCK: t = "block"; break; case MAN_HEAD: t = "block-head"; break; case MAN_BODY: t = "block-body"; break; case MAN_TBL: break; case MAN_EQN: t = "eqn"; break; default: abort(); /* NOTREACHED */ } switch (n->type) { case MAN_TEXT: p = n->string; break; case MAN_ELEM: /* FALLTHROUGH */ case MAN_BLOCK: /* FALLTHROUGH */ case MAN_HEAD: /* FALLTHROUGH */ case MAN_BODY: p = man_macronames[n->tok]; break; case MAN_ROOT: p = "root"; break; case MAN_TBL: break; case MAN_EQN: p = "EQ"; break; default: abort(); /* NOTREACHED */ } if (n->span) { assert(NULL == p && NULL == t); print_span(n->span, indent); } else { for (i = 0; i < indent; i++) putchar('\t'); printf("%s (%s) ", p, t); if (MAN_LINE & n->flags) putchar('*'); printf("%d:%d\n", n->line, n->pos + 1); } if (n->eqn) print_box(n->eqn->root->first, indent + 1); if (n->child) print_man(n->child, indent + 1); if (n->next) print_man(n->next, indent); }