Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

129 řádky
3.2 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. char * basename (const char *filename)
  4. {
  5. const char *p = filename + strlen (filename);
  6. while (p != filename)
  7. {
  8. p--;
  9. if (*p == '/' || *p == '\\')
  10. return ((char *) p + 1);
  11. }
  12. return ((char *) filename);
  13. }
  14. char *dirname(const char *path)
  15. {
  16. static char bname[1024];
  17. register const char *endp;
  18. /* Empty or NULL string gets treated as "." */
  19. if (path == NULL || *path == '\0') {
  20. (void)strncpy(bname, ".", sizeof bname);
  21. return(bname);
  22. }
  23. /* Strip trailing slashes */
  24. endp = path + strlen(path) - 1;
  25. while (endp > path && *endp == '\\' )
  26. endp--;
  27. /* Find the start of the dir */
  28. while (endp > path && *endp != '\\' )
  29. endp--;
  30. /* Either the dir is "/" or there are no slashes */
  31. if (endp == path) {
  32. (void)strncpy(bname, *endp == '\\' ? "\\" : ".", sizeof bname);
  33. return(bname);
  34. } else {
  35. do {
  36. endp--;
  37. } while (endp > path && *endp == '\\');
  38. }
  39. if (endp - path + 2 > sizeof(bname)) {
  40. return(NULL);
  41. }
  42. strncpy(bname, path, endp - path + 2);
  43. return(bname);
  44. }
  45. int opterr = 1, /* if error message should be printed */
  46. optind = 1, /* index into parent argv vector */
  47. optopt, /* character checked for validity */
  48. optreset; /* reset getopt */
  49. char *optarg; /* argument associated with option */
  50. #define BADCH (int)'?'
  51. #define BADARG (int)':'
  52. #define EMSG ""
  53. /*
  54. * getopt --
  55. * Parse argc/argv argument vector.
  56. */
  57. int
  58. getopt(nargc, nargv, ostr)
  59. int nargc;
  60. char * const *nargv;
  61. const char *ostr;
  62. {
  63. # define __progname nargv[0]
  64. static char *place = EMSG; /* option letter processing */
  65. char *oli; /* option letter list index */
  66. if (optreset || !*place) { /* update scanning pointer */
  67. optreset = 0;
  68. if (optind >= nargc || *(place = nargv[optind]) != '-') {
  69. place = EMSG;
  70. return (-1);
  71. }
  72. if (place[1] && *++place == '-') { /* found "--" */
  73. ++optind;
  74. place = EMSG;
  75. return (-1);
  76. }
  77. } /* option letter okay? */
  78. if ((optopt = (int)*place++) == (int)':' ||
  79. !(oli = strchr(ostr, optopt))) {
  80. /*
  81. * if the user didn't specify '-' as an option,
  82. * assume it means -1.
  83. */
  84. if (optopt == (int)'-')
  85. return (-1);
  86. if (!*place)
  87. ++optind;
  88. if (opterr && *ostr != ':')
  89. (void)printf("%s: illegal option -- %c\n", __progname, optopt);
  90. return (BADCH);
  91. }
  92. if (*++oli != ':') { /* don't need argument */
  93. optarg = NULL;
  94. if (!*place)
  95. ++optind;
  96. }
  97. else { /* need an argument */
  98. if (*place) /* no white space */
  99. optarg = place;
  100. else if (nargc <= ++optind) { /* no arg */
  101. place = EMSG;
  102. if (*ostr == ':')
  103. return (BADARG);
  104. if (opterr)
  105. (void)printf(
  106. "%s: option requires an argument -- %c\n",
  107. __progname, optopt);
  108. return (BADCH);
  109. }
  110. else /* white space */
  111. optarg = nargv[optind];
  112. place = EMSG;
  113. ++optind;
  114. }
  115. return (optopt); /* dump back option letter */
  116. }