* New alpha version 2.24.1
[alpine.git] / pith / osdep / lstcmpnt.c
bloba7297c5b5d41212e55f9ed10534a2e1fb41c6856
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: lstcmpnt.c 1074 2008-06-04 00:08:43Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006-2008 University of Washington
8 * Copyright 2013-2021 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include <system.h>
20 #include <general.h>
22 #include <string.h>
23 #include "../../pith/charconv/filesys.h"
24 #include "canaccess.h"
25 #include "lstcmpnt.h"
28 #ifdef _WINDOWS
30 #define FILE_SEP '\\'
32 #else /* UNIX */
34 #define FILE_SEP '/'
36 #endif /* UNIX */
40 /*----------------------------------------------------------------------
41 Return pointer to last component of pathname.
43 Args: filename -- The pathname.
45 Result: Returned pointer points to last component in the input argument.
46 ----*/
47 char *
48 last_cmpnt(char *filename)
50 char *p = NULL, *q = filename;
52 if(filename == NULL)
53 return(filename);
55 while((q = strchr(q, FILE_SEP)) != NULL)
56 if(*++q)
57 p = q;
59 #ifdef _WINDOWS
61 if(!p && isalpha((unsigned char) *filename) && *(filename+1) == ':' && *(filename+2))
62 p = filename + 2;
64 #endif
66 return(p);
71 * Like our_mkdir but it makes subdirs as well as the final dir
73 int
74 our_mkpath(char *path, mode_t mode)
76 char save, *q = path;
78 #ifdef _WINDOWS
79 if(isalpha((unsigned char) q[0]) && q[1] == ':' && q[2])
80 q = path + 3;
81 #endif
83 if(q == path && q[0] == FILE_SEP)
84 q = path + 1;
86 while((q = strchr(q, FILE_SEP)) != NULL){
87 save = *q;
88 *q = '\0';
89 if(can_access(path, ACCESS_EXISTS) != 0)
90 if(our_mkdir(path, mode) != 0){
91 *q = save;
92 return -1;
95 *q = save;
96 q++;
99 if(can_access(path, ACCESS_EXISTS) != 0 && our_mkdir(path, mode) != 0)
100 return -1;
102 return 0;