Library contents

C mkstemp

Create private scratch entries with distinct names.

A simple example

mkstemp

  1. Start with A temporary workspace
  2. Do this Create two independent temporary items
  3. Result Distinct items
Each creation obtains its own temporary location. This lets examples run without overwriting one another.

Syntax

C syntax
mkstemp(pattern)
Run these examples in C

Save one example at a time as lesson.c. Use C17 on macOS or Linux with a POSIX shell.

cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson

Isolate two simultaneous label previews

Two till operators can preview labels with the same product name at the same time. Ask the system to create two independent scratch entries and verify the names differ before writing any shared data.

From the programchar first[] = "/tmp/label-XXXXXX", second[] = "/tmp/label-XXXXXX"
Resultdistinct
lesson.c
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(void) {
    char first[] = "/tmp/label-XXXXXX", second[] = "/tmp/label-XXXXXX";
    int a = mkstemp(first);
    if (a == -1) return 1;
    int b = mkstemp(second);
    if (b != -1 && strcmp(first, second) != 0) puts("distinct");
    close(a); unlink(first);
    if (b == -1) return 1;
    close(b); unlink(second);
    return 0;
}
Output
distinct

The filesystem creates separate entries for the two jobs.

Advertisements

Locate a rehearsal score

The choir stores each score inside a season folder. Two students use distinct temporary workspaces with the same filename. Does a file saved by the first student appear in the second student’s workspace?

Givenspring / alto.txt
Worked resultSeparate

Locate a workshop handout

An instructor stores the current template in a class folder. Two students use distinct temporary workspaces with the same filename. Does a file saved by the first student appear in the second student’s workspace?

Givenceramics / glaze.pdf
Worked resultSeparate

What to watch for

These C examples use POSIX file APIs on macOS or Linux. Keep the handle or path returned by the creation API and clean up after the job.

Documentation for mkstemp