int login() { int log_flag = 0; system("clear"); printf("-------------------------------------------\n"); printf("\n"); printf("*******欢迎使用火车票订票系统 0.9版********\n"); printf("\n"); printf("\t请选择\n"); printf("\t\t1 用户登录\n"); printf("\t\t2 新用户注册\n"); printf("\t\t3 管理员登录\n"); printf("\t\t4 退出\n"); printf("\n"); printf("----------------中国铁道部-----------------\n"); printf("---------如有任何疑问请拨12306垂询---------\n"); printf("\n"); scanf("%d",&log_flag); my_getchar(); while(log_flag>4||log_flag<1) { printf("输入错误,请重试。\n"); scanf("%d",&log_flag); my_getchar(); } return log_flag; }
t_bistro *read_input(void) { t_bistro *bistro; if (!(bistro = malloc(sizeof (t_bistro)))) my_puterror(E_THE_WORLD_IS_OVER); if ((bistro->form = my_getchar() == 51)) read_rpn_op(bistro); else read_infix_op(bistro); if (!(bistro->base = malloc(sizeof (t_base)))) my_puterror(E_THE_WORLD_IS_OVER); if ((bistro->base->base_len = my_getchar()) < 2) my_puterror(E_ARITH); read_base(bistro); read_expr(bistro); return bistro; }
static void read_base(t_bistro *bistro) { int i; if (!(bistro->base->base = malloc(bistro->base->base_len))) my_puterror(E_THE_WORLD_IS_OVER); i = 0; while (i < bistro->base->base_len) bistro->base->base[i++] = my_getchar(); }
static void read_rpn_op(t_bistro *bistro) { bistro->op_emp = my_getchar(); bistro->op_ech = my_getchar(); bistro->op_neg = my_getchar(); bistro->op_add = my_getchar(); bistro->op_sub = my_getchar(); bistro->op_mul = my_getchar(); bistro->op_div = my_getchar(); bistro->op_divent = my_getchar(); bistro->type = EXPR_RPN; }
/* * Description: unbuffered single character input. * Version: 1.0 Date:2014.07.14 * Author: Jasper Li */ int main(void) { int my_getchar(void); int c; while((c=my_getchar()) != EOF) putchar(c); return 0; }
int getword(char * word, int max_len) { char * w = word; while(isspace(*w = my_getchar())) ; --max_len; if (!isalpha(*w) && *w != '_') { *++w = '\0'; return word[0]; } for(; (isalnum(*w) || *w == '_') && max_len; --max_len) *++w = my_getchar(); my_ungetc(*w); *w = '\0'; return word[0]; }
static void read_infix_op(t_bistro *bistro) { bistro->op_grp_beg = my_getchar(); bistro->op_grp_end = my_getchar(); bistro->op_add = my_getchar(); bistro->op_sub = bistro->op_neg = my_getchar(); bistro->op_mul = my_getchar(); bistro->op_div = my_getchar(); bistro->op_divent = my_getchar(); bistro->op_emp = bistro->op_add; bistro->type = EXPR_INFIX; }
char *my_putword(char *str, char *base, int *k, t_mytok flag) { char *word; int len; int i; i = 0; len = my_getchar(str, base, k, flag) + 1; if ((word = malloc(len * sizeof(*word))) == NULL) return (NULL); while (check_base(str[*k], base) == (int)flag && str[*k] != '\0') { word[i] = str[*k]; ++i; ++(*k); } word[i] = '\0'; return (word); }
int main () { /* IOCTL_TEST STUFF // attribute structures struct ioctl_test_t { int field1; char field2; } ioctl_test; ioctl_test.field1 = 10; ioctl_test.field2 = 'a'; ioctl (fd, IOCTL_TEST, &ioctl_test); */ struct keyboard_struct key; int fd = open ("/proc/ioctl_test", O_RDONLY); /* This loops until the system detects that the user has pressed the enter key */ int done = 0; char letter; while (!done) { /* the character is both in key and letter (debugging stuff I dealt with) */ letter = my_getchar(fd, key); if (letter == '\n') { printf("\nEntered if statement\n"); done = 1; } printf("%c",key.letter); } printf("Exiting"); fflush(stdin); return 0; }
int main (int argc, char *argv[]) { char *playlist_filename; int shuffle = 0; int random_play = 0; int repeat = 0; int slptime = 0; FILE *playlist_file; char **playlist; char *line; char *ptr; int volume = 80; int balance; int playlist_size; int argnr; int current_song = 0; int songs = 0; int i,x,j,pid; #ifdef LCD lcddev = fopen(LCD_DEV, "w"); if (lcddev == NULL) { fprintf(stderr, "Open failed for %s\n", LCD_DEV); return -1; } fprintf(lcddev, "\f"); fflush(lcddev); #else lcddev = stdout; #endif atexit(exit_code); #ifdef KEYPAD // keypad = openkeypad(KEYPAD_DEV); keypad = open(KEYPAD_DEV, O_RDONLY); if (keypad == -1) { fprintf(stderr, "Open failed for %s\n",KEYPAD_DEV); return -1; } #else keypad = 0; // assign stdinput to keypad savelocaltermios(); setlocaltermios(); #endif playlist_filename = NULL; mp3play_options = NULL; playlist_size = PLAYLIST_SIZE; i = 0; if (argc < 2) usage(0); // process options while ((i = getopt(argc, argv, "?hzvZR@:M:s:")) >= 0) { switch (i) { case 'z': shuffle = 1; break; case 'Z': random_play = 1; break; case 'R': repeat = 1; break; case 's': slptime = atoi(optarg); break; case '@': playlist_filename = (char *) malloc(strlen(optarg) + 1); strcpy(playlist_filename, optarg); break; case 'M': line = (char *) malloc(strlen(optarg) + 1); strcpy(line, optarg); mp3play_option_count = strchrcnt(line, ' ') + 1; mp3play_options = (char **) calloc(mp3play_option_count + 7, sizeof(char *)); mp3play_options[0] = MP3PLAYER; ptr = line; for (i = 1; i <= mp3play_option_count; i++) { mp3play_options[i] = (char *) calloc(1, strlen(line) + 1); ptr = strtok(ptr, " "); strcpy(mp3play_options[i], ptr); ptr = NULL; } free(line); break; case 'v': verbose = 1; break; case 'h': case '?': usage(0); break; } } argnr = optind; if (!playlist_filename) if (argnr >= argc) usage(1); if (!mp3play_options) { mp3play_options = (char **) calloc(4, sizeof(char *)); mp3play_options[0] = MP3PLAYER; mp3play_option_count = 1; mp3play_options[mp3play_option_count] = (char *) malloc(strlen("-l") + 1); strcpy(mp3play_options[mp3play_option_count], "-t"); mp3play_option_count++; mp3play_options[mp3play_option_count] = (char *) malloc(strlen("0") + 1); strcpy(mp3play_options[mp3play_option_count], "2"); } else { mp3play_option_count++; mp3play_options[mp3play_option_count] = (char *) malloc(strlen("-l") + 1); strcpy(mp3play_options[mp3play_option_count], "-l"); mp3play_option_count++; mp3play_options[mp3play_option_count] = (char *) malloc(strlen("0") + 1); strcpy(mp3play_options[mp3play_option_count], "0"); mp3play_option_count++; mp3play_options[mp3play_option_count] = (char *) malloc(strlen("-l") + 1); strcpy(mp3play_options[mp3play_option_count], "-t"); mp3play_option_count++; mp3play_options[mp3play_option_count] = (char *) malloc(strlen("0") + 1); strcpy(mp3play_options[mp3play_option_count], "2"); } srandom(time(NULL)); if (playlist_filename) { if (verbose) fprintf(stdout, "Reading Playlist from file:%s\n", playlist_filename); playlist_file = fopen(playlist_filename, "r"); if (playlist_file == NULL) { fprintf(stderr, "Open failed for playlist file:%s\n", playlist_filename); return -1; } playlist = (char **) calloc(playlist_size, sizeof(char *)); line = (char *) calloc(1, MAX_LINE + 1); if (verbose) fprintf(stdout, "Playlist entries:\n"); while(line != NULL) { if (songs >= playlist_size) { playlist_size += PLAYLIST_SIZE; playlist = (char **) realloc(playlist, playlist_size * sizeof(char *)); } line = fgets(line, MAX_LINE, playlist_file); if (*line != '\0') { playlist[songs] = (char *) calloc(1, strlen(line) + 1); line = strtok(line, "\n"); playlist[songs] = strcpy(playlist[songs], line); if (verbose) fprintf(stdout, "[%d] - %s\n",songs, playlist[songs]); songs++; } } free(line); songs--; } else { playlist = (char **) calloc(argc, sizeof(char *)); for (i = 0; i < (argc - argnr) ; i++) { playlist[i] = (char *) malloc(strlen(argv[i + argnr]) + 1); playlist[i] = strcpy(playlist[i], argv[i + argnr]); } songs = i - 1; } if (shuffle) { if (verbose) fprintf(stdout, "Shuffling.....\n"); for (x = 0; (x < 10000); x++) { i = ((unsigned int) random()) % songs; j = ((unsigned int) random()) % songs; line = playlist[i]; playlist[i] = playlist[j]; playlist[j] = line; } } if (verbose) fprintf(stdout, "Init complete - %d songs in playlist\n", songs + 1); // main menu loop current_song = 0; pid = 1; #ifdef KEYPAD for (;;) { #else while (input != QUIT) { #endif input = my_getchar(keypad); while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) { if (pid == mp3playpid) { if (verbose) fprintf(stdout, "mp3play child died\n"); if (lcddev) { fprintf(lcddev, "\f"); fflush(lcddev); } } else { if (verbose) fprintf(stdout, "another child died\n"); } } #ifndef KEYPAD if (input == 0x1b) { input = my_getchar(keypad); input = my_getchar(keypad); } #endif switch (input) { case UP: volume+=10; if (volume > 100) { volume = 100; } else { set_volume(volume); printtitle(playlist[current_song]); } input = 'c'; break; case DOWN: volume-=10; if (volume < 0) { volume = 0; break; } else { set_volume(volume); printtitle(playlist[current_song]); } input = 'c'; break; case RIGHT : if (slptime) sleep(slptime); if (random_play) { current_song = ((unsigned int) random()) % songs; if (verbose) fprintf(stdout, "[Random Song] - %d\n",current_song); } else { current_song++; if (verbose) fprintf(stdout, "[Next song]\n"); } if (current_song > songs) { if (repeat) { current_song = 0; play(playlist[current_song]); } else { current_song--; } } else play(playlist[current_song]); input = 'c'; break; case SELECT : if (verbose) fprintf(stdout, "[Play]\n"); play(playlist[current_song]); input = 'c'; break; case LEFT : if (random_play) { current_song = ((unsigned int) random()) % songs; if (verbose) fprintf(stdout, "[Random Song] - %d\n",current_song); } else { current_song--; if (verbose) fprintf(stdout, "[Prev song]\n"); } if (current_song < 0) { current_song = 0; } else play(playlist[current_song]); input = 'c'; break; case EXIT : if (verbose) fprintf(stdout, "[Stop]\n"); play(NULL); input = 'c'; break; default : break; } } play(NULL); } /****************************************************************************/ int exec_command(char **argv) { int i = 0; int pid; if (verbose) { fprintf(stdout, "executing: "); for (i = 0; argv[i] != NULL; i++) { fprintf(stdout, "%s ",argv[i]); } fprintf(stdout, "\n"); } pid = vfork(); if(pid == 0) { // close(0); // close(1); // close(2); execvp(argv[0], argv); _exit(0); } return pid; }