opkg_utils.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* opkg_utils.c - the opkg package management system
  2. Steven M. Ayer
  3. Copyright (C) 2002 Compaq Computer Corporation
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include <ctype.h>
  14. #include <sys/statvfs.h>
  15. #include "libbb/libbb.h"
  16. #include "opkg_utils.h"
  17. unsigned long get_available_kbytes(char *filesystem)
  18. {
  19. struct statvfs f;
  20. if (statvfs(filesystem, &f) == -1) {
  21. opkg_perror(ERROR, "Failed to statvfs for %s", filesystem);
  22. return 0;
  23. }
  24. // Actually ((sfs.f_bavail * sfs.f_frsize) / 1024)
  25. // and here we try to avoid overflow.
  26. if (f.f_frsize >= 1024)
  27. return (f.f_bavail * (f.f_frsize / 1024));
  28. else if (f.f_frsize > 0)
  29. return f.f_bavail / (1024 / f.f_frsize);
  30. opkg_msg(ERROR, "Unknown block size for target filesystem.\n");
  31. return 0;
  32. }
  33. /* something to remove whitespace, a hash pooper */
  34. char *trim_xstrdup(const char *src)
  35. {
  36. const char *end;
  37. /* remove it from the front */
  38. while (src && isspace(*src) && *src)
  39. src++;
  40. end = src + (strlen(src) - 1);
  41. /* and now from the back */
  42. while ((end > src) && isspace(*end))
  43. end--;
  44. end++;
  45. /* xstrndup will NULL terminate for us */
  46. return xstrndup(src, end - src);
  47. }
  48. int line_is_blank(const char *line)
  49. {
  50. const char *s;
  51. for (s = line; *s; s++) {
  52. if (!isspace(*s))
  53. return 0;
  54. }
  55. return 1;
  56. }