cat 程序 /** * Scat.c * Concatenate files and print on the stadard output * * Finish At Aug 19th,2009 * **/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> /* Max length of file name */ #define MAX_NAME 100 /* Name of command, max length is 50 */ static char commandname[MAX_NAME]; /* Some tags, store the information of options */ int btag = 0, ntag = 0, etag = 0, ttag = 0, stag = 0, vtag = 0; /* Show help information */ void showHelp(){ printf("Usage: %s {OPTION]... [FILE].../n" "concatenate FILE(s), or standard input, to standard output./n/n" "-A, --show-all equivalent to -vET/n" "-b, --number-nonblank number nonepty output lines/n" "-e equivalent to -vE/n" "-E, --show-ends display $ at end of each link/n" "-n, --number number all output lines/n" "-s, --squeeze-blank suppress repeated empty output lines/n" "t equivalent to -vT/n" "-T, --show-tabs display TAB characters as ^I/n" "-u (ignored)/n" "-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB/n" "--help display this help and exit/n" "--version output version information and exit/n/n" "With no FILE, read standard input./n", commandname); exit(0); } /* Show version */ void showVer(){ printf("%s 1.0/n/nWritten by soso./n", commandname); exit(0); } /* Long option error */ void showError(char *s){ printf("%s: unrecognized option '%s'/n" "Try `%s --help' for more information./n",commandname, s, commandname); exit(1); } /* Short option error */ void showErr(char c){ printf("%s: invalid option -- '%c'/n" "Try `%s --help' for more information./n", commandname, c, commandname); exit(1); } /* Print Contrl Char */ void dealCtrlChar(unsigned char c){ if(c < 32) printf("^%c", 64 + c); else if(c == 127) printf("^?"); else if(c > 127 && c < 137 || c > 138 && c < 256){ printf("M-"); dealCtrlChar(c - 128); } else if(c == 137) printf("M-^I"); else if(c == 138) printf("M-^J"); else printf("%c", c); } /* Read from stdardar input */ void stdInput(){ int readbytes, i, lineNum = 0; char preChar = '/n', pre2Char = ' '; char readChars[100]; while(1){ gets(readChars); readbytes = strlen(readChars); readChars[readbytes] = '/n'; for(i = 0; i < readbytes+1; i++){ if(preChar == '/n'){ if(pre2Char == '/n' && readChars[i] == '/n' && stag == 1) continue; if(ntag == 1 || (btag == 1 && readChars[i] != '/n')) printf("%6d ", ++lineNum); } if(readChars[i] == '/n') if(etag == 1) printf("$/n"); else printf("/n"); else if(readChars[i] == '/t') if(ttag == 1) printf("^I"); else printf("/t"); else if(vtag == 1) dealCtrlChar(readChars[i]); else printf("%c", readChars[i]); pre2Char = preChar; preChar = readChars[i]; } } exit(0); } /* Print the content of file */ void cat(char *name){ int filefd; int readbytes, i, lineNum = 0; char preChar = '/n', pre2Char = ' '; char readChars[100]; /* Open the file, if a error occurs, it will return */ if((filefd = open(name, O_RDONLY)) == -1){ fprintf(stderr, "%s: %s: ", commandname, name); perror(""); return; } while(readbytes = read(filefd, readChars, 100)){ /* Read file */ if(readbytes == -1){ fprintf(stderr, "%s: %s:", commandname, name); perror(""); return; } for(i = 0; i < readbytes; i++){ if(preChar == '/n'){ if(pre2Char == '/n' && readChars[i] == '/n' && stag == 1) continue; if(ntag == 1 || (btag == 1 && readChars[i] != '/n')) printf("%6d ", ++lineNum); } if(readChars[i] == '/n') if(etag == 1) printf("$/n"); else printf("/n"); else if(readChars[i] == '/t') if(ttag == 1) printf("^I"); else printf("/t"); else if(vtag == 1) dealCtrlChar(readChars[i]); else printf("%c", readChars[i]); pre2Char = preChar; preChar = readChars[i]; } } } int main(int argc, char * argv[]){ int i, j; int fileNo = 0; char filename[50][MAX_NAME]; /* Set name of command */ strcpy(commandname, argv[0]); /* Deal options */ for(i = 1; i < argc; i++) /* If the option is file name, store to filename */ if(argv[i][0] != '-') strcpy(filename[fileNo++], argv[i]); /* If the option is long option */ else if(argv[i][1] == '-') if(strcmp(argv[i], "--number-nonblank") == 0) btag = 1; else if(strcmp(argv[i], "--number") == 0) ntag = 1; else if(strcmp(argv[i], "--show-ends") == 0) etag = 1; else if(strcmp(argv[i], "--show-tabs") == 0) ttag = 1; else if(strcmp(argv[i], "--show-noprintint") == 0) vtag = 1; else if(strcmp(argv[i], "--squeeze-blank") == 0) stag = 1; else if(strcmp(argv[i], "--show-all") == 0){ ttag = 1; vtag = 1; etag = 1;} else if(strcmp(argv[i], "--help") == 0) showHelp(); else if(strcmp(argv[i], "--version") == 0) showVer(); else showError(argv[i]); /* If the option is short option */ else for(j = 1; argv[i][j] != '/0'; j++) switch(argv[i][j]){ case 'u': break; case 'b': btag = 1; break; case 'n': ntag = 1; break; case 'E': etag = 1; break; case 'T': ttag = 1; break; case 's': stag = 1; break; case 'v': vtag = 1; break; case 'e': etag = 1; vtag = 1; break; case 't': vtag = 1; ttag = 1; break; case 'A': vtag = 1; ttag = 1; etag = 1; break; default: showErr(argv[i][j]); } /* Deal options over */ /* If no file given, read from stdandar input */ if(fileNo == 0) stdInput(); else for(i = 0; i < fileNo; i++) cat(filename[i]); } head程序 /** * Shead.c * Similar to command head. * * strtol(), read(), lseek. * * Finish At Aug 18 **/ #include<stdio.h> #include<stdio.h> #include<string.h> #include<fcntl.h> #include<stdlib.h> char commandname[50]; int qtag = 0, vtag = 0, nctag = 0; /** When the number format is unaceptable **/ void numFormatErr(char *s){ printf("%s: %s: invalid number of bytes/n", commandname, s); exit(1); } /** Show help content **/ void showHelp(){ printf("Usage: %s [OPTION]... [FILE].../n" "Print the first 10 lines of each FILE to standard output./n" "With more than one FILE, precede each with a header giving the file name./n" "With no FILE, or when FILE is -, read standard input./n/n" "Mandatory arguments to long options are mandatory for short options too./n" " -c, --bytes=[-]N print the first N bytes of each file;/n" " with the leading `-', print all but the last/n" " N bytes of each file/n" " -n, --lines=[-]N print the first N lines instead of the first 10;/n" " with the leading `-', print all but the last/n" " N lines of each file/n" " -q, --quiet, --silent never print headers giving file names/n" " -v, --verbose always print headers giving file names/n" " --help display this help and exit/n" " --version output version information and exit/n/n" "N may have a multiplier suffix:/n" "b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,/n" "GB 1000*1000*1000, G 1024*1024*1024, and so on./n", commandname); exit(0); } void showVer(){ printf("%s 1.0/nWritten by soso./n", commandname); exit(0); } /** Convert string to number **/ int delOption(char *s){ char *left; int num, tax; num = (int) strtol(s, &left, 10); if(strcmp(left, "") == 0) tax = 1; else if(strcmp(left, "b") == 0) tax = 512; else if(strcmp(left, "kB") == 0 || strcmp(left, "KB") == 0) tax =1000; else if(strcmp(left, "k") == 0 || strcmp(left, "K") == 0) tax = 1024; else if(strcmp(left, "mB") == 0 || strcmp(left, "MB") == 0) tax = 1000*1000; else if(strcmp(left, "m") == 0 || strcmp(left, "M") == 0) tax = 1024*1024; else numFormatErr(s); return num * tax; } void showError(char *s){ printf("%s: unrecognized option '%s'/n" "Try `head --help' for more information./n", commandname, s); exit(1); } void noArgumentErr(char *s){ printf("%s: head: option '%s' requires an argument /n" "Try `head --help' for more information./n", commandname, s); exit(1); } void noArgErr(char c){ printf("%s: option requires an argument -- '%c'/n" "Try `head --help' for more information./n", commandname, c); exit(1); } void showErr(char c){ printf("%s: invalid option -- '%c'/n" "Try `head --help' for more information./n", commandname, c); exit(1); } /**When there is no filename argument **/ void stdInput(int num){ char ins[1000]; int lineCounts = 0, byteCounts = 0; if(vtag == 1 && qtag == 0) printf("==> standar input <==/n"); if(num < 0) while(1) gets(ins); if(nctag == 0) while(lineCounts++ != num){ gets(ins); puts(ins); } else while(byteCounts++ != num){ ins[0] = getchar(); putchar(ins[0]); } exit(0); } /** Print content **/ void showInfo(char *s, int num){ int i,j; int filefd; int enterNo = 0, bytes = 0, nbs, enterCounts = 0, byteCounts = 0; char readChars[101]; if((filefd = open(s, O_RDONLY)) == -1){ fprintf(stderr, "%s: cannot open '%s' for reading: ", commandname, s); perror(""); return; } if(vtag == 1 && qtag == 0) printf("==> %s <==/n", s); while(nbs = read(filefd, readChars, 100)){ if(nbs == -1){ fprintf(stderr, "%s: error reading `%s': ", commandname, s); perror(""); return; } if(nbs == 0) break; for(i = 0; i < nbs; i++) if(readChars[i] == '/n') enterNo++; bytes += nbs; } if(lseek(filefd, 0, SEEK_SET) != 0){ fprintf(stderr, "%s: error happened when deal `%s': ", commandname, s); perror(""); return; } if(nctag == 0){ if(num < 0) num = (enterNo - num) < 0 ? 0 : (enterNo - num); while(nbs = read(filefd, readChars, 100)) for(i = 0; i < nbs; i++){ if(enterCounts == num){ close(filefd); return; } printf("%c", readChars[i]); if(readChars[i] == '/n') ++enterCounts; } }else{ if(num < 0) num = (bytes - num) < 0 ? 0 : (bytes -num); while(nbs = read(filefd, readChars, 100)) for(i = 0; i < nbs; i++){ if(byteCounts == num){ close(filefd); return; } printf("%c",readChars[i]); ++byteCounts; } } } /** Main funtion **/ int main(int argc, char * argv[]){ int i,j, fileNo = 0; char number[50]; int num = 10; char filename[100][100]; strcpy(commandname, argv[0]); for(i = 1; i < argc; i++) if(argv[i][0] != '-') strcpy(filename[fileNo++], argv[i]); else if(argv[i][1] == '-') if(strcmp(argv[i], "--quiet") == 0) qtag = 1; else if(strcmp(argv[i], "--silent") == 0) qtag = 1; else if(strcmp(argv[i], "--verbose") == 0) vtag = 1; else if(strcmp(argv[i], "--help") == 0) showHelp(); else if(strcmp(argv[i], "--version") == 0) showVer(); else if(strncmp(argv[i], "--bytes", 7) == 0){ nctag = 1; if(argv[i][7] == '/0') if((++i) == argc) noArgumentErr(argv[i-1]); else num = delOption(argv[i]); else if(argv[i][7] == '='){ strcpy(number, &argv[i][8]); num = delOption(number); }else showError(argv[i]); } else if(strncmp(argv[i], "--lines", 7) == 0){ nctag = 0; if(argv[i][7] == '/0') if((++i) == argc) noArgumentErr(argv[i-1]); else num = delOption(argv[i]); else if(argv[i][7] == '='){ strcpy(number, &argv[i][8]); num = delOption(number); }else showError(argv[i]); } else showError(argv[i]); else for(j = 1; argv[i][j] != '/0'; j++) if(argv[i][j] == 'q') qtag = 1; else if(argv[i][j] == 'v') vtag = 1; else if(argv[i][j] == 'c'){ nctag = 1; if(argv[i][j+1] == '/0'){ if((++i) == argc) noArgErr(argv[i-1][j]); else num = delOption(argv[i]); } else num = delOption(&argv[i][j+1]); break; } else if(argv[i][j] == 'n'){ nctag = 1; if(argv[i][j+1] == '/0') if((++i) == argc) noArgErr(argv[i-1][j]); else num = delOption(argv[i]); else num = delOption(&argv[i][j+1]); break; } else showErr(argv[i][j]); /* deal option over */ if(fileNo == 0) stdInput(num);/*exit*/ if(fileNo == 1 && vtag == 0 && qtag == 0){ qtag = 1; showInfo(filename[0], num); }else{ if(vtag == 0 && qtag == 0) vtag = 1; for(i = 0; i < fileNo - 1; i++){ showInfo(filename[i], num); printf("/n"); } showInfo(filename[i], num); } } ls程序,包含以下两个文件<Sls.c> /** * Sls.c * * A small c program do the part of the system command `ls`. * A little option can be used like "-l", "-a", "-h". * To compile this program, file Slsmothod.c shuld be * include. This file just contain a main method. Other contents * are in Slsmethod.c. * * Finish At Aug 16 2010 * **/ #include<stdio.h> #include<unistd.h> #include<dirent.h> #include<stdlib.h> #include<string.h> char commandname[50]; int main(int argc, char * argv[]){ strcpy(commandname, argv[0]); int atag = 0, ltag = 0, htag = 0; int dirNo = 0; int i,j; char **dirname; dirname = (char **) malloc(sizeof(char *)); /* Analyse the options */ if(argc != 1) for(i = 1; i < argc; i++) if(strcmp(argv[i], "--help") == 0){ help(); exit(0); }else if(argv[i][0] != '-'){ dirname[dirNo] = (char *) malloc(100); strcpy(dirname[dirNo++], argv[i]); }else for(j = 1; argv[i][j] != '/0'; j++) if(argv[i][j] == 'a') atag = 1; else if(argv[i][j] == 'l') ltag = 1; else if(argv[i][j] == 'h') htag = 1; else { printf("%s: Invalid option -- '%c'/nTry `%s --help` for more information./n", commandname, argv[i][j], commandname); exit(1); } /** * When there are no options * or all options are start with `-` * the dirname will be "./" **/ if(dirNo == 0){ dirname[dirNo] = (char *) malloc(100); strcpy(dirname[dirNo++], "./"); } /** * Until now all informations have been stored * * "dirname" ---- All directorys * "dirNo" ---- Number of dir to be listed * "atag" ---- Option -a * "ltag" ---- Option -l * "htag" ---- Option -h **/ if(dirNo == 1) lsFiles(dirname[0], atag, ltag ,htag, 0); else for(i = 0; i < dirNo; i++){ lsFiles(dirname[i], atag, ltag, htag, 1); printf("/n"); } } <Slsmethod.c> /** * Slsmethod.c * * Finish At Aug 16 2010 * **/ #include<stdio.h> #include<sys/stat.h> #include<dirent.h> #include<sys/types.h> #include<errno.h> #include<string.h> #include<stdlib.h> #include<pwd.h> #include<grp.h> #include<unistd.h> #include<time.h> extern char commandname[50]; /** * Format output the filename with proper spaces **/ void pfile(char *name, int maxlen){ int i; printf("%s", name,maxlen); if(maxlen == 0) return; for(i = 0; i < maxlen - strlen(name) + 2; i++) printf(" "); } /** * Return a string containing file type * and propvi, just like "brwx--xr--" **/ void modeChar(mode_t mode,char * mc){ strcpy(mc, "----------"); if(!S_ISREG(mode)){ if(S_ISDIR(mode)) mc[0] = 'd'; else if(S_ISLNK(mode)) mc[0] = 'l'; else if(S_ISBLK(mode)) mc[0] = 'b'; else if(S_ISCHR(mode)) mc[0] = 'c'; else if(S_ISFIFO(mode)) mc[0] = 'p'; else if(S_ISSOCK(mode)) mc[0] = 's'; } if(mode & S_IRUSR) mc[1] = 'r'; if(mode & S_IWUSR) mc[2] = 'w'; if(mode & S_IXUSR) mc[3] = 'x'; if(mode & S_IRGRP) mc[4] = 'r'; if(mode & S_IWGRP) mc[5] = 'w'; if(mode & S_IXGRP) mc[6] = 'x'; if(mode & S_IROTH) mc[7] = 'r'; if(mode & S_IWOTH) mc[8] = 'w'; if(mode & S_IXOTH) mc[9] = 'x'; } /** * * lstat , opendir , fprintf , perror , strcpy , chdir , readdir , strcmp , * getpwuid , getgrgid , readlink , ctime , gmtime * * struct tm , struct dirent , DIR , struct stat , ssize_t * **/ void lsFiles(char *dirname,int atag,int ltag,int htag,int prDirTag){ struct tm *lstime; ssize_t ssi; int fileNo = 0; int i,j, maxlen = 0; char files[1000][100]; char swp[100],link[100]; DIR * dirPt; struct dirent *direntPt; struct stat filestat; if((dirPt = opendir(dirname)) == NULL){ if((lstat(dirname,&filestat)) == -1){ fprintf(stderr, commandname); fprintf(stderr,": cannot access "); perror(dirname); exit(1); } strcpy(files[fileNo++], dirname); } else { chdir(dirname); while((direntPt = readdir(dirPt)) != NULL){ strcpy(files[fileNo++], direntPt->d_name); maxlen = maxlen > strlen(files[fileNo-1]) ? maxlen : strlen(files[fileNo-1]); } } for(i = 0; i < fileNo-1; i++) for(j = i+1; j < fileNo ; j++) if(strcmp(files[i] ,files[j]) == 1){ strcpy(swp , files[i]); strcpy(files[i] ,files[j]); strcpy(files[j] ,swp); } if(prDirTag) printf("%s: /n", dirname); if(!ltag){ for(i = 0; i < fileNo; i++){ if(!atag&&files[i][0] == '.') continue; else pfile(files[i],maxlen); if(i%7 == 0 && i != 0) printf("/n"); } printf("/n"); } else for(i = 0; i< fileNo; i++){ if(!atag&&files[i][0] == '.') continue; else{ if((lstat(files[i],&filestat)) == -1){ perror(files[i]); exit(1); } modeChar(filestat.st_mode,swp); printf("%s",swp); printf("%4d ",filestat.st_nlink); printf("%-8s ", (getpwuid(filestat.st_uid))->pw_name); printf("%-8s ", (getgrgid(filestat.st_gid))->gr_name); /** * Output the size of the file, check out the h tag is * set. if it has set, K or M will be used to make it easy to * read. **/ if(htag == 1 && filestat.st_size > 1024*1024) printf("%8.1fM ",(float)(filestat.st_size)/1024.0/1024.0); else if(htag == 1 && filestat.st_size > 1024) printf("%8.1fK ",(float)(filestat.st_size)/1024.0); else printf("%9ld ", filestat.st_size); /** * Output the last modify time, adding 8 hours to * modify the timezone, then use gmtime to output formated * time. * Can be replace by follow : * printf("%.12s ",4 + ctime(&filestat.st_mtime)); **/ filestat.st_mtime += 8 * 3600; lstime = gmtime(&filestat.st_mtime); printf("%d-%2d-%2d %2d:%2d ",lstime->tm_year+1900, lstime->tm_mon+1, lstime->tm_mday, lstime->tm_hour, lstime->tm_min); printf("%s", files[i]); if(swp[0] == 'l'){ ssi = readlink(files[i], link, 100); link[ssi] = '/0'; printf(" -> %s",link); } printf("/n"); } } chdir(""); } void help(){ printf("Usage: %s [OPTION]... [FILE].../n" "List information about the FILEs (the current directory by default)./n" "-a, do not ignore entries starting with ./n" "-h, with -l, print sizes in human readable format/n" " (e.g., 1K 234M 2G)/n" "-l use a long listing format/n", commandname); exit(0); } wc程序,包含两个文件 <Swc.c> /** * Swc.c * * Output file information like words, chars and so on. * * Finish at Aug 17 2010 * **/ #include<stdio.h> #include<string.h> #define MAXFILES 100 #define MAXNAMELEN 100 char commandname[50]; long totalRows = 0, totalWords = 0, totalBytes = 0, totalMaxline = 0, totalChars = 0; int ltag = 0, wtag = 0, ctag = 0, bltag = 0, mtag = 0; int main(int argc, char * argv[]){ strcpy(commandname, argv[0]); char filenames[MAXFILES][MAXNAMELEN]; int fileNo = 0; int i, j, tag = 0; for(i = 1; i < argc; i++) if(argv[i][0] != '-') strcpy(filenames[fileNo++], argv[i]); else if(argv[i][1] == '-') if(strcmp(argv[i], "--bytes") == 0) ctag = tag = 1; else if(strcmp(argv[i], "--chars") == 0) mtag = tag = 1; else if(strcmp(argv[i], "--lines") == 0) ltag = tag = 1; else if(strcmp(argv[i], "--max-line-length) == 0") ==0) bltag = tag =1; else if(strcmp(argv[i], "--words") == 0) wtag = tag = 1; else if(strcmp(argv[i], "--help") == 0) showHelp(); else if(strcmp(argv[i], "--version") == 0) showVersion(); else showError(argv[i]); else for(j = 1; argv[i][j] != '/0'; j++) if(argv[i][j] == 'l') ltag = tag = 1; else if(argv[i][j] == 'w') wtag = tag = 1; else if(argv[i][j] == 'c') ctag = tag = 1; else if(argv[i][j] == 'L') bltag = tag = 1; else if(argv[i][j] == 'm') mtag = tag = 1; else showOptionErr(argv[i][j]); if(tag == 0) ltag = wtag = ctag = 1; /* Untill now, all options and name of files have been store. */ /* If no file name, read chars from stand input. */ if(fileNo == 0) stdInput(); else if(fileNo == 1) showInfo(filenames[fileNo-1]); else{ for(i = 0; i < fileNo; i++) showInfo(filenames[i]); showTotalInfo(); } } <Swcmethod.c> /** * Swcmethod.c * * Methods of Swc.c * * Finish at Aug 17 2010 * **/ #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #define CHARSONCE 100 extern char commandname[50]; extern long totalRows, totalWords, totalBytes, totalMaxline, totalChars; extern int ltag, wtag, ctag, bltag, mtag; /* When unknow option given, one of following method will happen. */ void showError(char *a){ printf("%s: unrecognized option '%s'/n", commandname, a); printf("Try `%s --help` for more information./n", commandname); exit(1); } void showOptionErr(char a){ printf("%s: invalid option -- '%c'/n", commandname, a); printf("Try `%s --help` for more information./n", commandname); exit(1); } /* Print help and exit */ void showHelp(){ printf("Usage: %s [OPTION]... [FILE].../n" "Print newline, word, and byte counts for each FILE, and a total line if/n" "more than one FILE is specified. With no FILE, or when FILE is -,/n" "read standard input./n" "-c, --bytes print the byte counts/n" "-m, --chars print the character counts/n" "-l, --lines print the newline counts/n" "-L, --max-line-length print the length of the longest line/n" "-w, --words print the word counts/n" " --help display this help and exit/n" " --version output version information and exit/n", commandname); exit(0); } /* Show version */ void showVersion(){ printf("%s 1.0/n", commandname); printf("Frist edition finish at Aug 17 2010/n"); printf("/n Written by soso./n"); exit(0); } /* When no file given */ void stdInput(){ char s[1000]; char pre = ' '; int size, i; long rows = 0, words = 0, bytes = 0, maxline = 0, chars = 0; while(1){ pre = ' '; gets(s); size = strlen(s); rows++; chars += size+1; bytes = chars; maxline = maxline > size ? maxline : size; for(i = 0; (i < size) && (s[i] != 27); i++){ if(((pre==' ')||(pre=='/t')||(pre=='/n'))&& (s[i]!=' ')&&(s[i]!='/t')&&(s[i]!='/n')) words++; pre = s[i]; } if(s[i] == 27) break; } chars -= 1; bytes = chars; /*print info*/ if(ltag == 1) printf("%8ld", rows); if(wtag == 1) printf("%8ld", words); if(ctag == 1) printf("%8ld", bytes); if(mtag == 1) printf("%8ld", chars); if(bltag == 1) printf("%8ld", maxline); printf("/n"); } /* Output main information */ void showInfo(char *name){ struct stat buf; int filefd; int nbytes, i, count = 0; char ch[CHARSONCE]; char pre = ' '; long rows = 0, words = 0, bytes = 0, maxline = 0, chars = 0; if(stat(name, &buf) == -1){ fprintf(stderr, commandname); fprintf(stderr, ": "); perror(name); return; } if(S_ISDIR(buf.st_mode)) printf("%s: %s: Is a directory/n", commandname, name); else{ if((filefd = open(name, O_RDONLY)) == -1){ fprintf(stderr, commandname); fprintf(stderr, ": "); perror(name); return; }/* set lows ,words and so on */ bytes = (long)buf.st_size; while((nbytes = read(filefd, ch, CHARSONCE)) > 0){ for(i = 0; i < nbytes; i++){ count++; if(((pre==' ')||(pre=='/t')||(pre=='/n'))&& (ch[i]!=' ')&&(ch[i]!='/t')&&(ch[i]!='/n')) words++; pre = ch[i]; if(ch[i] == '/n'){ rows++; maxline = maxline > count ? maxline : count - 1; count = 0; } } chars += nbytes; } } /*print info*/ if(ltag == 1) printf("%8ld", rows); if(wtag == 1) printf("%8ld", words); if(ctag == 1) printf("%8ld", bytes); if(mtag == 1) printf("%8ld", chars); if(bltag == 1) printf("%8ld", maxline); printf(" %s/n", name); /*now add to total*/ totalRows += rows; totalWords += words; totalBytes += bytes; totalMaxline += maxline; totalChars += chars; } void showTotalInfo(){ if(ltag == 1) printf("%8ld", totalRows); if(wtag == 1) printf("%8ld", totalWords); if(ctag == 1) printf("%8ld", totalBytes); if(mtag == 1) printf("%8ld", totalChars); if(bltag == 1) printf("%8ld", totalMaxline); printf(" total/n"); }