--- /dev/null
+/*
+
+ G N O K I I
+
+ A Linux/Unix toolset and driver for Nokia mobile phones.
+
+ Released under the terms of the GNU GPL, see file COPYING for more details.
+
+*/
+
+#include <string.h>\r
+#include <ctype.h>\r
+#include <time.h>\r
+
+#ifndef WIN32
+ #include <sys/types.h>
+ #include <sys/stat.h>
+ #include <stdlib.h>
+ #include <fcntl.h>
+ #include <signal.h>
+ #include <unistd.h>
+ #include <errno.h>
+#endif\r
+\r
+#include "misc.h"\r
+#include "gsm-common.h"\r
+
+#ifndef HAVE_TIMEOPS
+
+/* FIXME: I have timersub defined in sys/time.h :-( PJ
+ FIXME: Jano wants this function too... PJ
+
+int timersub(struct timeval *a, struct timeval *b, struct timeval *result) {
+ do {
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
+ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;
+ if ((result)->tv_usec < 0) {
+ --(result)->tv_sec;
+ (result)->tv_usec += 1000000;
+ }
+ } while (0);
+}
+*/
+
+#endif
+
+int GetLine(FILE *File, char *Line, int count) {
+
+ char *ptr;
+
+ if (fgets(Line, count, File)) {
+ ptr=Line+strlen(Line)-1;
+
+ while ( (*ptr == '\n' || *ptr == '\r') && ptr>=Line) *ptr--='\0';
+
+ return strlen(Line);
+ } else return -1;
+}
+
+/*
+ * like atoi, but of a non-null-terminated string of a specified portion
+ */
+int mem_to_int(const char str[], int len)
+{
+ char aux[81];
+
+ strncpy(aux, str, len);
+ aux[len]=0;
+ return( atoi(aux) );
+}
+
+/*
+ * make hexdump of Message
+ */
+#ifdef DEBUG
+void hexdump(u16 MessageLength, u8 *MessageBuffer)
+{
+
+ int count;
+ int n=0;
+ char string1[80]="";
+ char string2[80]="";
+ char hex1[10];
+ char hex2[10];
+
+ for (count = 0; count < MessageLength; count ++)
+ {
+ n++;
+
+ switch (MessageBuffer[count]) {
+ case 0x09:
+ sprintf(hex1,"%02x ",MessageBuffer[count]);
+ strcpy(hex2,".");
+ break;
+ default:
+ if (isprint(MessageBuffer[count]))
+ sprintf(hex1,"%02x%c ",MessageBuffer[count],MessageBuffer[count]);
+ else
+ sprintf(hex1,"%02x ",MessageBuffer[count]);
+
+ if (isprint(MessageBuffer[count])) sprintf(hex2,"%c",MessageBuffer[count]);
+ else strcpy(hex2,".");
+ break;
+ }
+
+ if ( n!=15 && count != MessageLength-1 ) hex1[3]='|';
+
+ strcat(string1,hex1);
+ strcat(string2,hex2);
+
+ if ( n==15 || count == MessageLength-1 )
+ {
+ fprintf(stdout,"%-60s%03x %s\n",string1,count+1,string2);
+ strcpy(string1,"");
+ strcpy(string2,"");
+ n=0;
+ }
+ }//for count
+
+ if (n!=0) fprintf (stdout,_("\n"));
+
+ fflush(stdout);
+}
+
+void txhexdump(u16 MessageLength, u8 *MessageBuffer)
+{
+ int count;
+ int n=0;
+
+ for (count = 0; count < MessageLength; count ++)
+ {
+ n++;
+ fprintf(stdout,_("%02x"),MessageBuffer[count]);
+ switch (MessageBuffer[count]) {
+ case 0x09:
+ fprintf(stdout,_(" |"));
+ break;
+ default:
+ if (isprint(MessageBuffer[count])) fprintf(stdout, _("%c|"),MessageBuffer[count]);
+ else fprintf(stdout,_(" |"));
+ break;
+ }
+
+ if (n==18)
+ {
+ fprintf (stdout,_("\n"));
+ n=0;
+ }
+ }//for count
+
+ if (n!=0) fprintf (stdout,_("\n"));
+
+ fflush(stdout);
+}
+#endif
+
+#ifndef WIN32
+
+#define max_buf_len 128
+#define lock_path "/var/lock/LCK.."
+
+/* Lock the device. Return allocated string with a lock name */
+char *lock_device(const char* port)
+{
+ char *lock_file = NULL;
+ char buffer[max_buf_len];
+ char *aux = rindex(port, '/');
+ int fd, len = strlen(aux) + strlen(lock_path);
+
+ memset(buffer, 0, sizeof(buffer));
+ lock_file = calloc(len + 1, 1);
+ if (!lock_file) {
+ fprintf(stderr, _("Cannot lock device\n"));
+ return NULL;
+ }
+ /* I think we don't need to use strncpy, as we should have enough
+ * buffer due to strlen results
+ */
+ strcpy(lock_file, lock_path);
+ strcat(lock_file, aux);
+
+ /* Check for the stale lockfile.
+ * The code taken from minicom by Miquel van Smoorenburg */
+ if ((fd = open(lock_file, O_RDONLY)) >= 0) {
+ char buf[max_buf_len];
+ int pid, n = 0;
+
+ n = read(fd, buf, sizeof(buf) - 1);
+ close(fd);
+ if (n > 0) {
+ pid = -1;
+ if (n == 4)
+ /* Kermit-style lockfile. */
+ pid = *(int *)buf;
+ else {
+ /* Ascii lockfile. */
+ buf[n] = 0;
+ sscanf(buf, "%d", &pid);
+ }
+ if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
+ fprintf(stderr, _("Lockfile is stale. Overriding it..\n"));
+ sleep(1);
+ unlink(lock_file);
+ } else
+ n = 0;
+ }
+ if (n == 0) {
+ free(lock_file);
+ fprintf(stderr, _("Device is already locked.\n"));
+ return NULL;
+ }
+ }
+
+ /* Try to create a new file, with 0644 mode */
+ fd = open(lock_file, O_CREAT | O_EXCL, 0644);
+ if (fd == -1) {
+ free(lock_file);
+ fprintf(stderr, _("Cannot lock device\n"));
+ return NULL;
+ }
+ sprintf(buffer, "%10ld gnokii\n", (long)getpid());
+ write(fd, buffer, strlen(buffer));
+ close(fd);
+ return lock_file;
+}
+
+/* Removes lock and frees memory */
+bool unlock_device(char *lock_file)
+{
+ int err;
+
+ if (!lock_file) {
+ fprintf(stderr, _("Cannot unlock device\n"));
+ return false;
+ }
+ err = unlink(lock_file);
+ free(lock_file);
+ return (err + 1);
+}
+#endif /* WIN32 */