mount.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. return 0;
  21. }
  22. int is_mounted(const char* mp)
  23. {
  24. int c=0;
  25. char buf[2048];
  26. FILE* f = fopen("/proc/mounts", "r");
  27. if(!f)
  28. {
  29. fprintf(stderr, "Errue unable to open '/proc/mounts' ");
  30. perror(" ");
  31. return -1;
  32. }
  33. while(c>=0)
  34. {
  35. next_word(f, buf);
  36. next_word(f, buf);
  37. if(!strcmp(buf, mp))
  38. {
  39. fclose(f);
  40. return 1;
  41. }
  42. while((c=next_word(f, buf))==0);
  43. }
  44. fclose(f);
  45. return 0;
  46. }
  47. int mount_samba(config_t* cfg)
  48. {
  49. int tomount=config_get_int(cfg, "mount");
  50. char buffer[4096];
  51. const char* ip = config_get_string(cfg, "mount.ip");
  52. const char* src = config_get_string(cfg, "mount.src");
  53. const char* dst = config_get_string(cfg, "mount.dst");
  54. const char* username = config_get_string(cfg, "mount.username");
  55. const char* password = config_get_string(cfg, "mount.password");
  56. if(!tomount) return 0;
  57. sprintf(buffer, "unc='%s',ip=%s,username=%s,password=%s", src, ip, username, password);
  58. printf("%s\n", buffer);
  59. if(mount(src, dst, "cifs", MS_MGC_VAL | MS_SILENT, buffer))
  60. {
  61. fprintf(stderr, "Erreur unable to mount : %s\n", strerror(errno));
  62. return errno;
  63. }
  64. return 0;
  65. }