这次作业很有用a2.pdf
atoi
int atoi(const char *s){int n=0, neg=0;while (isspace(*s)) s++;switch (*s) {case '-': neg=1;case '+': s++;}/* Compute n as a negative number to avoid overflow on INT_MIN */while (isdigit(*s))n = 10*n - (*s++ - '0');return neg ? n : -n;}
int型的最大值、最小值C/C++中int类型是32位的,范围是-2147483648到2147483647 。
strtok
我觉得作业里的这个函数有错
char *my_strtok(char * s, const char * sep){static char *p = NULL;if (s == NULL && ((s = p) == NULL))return NULL;s += strspn(s, sep);if (!*s)return p = NULL;p = s + strcspn(s, sep);if (*p)*p++ = '\0';elsep = NULL;return s;}
静态变量的值不能在编译时确定,应该是在执行时确定。C语言初始化一个全局变量或static变量时,只能用常量赋值,不能用变量赋值
opendir/readdir

#include <dirent.h>#include <stdio.h>void list_filenames(const char *dirpath){DIR *dp = opendir(dirpath);if (dp == NULL) return;// loop through directory entriesstruct dirent *entry;while ((entry = readdir(dp)) != NULL) {printf("%s\n",entry->d_name);}closedir(dp);}
PATH_MAX
Implement the mywhich program
//#include "samples/prototypes.h"#include <limits.h>#include <stdio.h>#include <string.h>#include <dirent.h>#include <stdbool.h>// You wrote this as an exercise in lab2! Grab the code from your lab checkoff sheet// at https://cs107.stanford.edu/cgi-bin/lab2const char *get_env_value(const char *envp[], const char *varname){for (int i = 0; envp[i] != NULL; i++){//printf("%s\n", envp[i]);char *result = strstr(envp[i], varname);if (result == envp[i]){//if result is null, don't do anything//envp[i] can be null but will be blocked by forresult += strlen(varname);if (*result == '=')return ++result;}}return NULL; // TODO}bool my_scan_token(const char **p_input, const char *delimiters, char buf[], size_t buflen){int deletion = strspn(*p_input, delimiters);*p_input += deletion;int num_tokens = strcspn(*p_input, delimiters);if (num_tokens > 0) {if (num_tokens > buflen - 1) {strncpy(buf, *p_input, buflen - 1);*p_input += buflen - 1;buf[buflen - 1] = '\0';} else {strncpy(buf, *p_input, num_tokens);*p_input += num_tokens;buf[num_tokens] = '\0';}return true;} else {return false;}}// This main is incomplete. It sketches the expected behavior for the case when// mywhich is invoked with no arguments. You are to first read and understand// this code, work out how to change/extend to suit your needs, and finally add// comments to document your strategy.int main(int argc, char *argv[], const char *envp[]){const char *searchpaths = get_env_value(envp, "MYPATH");if (searchpaths == NULL)searchpaths = get_env_value(envp, "PATH");if (argc == 1){char dir[PATH_MAX+1];const char *remaining = searchpaths;printf("Directories in search path:\n");while (my_scan_token(&remaining, ":", dir, sizeof(dir))){printf("%s\n", dir);}}else{argc--;argv++;char dir[PATH_MAX+1];//loop through argumentsfor (int i = 0; i < argc; i++){//check argumentif (!strlen(argv[i]))continue;unsigned char ifwild = 0;if (argv[i][0] == '+'){argv[i]++;if (!strlen(argv[i]))continue;ifwild = 1;}const char *remaining = searchpaths;//loop through dirs in pathwhile (my_scan_token(&remaining, ":", dir, sizeof(dir))){DIR *dp = opendir(dir);if (dp == NULL)continue;struct dirent *entry;unsigned char ifbreak = 0;//loop through files in dirwhile ((entry = readdir(dp)) != NULL){if (!ifwild){if (!strcmp(argv[i], entry->d_name)){printf("%s/%s\n", dir, entry->d_name);ifbreak = 1;break;}}else{char *find = strstr(entry->d_name, argv[i]);if (find)printf("%s/%s\n", dir, entry->d_name);}}closedir(dp);if (ifbreak)break;}}}return 0;}
一个比较综合的练习。
