/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2007
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    Midnight Code is a registered trademark of Ian Latter.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* midnightcode-pcmcia.c :: Midnight Code PCMCIA/Cardbus library	*/


#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include "preferences.h"
#include "midnightcode-pcmcia.h"


/* TODO: Replace with ioctl equivalent */
int
midnightcode_pcmcia_exists(void) {
  FILE *fp;
  char devices[32];
  char device_label[32];
  int device_major;


  if(access(MIDNIGHTCODE_PCMCIA_EXISTS_SOURCE, F_OK) != 0)
    return -1;

  if((fp = fopen(MIDNIGHTCODE_PCMCIA_EXISTS_SOURCE, "r")) == NULL)
    return -1;

  while(fgets(devices, 32, fp) != NULL) {
    if(sscanf(devices, "%d %s", &device_major, device_label) == 2) {
      if(strcmp("pcmcia", device_label) == 0) {
        fclose(fp);
        return device_major;
      }
    }
  }
  fclose(fp);


  return 0;
}

