Guest User

Untitled

a guest
Oct 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. static char *
  6. getpkgname(const char *str)
  7. {
  8. const char *p;
  9. char *buf;
  10. size_t len;
  11.  
  12. if ((p = strrchr(str, '-')) == NULL)
  13. return NULL;
  14.  
  15. if (strrchr(p, '_') == NULL)
  16. return NULL;
  17.  
  18. len = strlen(str) - strlen(p) + 1;
  19. buf = malloc(len);
  20. strncpy(buf, str, len-1);
  21. buf[len] = '\0';
  22.  
  23. return buf;
  24. }
  25.  
  26. static const char *
  27. getpkgver(const char *str)
  28. {
  29. const char *p;
  30.  
  31. if ((p = strrchr(str, '-')) == NULL)
  32. return NULL;
  33.  
  34. if (strrchr(p, '_') == NULL)
  35. return NULL;
  36.  
  37. return p + 1;
  38. }
  39.  
  40. int main(int argc, char **argv)
  41. {
  42. printf("name: %s ver: %s\n", getpkgname(argv[1]), getpkgver(argv[1]));
  43. return 0;
  44. }
Add Comment
Please, Sign In to add comment