~trn/rvi

ref: 09d332775538d2b468d5bb3cd48beeb0c95f6e96 rvi/src/rvi.cc -rw-r--r-- 7.8 KiB
09d33277 — Jeffrey H. Johnson Reformat code, no functional changes a month ago
                                                                                
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
/* rvi - Revision Control System Interface; editor wrapper */

/* v1.0.0 - v1.0.2 by Melekam Mtsegaye <mtsegaye-fm@rucus.ru.ac.za> */
/* v1.1.0 - v1.1.3 by Jeffrey H. Johnson <trnsz@pobox.com> */

#define RVI_VERSION "v1.1.3 (2021-05-03)"

#include    <ctype.h>
#include    <errno.h>
#include   <limits.h>
#include    <stdio.h>
#include   <stdlib.h>
#include   <string.h>
#include <sys/stat.h>
#include   <unistd.h>

#ifdef linux
#include <linux/binfmts.h>
#include  <linux/limits.h>
#define D_ARG_MAX MAX_ARG_STRLEN
#else  /* ifdef linux */
#define D_ARG_MAX sysconf(_SC_ARG_MAX)
#endif /* linux */

#ifndef D_ARG_MAX
#define D_ARG_MAX 255
#endif /* !D_ARG_MAX */

#ifdef linux
#ifndef PAGE_SIZE
#define PAGE_SIZE sysconf(_SC_PAGESIZE)
#endif /* !PAGE_SIZE */
#endif /* linux */

#define RCSDIR "RCS/\0"
#define RCSEXT ",v\0"

#define      UNLOCK "/usr/bin/rcs -u -q \"\0"
#define   CHECK_OUT  "/usr/bin/co -l -q \"\0"
#define CHECK_OUT_R     "/usr/bin/co -q \"\0"
#define    CHECK_IN  "/usr/bin/ci -u -q \"\0"

#define   REEDIT 2
#define    ABORT 4
#define   DG_YES 6
#define    DG_NO 8
#define DG_OTHER 0

void handle_error(int);
void do_filecheck(char *);
int  do_filechange_check(char *, struct stat *);
char *exec_cmd(char **, int, int flag = 0);
int  dialog();

int
main(
int argc,
char **argv
)
{
  fprintf(
    stdout,
    "%s: Revision Control System Interface %s\n",
    argv[0],
    RVI_VERSION);
  if (argc < 2 || argc > 3)
  {
    fprintf(stdout, "Usage: %s <filename>\n", argv[0]);
    exit(1);
  }

  do_filecheck(argv[1]);

  struct stat fileinfo;

  stat(argv[1], &fileinfo);

#ifdef DEBUG
  fprintf(
    stderr,
    "UID: %d EUID:%d GID:%d EGID:%d\n",
    fileinfo.st_uid,
    getenv("UID"),
    fileinfo.st_gid,
    getenv("GID"));
#endif /* ifdef DEBUG */

  char *editor;
  editor = getenv("EDITOR");
  if (!editor)
  {
    fprintf(stderr, "Error: EDITOR variable undefined\n");
    exit(1);
  }
  
  {
    char *params[3];
    params[0] = CHECK_OUT;
    params[1] = argv[1];
    params[2] = "\"";
    exec_cmd(params, 3);
    struct stat tmp;
    stat(argv[1], &tmp);
    fileinfo.st_mtime = tmp.st_mtime;
  }

  do
  {
    char *params[5];
    params[0] = editor;
    params[1] = " \0";
    params[2] = "\"";
    params[3] = argv[1];
    params[4] = "\"";
    exec_cmd(params, 5);
  }
  while ( do_filechange_check(argv[1], &fileinfo) == REEDIT );

  char *params[3];
  params[0] = CHECK_IN;
  params[1] = argv[1];
  params[2] = "\"";
  exec_cmd(params, 3);

  chown(argv[1], fileinfo.st_uid, fileinfo.st_gid);

  return ( 0 );
}

