basename.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* -*- Mode:C -*- basename: strips directory and suffix off filenames
  2. mly 850320
  3. Copyright (C) 1985 Richard M. Stallman
  4. Permission is granted to anyone to make or distribute
  5. verbatim copies of this program
  6. provided that the copyright notice and permission notice are preserved;
  7. and provided that the recipient is not asked to waive or limit his right
  8. to redistribute copies as permitted by this permission notice;
  9. and provided that anyone possessing a machine-executable copy
  10. is granted access to copy the source code, in machine-readable form,
  11. in some reasonable manner. Distribution of executable code without
  12. the source code must be accompanied prominently by this permission notice
  13. and a statement informing the recipient of how to obtain the source code.
  14. Permission is granted to distribute derived works or modified versions
  15. of this program or portions of it under the above conditions, with
  16. the additional requirement that the entire derivative or modified work
  17. must be covered by a permission notice identical to this one.
  18. Anything distributed with and usable only in conjunction with something
  19. derived from this program, whose useful purpose is conceptually
  20. to extend or adapt or add capabilities to this program,
  21. is to be considered as part of a modified version of this program
  22. under the requirements above.
  23. This software is distributed in the hope that it will be useful,
  24. but there is no warranty of any sort, and no contributor accepts
  25. responsibility for the consequences of using this program
  26. or for whether it serves any particular purpose.
  27. In other words, you are welcome to use, share and improve this program.
  28. You are forbidden to forbid anyone else to use, share and improve
  29. what you give them. Help stamp out software-hoarding! */
  30. /* first arg is filename, optional second is suffix to strip.
  31. basename /usr/foo/lossage/functions.l
  32. => functions.l
  33. basename /usr/foo/lossage/functions.l .l
  34. => functions
  35. basename functions.lisp p
  36. => functions.lis
  37. */
  38. #include <stdio.h>
  39. char *progname;
  40. main (argc,argv)
  41. char **argv;
  42. {
  43. register char *p, *name, *suffix;
  44. progname = argv[0];
  45. if (argc < 2)
  46. fatal ("no arguments given\n Expected a filename, and optionally a suffix to strip", argc);
  47. if (argc > 3)
  48. fatal ("too many arguments (%d)", argc - 1);
  49. name = p = argv[1];
  50. /* find last "/" in pathname */
  51. while (*p)
  52. if (*p++ == '/') name = p;
  53. if (argc == 2)
  54. suffix = 0;
  55. else { for (suffix = argv[2]; *suffix; suffix++);
  56. while (p > name && suffix > argv[2])
  57. if (*--suffix != *--p)
  58. /* mismatch */
  59. { suffix = 0;
  60. goto done; };
  61. suffix = p; }
  62. done:
  63. for (p = name; p != suffix && *p; p++)
  64. putc (*p, stdout);
  65. putc ('\n', stdout);
  66. exit(0);
  67. }
  68. /* print error message and die with exit code 1 */
  69. fatal (string, arg)
  70. char *string, *arg;
  71. {
  72. fprintf (stderr, "%s: ", progname);
  73. fprintf (stderr, string, arg);
  74. fprintf (stderr, "\n");
  75. exit (1);
  76. }