// USB device main loop static void* devmain(usbdevice* kb){ readlines_ctx linectx; readlines_ctx_init(&linectx); while(1){ pthread_mutex_lock(dmutex(kb)); // End thread when the handle is removed if(!IS_CONNECTED(kb)) break; // Read from FIFO const char* line; euid_guard_start; int lines = readlines(kb->infifo - 1, linectx, &line); euid_guard_stop; if(lines){ if(readcmd(kb, line)){ // USB transfer failed; destroy device closeusb(kb); break; } } // Update indicator LEDs for this keyboard. These are polled rather than processed during events because they don't update // immediately and may be changed externally by the OS. // (also, they can lock the keyboard if they're sent at the wrong time, at least on some firmwares) kb->vtable->updateindicators(kb, 0); // Wait a little bit and then read again pthread_mutex_unlock(dmutex(kb)); DELAY_SHORT(kb); } pthread_mutex_unlock(dmutex(kb)); readlines_ctx_free(linectx); return 0; }
int main(int argc, char *argv[]){ char numeric = 0; char reverse = 0; char caseInsensitive = 0; int nlines; char lines[MAXLINES*MAXLEN]; int arg = 1; while(arg < argc){ if(strcmp(argv[arg], "-n") == 0){ numeric = 1; } else if(strcmp(argv[arg], "-r") == 0){ reverse = 1; } else if(strcmp(argv[arg], "-f") == 0){ caseInsensitive = 1; } arg++; } if((nlines = readlines(linesptr, lines, MAXLINES)) >= 0){ qsort2((void **) linesptr, 0, nlines-1, (numeric ? numcmp : caseInsensitive ? strcasecmp : strcmp)); writelines(linesptr, nlines, reverse); return 0; } else { printf("input too big to sort\n"); return 1; } }
int main(int argc, char *argv[]) { int nlines; int numeric = 0; int asc = 1; int i; while (argc-- > 1) { if (strcmp(argv[argc], "-n") == 0) numeric = 1; else if (strcmp(argv[argc], "-r") == 0) asc = -1; } if (argc > 1 && strcmp(argv[1], "-n") == 0) numeric = 1; if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { myqsort((void **) lineptr, 0, nlines-1, (int (*)(void*,void*))(numeric ? numcmp : strcmp), asc); writelines(lineptr, nlines); return 0; } else { printf("input too big to sort\n"); return 1; } }
int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int i; for (i = 1; i < argc ; i++) { if (*argv[i] == '-') { switch (*(argv[i] + 1)) { case 'n': numeric = 1; break; case 'r': order = 1; break; case 'f': fold = 1; default: break; } } } if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { q_sort((void**)lineptr, 0, nlines-1,(int(*)(void*, void*))(numeric ? numcmp : (fold ? fstrcmp : strcmp)), order); printf("\nResult:\n======\n"); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } }
int main(int argc, char *argv[]) { int ret = 0; int nlines = 0; /* number of input lines read */ printf("\ntesting atoi %d", atoi("123")); printf("\ntesting atoi %d", atoi("23")); printf("\nThis program alphabetically sorts the input lines\nGive me some input\n"); if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { printf("\n\t The lines in unsorted order are:\n"); writelines(lineptr, nlines); if (argc == 1) { qsort(lineptr, nlines, sizeof(char *), cmpstringp); printf("\n\t The lines sorted alphabetically in growing order are:\n"); writelines(lineptr, nlines); } else if (argc == 2) { if(!strcmp(argv[1], "-r")) { qsort(lineptr, nlines, sizeof(char *), cmpstringp_inv); printf("\n\t The lines sorted alphabetically in reversed order are:\n"); writelines(lineptr, nlines); } if(!strcmp(argv[1], "-n")) { qsort(lineptr, nlines, sizeof(char *), numcmp); printf("\n\t The lines sorted in num order are:\n"); writelines(lineptr, nlines); } } else if (argc == 3) { if( (!strcmp(argv[1], "-r") && (!strcmp(argv[2], "-n"))) || ((!strcmp(argv[1], "-n")) && (!strcmp(argv[2], "-r"))) ) qsort(lineptr, nlines, sizeof(char *), numcmp_inv); printf("\n\t The lines sorted in reversed num order are:\n"); writelines(lineptr, nlines); } else { printf("\n error: wrong comand line parameters \n"); return ret + 1; } return ret; } else { printf("error: input too big to sort\n"); return ret + 1; } return ret; }
// USB device main loop static void* devmain(usbdevice* kb){ // dmutex should still be locked when this is called int kbfifo = kb->infifo - 1; readlines_ctx linectx; readlines_ctx_init(&linectx); while(1){ pthread_mutex_unlock(dmutex(kb)); // Read from FIFO const char* line; int lines = readlines(kbfifo, linectx, &line); pthread_mutex_lock(dmutex(kb)); // End thread when the handle is removed if(!IS_CONNECTED(kb)) break; if(lines){ if(readcmd(kb, line)){ // USB transfer failed; destroy device closeusb(kb); break; } } } pthread_mutex_unlock(dmutex(kb)); readlines_ctx_free(linectx); return 0; }
int main( int argc, char *argv[]) { int nlines; /* number of input lines read */ int c, numeric = 0, reverse = 0; /*flags for options */ while(--argc > 0 && (*++argv)[0] == '-') while(c = *++argv[0]) switch(c) { case 'n': numeric = 1; break; case 'r': reverse = 1; break; default: printf("sort: illegal option %c\n",c); break; } if((nlines = readlines(lineptr,MAXLINES)) >= 0) { qsort((void **) lineptr, 0, nlines-1, (int (*)(void*,void*))(numeric ? numcmp : strcmp),reverse); writelines(lineptr,nlines); return 0; } else { printf("input too big to sort \n"); return 1; } }
int main(int argc, char* argv[]) { if (argc != 2) { std::cout << "Need filename" << std::endl; return -1; } char* filename = argv[1]; std::vector<std::string*>* strReps = readlines(filename); std::vector<std::vector<int>*>* vectorReps = map(strReps, &transform); std::vector<int>* vectorRes = sum(vectorReps); std::string* strRes = transform_1(vectorRes); std::cout << *strRes << std::endl; assert((*strRes).substr(0, 10) == std::string("5537376230")); for (int i = 0; i < strReps->size(); i++) { delete strReps->at(i); } delete strReps; for (int i = 0; i < vectorReps->size(); i++) { delete vectorReps->at(i); } delete vectorReps; delete vectorRes; delete strRes; return 0; }
void impl( char * path ) { FILE * fp = fopen(path,"rb"); if( !fp ) { perror(path); return; } int nlines = 0; char * lineptr[MAXLINES]; if( (nlines = readlines( fp, lineptr, MAXLINES)) >= 0 ) { int (*comparator)(void*,void*) = IS_NUMERIC ? (IS_NUMERIC_FIELD ? ((int(*)(void*,void*))numfieldcmp) : ((int(*)(void*,void*))numcmp) ) : IS_DICTIONARY ? (IS_DICTIONARY_FIELD ? ((int(*)(void*,void*))strDirectoryOrderFieldcmp) : ((int(*)(void*,void*))strDirectoryOrdercmp) ) : IS_CASE_INSENSITIVE ? (IS_CASE_INSENSITIVE_FIELD ? ((int(*)(void*,void*))strcaseFieldcmp) : ((int(*)(void*,void*))strcasecmp) ) : (int(*)(void*,void*))strcmp; qsort_ex_5_14( (void **)lineptr, 0, nlines - 1, comparator); writelines( lineptr, nlines); } else { printf("Error, too many lines to sort, max is %d\n",MAXLINES); return; } fclose(fp); }
int main(int argc, char *argv[]) { int nlines, numeric = 0, reverse = 0; while (*++argv) { if (**argv == '-') { while (*++*argv) { if (**argv == 'r') { reverse = 1; } if (**argv == 'n') { numeric = 1; } } } else { printf("format error: sort -rn\n"); exit(0); } } if (argc > 1 && strcmp(argv[1], "-n") == 0) { numeric = 1; } if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsortx((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))(numeric ? numcmp : strcmp)); writelines(lineptr, nlines, reverse); return 0; } else { printf("error: inut too big to sort\n"); return -1; } }
// sort input lines int main(int argc, char *argv[]) { int nlines; int (*cmpfunc)(void*,void*) = (int (*)(void*,void*)) strcmp; int direction = 1; // sorting normally (low -> high) while (--argc > 0 && (*++argv)[0] == '-') while (*++argv[0]) switch (*argv[0]) { case 'n': // numerically cmpfunc = (int (*)(void*,void*)) numcmp; break; case 'r': // reverse direction = -1; break; case 'f': // fold cases cmpfunc = (int (*)(void*,void*)) strcmpf; break; default: printf("Unknown option '%c'\n", *argv[0]); argc = 0; break; } if (argc < 0) { printf("Usage: sortlines [-n] [-r] [-f]\n"); return 1; } else if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsortf((void **) lineptr, 0, nlines-1, cmpfunc, direction); writelines(lineptr, nlines); return 0; } else { printf("input too big to sort\n"); return 1; } }
/* sort input lines */ int main(int argc, char **argv) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ #ifndef ORIGINAL int reverse = 0; char lineptr2[MAXLINES][MAXLEN]; nlines = MAXLINES; while (nlines-- > 0) { lineptr[nlines] = lineptr2[nlines]; } while (*++argv) { if (strcmp(*argv, "-r") == 0) { reverse = 1; } else if (strcmp(*argv, "-n") == 0) { numeric = 1; } } #else if (argc > 1 && strcmp(argv[1], "-n") == 0) { numeric = 1; } #endif if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort((void **)lineptr, 0, nlines - 1, (int (*)(void *, void*))(numeric == 1 ? numcmp : strcmp)); writelines(lineptr, nlines, reverse); return 0; } else { printf("error: input too big to sort\n"); return 1; } }
int main(int argc, char **argv) { // save lines inputed in a pointer array int lines, revert, fold, directory; fold = directory = 0; revert = 1; // Use dynamic allocation memory: Umm... maybe in the next time. char alloc[ALLOCSIZE]; char *ptarr[MAXLINES]; // Check argument while (--argc > 0 && (*++argv)[0] == '-') { char *p; p = argv[0]; if (!setopt(p,&directory, &revert, &fold)) { youneedhelp(); return EXIT_FAILURE; } } while ((lines = readlines(ptarr, MAXLINES, alloc)) > 0) { // any pointer can be cast to void * and back again without loss of information. int (*comp)(void *, void *, int); // Is it too long :( comp = (int (*)(void *, void *, int))(directory ? strdfcmp : strfcmp); // sorting use quick sort algorithm. myqsort((void**)ptarr, 0, lines-1, comp, fold, revert); writelines(ptarr,lines); } return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { if (!initedit()) return 0; if (argc > 1) { filename = strdup(argv[1]); if (filename == NULL) { fprintf(stderr, "No memory\n"); termedit(); return 1; } if (!readlines(filename, 1)) { termedit(); return 1; } if (lastnum) setcurnum(1); dirty = FALSE; } docommands(); termedit(); return 0; }
/*Program starts*/ int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int reverse = 0; /* 1 if reverse sort */ int foldcase = 0; /* 1 if folding case */ int dirorder = 0; /* 1 if "directory order" comparision */ /* storage supplied by main */ char linebuf[MAXLEN * MAXLINES]; /* check options */ if (argc > 1) if (getopt(argv, &numeric, &reverse, &foldcase, &dirorder) == -1) return 2; /* read - sort - write */ if ((nlines = readlines(lineptr, linebuf, MAXLINES)) > 0) { qsort1((void **) lineptr, 0, nlines -1, (int (*)(void *, void *))(numeric ? numcmp : (foldcase ? strcasecmp : strcmp)), reverse, dirorder); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } return 0; }
int main(int argc, char *argv[]) { int nlines, i; int numeric = 0; int sort_dir = ASC; /* positive 1 for ascending, negative 1 for descending */ /* check command line arguments and toggle flags*/ for(i = 1; i < argc; i++){ if(*argv[i] == '-'){; if(strchr(argv[i], 'n') != NULL){ numeric = 1; } if(strchr(argv[i], 'r') != NULL){ sort_dir = DESC; } } } if((nlines = readlines(lineptr, MAXLINES)) >= 0){ sm_qsort((void**) lineptr, 0, nlines-1, (int (*)(const void*, const void*, const int))(numeric ? numcmp : sm_strcmp), sort_dir); writelines(lineptr, nlines); return 0; } else{ printf("input too big to sort\n"); return 1; } }
/* sort input lines */ int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int reverse = 0; /* 1 if reverset sort */ getop(argc, argv, &numeric, &reverse); if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { if (numeric){ if (reverse) { _qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) rnumcmp); } else { _qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) numcmp); } } else { if (reverse) { _qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) rstrcmp); } else { _qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) strcmp); } } writelines (lineptr, nlines); return 0; } else { printf ("input too big to sort\n"); return 1; } }
/* sort input lines */ main(int argc, char *argv[]) { int nlines,reverse = 1; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ while(--argc > 0 && (*++argv)[0] == '-') { int c; while( c = *++argv[0] ) { switch(c) { case 'n': numeric = 1; break; case 'r': reverse = -1; break; } } } if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort((void**) lineptr, 0, nlines-1, reverse, (int (*)(void*,void*))(numeric ? numcmp : strcmp)); writelines(lineptr, nlines); getchar(); return 0; } else { printf("input too big to sort\n"); return 1; } }
int main() { char buf[MAX_BUF_LEN] = {0}; readlines(lineptr, MAXLINES, buf); return 0; }
int main(int argc, char *argv[]) { int ret = 0; int nlines = 0; /* number of input lines read */ int n = N; printf("\n This program prints the last n lines from the input"); printf("\n\t nis 10 by default but we can change it by running the program with %s -n new_n_value\n", argv[0]); if( (nlines = readlines(lineptr, MAXLINES)) > 0) { printf("\n"); if (argc == 1) { ///printing the last 10 lines from the input writelines(lineptr, nlines, n); } else if (argc == 3) { /// printing the last n lines from the input writelines(lineptr, nlines, atoi(argv[2])); } else printf("erorr for command line arguments"); } return ret; }
int main(int argc, char *argv[]) { int opt; int reverse = 0; int fold = 0; int total = -1; char *lines[MAXLINES]; while ((opt = getopt(argc, argv, "rhf")) != -1) { switch (opt) { case 'r': reverse = 1; break; case 'f': fold = 1; break; case 'h': usage(argv[0]); break; default: fprintf(stderr, "unknow opt: %c\n", opt); break; } } if ((total = readlines(lines, MAXLINES)) >= 0) { if (fold) { qsort_c(lines, 0, total, charcmp); } else { qsort_c(lines, 0, total, strcmp); } writelines(lines, total + 1, reverse); } exit(0); }
int main(int argc, char **argv) { int nlines; unsigned opts = 0; while(*++argv != NULL) { if((*argv)[0] == '-') { while(*++*argv) { switch(**argv) { case 'n': opts |= OPT_NUMERIC; break; case 'r': opts |= OPT_REVERSE; break; } } } } if((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort_lomuto((void **) lineptr, 0, nlines-1, (int (*)(void *, void *))(opts & OPT_NUMERIC ? numcmp : strcmp), opts); writelines(lineptr, nlines); return 0; } else { printf("Input too large to sort.\n"); return 1; } }
int main(int argc, char *argv[]) { int nlines; char linestor[MAXSTOR]; int print_n = 10; if (argc == 1) print_n = 10; else if (argc == 2 && (*++argv)[0] == '-') { print_n = atoi(argv[0] + 1); if (print_n > MAXLINES && print_n <= 0) { print_n = 10; } } else printf("Usage: tail [-n]\n"); if ((nlines = readlines(lineptr, linestor, MAXLINES)) >= 0) { if (nlines < print_n) print_n = nlines; printf("\n\nThe last %d lines:\n", print_n); writelines(lineptr, nlines, print_n); return 0; } else { printf("error: input too big to tail\n"); return 1; } }
/* sort input lines */ int main(int argc, char *argv[]) { char *lineptr[MAXLINES]; /* pointers to text lines */ int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ /* If the optional argument -n is given, it will sort the input lines * numerically instead of lexicographically. */ if (argc > 1 && strCmp(argv[1], "-n") == 0) numeric = 1; /* lineptr是一个指针,指向的变量类型是char *, 则lineptr对应的数据类型是 * char **,后面一个 * 表示这是一个指针,前面的char * 表示指针指向的类型 * 是 char *; 所以要下面使用 void ** 来进行强制转换,而不是 void * */ if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort((void **)lineptr, 0, nlines - 1, (int (*)(const void*, const void*))(numeric ? numcmp : strCmp)); printf("-------------After sort----------\n"); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } }
int main(){ char line[MAXLINE]; char *lines[MAXLINE]; int n = readlines(lines, MAXLINE); for(int i = 0; i < n; i++){ printf("%s", lines[i]); } return 0; }
int main(int argc, char *argv[]) { char *lineptr[MAX]; int nlines = readlines(lineptr, MAX); assert(nlines == 4); assert(!strcmp(lineptr[0], "first line")); assert(!strcmp(lineptr[3], "last line")); printf("--- start write lines ---\n"); writelines(lineptr, nlines); }
int main(){ char *lines[MAXLINE]; int number; if( (number = readlines(lines, MAXLINE)) >= 0){ qsort(lines, 0, number - 1); writelines(lines, number); }else{ printf("error: input too big to sort\n"); return 1; } }
int main(){ int nlines; if((nlines = readlines(lineptr,MAXLINE)) >=0 ){ qsort(lineptr,0,nlines-1); writelines(lineptr,nlines); return 0; }else{ printf("error"); return 1; } }
int main (int argc, char *argv[]) { int c, nlines; while (--argc > 0 && (*++argv)[0] == '-') while ((c = *++argv[0])) switch (c) { case 'd': directory = !0; break; case 'f': fold = !0; break; case 'n': numeric = !0; break; case 'r': reverse = -1; break; default: printf("Error: Illegal option '%c'.\n", c); return 1; } if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort((void **) lineptr, 0, nlines -1, (int (*)(void *, void *)) (numeric ? numcmp : strcmp)); writelines(lineptr, nlines); return 0; } else { printf("Error: Input too big to sort!\n"); return 1; } }
int main() { /* Example usage */ char *lineptr[MAXLINES]; char p[MAXLEN*MAXLINES]; int i, j; printf("Reading lines, terminate with EOF...\n"); j = readlines(lineptr, p, MAXLEN*MAXLINES, MAXLINES); printf("Dumping lines...\n"); for (i = 0; i < j; i++) printf("%s\n", lineptr[i]); return 0; }