Ejecter lecteur cd

Ejecter lecteur cd - C - Programmation

Marsh Posté le 20-12-2006 à 20:28:10    

Bonjour,
Cela fait un moment que je cherche sur le web un code en c pour ejecter un lecteur mais je ne trouve pas.
Si quelqu'un pouvait m'aider.
merci beaucoup

Reply

Marsh Posté le 20-12-2006 à 20:28:10   

Reply

Marsh Posté le 20-12-2006 à 20:31:14    

La majorité des lecteurs CDs étant vissés aux boitiers respectifs de leurs PCs, faut chercher les fonctions hTournevis et unscrewCaseScrew dans l'API win32 pour pouvoir enlever les visses


---------------
Stick a parrot in a Call of Duty lobby, and you're gonna get a racist parrot. — Cody
Reply

Marsh Posté le 20-12-2006 à 20:40:52    

masklinn a écrit :

La majorité des lecteurs CDs étant vissés aux boitiers respectifs de leurs PCs, faut chercher les fonctions hTournevis et unscrewCaseScrew dans l'API win32 pour pouvoir enlever les visses


 [:roxelay]


---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
Reply

Marsh Posté le 20-12-2006 à 20:47:47    

et en étant sérieux cela fait quoi?

Reply

Marsh Posté le 20-12-2006 à 21:01:29    

mx321 a écrit :

et en étant sérieux cela fait quoi?


Tu veux vraiment éjecter le lecteur ? Il t'a fait quoi ?
 


---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
Reply

Marsh Posté le 20-12-2006 à 21:04:55    

Voilà [:dawa]

