这次作业很有用a2.pdf

atoi

  1. int atoi(const char *s)
  2. {
  3. int n=0, neg=0;
  4. while (isspace(*s)) s++;
  5. switch (*s) {
  6. case '-': neg=1;
  7. case '+': s++;
  8. }
  9. /* Compute n as a negative number to avoid overflow on INT_MIN */
  10. while (isdigit(*s))
  11. n = 10*n - (*s++ - '0');
  12. return neg ? n : -n;
  13. }

int型的最大值、最小C/C++中int类型是32位的,范围是-2147483648到2147483647 。

strtok

我觉得作业里的这个函数有错

  1. char *my_strtok(char * s, const char * sep)
  2. {
  3. static char *p = NULL;
  4. if (s == NULL && ((s = p) == NULL))
  5. return NULL;
  6. s += strspn(s, sep);
  7. if (!*s)
  8. return p = NULL;
  9. p = s + strcspn(s, sep);
  10. if (*p)
  11. *p++ = '\0';
  12. else
  13. p = NULL;
  14. return s;
  15. }

静态变量的值不能在编译时确定,应该是在执行时确定。C语言初始化一个全局变量或static变量时,只能用常量赋值,不能用变量赋值
image.png

opendir/readdir

image.png

  1. #include <dirent.h>
  2. #include <stdio.h>
  3. void list_filenames(const char *dirpath)
  4. {
  5. DIR *dp = opendir(dirpath);
  6. if (dp == NULL) return;
  7. // loop through directory entries
  8. struct dirent *entry;
  9. while ((entry = readdir(dp)) != NULL) {
  10. printf("%s\n",entry->d_name);
  11. }
  12. closedir(dp);
  13. }

PATH_MAX

image.png

Implement the mywhich program

  1. //#include "samples/prototypes.h"
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <dirent.h>
  6. #include <stdbool.h>
  7. // You wrote this as an exercise in lab2! Grab the code from your lab checkoff sheet
  8. // at https://cs107.stanford.edu/cgi-bin/lab2
  9. const char *get_env_value(const char *envp[], const char *varname)
  10. {
  11. for (int i = 0; envp[i] != NULL; i++){
  12. //printf("%s\n", envp[i]);
  13. char *result = strstr(envp[i], varname);
  14. if (result == envp[i]){
  15. //if result is null, don't do anything
  16. //envp[i] can be null but will be blocked by for
  17. result += strlen(varname);
  18. if (*result == '=')
  19. return ++result;
  20. }
  21. }
  22. return NULL; // TODO
  23. }
  24. bool my_scan_token(const char **p_input, const char *delimiters, char buf[], size_t buflen)
  25. {
  26. int deletion = strspn(*p_input, delimiters);
  27. *p_input += deletion;
  28. int num_tokens = strcspn(*p_input, delimiters);
  29. if (num_tokens > 0) {
  30. if (num_tokens > buflen - 1) {
  31. strncpy(buf, *p_input, buflen - 1);
  32. *p_input += buflen - 1;
  33. buf[buflen - 1] = '\0';
  34. } else {
  35. strncpy(buf, *p_input, num_tokens);
  36. *p_input += num_tokens;
  37. buf[num_tokens] = '\0';
  38. }
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44. // This main is incomplete. It sketches the expected behavior for the case when
  45. // mywhich is invoked with no arguments. You are to first read and understand
  46. // this code, work out how to change/extend to suit your needs, and finally add
  47. // comments to document your strategy.
  48. int main(int argc, char *argv[], const char *envp[])
  49. {
  50. const char *searchpaths = get_env_value(envp, "MYPATH");
  51. if (searchpaths == NULL)
  52. searchpaths = get_env_value(envp, "PATH");
  53. if (argc == 1){
  54. char dir[PATH_MAX+1];
  55. const char *remaining = searchpaths;
  56. printf("Directories in search path:\n");
  57. while (my_scan_token(&remaining, ":", dir, sizeof(dir))){
  58. printf("%s\n", dir);
  59. }
  60. }else{
  61. argc--;
  62. argv++;
  63. char dir[PATH_MAX+1];
  64. //loop through arguments
  65. for (int i = 0; i < argc; i++){
  66. //check argument
  67. if (!strlen(argv[i]))
  68. continue;
  69. unsigned char ifwild = 0;
  70. if (argv[i][0] == '+'){
  71. argv[i]++;
  72. if (!strlen(argv[i]))
  73. continue;
  74. ifwild = 1;
  75. }
  76. const char *remaining = searchpaths;
  77. //loop through dirs in path
  78. while (my_scan_token(&remaining, ":", dir, sizeof(dir))){
  79. DIR *dp = opendir(dir);
  80. if (dp == NULL)
  81. continue;
  82. struct dirent *entry;
  83. unsigned char ifbreak = 0;
  84. //loop through files in dir
  85. while ((entry = readdir(dp)) != NULL){
  86. if (!ifwild){
  87. if (!strcmp(argv[i], entry->d_name)){
  88. printf("%s/%s\n", dir, entry->d_name);
  89. ifbreak = 1;
  90. break;
  91. }
  92. }else{
  93. char *find = strstr(entry->d_name, argv[i]);
  94. if (find)
  95. printf("%s/%s\n", dir, entry->d_name);
  96. }
  97. }
  98. closedir(dp);
  99. if (ifbreak)
  100. break;
  101. }
  102. }
  103. }
  104. return 0;
  105. }

一个比较综合的练习。