-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathutility.cc
976 lines (848 loc) · 20.9 KB
/
utility.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
* Copyright ViewTouch, Inc., 1995, 1996, 1997, 1998
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* utility.cc - revision 124 (10/6/98)
* Implementation of utility module
*/
#include "manager.hh"
#include "utility.hh"
#include <unistd.h>
#include <ctime>
#include <errno.h>
#include <string>
#include <cstring>
#include <cctype>
#include <iostream>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm> // std::replace
#ifdef DMALLOC
#include <dmalloc.h>
#endif
char* progname = NULL;
int progname_maxlen = 0;
void vt_init_setproctitle(int argc, char* argv[])
{
#ifdef BSD
progname = NULL;
progname_maxlen = 0;
#else
// This is mostly for Linux, maybe others.
// clear out the arguments (1..N) so we get a nice process list
int idx = 1;
int idx2 = 0;
while (idx < argc)
{
idx2 = 0;
while (argv[idx][idx2] != '\0')
{
argv[idx][idx2] = '\0';
idx2 += 1;
}
idx += 1;
}
progname = argv[0];
progname_maxlen = strlen(progname) - 1;
#endif
}
/****
* vt_setproctitle: Returns 1 on error, 0 otherwise.
****/
int vt_setproctitle(const char* title)
{
FnTrace("setproctitle()");
int retval = 1;
#ifdef BSD
setproctitle("%s", title);
retval = 0;
#else
if (progname != NULL)
{
// This isn't intended to set an arbitrary length title. It's
// intended to set a short title after a long title (AKA,
// right now, vt_main starts off with a title set by the OS of
// "/usr/viewtouch/bin/vt_main"; I change that to "vt_main
// pri" or "vt_main dns"). If you need to set a title longer
// than the starting title, you'll need to rewrite this (save
// off the environment, etc.).
strncpy(progname, title, progname_maxlen);
progname[progname_maxlen] = '\0';
retval = 0;
}
#endif
return retval;
}
/**** Str Class ****/
// Constructors
//temp for testing
//int Str::nAllocated = 0;
Str::Str()
{}
Str::Str(const std::string &str)
{
data = str;
}
Str::Str(const Str &s)
{
data = s.Value();
}
// Destructor
Str::~Str()
{}
// Member Functions
int Str::Clear()
{
FnTrace("Str::Clear()");
data.clear();
return 0;
}
bool Str::Set(const char *str)
{
FnTrace("Str::Set(const char *)");
data = str;
return true;
}
bool Str::Set(const std::string &str)
{
FnTrace("Str::Set(const std::string &)");
data = str;
return true;
}
bool Str::Set(const int val)
{
FnTrace("Str::Set(int)");
data = std::to_string(val);
return true;
}
bool Str::Set(const Flt val)
{
FnTrace("Str::(Flt)");
data = std::to_string(val);
return true;
}
void Str::ChangeAtoB(const char a, const char b)
{
FnTrace("Str::ChangeAtoB()");
std::replace(data.begin(), data.end(), a, b);
}
int Str::IntValue() const
{
FnTrace("Str::IntValue()");
if (!data.empty())
return std::stoi(data);
else
return 0;
}
Flt Str::FltValue() const
{
FnTrace("Str::FltValue()");
if (!data.empty())
return std::stod(data);
else
return 0.0;
}
const char* Str::Value() const
{
FnTrace("Str::Value()");
return data.c_str();
}
const char* Str::c_str() const
{
FnTrace("Str::c_str()");
return data.c_str();
}
std::string Str::str() const
{
FnTrace("Str::Value()");
return data;
}
/****
* ValueSet: Always return the new value of the variable, but
* only set it if set is non-NULL.
****/
const char* Str::ValueSet(const char* set)
{
FnTrace("Str::ValueSet()");
if (set)
Set(set);
return Value();
}
bool Str::empty() const
{
return data.empty();
}
size_t Str::size() const
{
return data.size();
}
int Str::operator > (const Str &s) const
{
FnTrace("Str::operator >()");
return this->data > s.data;
}
int Str::operator < (const Str &s) const
{
FnTrace("Str::operator <()");
return this->data < s.data;
}
int Str::operator == (const Str &s) const
{
FnTrace("Str::opterator ==()");
return this->data == s.data;
}
bool Str::operator ==(const std::string &s) const
{
FnTrace("Str::opterator ==()");
return this->data == s;
}
int Str::operator != (const Str &s) const
{
FnTrace("Str::operator !=()");
return this->data != s.data;
}
/**** Region Class ****/
// Constructors
RegionInfo::RegionInfo()
{
x = 0;
y = 0;
w = 0;
h = 0;
}
RegionInfo::RegionInfo(RegionInfo &r)
{
x = r.x;
y = r.y;
w = r.w;
h = r.h;
}
RegionInfo::RegionInfo(RegionInfo *r)
{
x = r->x;
y = r->y;
w = r->w;
h = r->h;
}
RegionInfo::RegionInfo(int rx, int ry, int rw, int rh)
{
x = rx;
y = ry;
w = rw;
h = rh;
}
//Destructor
RegionInfo::~RegionInfo()
{
}
// Member Functions
int RegionInfo::Fit(int rx, int ry, int rw, int rh)
{
FnTrace("RegionInfo::Fit()");
if (w == 0 && h == 0)
return SetRegion(rx, ry, rw, rh);
int x2 = x + w;
int y2 = y + h;
int rx2 = rx + rw;
int ry2 = ry + rh;
if (rx2 > x2) x2 = rx2;
if (ry2 > y2) y2 = ry2;
if (rx < x) x = rx;
if (ry < y) y = ry;
w = x2 - x;
h = y2 - y;
return 0;
}
int RegionInfo::Intersect(int rx, int ry, int rw, int rh)
{
FnTrace("RegionInfo::Intersect()");
int x2 = x + w;
int y2 = y + h;
int rx2 = rx + rw;
int ry2 = ry + rh;
if (rx2 < x2) x2 = rx2;
if (ry2 < y2) y2 = ry2;
if (rx > x) x = rx;
if (ry > y) y = ry;
w = x2 - x;
h = y2 - y;
return 0;
}
/**** Price Class ****/
// Constructor
Price::Price(int price_amount, int price_type)
{
}
// Member Functions
int Price::Read(InputDataFile &df, int version)
{
return 1;
}
int Price::Write(OutputDataFile &df, int version)
{
return 1;
}
const char* Price::Format(int sign)
{
return NULL;
}
const char* Price::Format(const char* buffer, int sign)
{
return NULL;
}
const char* Price::SimpleFormat()
{
return NULL;
}
const char* Price::SimpleFormat(const char* buffer)
{
return NULL;
}
/**** Other Functions ****/
std::string StringToLower(const std::string &str)
{
FnTrace("StringToLower()");
std::string data = str;
for (char &c : data)
{
c = std::tolower(c);
}
return data;
}
std::string StringToUpper(const std::string &str)
{
FnTrace("StringToUpper()");
std::string data = str;
for (char &c : data)
{
c = std::toupper(c);
}
return data;
}
/****
* StripWhiteSpace: remove all whitespace characters from the beginning
* and the end of string.
****/
int StripWhiteSpace(genericChar* longstr)
{
FnTrace("StripWhiteSpace()");
int idx = 0;
int storeidx = 0; // where to move characters to
int len = strlen(longstr);
int count = 0;
// strip from the beginning
while (isspace(longstr[idx]))
idx += 1;
count += 0;
while (idx > 0 && idx < len)
{
longstr[storeidx] = longstr[idx];
storeidx += 1;
idx += 1;
}
if (storeidx > 0)
longstr[storeidx] = '\0';
// strip from the end
len = strlen(longstr) - 1;
while (len > 0 && isspace(longstr[len]))
{
longstr[len] = '\0';
len -= 1;
count += 1;
}
return count;
}
std::string AdjustCase(const std::string &str)
{
FnTrace("AdjustCase()");
std::string data = str;
bool capital = true;
for (char &c : data)
{
if (isspace(c) || ispunct(c))
{
capital = true; // next character should be upper case
} else if (capital)
{
c = std::toupper(c);
capital = false; // next character should be lower case
} else
{
c = std::tolower(c);
}
}
return data;
}
std::string StringAdjustSpacing(const std::string &str)
{
FnTrace("StringAdjustSpacing()");
std::string data;
data.reserve(str.size());
bool space = true; // flag to shorten spaces to one digit
for (const char &s : str)
{
if (std::isspace(s))
{
if (!space)
{
space = true; // add only first space
data.push_back(' ');
}
}
else if (std::isprint(s))
{
data.push_back(s);
space = false;
}
}
if (space && !data.empty())
{
data.pop_back(); // remove trailing space
}
return data;
}
std::string AdjustCaseAndSpacing(const std::string &str)
{
FnTrace("AdjustCaseAndSpacing()");
return AdjustCase(StringAdjustSpacing(str));
}
const genericChar* NextName(const genericChar* name, const genericChar* *list)
{
FnTrace("NextName()");
int idx = 0;
// find the current string
while (list[idx] != NULL && (strcmp(name, list[idx]) != 0))
idx += 1;
// advance to next
if (list[idx] != NULL)
idx += 1;
// wrap to beginning if necessary
if (list[idx] == NULL)
idx = 0;
return list[idx];
}
int NextValue(int val, int *val_array)
{
FnTrace("NextValue()");
int idx = CompareList(val, val_array);
++idx;
if (val_array[idx] < 0)
idx = 0;
return val_array[idx];
}
int ForeValue(int val, int *val_array)
{
FnTrace("ForeValue()");
int idx = CompareList(val, val_array);
--idx;
if (idx < 0)
{
idx = 0;
while (val_array[idx] >= 0)
++idx;
}
return val_array[idx];
}
/****
* NextToken: similar to strtok_r, except that it returns success or
* failure and the destination string is passed as an argument.
* sep is allowed to change between calls. The function never backtracks,
* so the next token, whether the same or different, will always be
* found (or not found) after the previous token. idx must point to a
* storage space for the index. src is NOT modified by NextToken.
****/
int NextToken(genericChar* dest, const genericChar* src, genericChar sep, int *idx)
{
FnTrace("NextToken()");
int destidx = 0;
int retval = 0;
if (src[*idx] != '\0')
{
while (src[*idx] != '\0' && src[*idx] != sep)
{
dest[destidx] = src[*idx];
destidx += 1;
*idx += 1;
}
dest[destidx] = '\0';
// gobble up the token and any extra tokens
while (src[*idx] == sep && src[*idx] != '\0')
*idx += 1;
retval = 1;
}
return retval;
}
/****
* NextInteger: Like NextToken, but converts the resulting string to
* an integer and stores that value in dest.
****/
int NextInteger(int *dest, const genericChar* src, genericChar sep, int *idx)
{
FnTrace("NextInteger()");
genericChar buffer[STRLONG];
int retval = 0;
if (NextToken(buffer, src, sep, idx))
{
*dest = atoi(buffer);
retval = 1;
}
return retval;
}
int BackupFile(const genericChar* filename)
{
FnTrace("BackupFile()");
if (DoesFileExist(filename) == 0)
return 1; // No file to backup
genericChar bak[256];
sprintf(bak, "%s.bak", filename);
if (DoesFileExist(bak))
{
genericChar bak2[256];
sprintf(bak2, "%s.bak2", filename);
// delete *.bak2
unlink(bak2);
// move *.bak to *.bak2
link(bak, bak2);
unlink(bak);
}
// move * to *.bak
link(filename, bak);
unlink(filename);
return 0;
}
int RestoreBackup(const genericChar* filename)
{
FnTrace("RestoreBackup()");
genericChar str[256];
sprintf(str, "%s.bak", filename);
if (DoesFileExist(str) == 0)
return 1; // No backup to restore
sprintf(str, "/bin/cp %s.bak %s", filename, filename);
return system(str);
}
int FltToPrice(Flt value)
{
FnTrace("FltToPrice()");
if (value >= 0.0)
return (int) ((value * 100.0) + .5);
else
return (int) ((value * 100.0) - .5);
}
Flt PriceToFlt(int price)
{
FnTrace("PriceToFlt()");
return (Flt) ((Flt) price / 100.0);
}
int FltToPercent(Flt value)
{
FnTrace("FltToPercent()");
if (value >= 0.0)
return (int) ((value * 10000.0) + .5);
else
return (int) ((value * 10000.0) - .5);
}
Flt PercentToFlt(int percent)
{
FnTrace("PercentToFlt()");
return (Flt) percent / 10000.0;
}
const char* FindStringByValue(int val, int val_list[], const genericChar* str_list[], const genericChar* unknown)
{
FnTrace("FindStringByValue()");
for (int i = 0; str_list[i] != NULL; ++i)
{
if (val == val_list[i])
return str_list[i];
}
return unknown;
}
int FindValueByString(const genericChar* val, int val_list[], const genericChar* str_list[], int unknown)
{
FnTrace("FindValueByString()");
int retval = unknown;
int idx = 0;
while (val_list[idx] >= 0 && retval == unknown)
{
if (strcmp(val, str_list[idx]) == 0)
retval = val_list[idx];
idx += 1;
}
return retval;
}
/****
* FindIndexOfValue: The Name[] and Value[] arrays in labels.cc are not necessarily
* in order. For example, compare the FAMILY_ constants in sales.hh to the
* FamilyName[] and FamilyValue[] arrays. This function compares value to the
* elements of FamilyValue and returns the index of the match, or -1 if no
* match was found.
****/
//FIX BAK-->This function and FindStringByValue should not be necessary.
//FIX Should reorder the Name[] and Value[] arrays to coincide with the
//FIX constants wherever possible.
int FindIndexOfValue(int value, int val_list[], int unknown)
{
FnTrace("FindIndexOfValue()");
int retval = unknown;
int idx = 0;
int fvalue = val_list[idx];
while (fvalue >= 0)
{
if (value == fvalue)
{
retval = idx;
fvalue = -1; // exit the loop
}
else
{
idx += 1;
fvalue = val_list[idx];
}
}
return retval;
}
int DoesFileExist(const genericChar* filename)
{
FnTrace("DoesFileExist()");
if (filename == NULL)
return 0;
int status = access(filename, F_OK);
return (status == 0); // Return true if file exists
}
/****
* EnsureFileExists: Creates the file or directory if it does not
* already exist. Returns 0 if the file exists or if it is successfully
* created. Returns 1 on error.
****/
int EnsureFileExists(const genericChar* filename)
{
FnTrace("EnsureFileExists()");
int retval = 1;
if (filename == NULL)
return retval;
if (DoesFileExist(filename) == 0)
{
if (mkdir(filename, 0) == 0)
{
if (chmod(filename, DIR_PERMISSIONS) == 0)
retval = 0;
}
}
else
retval = 0;
return retval;
}
int DeleteFile(const genericChar* filename)
{
FnTrace("DeleteFile()");
if (filename == NULL || strlen(filename) <= 0 || access(filename, F_OK))
return 1;
unlink(filename);
return 0;
}
int StringCompare(const std::string &str1, const std::string &str2, int len)
{
FnTrace("StringCompare()");
if (len < 0)
{
// compare full string
const std::string str1_lower = StringToLower(str1);
const std::string str2_lower = StringToLower(str2);
return str1_lower.compare(str2_lower);
}
// compare up to len
const std::string str1_lower = StringToLower(str1.substr(0, static_cast<size_t>(len)));
const std::string str2_lower = StringToLower(str2.substr(0, static_cast<size_t>(len)));
return str1_lower.compare(str2_lower);
}
/****
* StringInString: We want to find needle inside haystack, case-insensitive.
* Returns 1 if the string is found, 0 otherwise.
* NOTE: this function does not return the position of needle in
* haystack, just whether or not it is there.
****/
int StringInString(const genericChar* haystack, const genericChar* needle)
{
const genericChar* c1 = (const genericChar* )haystack;
const genericChar* l1 = (const genericChar* )haystack;
const genericChar* c2 = (const genericChar* )needle;
int match = 0;
while (*c1 && *c2)
{
if (tolower(*c1) == tolower(*c2))
{
if (match == 0)
l1 = c1;
c1++;
c2++;
match = 1;
}
else
{
if (match)
{
c2 = (const genericChar* )needle;
c1 = l1;
}
match = 0;
c1++;
}
}
if (*c2 == '\0')
return 1;
else
return 0;
}
// FIX - convert the following three functions into a single template if possible
int CompareList(const genericChar* str, const genericChar* list[], int unknown)
{
FnTrace("CompareList(char)");
for (int i = 0; list[i] != NULL; ++i)
{
if (StringCompare(str, list[i]) == 0)
return i;
}
return unknown;
}
int CompareList(int val, int list[], int unknown)
{
FnTrace("CompareList(int)");
for (int i = 0; list[i] >= 0; ++i)
{
if (val == list[i])
return i;
}
return unknown;
}
/****
* HasSpace: returns 1 if word contains a space character,
* 0 otherwise.
****/
int HasSpace(const genericChar* word)
{
FnTrace("HasSpace()");
int len = strlen(word);
int idx;
int has_space = 0;
for (idx = 0; idx < len; idx++)
{
if (isspace(word[idx]))
{
has_space = 1;
idx = len;
}
}
return has_space;
}
/****
* CompareListN: search for str in each element of list (similar to
* strncmp). Returns the list[] index of the first match, or
* the unknown parameter if no match is found.
* NOTE: There is the problem of the list having both "next"
* and "nextsearch", so that next always matches. The assumption
* made is that if we want a truncated search ("nextsearch" matches
* "nextsearch WORD") there will always be a space following the
* keyword. If there is no space, the two words must match
* exactly, in length as well as content.
*
* if word has a space
* search for item within word
* else if word and item are equal length
* search for exact match
* if match
* return index within list (not within string)
****/
int CompareListN(const genericChar* list[], const genericChar* word, int unknown)
{
FnTrace("CompareListN()");
int wordlen = strlen(word);
int idx = 0;
int itemlen;
int search = -1;
while (list[idx] != NULL)
{
search = -1;
itemlen = strlen(list[idx]);
if (list[idx][itemlen - 1] == ' ')
search = StringCompare(word, list[idx], itemlen);
else if (itemlen == wordlen)
search = StringCompare(word, list[idx]);
if (search == 0)
return idx;
idx += 1;
}
return -1;
}
/*********************************************************************
* I want to control access to the /dev files, but apparently flock
* can't touch them. I also want to do locking uniformly and
* transparently. So we create two functions here. LockDevice will
* return a positive ID on success, a negative number on failure.
* The returned ID must be passed to UnlockDevice to remove the lock.
********************************************************************/
#define LOCK_DIR VIEWTOUCH_PATH "/bin/.lock"
int LockDevice(const genericChar* devpath)
{
FnTrace("LockDevice()");
int retval = 0;
genericChar lockpath[STRLONG];
genericChar buffer[STRLONG];
int idx = 0;
struct stat sb;
// make sure the lock directory exists
if (stat(LOCK_DIR, &sb))
{
if (mkdir(LOCK_DIR, 0755))
perror("Cannot create lock directory");
chmod(LOCK_DIR, 0755);
}
// convert the file name from /dev/lpt0 to .dev.lpt0
strcpy(buffer, devpath);
while (buffer[idx] != '\0')
{
if (buffer[idx] == '/')
buffer[idx] = '.';
idx += 1;
}
snprintf(lockpath, STRLONG, "%s/%s", LOCK_DIR, buffer);
retval = open(lockpath, O_WRONLY | O_CREAT, 0755);
if (retval > 0)
{
if (flock(retval, LOCK_EX))
{
close(retval);
retval = -1;
}
}
else if (retval < 0)
{
printf("LockDevice error %d for %s", errno, devpath);
}
return retval;
}
int UnlockDevice(int id)
{
FnTrace("UnlockDevice()");
int retval = 1;
if (id > 0)
{
if (flock(id, LOCK_UN) == 0)
{
if (close(id) == 0)
retval = 0;
// Should the file be deleted?
}
}
return retval;
}