Code :
  1. /********************************************************************
  2. *
  3. *        L I N U X   E J E C T    C O M M A N D
  4. *
  5. *          by Jeff Tranter (tranter@pobox.com)
  6. *
  7. ********************************************************************
  8. *
  9. * Copyright (C) 1994-2001 Jeff Tranter (tranter@pobox.com)
  10. * Copyright (C) 2004, 2005 Frank Lichtenheld (djpig@debian.org)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. ********************************************************************
  27. *
  28. * See the man page for a description of what this program does and what
  29. * the requirements to run it are.
  30. *
  31. */
  32.  
  33. #include "i18n.h"
  34.  
  35. #ifndef DEFAULTDEVICE
  36. #error DEFAULTDEVICE not set, check Makefile
  37. #endif
  38.  
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #include <limits.h>
  45.  
  46. #ifdef GETOPTLONG
  47. #include <getopt.h>
  48. #endif /* GETOPTLONG */
  49. #include <errno.h>
  50. #include <regex.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <sys/ioctl.h>
  54. #include <sys/wait.h>
  55. #include <sys/mtio.h>
  56. #include <sys/mount.h>
  57.  
  58. #if defined(__linux__)
  59. #include <linux/version.h>
  60. /* handy macro found in 2.1 kernels, but not in older ones */
  61. #ifndef KERNEL_VERSION
  62. #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  63. #endif
  64. #include <linux/types.h>
  65. #include <linux/cdrom.h>
  66. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
  67. #include <linux/ucdrom.h>
  68. #endif
  69. #include <linux/fd.h>
  70. #include <scsi/scsi.h>
  71. #include <scsi/sg.h>
  72. #include <scsi/scsi_ioctl.h>
  73. #include <sys/time.h>
  74.  
  75. /* Used by the ToggleTray() function. If ejecting the tray takes this
  76. * time or less, the tray was probably already ejected, so we close it
  77. * again.
  78. */
  79. #define TRAY_WAS_ALREADY_OPEN_USECS  200000    /* about 0.2 seconds */
  80.  
  81.  
  82. #define CLOSE(fd) if (close(fd)==-1) { \
  83.    perror(programName); \
  84.    exit(1); \
  85. }
  86.  
  87. #define FCLOSE(fd) if (fclose(fd)==-1) { \
  88.    perror(programName); \
  89.    exit(1); \
  90. }
  91.  
  92. #define HAVE_EJECT_SCSI
  93. #define HAVE_EJECT_FLOPPY
  94. #define HAVE_EJECT_TAPE
  95.  
  96. #elif defined(__FreeBSD_kernel__)
  97. #include <sys/cdio.h>
  98. #endif /* if defined(__linux__) */
  99.  
  100.  
  101. #define CLOSE(fd) if (close(fd)==-1) { \
  102.    perror(programName); \
  103.    exit(1); \
  104. }
  105.  
  106. #define FCLOSE(fd) if (fclose(fd)==-1) { \
  107.    perror(programName); \
  108.    exit(1); \
  109. }
  110.  
  111. /* Global Variables */
  112. static const char *version = VERSION; /* program version */
  113. int a_option = 0; /* command flags and arguments */
  114. int c_option = 0;
  115. int d_option = 0;
  116. int f_option = 0;
  117. int h_option = 0;
  118. int n_option = 0;
  119. int q_option = 0;
  120. int r_option = 0;
  121. int s_option = 0;
  122. int t_option = 0;
  123. int T_option = 0;
  124. int X_option = 0;
  125. int v_option = 0;
  126. int x_option = 0;
  127. int p_option = 0;
  128. int m_option = 0;
  129. int a_arg = 0;
  130. int c_arg = 0;
  131. int x_arg = 0;
  132. static char *programName; /* used in error messages */
  133.  
  134. /*
  135. * These are the basenames of devices which can have multiple
  136. * partitions per device.
  137. */
  138. static const char *partitionDevice[] = {
  139.     "hd",
  140.     "sd",
  141.     "xd",
  142.     "dos_hd",
  143.     "mfm",
  144.     "ad",
  145.     "ed",
  146.     "ftl",
  147.     "pd",
  148.     0};
  149.  
  150.  
  151. /* Display command usage on standard error and exit. */
  152. static void usage()
  153. {
  154. //    perror(_("%s: device is `%s'\n" ));
  155.     fprintf(stderr,_(
  156. "Eject version %s by Jeff Tranter (tranter@pobox.com)\n"
  157. "Usage:\n"
  158. "  eject -h                -- display command usage and exit\n"
  159. "  eject -V                -- display program version and exit\n"
  160. "  eject [-vnrsfqpm] [<name>]        -- eject device\n"
  161. "  eject [-vn] -d            -- display default device\n"
  162. "  eject [-vn] -a on|off|1|0 [<name>]    -- turn auto-eject feature on or off\n"
  163. "  eject [-vn] -c <slot> [<name>]    -- switch discs on a CD-ROM changer\n"
  164. "  eject [-vn] -t [<name>]        -- close tray\n"
  165. "  eject [-vn] -T [<name>]        -- toggle tray\n"
  166. "  eject [-vn] -x <speed> [<name>]    -- set CD-ROM max speed\n"
  167. "  eject [-vn] -X [<name>]        -- list CD-ROM available speeds\n"
  168. "Options:\n"
  169. "  -v\t-- enable verbose output\n"
  170. "  -n\t-- don't eject, just show device found\n"
  171. "  -r\t-- eject CD-ROM\n"
  172. #ifdef HAVE_EJECT_SCSI
  173. "  -s\t-- eject SCSI device\n"
  174. #endif
  175. #ifdef HAVE_EJECT_FLOPPY
  176. "  -f\t-- eject floppy\n"
  177. #endif
  178. #ifdef HAVE_EJECT_TAPE
  179. "  -q\t-- eject tape\n"
  180. #endif
  181. "  -p\t-- use /proc/mounts instead of /etc/mtab\n"
  182. "  -m\t-- do not unmount device even if it is mounted\n"
  183. )
  184. , version);
  185. #ifdef GETOPTLONG
  186.     fprintf(stderr,_(
  187. "Long options:\n"
  188. "  -h --help   -v --verbose      -d --default\n"
  189. "  -a --auto   -c --changerslot  -t --trayclose  -x --cdspeed\n"
  190. "  -r --cdrom"
  191. #ifdef HAVE_EJECT_SCSI
  192. "  -s --scsi"
  193. #endif
  194. #ifdef HAVE_EJECT_FLOPPY
  195. "         -f --floppy"
  196. #endif
  197. "     -X --listspeed"
  198. #ifdef HAVE_EJECT_TAPE
  199. "     -q --tape"
  200. #endif
  201. "\n"
  202. "  -n --noop   -V --version\n"
  203. "  -p --proc   -m --no-unmount   -T --traytoggle\n" ));
  204. #endif /* GETOPTLONG */
  205.     fprintf(stderr,_(
  206. "Parameter <name> can be a device file or a mount point.\n"
  207. "If omitted, name defaults to `%s'.\n"
  208. "By default tries -r, -s, -f, and -q in order until success.\n" ),
  209.             DEFAULTDEVICE);
  210.  exit(1);
  211. }
  212.  
  213.  
  214. /* Handle command line options. */
  215. static void parse_args(int argc, char **argv, char **device)
  216. {
  217.     const char *flags = "a:c:x:dfhnqrstTXvVpm";
  218. #ifdef GETOPTLONG
  219.     static struct option long_options[] =
  220.     {
  221.         {"help",    no_argument,       NULL, 'h'},
  222.         {"verbose",    no_argument,       NULL, 'v'},
  223.         {"default",    no_argument,       NULL, 'd'},
  224.         {"auto",    required_argument, NULL, 'a'},
  225.         {"changerslot", required_argument, NULL, 'c'},
  226.         {"trayclose",    no_argument,       NULL, 't'},
  227.         {"traytoggle",    no_argument,       NULL, 'T'},
  228.         {"cdspeed",    required_argument, NULL, 'x'},
  229.         {"listspeed",    no_argument,       NULL, 'X'},
  230.         {"noop",    no_argument,       NULL, 'n'},
  231.         {"cdrom",    no_argument,       NULL, 'r'},
  232.         {"scsi",    no_argument,       NULL, 's'},
  233.         {"floppy",    no_argument,       NULL, 'f'},
  234.         {"tape",    no_argument,       NULL, 'q'},
  235.         {"version",    no_argument,       NULL, 'V'},
  236.         {"proc",    no_argument,       NULL, 'p'},
  237.         {"no-unmount",    no_argument,       NULL, 'm'},
  238.         {0, 0, 0, 0}
  239.     };
  240.     int option_index;
  241. #endif /* GETOPTLONG */
  242.     int c;
  243.  
  244. #ifdef GETOPTLONG
  245.     while ((c = getopt_long(argc, argv, flags, long_options, &option_index)) != EOF) {
  246. #else
  247.     while ((c = getopt(argc, argv, flags)) != EOF) {
  248. #endif /* GETOPTLONG */
  249.         switch (c) {
  250.           case 'a':
  251.               a_option = 1;
  252.               if (!strcmp(optarg, "0" ))
  253.                   a_arg = 0;
  254.               else if (!strcmp(optarg, "off" ))
  255.                   a_arg = 0;
  256.               else if (!strcmp(optarg, "1" ))
  257.                   a_arg = 1;
  258.               else if (!strcmp(optarg, "on" ))
  259.                   a_arg = 1;
  260.               else {
  261.                   fprintf(stderr, _("%s: invalid argument to --auto/-a option\n" ), programName);
  262.                   exit(1);
  263.               }
  264.               break;
  265.           case 'c':
  266.               c_option = 1;
  267.               /* atoi() returns 0 on error, so "0" must be parsed separately */
  268.               if (!strcmp(optarg, "0" ))
  269.                   c_arg = 0;
  270.               else {
  271.                   c_arg = atoi(optarg);
  272.                   if (c_arg <= 0) {
  273.                       fprintf(stderr, _("%s: invalid argument to --changerslot/-c option\n" ), programName);
  274.                       exit(1);
  275.                   }
  276.               }
  277.               break;
  278.           case 'x':
  279.               x_option = 1;
  280.               if (!strcmp(optarg, "0" ))
  281.                   x_arg = 0;
  282.               else {
  283.                   x_arg = atoi(optarg);
  284.                   if (x_arg <= 0) {
  285.                       fprintf(stderr, _("%s: invalid argument to --cdspeed/-x option\n" ), programName);
  286.                       exit(1);
  287.                   }
  288.               }
  289.               break;
  290.           case 'd':
  291.               d_option = 1;
  292.               break;
  293.           case 'f':
  294.               f_option = 1;
  295.               break;
  296.           case 'h':
  297.               usage();
  298.               exit(0);
  299.               break;
  300.           case 'm':
  301.               m_option = 1;
  302.               break;
  303.           case 'n':
  304.               n_option = 1;
  305.               break;
  306.           case 'p':
  307.               p_option = 1;
  308.               break;
  309.           case 'q':
  310.               q_option = 1;
  311.               break;
  312.           case 'r':
  313.               r_option = 1;
  314.               break;
  315.           case 's':
  316.               s_option = 1;
  317.               break;
  318.           case 't':
  319.               t_option = 1;
  320.               break;
  321.           case 'X':
  322.               X_option = 1;
  323.               break;
  324.           case 'T':
  325.               T_option = 1;
  326.               break;
  327.           case 'v':
  328.               v_option = 1;
  329.               break;
  330.           case 'V':
  331.               printf(_("eject version %s by Jeff Tranter (tranter@pobox.com)\n" ), version);
  332.               exit(0);
  333.               break;
  334.           case '?':
  335.               exit(1);
  336.               break;
  337.         }
  338.     }
  339.     /* check for a single additional argument */
  340.     if ((argc - optind) > 1) {
  341.         fprintf(stderr, _("%s: too many arguments\n" ), programName);
  342.         exit(1);
  343.     }
  344.     if ((argc - optind) == 1) { /* one argument */
  345.         *device = strdup(argv[optind]);
  346.     }
  347. }
  348.  
  349.  
  350. /* Return 1 if file/device exists, 0 otherwise. */
  351. static int FileExists(const char *name, const int try, int *found)
  352. {
  353.  
  354.        if (!found) return -1;
  355.        /*
  356.      * access() uses the UID, not the EUID. This way a normal user
  357.      * cannot find out if a file (say, /root/fubar) exists or not, even
  358.      * if eject is SUID root
  359.      */
  360.     if (access (name, F_OK) == 0) {
  361.       (*found)++;
  362.       if (try <= (*found))
  363.         return 1;
  364.       else
  365.         return 0;
  366.     } else {
  367.       return 0;
  368.     }
  369. }
  370.  
  371.  
  372. /*
  373. * Given name, such as foo, see if any of the following exist:
  374. *
  375. * foo (if foo starts with '.' or '/')
  376. * /dev/foo
  377. * /media/foo
  378. * /mnt/foo
  379. * /dev/cdroms/foo
  380. * /dev/cdroms/foo0
  381. * /dev/dsk/foo
  382. * /dev/rdsk/foo
  383. * ./foo
  384. *
  385. * If found, return the full path. If not found, return 0.
  386. * Returns pointer to dynamically allocated string.
  387. */
  388. static char *FindDevice(const char *name)
  389. {
  390.     char *buf;
  391.     static int try = 0;
  392.     int found = 0;
  393.  
  394.     buf = (char *) malloc(strlen(name)+14); /* to allow for "/dev/cdroms/ + "0" + null */
  395.     if (buf==NULL) {
  396.         fprintf(stderr, _("%s: could not allocate memory\n" ), programName);
  397.         exit(1);
  398.     }
  399.  
  400.     if (try == INT_MAX) {
  401.       fprintf(stderr, _("%s: FindDevice called too often\n" ), programName );;
  402.       exit(1);
  403.     } else
  404.       try++;
  405.  
  406.     if ((name[0] == '.') || (name[0] == '/')) {
  407.         strcpy(buf, name);
  408.         if (FileExists(buf, try, &found))
  409.             return buf;
  410.     }
  411.  
  412.     strcpy(buf, "/dev/" );
  413.     strcat(buf, name);
  414.     if (FileExists(buf, try, &found))
  415.         return buf;
  416.  
  417.     strcpy(buf, "/media/" );
  418.     strcat(buf, name);
  419.     if (FileExists(buf, try, &found))
  420.         return buf;
  421.     
  422.     strcpy(buf, "/mnt/" );
  423.     strcat(buf, name);
  424.     if (FileExists(buf, try, &found))
  425.         return buf;
  426.  
  427.     /* for devfs under Linux */
  428.     strcpy(buf, "/dev/cdroms/" );
  429.     strcat(buf, name);
  430.     if (FileExists(buf, try, &found))
  431.         return buf;
  432.  
  433.     strcpy(buf, "/dev/cdroms/" );
  434.     strcat(buf, name);
  435.     strcat(buf, "0" );
  436.     if (FileExists(buf, try, &found))
  437.         return buf;
  438.  
  439.     /* for devfs under Solaris */
  440.     strcpy(buf, "/dev/rdsk/" );
  441.     strcat(buf, name);
  442.     if (FileExists(buf, try, &found))
  443.         return buf;
  444.  
  445.     strcpy(buf, "/dev/dsk/" );
  446.     strcat(buf, name);
  447.     if (FileExists(buf, try, &found))
  448.         return buf;
  449.  
  450.     strcpy(buf, "./" );
  451.     strcat(buf, name);
  452.     if (FileExists(buf, try, &found))
  453.         return buf;
  454.  
  455.     free(buf);
  456.     buf = 0;
  457.     return 0;
  458. }
  459.  
  460.  
  461. /* Set or clear auto-eject mode. */
  462. static void AutoEject(int fd, int onOff)
  463. {
  464.     int status = -1;
  465.  
  466. #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
  467.     if (onOff)
  468.         status = ioctl(fd, CDROM_SET_OPTIONS, CDO_AUTO_EJECT);
  469.     else
  470.         status = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_AUTO_EJECT);
  471. #else
  472.     errno = ENOSYS;
  473. #endif
  474.     if (status < 0) {
  475.         fprintf(stderr, _("%s: CD-ROM auto-eject command failed: %s\n" ), programName, strerror(errno));
  476.         exit(1);
  477.     }
  478. }
  479.  
  480.  
  481. /*
  482. * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
  483. * CDROMLOADFROMSLOT.
  484. */
  485. static void ChangerSelect(int fd, int slot)
  486. {
  487.     int status;
  488.  
  489. #ifdef CDROM_SELECT_DISC
  490.     status = ioctl(fd, CDROM_SELECT_DISC, slot);
  491.     if (status < 0) {
  492.         fprintf(stderr, _("%s: CD-ROM select disc command failed: %s\n" ), programName, strerror(errno));
  493.         exit(1);
  494.     }
  495. #elif defined CDROMLOADFROMSLOT
  496.     status = ioctl(fd, CDROMLOADFROMSLOT, slot);
  497.     if (status != 0) {
  498.         fprintf(stderr, _("%s: CD-ROM load from slot command failed: %s\n" ), programName, strerror(errno));
  499.         exit(1);
  500.     }
  501. #else
  502.    fprintf(stderr, _("%s: IDE/ATAPI CD-ROM changer not supported by this kernel\n" ), programName);
  503. #endif
  504. }
  505.  
  506.  
  507. /*
  508. * Close tray. Not supported by older kernels.
  509. */
  510. static void CloseTray(int fd)
  511. {
  512.     int status;
  513.  
  514. #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
  515. #if defined(CDROMCLOSETRAY)
  516.     status = ioctl(fd, CDROMCLOSETRAY);
  517. #elif defined(CDIOCCLOSE)
  518.     status = ioctl(fd, CDIOCCLOSE);
  519. #endif
  520.     if (status != 0) {
  521.         fprintf(stderr, _("%s: CD-ROM tray close command failed: %s\n" ), programName, strerror(errno));
  522.         exit(1);
  523.     }
  524. #else
  525.    fprintf(stderr, _("%s: CD-ROM tray close command not supported by this kernel\n" ), programName);
  526. #endif
  527. }
  528.  
  529. /*
  530. * Toggle tray.
  531. *
  532. * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
  533. * Sybren Stuvel <sybren@thirdtower.com>
  534. *
  535. * Not supported by older kernels because it might use
  536. * CloseTray().
  537. *
  538. */
  539. static void ToggleTray(int fd)
  540. {
  541.     struct timeval time_start, time_stop;
  542.     int time_elapsed;
  543.  
  544. #ifdef CDROMCLOSETRAY
  545.  
  546.     /* Try to open the CDROM tray and measure the time therefor
  547.      * needed.  In my experience the function needs less than 0.05
  548.      * seconds if the tray was already open, and at least 1.5 seconds
  549.      * if it was closed.  */
  550.     gettimeofday(&time_start, NULL);
  551.     
  552.     /* Send the CDROMEJECT command to the device. */
  553.     if (ioctl(fd, CDROMEJECT, 0) < 0) {
  554.         perror("ioctl" );
  555.         exit(1);
  556.     }
  557.  
  558.     /* Get the second timestamp, to measure the time needed to open
  559.      * the tray.  */
  560.     gettimeofday(&time_stop, NULL);
  561.  
  562.     time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) -
  563.         (time_start.tv_sec * 1000000 + time_start.tv_usec);
  564.  
  565.     /* If the tray "opened" too fast, we can be nearly sure, that it
  566.      * was already open. In this case, close it now. Else the tray was
  567.      * closed before. This would mean that we are done.  */
  568.     if (time_elapsed < TRAY_WAS_ALREADY_OPEN_USECS)
  569.         CloseTray(fd);
  570.  
  571. #else
  572.    fprintf(stderr, _("%s: CD-ROM tray toggle command not supported by this kernel\n" ), programName);
  573. #endif
  574.     
  575. }
  576.  
  577. /*
  578. * Select Speed of CD-ROM drive.
  579. * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
  580. */
  581. static void SelectSpeedCdrom(int fd, int speed)
  582. {
  583.     int status;
  584.  
  585. #ifdef CDROM_SELECT_SPEED
  586.     status = ioctl(fd, CDROM_SELECT_SPEED, speed);
  587.     if (status != 0) {
  588.         fprintf(stderr, _("%s: CD-ROM select speed command failed: %s\n" ), programName, strerror(errno));
  589.         exit(1);
  590.     }
  591. #else
  592.    fprintf(stderr, _("%s: CD-ROM select speed command not supported by this kernel\n" ), programName);
  593. #endif
  594. }
  595.  
  596. /*
  597. * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed is correctly reported
  598. */
  599. static int ReadSpeedCdrom(const char *shortName)
  600. {
  601.     char line[512];
  602.     char *str_speed, *str_name;
  603.     int drive_number = -1, i;
  604.     FILE *f = fopen("/proc/sys/dev/cdrom/info", "r" );
  605.     
  606.     if (f == NULL) {
  607.         fprintf(stderr, _("%s: unable to read the speed from /proc/sys/dev/cdrom/info\n" ), programName);
  608.         exit(1);
  609.     }
  610.     
  611.     while (!feof(f)) {
  612.         fgets(line, sizeof(line), f);
  613.  
  614.         /* find drive number from shortName in line "drive name" */
  615.         if (drive_number == -1) {
  616.             if (strncmp(line, "drive name:", 11) == 0) {
  617.                 str_name = strtok(&line[11], "\t " );
  618.                 drive_number = 0;
  619.                 while (strncmp(shortName, str_name, strlen(shortName)) != 0) {
  620.                     drive_number++;
  621.                     str_name = strtok(NULL, "\t " );
  622.                     if (str_name == NULL) {
  623.                         fprintf(stderr, _("%s: error while finding CD-ROM name\n" ), programName);
  624.                         exit(1);
  625.                     }
  626.                 }
  627.             }
  628.         /* find line "drive speed" and read the correct speed */
  629.         } else {
  630.             if (strncmp(line, "drive speed:", 12) == 0) {
  631.                 str_speed = strtok(&line[12], "\t " );
  632.                 for (i = 1; i < drive_number; i++)
  633.                     str_speed = strtok(NULL, "\t " );
  634.  
  635.                 if (str_speed == NULL) {
  636.                     fprintf(stderr, _("%s: error while reading speed\n" ), programName);
  637.                     exit(1);
  638.                 }
  639.                 return atoi(str_speed);
  640.             }
  641.         }
  642.     }
  643.  
  644.     fprintf(stderr, _("%s: error while reading speed\n" ), programName);
  645.     exit(1);
  646.     return -1;
  647. }
  648.  
  649.  
  650. /*
  651. * List Speed of CD-ROM drive.
  652. */
  653. static void ListSpeedCdrom(const char *fullName, int fd)
  654. {
  655. #ifdef CDROM_SELECT_SPEED
  656.     int max_speed, curr_speed = 0, prev_speed;
  657.     char *shortName = rindex(fullName, '/') + 1;
  658.     
  659.     SelectSpeedCdrom(fd, 0);
  660.     max_speed = ReadSpeedCdrom(shortName);
  661.     while (curr_speed < max_speed) {
  662.         prev_speed = curr_speed;
  663.         SelectSpeedCdrom(fd, prev_speed + 1);
  664.         curr_speed = ReadSpeedCdrom(shortName);
  665.         if (curr_speed > prev_speed)
  666.             printf("%d ", curr_speed);
  667.         else
  668.             curr_speed = prev_speed + 1;
  669.     }
  670.  
  671.     printf("\n" );
  672. #else
  673.     fprintf(stderr, _("%s: CD-ROM select speed command not supported by this kernel\n" ), programName);
  674. #endif
  675. }
  676.  
  677. /*
  678. * Eject using CDROMEJECT ioctl. Return 1 if successful, 0 otherwise.
  679. */
  680. static int EjectCdrom(int fd)
  681. {
  682.     int status = -1;
  683.  
  684. #if defined(CDROMEJECT)
  685.     status = ioctl(fd, CDROMEJECT);
  686. #elif defined(CDIOCEJECT)
  687.     status = ioctl(fd, CDIOCEJECT);
  688. #else
  689. /* Some kernels implement cdrom-eject only, but I don't think any kernel in the
  690.   world would implement eject only for non-cdrom drives.  Let's die. */
  691. # error
  692. #endif
  693.     return (status == 0);
  694. }
  695.  
  696. #ifdef HAVE_EJECT_SCSI
  697. /*
  698. * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
  699. */
  700. static int EjectScsi(int fd)
  701. {
  702.     int status, k;
  703.     sg_io_hdr_t io_hdr;
  704.     unsigned char allowRmBlk[6] = {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0};
  705.     unsigned char startStop1Blk[6] = {START_STOP, 0, 0, 0, 1, 0};
  706.     unsigned char startStop2Blk[6] = {START_STOP, 0, 0, 0, 2, 0};
  707.     unsigned char inqBuff[2];
  708.     unsigned char sense_buffer[32];
  709.  
  710.     if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
  711.       printf("not an sg device, or old sg driver\n" );
  712.       return 0;
  713.     }
  714.  
  715.     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  716.     io_hdr.interface_id = 'S';
  717.     io_hdr.cmd_len = 6;
  718.     io_hdr.mx_sb_len = sizeof(sense_buffer);
  719.     io_hdr.dxfer_direction = SG_DXFER_NONE;
  720.     io_hdr.dxfer_len = 0;
  721.     io_hdr.dxferp = inqBuff;
  722.     io_hdr.sbp = sense_buffer;
  723.     io_hdr.timeout = 2000;
  724.  
  725.     io_hdr.cmdp = allowRmBlk;
  726.     status = ioctl(fd, SG_IO, (void *)&io_hdr);
  727.     if (status < 0)
  728.         return 0;
  729.  
  730.     io_hdr.cmdp = startStop1Blk;
  731.     status = ioctl(fd, SG_IO, (void *)&io_hdr);
  732.     if (status < 0)
  733.         return 0;
  734.  
  735.     io_hdr.cmdp = startStop2Blk;
  736.     status = ioctl(fd, SG_IO, (void *)&io_hdr);
  737.     if (status < 0)
  738.         return 0;
  739.  
  740.     /* force kernel to reread partition table when new disc inserted */
  741.     status = ioctl(fd, BLKRRPART);
  742.     return 1;
  743. }
  744. #endif
  745.  
  746.  
  747. #ifdef HAVE_EJECT_FLOPPY
  748. /*
  749. * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
  750. */
  751. static int EjectFloppy(int fd)
  752. {
  753.     int status;
  754.  
  755.     status = ioctl(fd, FDEJECT);
  756.     return (status >= 0);
  757. }
  758. #endif
  759.  
  760.  
  761. #ifdef HAVE_EJECT_TAPE
  762. /*
  763. * Eject using tape ioctl. Return 1 if successful, 0 otherwise.
  764. */
  765. static int EjectTape(int fd)
  766. {
  767.     int status;
  768.     struct mtop op;
  769.  
  770.     op.mt_op = MTOFFL; /* rewind and eject */
  771.     op.mt_count = 0;   /* not used */
  772.     status = ioctl(fd, MTIOCTOP, &op);
  773.     return (status >= 0);
  774. }
  775. #endif
  776.  
  777.  
  778. /* Unmount a device. */
  779. static void Unmount(const char *fullName)
  780. {
  781.     int status;
  782.  
  783.     switch (fork()) {
  784.       case 0: /* child */
  785.           setuid(getuid()); /* reduce likelyhood of security holes when running setuid */
  786.           if(p_option) {
  787.               execlp("pumount", "pumount", fullName, "-n", NULL);
  788.               execlp("umount", "umount", fullName, "-n", NULL);
  789.           } else {
  790.               execlp("pumount", "pumount", fullName, NULL);
  791.               execlp("umount", "umount", fullName, NULL);
  792.           }
  793.           fprintf(stderr, _("%s: unable to exec umount of `%s': %s\n" ),
  794.                   programName, fullName, strerror(errno));
  795.           exit(1);
  796.           break;
  797.       case -1:
  798.           fprintf(stderr, _("%s: unable to fork: %s\n" ), programName, strerror(errno));
  799.           break;
  800.       default: /* parent */
  801.           wait(&status);
  802.           if (WIFEXITED(status) == 0) {
  803.               fprintf(stderr, _("%s: unmount of `%s' did not exit normally\n" ), programName, fullName);
  804.               exit(1);
  805.           }
  806.           if (WEXITSTATUS(status) != 0) {
  807.               fprintf(stderr, _("%s: unmount of `%s' failed\n" ), programName, fullName);
  808.               exit(1);
  809.           }
  810.           break;
  811.     }
  812. }
  813.  
  814.  
  815. /* Open a device file. */
  816. static int OpenDevice(const char *fullName)
  817. {
  818.     int fd = open(fullName, O_RDONLY|O_NONBLOCK);
  819.     if (fd == -1) {
  820.         fprintf(stderr, _("%s: unable to open `%s'\n" ), programName, fullName);
  821.         exit(1);
  822.     }
  823.     return fd;
  824. }
  825.  
  826.  
  827. /*
  828. * Get major and minor device numbers for a device file name, so we
  829. * can check for duplicate devices.
  830. */
  831. static int GetMajorMinor(const char *name, int *maj, int *min)
  832. {
  833.     struct stat sstat;
  834.     if (maj) *maj = -1;
  835.     if (min) *min = -1;
  836.     if (stat(name, &sstat) == -1)
  837.         return -1;
  838.     if (! S_ISBLK(sstat.st_mode) && ! S_ISCHR(sstat.st_mode))
  839.         return -1;
  840.     if (maj) *maj = major(sstat.st_rdev);
  841.     if (min) *min = minor(sstat.st_rdev);
  842.     return 0;
  843. }
  844.  
  845.  
  846. /*
  847. * See if device has been mounted by looking in mount table.  If so, set
  848. * device name and mount point name, and return 1, otherwise return 0.
  849. */
  850. static int MountedDevice(const char *name, char **mountName, char **deviceName)
  851. {
  852.     FILE *fp;
  853.     char line[1024];
  854.     char s1[1024];
  855.     char s2[1024];
  856.     int rc;
  857.  
  858.     int maj;
  859.     int min;
  860.  
  861.     GetMajorMinor(name, &maj, &min);
  862.  
  863.     fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab" ), "r" );
  864.     if (fp == NULL)
  865.     {
  866.         fprintf(stderr, _("unable to open %s: %s\n" ), (p_option ? "/proc/mounts" : "/etc/mtab" ), strerror(errno));
  867.         exit(1);
  868.     }
  869.  
  870.     while (fgets(line, sizeof(line), fp) != 0) {
  871.         rc = sscanf(line, "%1023s %1023s", s1, s2);
  872.         if (rc >= 2) {
  873.             int mtabmaj, mtabmin;
  874.             GetMajorMinor(s1, &mtabmaj, &mtabmin);
  875.             if (((strcmp(s1, name) == 0) || (strcmp(s2, name) == 0)) ||
  876.                 ((maj != -1) && (maj == mtabmaj) && (min == mtabmin))) {
  877.                 FCLOSE(fp);
  878.                 *deviceName = strdup(s1);
  879.                 *mountName = strdup(s2);
  880.                 return 1;
  881.             }
  882.         }
  883.     }
  884.     *deviceName = 0;
  885.     *mountName = 0;
  886.     FCLOSE(fp);
  887.     return 0;
  888. }
  889.  
  890.  
  891. /*
  892. * See if device can be mounted by looking in /etc/fstab.
  893. * If so, set device name and mount point name, and return 1,
  894. * otherwise return 0.
  895. */
  896. static int MountableDevice(const char *name, char **mountName, char **deviceName)
  897. {
  898.     FILE *fp;
  899.     char line[1024];
  900.     char s1[1024];
  901.     char s2[1024];
  902.     int rc;
  903.  
  904.     fp = fopen("/etc/fstab", "r" );
  905.     if (fp == NULL) {
  906. /*
  907. * /etc/fstab may be unreadable in some situations due to passwords in the
  908. * file.
  909. */
  910. /*        fprintf(stderr, _("%s: unable to open /etc/fstab: %s\n" ), programName, strerror(errno));
  911.         exit(1);*/
  912.         if (v_option) {
  913.             printf( _("%s: unable to open /etc/fstab: %s\n" ), programName, strerror(errno));
  914.         }
  915.         return -1;
  916.     }
  917.  
  918.     while (fgets(line, sizeof(line), fp) != 0) {
  919.         rc = sscanf(line, "%1023s %1023s", s1, s2);
  920.         if (rc >= 2 && s1[0] != '#' && strcmp(s2, name) == 0) {
  921.             FCLOSE(fp);
  922.             *deviceName = strdup(s1);
  923.             *mountName = strdup(s2);
  924.             return 1;
  925.         }
  926.     }
  927.     FCLOSE(fp);
  928.     return 0;
  929. }
  930.  
  931.  
  932. /*
  933. * Step through mount table and unmount all devices that match a regular
  934. * expression.
  935. */
  936. static void UnmountDevices(const char *pattern)
  937. {
  938.     regex_t preg;
  939.     FILE *fp;
  940.     char s1[1024];
  941.     char s2[1024];
  942.     char line[1024];
  943.     int status;
  944.  
  945.     if (regcomp(&preg, pattern, REG_EXTENDED)!=0) {
  946.         perror(programName);
  947.         exit(1);
  948.     }
  949.  
  950.     fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab" ), "r" );
  951.     if (fp == NULL)
  952.     {
  953.         fprintf(stderr, _("unable to open %s: %s\n" ),(p_option ? "/proc/mounts" : "/etc/mtab" ), strerror(errno));
  954.         exit(1);
  955.     }
  956.  
  957.     while (fgets(line, sizeof(line), fp) != 0) {
  958.         status = sscanf(line, "%1023s %1023s", s1, s2);
  959.         if (status >= 2) {
  960.             status = regexec(&preg, s1, 0, 0, 0);
  961.             if (status == 0) {
  962.                 if (v_option)
  963.                     printf(_("%s: unmounting `%s'\n" ), programName, s2);
  964.                 Unmount(s2);
  965.             }
  966.         }
  967.     }
  968.     regfree(&preg);
  969.     FCLOSE(fp);
  970. }
  971.  
  972.  
  973. /* Check if name is a symbolic link. If so, return what it points to. */
  974. static char *SymLink(const char *name)
  975. {
  976.     int status;
  977.     char s1[PATH_MAX];
  978.     char s2[PATH_MAX];
  979.     char s4[PATH_MAX];
  980.     char result[PATH_MAX];
  981.     char *s3;
  982.  
  983.     memset(s1, 0, sizeof(s1));
  984.     memset(s2, 0, sizeof(s2));
  985.     memset(s4, 0, sizeof(s4));
  986.     memset(result, 0, sizeof(result));
  987.  
  988.     status = readlink(name, s1, sizeof(s1) - 1);
  989.  
  990.     if (status == -1)
  991.         return 0;
  992.  
  993.     s1[status] = 0;
  994.     if (s1[0] == '/') { /* absolute link */
  995.         return strdup(s1);
  996.     } else { /* relative link, add base name */
  997.         strncpy(s2, name, sizeof(s2)-1);
  998.         s3 = strrchr(s2, '/');
  999.         if (s3 != 0) {
  1000.             s3[1] = 0;
  1001.             snprintf(result, sizeof(result)-1, "%s%s", s2, s1);
  1002.         }
  1003.     }
  1004.     realpath(result, s4);
  1005.     return strdup(s4);
  1006. }
  1007.  
  1008.  
  1009. /*
  1010. * Given a name, see if it matches a pattern for a device that can have
  1011. * multiple partitions.  If so, return a regular expression that matches
  1012. * partitions for that device, otherwise return 0.
  1013. */
  1014. static char *MultiplePartitions(const char *name)
  1015. {
  1016.     int i = 0;
  1017.     int status;
  1018.     regex_t preg;
  1019.     char pattern[256];
  1020.     char *result = 0;
  1021.  
  1022.     for (i = 0; partitionDevice[i] != 0; i++) {
  1023.         /* look for ^/dev/foo[a-z]([0-9]?[0-9])?$, e.g. /dev/hda1 */
  1024.         strcpy(pattern, "^/dev/" );
  1025.         strcat(pattern, partitionDevice[i]);
  1026.         strcat(pattern, "[a-z]([0-9]?[0-9])?$" );
  1027.         if (regcomp(&preg, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  1028.             perror(programName);
  1029.             exit(1);
  1030.         }
  1031.         status = regexec(&preg, name, 1, 0, 0);
  1032.         regfree(&preg);
  1033.         if (status == 0) {
  1034.             result = (char *) malloc(strlen(name) + 25);
  1035.             if (result == NULL) {
  1036.                 fprintf(stderr, _("%s: could not allocate memory\n" ), programName);
  1037.                 exit(1);
  1038.             }
  1039.             strcpy(result, name);
  1040.             result[strlen(partitionDevice[i]) + 6] = 0;
  1041.             strcat(result, "([0-9]?[0-9])?$" );
  1042.             if (v_option)
  1043.                 printf(_("%s: `%s' is a multipartition device\n" ), programName, name);
  1044.             return result;
  1045.         }
  1046.     }
  1047.     if (v_option)
  1048.         printf(_("%s: `%s' is not a multipartition device\n" ), programName, name);
  1049.     return 0;
  1050. }
  1051.  
  1052.  
  1053. /* handle -x option */
  1054. static void HandleXOption(char *deviceName)
  1055. {
  1056.     int fd;        /* file descriptor for device */
  1057.     if (x_option) {
  1058.         if (v_option)
  1059.         {
  1060.             if (x_arg == 0)
  1061.                 printf(_("%s: setting CD-ROM speed to auto\n" ), programName);
  1062.             else
  1063.                 printf(_("%s: setting CD-ROM speed to %dX\n" ), programName, x_arg);
  1064.         }
  1065.         fd = OpenDevice(deviceName);
  1066.         SelectSpeedCdrom(fd, x_arg);
  1067.         exit(0);
  1068.     }
  1069. }
  1070.  
  1071.  
  1072. /* main program */
  1073. int main(int argc, char **argv)
  1074. {
  1075.     const char *defaultDevice = DEFAULTDEVICE;  /* default if no name passed by user */
  1076.     int worked = 0;    /* set to 1 when successfully ejected */
  1077.     char *device = 0;  /* name passed from user */
  1078.     char *fullName;    /* expanded name */
  1079.     char *fullNameOrig;/* expanded name (links not resolved) */
  1080.     char *deviceName;  /* name of device */
  1081.     char *linkName;    /* name of device's symbolic link */
  1082.     char *mountName;   /* name of device's mount point */
  1083.     int fd;        /* file descriptor for device */
  1084.     int mounted = 0;   /* true if device is mounted */
  1085.     int mountable = 0; /* true if device is in /etc/fstab */
  1086.     int result = 0;    /* store the result of a operation */
  1087.     char *pattern;       /* regex for device if multiple partitions */
  1088.     int ld = 6;       /* symbolic link max depth */
  1089.  
  1090.     I18NCODE
  1091.  
  1092.     /* program name is global variable used by other procedures */
  1093.     programName = strdup(argv[0]);
  1094.  
  1095.     /* parse the command line arguments */
  1096.     parse_args(argc, argv, &device);
  1097.  
  1098.  
  1099.     /* handle -d option */
  1100.     if (d_option) {
  1101.         printf(_("%s: default device: `%s'\n" ), programName, defaultDevice);
  1102.         exit(0);
  1103.     }
  1104.  
  1105.     /* if no device, use default */
  1106.     if (device == 0) {
  1107.         device = strdup(defaultDevice);
  1108.         if (v_option)
  1109.             printf(_("%s: using default device `%s'\n" ), programName, device);
  1110.     }
  1111.  
  1112.     /* Strip any trailing slash from name in case user used bash/tcsh
  1113.        style filename completion (e.g. /mnt/cdrom/) */
  1114.     if (device[strlen(device)-1] == '/')
  1115.         device[strlen(device)-1] = 0;
  1116.  
  1117.     if (v_option)
  1118.         printf(_("%s: device name is `%s'\n" ), programName, device);
  1119.  
  1120.     do {
  1121.       /* figure out full device or mount point name */
  1122.       fullName = FindDevice(device);
  1123.       if (fullName == 0) {
  1124.         fprintf(stderr, _("%s: unable to find or open device for: `%s'\n" ),
  1125.             programName, device);
  1126.         exit(1);
  1127.       }
  1128.       if (v_option)
  1129.         printf(_("%s: expanded name is `%s'\n" ), programName, fullName);
  1130.  
  1131.       /* check for a symbolic link */
  1132.       /* /proc/mounts doesn't resolve symbolic links */
  1133.       fullNameOrig = strdup(fullName);
  1134.       linkName = strdup(fullName); /* ensure linkName is initialized */
  1135.       if (!p_option) {
  1136.         while ((linkName = SymLink(fullName)) && (ld > 0)) {
  1137.           if (v_option)
  1138.         printf(_("%s: `%s' is a link to `%s'\n" ), programName,
  1139.                fullName, linkName);
  1140.           free(fullName);
  1141.           fullName = strdup(linkName);
  1142.           free(linkName);
  1143.           linkName = 0;
  1144.           ld--;
  1145.         }
  1146.       }
  1147.       /* handle max depth exceeded option */
  1148.       if (ld <= 0) {
  1149.         printf(_("%s: maximum symbolic link depth exceeded: `%s'\n" ), programName, fullName);
  1150.         exit(1);
  1151.       }
  1152.  
  1153.       /* if mount point, get device name */
  1154.       mounted = MountedDevice(fullName, &mountName, &deviceName);
  1155.       if (v_option) {
  1156.         if (mounted)
  1157.           printf(_("%s: `%s' is mounted at `%s'\n" ), programName,
  1158.              deviceName, mountName);
  1159.         else
  1160.           printf(_("%s: `%s' is not mounted\n" ), programName, fullName);
  1161.       }
  1162.       if (!mounted) {
  1163.         deviceName = strdup(fullName);
  1164.       }
  1165.  
  1166.       /* if not currently mounted, see if it is a possible mount point */
  1167.       if (!mounted) {
  1168.         mountable = MountableDevice(fullName, &mountName, &deviceName);
  1169.         /* if return value -1 then fstab could not be read */
  1170.         if (v_option && mountable >= 0) {
  1171.           if (mountable)
  1172.         printf(_("%s: `%s' can be mounted at `%s'\n" ), programName, deviceName, mountName);
  1173.           else
  1174.         printf(_("%s: `%s' is not a mount point\n" ), programName, fullName);
  1175.         }
  1176.       }
  1177.  
  1178.       result = GetMajorMinor(deviceName, NULL, NULL);
  1179.       if (result == -1) {
  1180.           fprintf(stderr,
  1181.               _("%s: tried to use `%s' as device name but it is no block device\n" ),
  1182.               programName, deviceName);
  1183.       }
  1184.  
  1185.     } while (result == -1);
  1186.  
  1187.     /* handle -n option */
  1188.     if (n_option) {
  1189.         printf(_("%s: device is `%s'\n" ), programName, deviceName);
  1190.         if (v_option)
  1191.             printf(_("%s: exiting due to -n/--noop option\n" ), programName);
  1192.         exit(0);
  1193.     }
  1194.  
  1195.     /* handle -a option */
  1196.     if (a_option) {
  1197.         if (v_option) {
  1198.             if (a_arg)
  1199.                 printf(_("%s: enabling auto-eject mode for `%s'\n" ), programName, deviceName);
  1200.             else
  1201.                 printf(_("%s: disabling auto-eject mode for `%s'\n" ), programName, deviceName);
  1202.         }
  1203.         fd = OpenDevice(deviceName);
  1204.         AutoEject(fd, a_arg);
  1205.         exit(0);
  1206.     }
  1207.  
  1208.     /* handle -t option */
  1209.     if (t_option) {
  1210.         if (v_option)
  1211.             printf(_("%s: closing tray\n" ), programName);
  1212.         fd = OpenDevice(deviceName);
  1213.         CloseTray(fd);
  1214.         HandleXOption(deviceName);
  1215.         exit(0);
  1216.     }
  1217.  
  1218.     /* handle -X option */
  1219.     if (X_option) {
  1220.         if (v_option)
  1221.             printf(_("%s: listing CD-ROM speed\n" ), programName);
  1222.         fd = OpenDevice(deviceName);
  1223.         ListSpeedCdrom(deviceName, fd);
  1224.         exit(0);
  1225.     }
  1226.  
  1227.     /* handle -x option only */
  1228.     if (!c_option) HandleXOption(deviceName);
  1229.  
  1230.     /* unmount device if mounted */
  1231.     if ((m_option != 1) && mounted) {
  1232.         if (v_option)
  1233.             printf(_("%s: unmounting device `%s' from `%s'\n" ), programName, deviceName, mountName);
  1234.         Unmount(mountName);
  1235.     }
  1236.  
  1237.     /* if it is a multipartition device, unmount any other partitions on
  1238.        the device */
  1239.     pattern = MultiplePartitions(deviceName);
  1240.     if ((m_option != 1) && (pattern != 0))
  1241.         UnmountDevices(pattern);
  1242.  
  1243.     /* handle -T option */
  1244.     if (T_option) {
  1245.         if (v_option)
  1246.             printf(_("%s: toggling tray\n" ), programName);
  1247.         fd = OpenDevice(deviceName);
  1248.         ToggleTray(fd);
  1249.         HandleXOption(deviceName);
  1250.         exit(0);
  1251.     }
  1252.     
  1253.     /* handle -c option */
  1254.     if (c_option) {
  1255.         if (v_option)
  1256.             printf(_("%s: selecting CD-ROM disc #%d\n" ), programName, c_arg);
  1257.         fd = OpenDevice(deviceName);
  1258.         ChangerSelect(fd, c_arg);
  1259.         HandleXOption(deviceName);
  1260.         exit(0);
  1261.     }
  1262.  
  1263.     /* if user did not specify type of eject, try all four methods */
  1264.     if ((r_option + s_option + f_option + q_option) == 0) {
  1265.         r_option = s_option = f_option = q_option = 1;
  1266.     }
  1267.  
  1268.     /* open device */
  1269.     fd = OpenDevice(deviceName);
  1270.  
  1271.     /* try various methods of ejecting until it works */
  1272.     if (r_option) {
  1273.         if (v_option)
  1274.             printf(_("%s: trying to eject `%s' using CD-ROM eject command\n" ), programName, deviceName);
  1275.         worked = EjectCdrom(fd);
  1276.         if (v_option) {
  1277.             if (worked)
  1278.                 printf(_("%s: CD-ROM eject command succeeded\n" ), programName);
  1279.             else
  1280.                 printf(_("%s: CD-ROM eject command failed\n" ), programName);
  1281.         }
  1282.     }
  1283.  
  1284. #ifdef HAVE_EJECT_SCSI
  1285.     if (s_option && !worked) {
  1286.         if (v_option)
  1287.             printf(_("%s: trying to eject `%s' using SCSI commands\n" ), programName, deviceName);
  1288.         worked = EjectScsi(fd);
  1289.         if (v_option) {
  1290.             if (worked)
  1291.                 printf(_("%s: SCSI eject succeeded\n" ), programName);
  1292.             else
  1293.                 printf(_("%s: SCSI eject failed\n" ), programName);
  1294.         }
  1295.     }
  1296. #endif
  1297.  
  1298. #ifdef HAVE_EJECT_FLOPPY
  1299.     if (f_option && !worked) {
  1300.         if (v_option)
  1301.             printf(_("%s: trying to eject `%s' using floppy eject command\n" ), programName, deviceName);
  1302.         worked = EjectFloppy(fd);
  1303.         if (v_option) {
  1304.             if (worked)
  1305.                 printf(_("%s: floppy eject command succeeded\n" ), programName);
  1306.             else
  1307.                 printf(_("%s: floppy eject command failed\n" ), programName);
  1308.         }
  1309.     }
  1310. #endif
  1311.  
  1312. #ifdef HAVE_EJECT_TAPE
  1313.     if (q_option && !worked) {
  1314.         if (v_option)
  1315.             printf(_("%s: trying to eject `%s' using tape offline command\n" ), programName, deviceName);
  1316.         worked = EjectTape(fd);
  1317.         if (v_option) {
  1318.             if (worked)
  1319.                 printf(_("%s: tape offline command succeeded\n" ), programName);
  1320.             else
  1321.                 printf(_("%s: tape offline command failed\n" ), programName);
  1322.         }
  1323.     }
  1324. #endif
  1325.  
  1326.     if (!worked) {
  1327.         fprintf(stderr, _("%s: unable to eject, last error: %s\n" ), programName, strerror(errno));
  1328.         exit(1);
  1329.     }
  1330.  
  1331.     /* cleanup */
  1332.     CLOSE(fd);
  1333.     free(device);
  1334.     free(deviceName);
  1335.     free(fullName);
  1336.     free(fullNameOrig);
  1337.     free(linkName);
  1338.     free(mountName);
  1339.     free(pattern);
  1340.     exit(0);
  1341. }


---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 20-12-2006 à 21:16:32    

0x90 a écrit :

Voilà [:dawa]

Code :
  1. "  -f\t-- eject floppy\n"
  2. "  -q\t-- eject tape\n"



[:roi]


---------------
Stick a parrot in a Call of Duty lobby, and you're gonna get a racist parrot. — Cody
Reply

Marsh Posté le 22-12-2006 à 20:40:04    

Bonsoir,
Sinon, sous windows, faut regarder du coté de la bibliotheque multimedia MCI dans la msdn.
un lien pour t'aider:
http://msdn.microsoft.com/library/ [...] 32_set.asp

Reply

Marsh Posté le 22-12-2006 à 20:53:22    

breizhbugs a écrit :

Bonsoir,
Sinon, sous windows, faut regarder du coté de la bibliotheque multimedia MCI dans la msdn.
un lien pour t'aider:
http://msdn.microsoft.com/library/ [...] 32_set.asp


J'ai proposé ça un jour, je me suis fait traité de ringard, je ne dis plus rien.
 


---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
Reply

Marsh Posté le 22-12-2006 à 21:05:24    

breizhbugs a écrit :

Bonsoir,
Sinon, sous windows, faut regarder du coté de la bibliotheque multimedia MCI dans la msdn.
un lien pour t'aider:
http://msdn.microsoft.com/library/ [...] 32_set.asp


 
ringard !

Reply

Marsh Posté le 22-12-2006 à 21:05:24   

Reply

Marsh Posté le 22-12-2006 à 23:28:30    

C'est vrai que j'ai jamais trop compris pourquoi faut utiliser une bibliotheque multimedia pour ouvrir la trappe.
Sinon je suppose qu'il faut utiliser DeviceIOtrucmachinchose...
 
@tamahome: c'est ringard de critiquer sans proposer mieux!

Reply

Marsh Posté le 23-12-2006 à 10:52:57    

c t pour -hs- ;o)

Reply

Marsh Posté le 23-12-2006 à 13:53:41    

http://www.codeproject.com/system/eject_cdrom.asp


---------------
What if I were smiling and running into your arms? Would you see then what I see now?  
Reply

Marsh Posté le 23-12-2006 à 14:30:10    


MCI...
Moi, ça me va, tant pis si c'est ringard, du moment que ça marche...
 


---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
Reply

Marsh Posté le 23-12-2006 à 15:01:01    

moi aussi je suis ringard je sais


---------------
What if I were smiling and running into your arms? Would you see then what I see now?  
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed