Subversion Repositories SvarDOS

Rev

Rev 245 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 245 Rev 249
Line 1... Line 1...
1
/*
1
/*
2
 * This file is part of FDNPKG
2
 * This file is part of pkginst
3
 * Copyright (C) 2013-2016 Mateusz Viste, All rights reserved.
3
 * Copyright (C) 2013-2021 Mateusz Viste, All rights reserved.
4
 */
4
 */
5
 
5
 
6
#include <stdio.h>   /* fopen, fclose... */
6
#include <stdio.h>   /* fopen, fclose... */
7
#include <string.h>  /* strcasecmp() */
7
#include <string.h>  /* strcasecmp() */
8
 
8
 
9
#include "lsm.h"     /* include self for control */
-
 
10
#include "rtrim.h"
-
 
11
#include "version.h"
9
#include "helpers.h"
12
 
10
 
13
/* reads a line from a file descriptor, and writes it to *line, the *line array is filled no more than maxlen bytes. returns the number of byte read on success, or a negative value on failure (reaching EOF is considered an error condition) */
-
 
14
static int readline_fromfile(FILE *fd, char *line, int maxlen) {
-
 
15
  int bytebuff, linelen = 0;
-
 
16
  for (;;) {
-
 
17
    bytebuff = fgetc(fd);
-
 
18
    if (bytebuff == EOF) {
-
 
19
      line[linelen] = 0;
-
 
20
      if (linelen == 0) return(-1);
-
 
21
      return(linelen);
-
 
22
    }
-
 
23
    if (bytebuff < 0) return(-1);
-
 
24
    if (bytebuff == '\r') continue; /* ignore CR */
11
#include "lsm.h"     /* include self for control */
25
    if (bytebuff == '\n') {
-
 
26
      line[linelen] = 0;
-
 
27
      return(linelen);
-
 
28
    }
-
 
29
    if (linelen < maxlen) line[linelen++] = bytebuff;
-
 
30
  }
-
 
31
}
-
 
32
 
12
 
33
 
13
 
34
/* Loads metadata from an LSM file. Returns 0 on success, non-zero on error. */
14
/* Loads metadata from an LSM file. Returns 0 on success, non-zero on error. */
35
int readlsm(const char *filename, char *version, int version_maxlen) {
15
int readlsm(const char *filename, char *version, size_t version_maxlen) {
36
  char linebuff[128];
16
  char linebuff[128];
37
  char *valuestr;
17
  char *valuestr;
38
  int x;
-
 
39
  FILE *fd;
18
  FILE *fd;
40
  /* reset fields to be read to empty values */
19
  /* reset fields to be read to empty values */
41
  version[0] = 0;
20
  version[0] = 0;
42
  /* open the file */
21
  /* open the file */
43
  fd = fopen(filename, "rb");
22
  fd = fopen(filename, "rb");
44
  if (fd == NULL) return(-1);
23
  if (fd == NULL) return(-1);
45
  /* read the LSM file line by line */
24
  /* read the LSM file line by line */
-
 
25
 
46
  while (readline_fromfile(fd, linebuff, sizeof(linebuff) - 1) >= 0) {
26
  while (freadtokval(fd, linebuff, sizeof(linebuff), &valuestr, ':') == 0) {
47
    for (x = 0;; x++) {
-
 
48
      if (linebuff[x] == 0) {
27
    if (valuestr != NULL) {
49
        x = -1;
-
 
50
        break;
-
 
51
      } else if (linebuff[x] == ':') {
-
 
52
        break;
-
 
53
      }
-
 
54
    }
-
 
55
    if (x > 0) {
-
 
56
      linebuff[x] = 0;
-
 
57
      valuestr = linebuff + x + 1;
-
 
58
      trim(linebuff);
-
 
59
      trim(valuestr);
-
 
60
      if (strcasecmp(linebuff, "version") == 0) snprintf(version, version_maxlen, "%s", valuestr);
28
      if (strcasecmp(linebuff, "version") == 0) snprintf(version, version_maxlen, "%s", valuestr);
61
    }
29
    }
62
  }
30
  }
63
  fclose(fd);
31
  fclose(fd);
64
  return(0);
32
  return(0);