summaryrefslogtreecommitdiff
path: root/pith/osdep/lstcmpnt.c
blob: 57e39f8eb4bbbd5cf57b6e47c49bf6548ab81c7f (plain)
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
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: lstcmpnt.c 1074 2008-06-04 00:08:43Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2008 University of Washington
 * Copyright 2013-2018 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include <system.h>
#include <general.h>

#include <string.h>
#include "../../pith/charconv/filesys.h"
#include "canaccess.h"
#include "lstcmpnt.h"


#ifdef	_WINDOWS

#define	FILE_SEP		'\\'

#else  /* UNIX */

#define	FILE_SEP		'/'

#endif /* UNIX */



/*----------------------------------------------------------------------
      Return pointer to last component of pathname.

  Args: filename     -- The pathname.
 
 Result: Returned pointer points to last component in the input argument.
  ----*/
char *
last_cmpnt(char *filename)
{
    char *p = NULL, *q = filename;

    if(filename == NULL)
      return(filename);

    while((q = strchr(q, FILE_SEP)) != NULL)
      if(*++q)
	p = q;

#ifdef	_WINDOWS

    if(!p && isalpha((unsigned char) *filename) && *(filename+1) == ':' && *(filename+2))
      p = filename + 2;

#endif

    return(p);
}


/*
 * Like our_mkdir but it makes subdirs as well as the final dir
 */
int
our_mkpath(char *path, mode_t mode)
{
    char save, *q = path;

#ifdef	_WINDOWS
    if(isalpha((unsigned char) q[0]) && q[1] == ':' && q[2])
      q = path + 3;
#endif

    if(q == path && q[0] == FILE_SEP)
      q = path + 1;

    while((q = strchr(q, FILE_SEP)) != NULL){
	save = *q;
	*q = '\0';
	if(can_access(path, ACCESS_EXISTS) != 0)
	  if(our_mkdir(path, mode) != 0){
	      *q = save;
	      return -1;
	  }

	*q = save;
	q++;
    }

    if(can_access(path, ACCESS_EXISTS) != 0 && our_mkdir(path, mode) != 0)
      return -1;

    return 0;
}