Subversion Repositories SvarDOS

Rev

Rev 225 | Rev 236 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
2
 * FDINST - lightweigth FreeDOS package installer
3
 * Copyright (C) 2015-2017 Mateusz Viste
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * IN THE SOFTWARE.
22
 */
23
 
24
 
25
#include <stdio.h>    /* printf() */
26
#include <stdlib.h>   /* malloc() and friends */
27
#include <string.h>   /* strcasecmp() */
28
 
29
#include "libunzip.h"
30
#include "pkginst.h"
31
#include "pkgrem.h"
32
#include "readenv.h"
33
#include "version.h"
34
 
35
 
36
enum ACTIONTYPES {
37
  ACTION_INSTALL,
38
  ACTION_REMOVE,
39
  ACTION_HELP
40
};
41
 
42
 
43
static int showhelp(void) {
44
  printf("FDINST v" PVER " Copyright (C) " PDATE " Mateusz Viste\n"
45
         "\n"
46
         "FDINST is a lightweigth package installer for FreeDOS. It is an alternative\n"
47
         "to FDNPKG, when only basic, local install/remove actions are necessary. FDINST\n"
48
         "is a 16-bit, 8086-compatible application running in real mode.\n"
49
         "\n"
50
         "Usage: FDINST install package.zip\n"
51
         "       FDINST remove package\n"
52
         "\n"
53
         "FDINST is published under the MIT license, and shares most of its source code\n"
54
         "with FDNPKG to guarantee consistent behaviour of both tools. It also uses\n"
55
         "FDNPKG's configuration file.\n"
56
         );
57
  return(1);
58
}
59
 
60
 
61
static enum ACTIONTYPES parsearg(int argc, char **argv) {
62
  /* I expect exactly 2 arguments (ie argc == 3) */
63
  if (argc != 3) return(ACTION_HELP);
226 mateuszvis 64
 
219 mateuszvis 65
  /* look for valid actions */
66
  if (strcasecmp(argv[1], "install") == 0) {
226 mateuszvis 67
    return(ACTION_INSTALL);
219 mateuszvis 68
  } else if (strcasecmp(argv[1], "remove") == 0) {
226 mateuszvis 69
    return(ACTION_REMOVE);
70
  } else {
71
    return(ACTION_HELP);
219 mateuszvis 72
  }
73
}
74
 
75
 
225 mateuszvis 76
static int pkginst(char *file, int flags, char *dosdir, char *tempdir, struct customdirs *dirlist) {
219 mateuszvis 77
  char pkgname[32];
78
  int t, lastpathdelim = -1, u = 0;
79
  char *buffmem1k;
80
  struct ziplist *zipfileidx;
81
  FILE *zipfilefd;
82
  for (t = 0; file[t] != 0; t++) {
83
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
84
  }
85
  /* copy the filename into pkgname (without path elements) */
86
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
87
  pkgname[u] = 0; /* terminate the string */
88
  /* truncate the file's extension (.zip) */
89
  for (t = u; t > 0; t--) {
90
    if (pkgname[t] == '.') {
91
      pkgname[t] = 0;
92
      break;
93
    }
94
  }
95
  /* allocate some memory for pkginst_preparepackage() to do its job */
96
  buffmem1k = malloc(1024);
97
  if (buffmem1k == NULL) {
98
    puts("ERROR: Out of memory");
99
    return(1);
100
  }
101
  /* prepare the zip file and install it */
225 mateuszvis 102
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist, buffmem1k);
219 mateuszvis 103
  free(buffmem1k);
104
  if (zipfileidx != NULL) {
105
    int res = 0;
225 mateuszvis 106
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
219 mateuszvis 107
    fclose(zipfilefd);
108
    return(res);
109
  } else {
110
    fclose(zipfilefd);
111
    return(1);
112
  }
113
}
114
 
115
 
116
int main(int argc, char **argv) {
117
  int res, flags;
118
  enum ACTIONTYPES action;
119
  char *dosdir, *tempdir, *cfgfile;
120
  struct customdirs *dirlist;
121
 
122
  action = parsearg(argc, argv);
123
  if (action == ACTION_HELP) return(showhelp());
124
 
125
  /* allocate some bits for cfg file's location */
126
  cfgfile = malloc(256);
127
  if (cfgfile == NULL) {
128
    puts("ERROR: Out of memory");
129
    return(1);
130
  }
131
 
132
  /* read all necessary environment variables */
133
  if (readenv(&dosdir, &tempdir, cfgfile, 256) != 0) {
134
    free(cfgfile);
135
    return(1);
136
  }
137
 
138
  /* load configuration */
139
  flags = 0;
140
  dirlist = NULL;
225 mateuszvis 141
  if (loadconf(cfgfile, &dirlist, &flags) < 0) return(5);
219 mateuszvis 142
 
143
  /* free the cfgfile buffer, I won't need the config file's location any more */
144
  free(cfgfile);
145
  cfgfile = NULL;
146
 
147
  switch (action) {
148
    case ACTION_INSTALL:
225 mateuszvis 149
      res = pkginst(argv[2], flags, dosdir, tempdir, dirlist);
219 mateuszvis 150
      break;
151
    case ACTION_REMOVE:
225 mateuszvis 152
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 153
      break;
154
    default:
155
      res = showhelp();
156
      break;
157
  }
158
 
159
  if (res != 0) return(1);
160
  return(0);
161
}