You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

124 lines
3.1 KiB

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