utils.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2005 - Alejandro Liu Ly <alejandro_liu@hotmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * @project mfstool
  20. * @module utils
  21. * @section 3
  22. * @doc Miscellaneous utilities
  23. */
  24. #include <stdarg.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "minix_fs.h"
  30. #include "protos.h"
  31. /**
  32. * Print an error message and die.
  33. * @param s - string format
  34. * @effect This function causes the program to die
  35. */
  36. void fatalmsg(const char *s,...) {
  37. va_list p;
  38. va_start(p,s);
  39. vfprintf(stderr,s,p);
  40. va_end(p);
  41. putc('\n',stderr);
  42. exit(-1);
  43. }
  44. /**
  45. * Like fatalmsg but also show the errno message.
  46. * @param s - string format
  47. * @effect This function causes the program to die.
  48. */
  49. void die(const char *s,...) {
  50. va_list p;
  51. va_start(p,s);
  52. vfprintf(stderr,s,p);
  53. va_end(p);
  54. putc(':',stderr);
  55. putc(' ',stderr);
  56. perror(NULL);
  57. putc('\n',stderr);
  58. exit(errno);
  59. }
  60. /**
  61. * Seek file to the specified block
  62. * @param fp - Pointer to the filesystem file.
  63. * @param blk - Block to go to
  64. * @return fp
  65. */
  66. FILE *goto_blk(FILE *fp,int blk) {
  67. fflush(fp);
  68. if (fseek(fp,blk*BLOCK_SIZE,SEEK_SET)) {
  69. die("fseek");
  70. }
  71. return fp;
  72. }
  73. /**
  74. * Write cnt bytes to file.
  75. * @param fp - Pointer to filesystem file.
  76. * @param buff - data to write
  77. * @param cnt - bytes to write
  78. * @return buff
  79. */
  80. void *dofwrite(FILE *fp,void *buff,int cnt) {
  81. if (cnt != fwrite(buff,1,cnt,fp)) {
  82. die("fwrite");
  83. }
  84. return buff;
  85. }
  86. /**
  87. * Read cnt bytes from file
  88. * @param fp - Pointer to filesystem file.
  89. * @param buff - Buffer (BLOCK_SIZE) big.
  90. * @param cnt - bytes to read
  91. * @return buff
  92. */
  93. void *dofread(FILE *fp,void *buff,int cnt) {
  94. if (cnt != fread(buff,1,cnt,fp)) {
  95. die("fread");
  96. }
  97. return buff;
  98. }
  99. /**
  100. * Allocate memory (w/error handling) and memory clearing.
  101. * @param size - number of bytes to allocate
  102. * @param elm - Initialize memory to this value. -1 for no initialization
  103. * @return pointer to newly allocated memory.
  104. */
  105. void *domalloc(unsigned long size,int elm) {
  106. void *ptr = malloc(size);
  107. if (!ptr) {
  108. die("malloc");
  109. }
  110. if (elm >= 0) {
  111. memset(ptr,elm,size);
  112. }
  113. return ptr;
  114. }
  115. /**
  116. * Wrapper around getuid
  117. */
  118. int dogetuid(void) {
  119. if (opt_squash) return 0;
  120. return getuid();
  121. }
  122. /**
  123. * Wrapper around getgid
  124. */
  125. int dogetgid(void) {
  126. if (opt_squash) return 0;
  127. return getgid();
  128. }