File: rename_unlink.c

package info (click to toggle)
emoslib 2%3A4.4.5-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 359,232 kB
  • ctags: 13,125
  • sloc: fortran: 93,166; ansic: 27,958; sh: 7,500; f90: 5,209; perl: 604; cpp: 305; makefile: 78; python: 53
file content (81 lines) | stat: -rw-r--r-- 1,696 bytes parent folder | download | duplicates (6)
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
/**
* Copyright 1981-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence
* Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "common/fortint.h"

/*
// NB. Don't need the versions without underscore, they exist in
//     standard libraries (?!).
*/
#define RENAME rename_
#define UNLINK unlink_
#define GETPID getpid_

fortint GETPID(void) {
    return (fortint) getpid();
}

fortint RENAME( char * oldname, char * newname, int lold, int lnew) {
char * old, * new;
fortint status;

    old = (char *) malloc(lold+1);
    if( old == NULL ) {
      perror("RENAME: malloc failed for old");
      return (fortint) -1;
    }
    new = (char *) malloc(lnew+1);
    if( new == NULL ) {
      perror("RENAME: malloc failed for new");
      free(old);
      return (fortint) -1;
    }

    strncpy(old,oldname,lold);
    strncpy(new,newname,lnew);

    old[lold] = '\0';
    new[lold] = '\0';

    status = (fortint) rename(old,new);

    free(old);
    free(new);

    return status;
}

fortint UNLINK( char * oldname, int lold) {
char * old;
fortint status;

    old = (char *) malloc(lold+1);
    if( old == NULL ) {
      perror("UNLINK: malloc failed for old");
      return (fortint) -1;
    }

    strncpy(old,oldname,lold);
    old[lold] = '\0';

    status = (fortint) unlink(old);

    free(old);

    return status;
}