void
do_filecheck(
char *filename
)
{
  char *rcspath;
  int fname_p;
  int str_ln = strlen(filename) - 1;

  for (fname_p = str_ln; fname_p > 0 && filename[fname_p] != '/'; fname_p--)
  {
    ;
  }

  if (fname_p < 1 && filename[0] == '/')
  {
    rcspath = "/\0";
    fname_p++;
  }
  else if (fname_p < 1)
  {
    rcspath = "./\0";
  }
  else
  {
    rcspath = new char[fname_p + 1];
    strncpy(rcspath, filename, ++fname_p);
  }

  struct stat *filecheck = new struct stat;

  memset(filecheck, -1, sizeof ( struct stat ));
  stat(rcspath, filecheck);
  if (!(( filecheck->st_mode - S_IFDIR | S_IWUSR )
        == filecheck->st_mode - S_IFDIR ))
  {
    fprintf(stderr, "Error: No write permission for \"%s\"\n", rcspath);
    exit(1);
  }

  int rcsdir_exists = 0, rcsfile_exists = 0;

  filecheck->st_size = -1;
  memset(filecheck, -1, sizeof ( struct stat ));
  char *FULLRCSPATH;

  {
    char *params[2];
    params[0] = rcspath;
    params[1] = RCSDIR;
    FULLRCSPATH = exec_cmd(params, 2, 1);
  }
  stat(FULLRCSPATH, filecheck);
  if (S_ISDIR(filecheck->st_mode))
  {
    rcsdir_exists = 1;
    if (rcsdir_exists)
    {
      char *param[3];
      param[0] = FULLRCSPATH;
      param[1] = filename + fname_p;
      param[2] = RCSEXT;
      memset(filecheck, -1, sizeof ( struct stat ));
      char *fname = exec_cmd(param, 3, 1);
      char *qfnme = strstr(fname, "\"");
      if (!qfnme)
      {
        stat(fname, filecheck);
        if (S_ISREG(filecheck->st_mode))
        {
          rcsfile_exists = 1;
        }
      }
      else
      {
        fprintf(stderr, "Error: Filename may not contain double quotes.\n");
        exit(1);
      }
    }
  }

  memset(filecheck, -1, sizeof ( struct stat ));
  stat(filename, filecheck);
  int file_exists = 0;

  if (S_ISREG(filecheck->st_mode))
  {
    file_exists = 1;
  }

  if (!file_exists && !rcsfile_exists)
  {
    fprintf(stdout, "Warning: \"%s\" could not be found.\n", filename);
    fprintf(stdout, "Create it and check it into RCS? [N] ");
    if (dialog() != DG_YES)
    {
      exit(0);
    }

#ifndef DEBUG
    FILE *f = fopen(filename, "a");
    if (!f)
    {
      fprintf(stderr, "Error: Failed to create \"%s\": ", filename);
      handle_error(-1);
    }
    else
    {
      fclose(f);
    }

    if (!rcsdir_exists)
    {
      mkdir(FULLRCSPATH, S_IRWXU | S_IRWXG | S_IRWXO);
    }

#endif /* ifndef DEBUG */
    delete[] FULLRCSPATH;
    char *params[3];
    params[0] = CHECK_IN;
    params[1] = filename;
    params[2] = "\"";
    exec_cmd(params, 3);
  }
  else if (!rcsfile_exists && file_exists)
  {
    fprintf(
      stdout,
      "\"%s\" is not under RCS control; do initial check in? [N] ",
      filename);
    if (dialog() != DG_YES)
    {
      exit(0);
    }

#ifndef DEBUG
    if (!rcsdir_exists)
    {
      mkdir(FULLRCSPATH, S_IRWXU | S_IRWXG | S_IRWXO);
    }

#endif /* ifndef DEBUG */
    delete[] FULLRCSPATH;
    char *params[3];
    params[0] = CHECK_IN;
    params[1] = filename;
    params[2] = "\"";
    exec_cmd(params, 3);
  }
  else if (file_exists & rcsfile_exists)
  {
    memset(filecheck, -1, sizeof ( struct stat ));
    stat(filename, filecheck);
    if (( filecheck->st_mode - S_IFREG | S_IWUSR )
        == filecheck->st_mode - S_IFREG
          || ( filecheck->st_mode - S_IFREG | S_IWGRP )
            == filecheck->st_mode - S_IFREG
              || ( filecheck->st_mode - S_IFREG | S_IWOTH )
                == filecheck->st_mode - S_IFREG)
    {
      fprintf(stderr, "Error: \"%s\" already checked out.\n", filename);
      exit(1);
    }
  }

  delete filecheck;
}

int
do_filechange_check(
char *fname,
struct stat *original
)
{
  struct stat finfo;

  stat(fname, &finfo);
  if (finfo.st_mtime != original->st_mtime)
  {
    fprintf(
      stdout,
      "File \"%s\" has been modified.\n[A]ccept, [R]e-edit, or [D]iscard? [A] ",
      fname);
    int key = dialog();
    if (key == ABORT)
    {
      {
        char *params[3];
        params[0] = UNLOCK;
        params[1] = fname;
        params[2] = "\"";
        exec_cmd(params, 3);
      }
      remove(fname);
      {
        char *params[3];
        params[0] = CHECK_OUT_R;
        params[1] = fname;
        params[2] = "\"";
        exec_cmd(params, 3);
      }
      exit(0);
    }
    else
    {
      return ( key );
    }
  }

  return ( DG_OTHER );
}

char *
exec_cmd(
char **params,
int len,
int flag
)
{
  char *command = new char[D_ARG_MAX];
  int buf_ptr = 0;

  for (int i = 0; i < len; i++)
  {
    strncpy(command + buf_ptr, params[i], D_ARG_MAX - buf_ptr);
    buf_ptr += strlen(params[i]);
  }

  strncpy(command + buf_ptr + 1, "\"", D_ARG_MAX - buf_ptr);

#ifndef DEBUG
  if (flag)
  {
    return ( command );
  }

  handle_error(system(command));
#else  /* ifndef DEBUG */
  fprintf(stderr, "%s\n", command);
  if (flag)
  {
    return ( command );
  }

#endif /* ifndef DEBUG */
  delete[] command;
  return ((char *)NULL );
}

void
handle_error(
int err
)
{
  switch (err)
  {
  case 127:
    fprintf(stderr, "Error: execve call failed\n");
    exit(1);

  case -1:
    fprintf(stderr, "Error: %s\n", strerror(errno));
    exit(1);

  default:
    break;
  }
}

int
dialog()
{
  char response[10];

  fgets(response, 10, stdin);
  response[0] = toupper(response[0]);
  switch (response[0])
  {
  case 'D':
    return ( ABORT );

  case 'R':
    return ( REEDIT );

  case 'Y':
    return ( DG_YES );

  case 'N':
    return ( DG_NO );
  }
  return ( DG_OTHER );
}