123 lines
2.9 KiB
C
123 lines
2.9 KiB
C
/* ****************************************************************************
|
|
* comlist/builtin/macos.c -- find out MacOS/OS X serial devices.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libcasio.
|
|
* libcasio is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libcasio 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 Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************* */
|
|
#include "../builtin.h"
|
|
#ifndef LIBCASIO_DISABLED_MAC_SERIAL
|
|
# include <dirent.h>
|
|
|
|
typedef struct {
|
|
char *path;
|
|
DIR *dp;
|
|
char *start;
|
|
} icookie_t;
|
|
|
|
/**
|
|
* next_serial_port:
|
|
* Get the next serial port.
|
|
*
|
|
* @arg cookie the iCookie.
|
|
* @arg ptr the pointer to the next path.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
CASIO_LOCAL int CASIO_EXPORT next_serial_port(icookie_t *cookie,
|
|
char const **ptr)
|
|
{
|
|
struct dirent *dr;
|
|
|
|
while (1) {
|
|
/* Get the next entry. */
|
|
|
|
if (!(dr = readdir(cookie->dp)))
|
|
return (casio_error_iter);
|
|
|
|
/* Check the name. */
|
|
|
|
if (strncmp(dr->d_name, "cu.", 3))
|
|
continue;
|
|
|
|
/* We have found a serial port!
|
|
* Just copy it into our buffer and return it. */
|
|
|
|
strcpy(cookie->start, dr->d_name);
|
|
break;
|
|
}
|
|
|
|
*ptr = cookie->path;
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* end_iter:
|
|
* End the iterator by freeing all related allocated resources.
|
|
*
|
|
* @arg cookie the iCookie.
|
|
*/
|
|
|
|
CASIO_LOCAL void CASIO_EXPORT end_iter(icookie_t *cookie)
|
|
{
|
|
closedir(d);
|
|
casio_free(cookie);
|
|
}
|
|
|
|
/* Functions. */
|
|
|
|
CASIO_LOCAL casio_iter_funcs_t mac_serial_functions = {
|
|
(casio_next_t *)&next_serial_port,
|
|
NULL,
|
|
(casio_end_t *)&end_iter
|
|
};
|
|
|
|
/**
|
|
* casio_iter_mac_serial:
|
|
* Iterate on serial devices under MacOS/OS X.
|
|
*
|
|
* @arg iterp the iterator to create.
|
|
* @return the error.
|
|
*/
|
|
|
|
int CASIO_EXPORT casio_iter_mac_serial(casio_iter_t **iterp)
|
|
{
|
|
icookie_t *cookie;
|
|
|
|
/* Create the cookie. */
|
|
|
|
if (!(cookie = casio_alloc(1, sizeof(icookie_t) + PATH_MAX + 1)))
|
|
return (casio_error_alloc);
|
|
|
|
cookie->path = (char *)(void *)&cookie[1];
|
|
|
|
/* Prepare the path and open the directory stream. */
|
|
|
|
strcpy(cookie->path, "/dev/");
|
|
cookie->start = strchr(cookie->path, '\0');
|
|
|
|
if (!(cookie->dp = opendir(cookie->path))) {
|
|
casio_free(cookie);
|
|
|
|
/* TODO: check errno */
|
|
return (casio_error_unknown);
|
|
}
|
|
|
|
/* Create the iterator. */
|
|
|
|
return (casio_iter(iterp, cookie, mac_serial_functions));
|
|
}
|
|
|
|
#endif /* LIBCASIO_DISABLED_MAC_SERIAL */
|