fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. static void find_destructive(char *s) {
  5. char *p_sl = strrchr(s, '/');
  6. if (p_sl) {
  7. *p_sl = '\0';
  8. printf("[%s] [%s]\n", s, p_sl + 1);
  9. } else {
  10. printf("Cannot find any slashes.\n");
  11. }
  12. }
  13.  
  14. static void find_transparent(const char *s) {
  15. const char *p_sl = strrchr(s, '/');
  16. if (p_sl) {
  17. char *first = (char *)malloc(p_sl - s + 1);
  18. if ( ! first) {
  19. perror("malloc for a temp buffer: ");
  20. return;
  21. }
  22. memcpy(first, s, p_sl - s);
  23. first[p_sl - s] = '\0';
  24. printf("[%s] [%s]\n", first, p_sl + 1);
  25. free(first);
  26. } else {
  27. printf("Cannot find any slashes.\n");
  28. }
  29. }
  30.  
  31. int main() {
  32. char s[] = "home/usr/wow/muchprogram";
  33.  
  34. find_transparent(s);
  35. find_destructive(s);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
[home/usr/wow] [muchprogram]
[home/usr/wow] [muchprogram]