mount.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "mount.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <sys/mount.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. int next_word(FILE* f, char* out)
  12. {
  13. int i,c;
  14. for(i=0; (c=fgetc(f))!='\t' && c!=' ' && c!='\n' && c!=EOF; i++)
  15. out[i]=c;
  16. out[i]=0;
  17. if(c=='\n') return 1;
  18. if(c==' ' || c=='\t') return 0;
  19. if(c==EOF) return -1;
  20. }
  21. int is_mounted(const char* mp)
  22. {
  23. int c=0;
  24. char buf[2048];
  25. FILE* f = fopen("/proc/mounts", "r");
  26. if(!f)
  27. {
  28. fprintf(stderr, "Errue unable to open '/proc/mounts' ");
  29. perror(" ");
  30. return -1;
  31. }
  32. while(c>=0)
  33. {
  34. next_word(f, buf);
  35. next_word(f, buf);
  36. if(!strcmp(buf, mp))
  37. {
  38. fclose(f);
  39. return 1;
  40. }
  41. while((c=next_word(f, buf))==0);
  42. }
  43. fclose(f);
  44. return 0;
  45. }
  46. int mount_samba(config_t* cfg)
  47. {
  48. int tomount=config_get_int(cfg, "mount");
  49. char buffer[4096];
  50. const char* ip = config_get_string(cfg, "mount.ip");
  51. const char* src = config_get_string(cfg, "mount.src");
  52. const char* dst = config_get_string(cfg, "mount.dst");
  53. const char* username = config_get_string(cfg, "mount.username");
  54. const char* password = config_get_string(cfg, "mount.password");
  55. if(!tomount) return 0;
  56. sprintf(buffer, "unc=%s,ip=%s,username=%s,password=%s", src, ip, username, password);
  57. printf("%s\n", buffer);
  58. if(mount(src, dst, "cifs", MS_MGC_VAL | MS_SILENT, buffer))
  59. {
  60. fprintf(stderr, "Erreur unable to mount : %s\n", strerror(errno));
  61. return errno;
  62. }
  63. return 0;
  64. }